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

Type representing a long. 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 long.c:

Go to the source code of this file.

Functions

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() More...
 
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() More...
 
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() More...
 
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() More...
 
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() More...
 
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() More...
 
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() More...
 

Variables

const struct ConfigSetType cst_long
 Config type representing a long. More...
 

Detailed Description

Type representing a long.

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 long.c.

Function Documentation

◆ long_string_set()

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

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

Definition at line 43 of file long.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  long num = 0;
53  if (mutt_str_atol(value, &num) < 0)
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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#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_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:188
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:

◆ long_string_get()

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

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

Definition at line 91 of file long.c.

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 }
#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:

◆ long_native_set()

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

Set a Long config item by long - Implements ConfigSetType::native_set()

Definition at line 108 of file long.c.

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 }
#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_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ long_native_get()

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

Get a long from a Long config item - Implements ConfigSetType::native_get()

Definition at line 135 of file long.c.

137 {
138  return *(long *) var;
139 }

◆ long_string_plus_equals()

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

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

Definition at line 144 of file long.c.

147 {
148  long num = 0;
149  if (!value || (value[0] == '\0') || (mutt_str_atol(value, &num) < 0))
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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define NONULL(x)
Definition: string2.h:37
#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_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:188
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:

◆ long_string_minus_equals()

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

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

Definition at line 180 of file long.c.

183 {
184  long num = 0;
185  if (!value || (value[0] == '\0') || (mutt_str_atol(value, &num) < 0))
186  {
187  mutt_buffer_printf(err, _("Invalid long: %s"), 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 }
#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_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:188
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:

◆ long_reset()

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

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

Definition at line 216 of file long.c.

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 }
#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_long

const struct ConfigSetType cst_long
Initial value:
= {
"long",
NULL,
}
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_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
#define DT_LONG
a number (long)
Definition: types.h:33
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_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_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_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

Config type representing a long.

Definition at line 237 of file long.c.