NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
long.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <stddef.h>
35 #include <stdint.h>
36 #include "mutt/lib.h"
37 #include "set.h"
38 #include "types.h"
39 
43 static int long_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
44  const char *value, struct Buffer *err)
45 {
46  if (!value || (value[0] == '\0'))
47  {
48  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
50  }
51 
52  long num = 0;
53  if (!mutt_str_atol_full(value, &num))
54  {
55  mutt_buffer_printf(err, _("Invalid long: %s"), value);
57  }
58 
59  if ((num < 0) && (cdef->type & DT_NOT_NEGATIVE))
60  {
61  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
63  }
64 
65  if (var)
66  {
67  if (num == (*(long *) var))
69 
70  if (cdef->validator)
71  {
72  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
73 
74  if (CSR_RESULT(rc) != CSR_SUCCESS)
75  return rc | CSR_INV_VALIDATOR;
76  }
77 
78  *(long *) var = num;
79  }
80  else
81  {
82  cdef->initial = num;
83  }
84 
85  return CSR_SUCCESS;
86 }
87 
91 static int long_string_get(const struct ConfigSet *cs, void *var,
92  const struct ConfigDef *cdef, struct Buffer *result)
93 {
94  int value;
95 
96  if (var)
97  value = *(long *) var;
98  else
99  value = (int) cdef->initial;
100 
101  mutt_buffer_printf(result, "%d", value);
102  return CSR_SUCCESS;
103 }
104 
108 static int long_native_set(const struct ConfigSet *cs, void *var,
109  const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
110 {
111  if ((value < 0) && (cdef->type & DT_NOT_NEGATIVE))
112  {
113  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
115  }
116 
117  if (value == (*(long *) var))
119 
120  if (cdef->validator)
121  {
122  int rc = cdef->validator(cs, cdef, value, err);
123 
124  if (CSR_RESULT(rc) != CSR_SUCCESS)
125  return rc | CSR_INV_VALIDATOR;
126  }
127 
128  *(long *) var = value;
129  return CSR_SUCCESS;
130 }
131 
135 static intptr_t long_native_get(const struct ConfigSet *cs, void *var,
136  const struct ConfigDef *cdef, struct Buffer *err)
137 {
138  return *(long *) var;
139 }
140 
144 static int long_string_plus_equals(const struct ConfigSet *cs, void *var,
145  const struct ConfigDef *cdef,
146  const char *value, struct Buffer *err)
147 {
148  long num = 0;
149  if (!mutt_str_atol_full(value, &num))
150  {
151  mutt_buffer_printf(err, _("Invalid long: %s"), NONULL(value));
152  return CSR_ERR_INVALID | CSR_INV_TYPE;
153  }
154 
155  long result = *((long *) var) + num;
156  if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
157  {
158  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
160  }
161 
162  if (result == (*(long *) var))
164 
165  if (cdef->validator)
166  {
167  int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
168 
169  if (CSR_RESULT(rc) != CSR_SUCCESS)
170  return rc | CSR_INV_VALIDATOR;
171  }
172 
173  *(long *) var = result;
174  return CSR_SUCCESS;
175 }
176 
180 static int long_string_minus_equals(const struct ConfigSet *cs, void *var,
181  const struct ConfigDef *cdef,
182  const char *value, struct Buffer *err)
183 {
184  long num = 0;
185  if (!mutt_str_atol_full(value, &num))
186  {
187  mutt_buffer_printf(err, _("Invalid long: %s"), NONULL(value));
188  return CSR_ERR_INVALID | CSR_INV_TYPE;
189  }
190 
191  long result = *((long *) var) - num;
192  if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
193  {
194  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
196  }
197 
198  if (result == (*(long *) var))
200 
201  if (cdef->validator)
202  {
203  int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
204 
205  if (CSR_RESULT(rc) != CSR_SUCCESS)
206  return rc | CSR_INV_VALIDATOR;
207  }
208 
209  *(long *) var = result;
210  return CSR_SUCCESS;
211 }
212 
216 static int long_reset(const struct ConfigSet *cs, void *var,
217  const struct ConfigDef *cdef, struct Buffer *err)
218 {
219  if (cdef->initial == (*(long *) var))
221 
222  if (cdef->validator)
223  {
224  int rc = cdef->validator(cs, cdef, cdef->initial, err);
225 
226  if (CSR_RESULT(rc) != CSR_SUCCESS)
227  return (rc | CSR_INV_VALIDATOR);
228  }
229 
230  *(long *) var = cdef->initial;
231  return CSR_SUCCESS;
232 }
233 
237 const struct ConfigSetType CstLong = {
238  DT_LONG,
239  "long",
246  long_reset,
247  NULL, // destroy
248 };
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
static intptr_t long_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a long from a Long config item - Implements ConfigSetType::native_get() -.
Definition: long.c:135
static int long_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Long config item by long - Implements ConfigSetType::native_set() -.
Definition: long.c:108
static int long_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Long to its initial value - Implements ConfigSetType::reset() -.
Definition: long.c:216
static int long_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Long as a string - Implements ConfigSetType::string_get() -.
Definition: long.c:91
static int long_string_minus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Subtract from a Long by string - Implements ConfigSetType::string_minus_equals() -.
Definition: long.c:180
static int long_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a Long by string - Implements ConfigSetType::string_plus_equals() -.
Definition: long.c:144
static int long_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Long by string - Implements ConfigSetType::string_set() -.
Definition: long.c:43
const struct ConfigSetType CstLong
Config type representing a long.
Definition: long.c:237
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
A collection of config items.
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:34
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:260
Constants for all the config types.
#define DT_LONG
a number (long)
Definition: types.h:33
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47