NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
number.c File Reference

Type representing a number. More...

#include "config.h"
#include <stddef.h>
#include <limits.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "set.h"
#include "types.h"
+ Include dependency graph for number.c:

Go to the source code of this file.

Functions

static int number_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Number by string - Implements ConfigSetType::string_set() More...
 
static int number_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Number as a string - Implements ConfigSetType::string_get() More...
 
static int number_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Number config item by int - Implements ConfigSetType::native_set() More...
 
static intptr_t number_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get an int from a Number config item - Implements ConfigSetType::native_get() More...
 
static int number_string_plus_equals (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Add to a Number by string - Implements ConfigSetType::string_plus_equals() More...
 
static int number_string_minus_equals (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Subtract from a Number by string - Implements ConfigSetType::string_minus_equals() More...
 
static int number_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Number to its initial value - Implements ConfigSetType::reset() More...
 

Variables

const struct ConfigSetType cst_number
 Config type representing a number. More...
 

Detailed Description

Type representing a number.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file number.c.

Function Documentation

◆ number_string_set()

static int number_string_set ( const struct ConfigSet cs,
void *  var,
struct ConfigDef cdef,
const char *  value,
struct Buffer err 
)
static

Set a Number by string - Implements ConfigSetType::string_set()

Definition at line 43 of file number.c.

45 {
46  if (!value || (value[0] == '\0'))
47  {
48  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
50  }
51 
52  int num = 0;
53  if (mutt_str_atoi(value, &num) < 0)
54  {
55  mutt_buffer_printf(err, _("Invalid number: %s"), value);
57  }
58 
59  if ((num < SHRT_MIN) || (num > SHRT_MAX))
60  {
61  mutt_buffer_printf(err, _("Number is too big: %s"), value);
63  }
64 
65  if ((num < 0) && (cdef->type & DT_NOT_NEGATIVE))
66  {
67  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
69  }
70 
71  if (var)
72  {
73  if (num == (*(short *) var))
75 
76  if (cdef->validator)
77  {
78  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
79 
80  if (CSR_RESULT(rc) != CSR_SUCCESS)
81  return rc | CSR_INV_VALIDATOR;
82  }
83 
84  *(short *) var = num;
85  }
86  else
87  {
88  cdef->initial = num;
89  }
90 
91  return CSR_SUCCESS;
92 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:252
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
intptr_t initial
Initial value.
Definition: set.h:66
#define _(a)
Definition: message.h:28
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
#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
+ Here is the call graph for this function:

◆ number_string_get()

static int number_string_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer result 
)
static

Get a Number as a string - Implements ConfigSetType::string_get()

Definition at line 97 of file number.c.

99 {
100  int value;
101 
102  if (var)
103  value = *(short *) var;
104  else
105  value = (int) cdef->initial;
106 
107  mutt_buffer_printf(result, "%d", value);
108  return CSR_SUCCESS;
109 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
+ Here is the call graph for this function:

◆ number_native_set()

static int number_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Number config item by int - Implements ConfigSetType::native_set()

Definition at line 114 of file number.c.

117 {
118  if ((value < SHRT_MIN) || (value > SHRT_MAX))
119  {
120  mutt_buffer_printf(err, _("Invalid number: %ld"), value);
121  return CSR_ERR_INVALID | CSR_INV_TYPE;
122  }
123 
124  if ((value < 0) && (cdef->type & DT_NOT_NEGATIVE))
125  {
126  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
128  }
129 
130  if (value == (*(short *) var))
132 
133  if (cdef->validator)
134  {
135  int rc = cdef->validator(cs, cdef, value, err);
136 
137  if (CSR_RESULT(rc) != CSR_SUCCESS)
138  return rc | CSR_INV_VALIDATOR;
139  }
140 
141  *(short *) var = value;
142  return CSR_SUCCESS;
143 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define _(a)
Definition: message.h:28
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
#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
+ Here is the call graph for this function:

◆ number_native_get()

static intptr_t number_native_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Get an int from a Number config item - Implements ConfigSetType::native_get()

Definition at line 148 of file number.c.

150 {
151  return *(short *) var;
152 }

◆ number_string_plus_equals()

static int number_string_plus_equals ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
const char *  value,
struct Buffer err 
)
static

Add to a Number by string - Implements ConfigSetType::string_plus_equals()

Definition at line 157 of file number.c.

160 {
161  int num = 0;
162  if (!value || (value[0] == '\0') || (mutt_str_atoi(value, &num) < 0))
163  {
164  mutt_buffer_printf(err, _("Invalid number: %s"), NONULL(value));
165  return CSR_ERR_INVALID | CSR_INV_TYPE;
166  }
167 
168  int result = *((short *) var) + num;
169  if ((result < SHRT_MIN) || (result > SHRT_MAX))
170  {
171  mutt_buffer_printf(err, _("Number is too big: %s"), value);
172  return CSR_ERR_INVALID | CSR_INV_TYPE;
173  }
174 
175  if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
176  {
177  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
179  }
180 
181  if (cdef->validator)
182  {
183  int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
184 
185  if (CSR_RESULT(rc) != CSR_SUCCESS)
186  return rc | CSR_INV_VALIDATOR;
187  }
188 
189  *(short *) var = result;
190  return CSR_SUCCESS;
191 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define NONULL(x)
Definition: string2.h:37
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:252
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define _(a)
Definition: message.h:28
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define CSR_RESULT(x)
Definition: set.h:52
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
#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
+ Here is the call graph for this function:

◆ number_string_minus_equals()

static int number_string_minus_equals ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
const char *  value,
struct Buffer err 
)
static

Subtract from a Number by string - Implements ConfigSetType::string_minus_equals()

Definition at line 196 of file number.c.

199 {
200  int num = 0;
201  if (!value || (value[0] == '\0') || (mutt_str_atoi(value, &num) < 0))
202  {
203  mutt_buffer_printf(err, _("Invalid number: %s"), value);
204  return CSR_ERR_INVALID | CSR_INV_TYPE;
205  }
206 
207  int result = *((short *) var) - num;
208  if ((result < SHRT_MIN) || (result > SHRT_MAX))
209  {
210  mutt_buffer_printf(err, _("Number is too big: %s"), value);
211  return CSR_ERR_INVALID | CSR_INV_TYPE;
212  }
213 
214  if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
215  {
216  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
218  }
219 
220  if (cdef->validator)
221  {
222  int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
223 
224  if (CSR_RESULT(rc) != CSR_SUCCESS)
225  return rc | CSR_INV_VALIDATOR;
226  }
227 
228  *(short *) var = result;
229  return CSR_SUCCESS;
230 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:252
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define _(a)
Definition: message.h:28
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define CSR_RESULT(x)
Definition: set.h:52
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
#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
+ Here is the call graph for this function:

◆ number_reset()

static int number_reset ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Reset a Number to its initial value - Implements ConfigSetType::reset()

Definition at line 235 of file number.c.

237 {
238  if (cdef->initial == (*(short *) var))
240 
241  if (cdef->validator)
242  {
243  int rc = cdef->validator(cs, cdef, cdef->initial, err);
244 
245  if (CSR_RESULT(rc) != CSR_SUCCESS)
246  return rc | CSR_INV_VALIDATOR;
247  }
248 
249  *(short *) var = cdef->initial;
250  return CSR_SUCCESS;
251 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48

Variable Documentation

◆ cst_number

const struct ConfigSetType cst_number
Initial value:
= {
"number",
NULL,
}
static int number_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a Number by string - Implements ConfigSetType::string_plus_equals()
Definition: number.c:157
static int number_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Number as a string - Implements ConfigSetType::string_get()
Definition: number.c:97
static int number_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Number by string - Implements ConfigSetType::string_set()
Definition: number.c:43
static int number_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Number to its initial value - Implements ConfigSetType::reset()
Definition: number.c:235
static int number_string_minus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Subtract from a Number by string - Implements ConfigSetType::string_minus_equals() ...
Definition: number.c:196
static int number_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Number config item by int - Implements ConfigSetType::native_set()
Definition: number.c:114
#define DT_NUMBER
a number
Definition: types.h:35
static intptr_t number_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int from a Number config item - Implements ConfigSetType::native_get()
Definition: number.c:148

Config type representing a number.

Definition at line 256 of file number.c.