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

Type representing an enumeration. More...

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

Go to the source code of this file.

Functions

static int enum_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set an Enumeration by string - Implements ConfigSetType::string_set() More...
 
static int enum_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get an Enumeration as a string - Implements ConfigSetType::string_get() More...
 
static int enum_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set an Enumeration config item by int - Implements ConfigSetType::native_set() More...
 
static intptr_t enum_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get an int object from an Enumeration config item - Implements ConfigSetType::native_get() More...
 
static int enum_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset an Enumeration to its initial value - Implements ConfigSetType::reset() More...
 

Variables

const struct ConfigSetType cst_enum
 Config type representing an enumeration. More...
 

Detailed Description

Type representing an enumeration.

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

Function Documentation

◆ enum_string_set()

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

Set an Enumeration by string - Implements ConfigSetType::string_set()

Definition at line 44 of file enum.c.

46 {
47  if (!cs || !cdef || !value)
48  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
49 
50  struct EnumDef *ed = (struct EnumDef *) cdef->data;
51  if (!ed || !ed->lookup)
52  return CSR_ERR_CODE;
53 
54  int num = mutt_map_get_value(value, ed->lookup);
55  if (num < 0)
56  {
57  mutt_buffer_printf(err, _("Invalid enum value: %s"), value);
58  return (CSR_ERR_INVALID | CSR_INV_TYPE);
59  }
60 
61  if (var)
62  {
63  if (num == (*(unsigned char *) var))
64  return (CSR_SUCCESS | CSR_SUC_NO_CHANGE);
65 
66  if (cdef->validator)
67  {
68  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
69 
70  if (CSR_RESULT(rc) != CSR_SUCCESS)
71  return (rc | CSR_INV_VALIDATOR);
72  }
73 
74  *(unsigned char *) var = num;
75  }
76  else
77  {
78  cdef->initial = num;
79  }
80 
81  return CSR_SUCCESS;
82 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
An enumeration.
Definition: enum.h:31
#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
intptr_t data
Extra variable data.
Definition: set.h:67
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
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
struct Mapping * lookup
Definition: enum.h:35
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
#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
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
+ Here is the call graph for this function:

◆ enum_string_get()

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

Get an Enumeration as a string - Implements ConfigSetType::string_get()

Definition at line 87 of file enum.c.

89 {
90  if (!cs || !cdef)
91  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
92 
93  unsigned int value;
94 
95  if (var)
96  value = *(unsigned char *) var;
97  else
98  value = (int) cdef->initial;
99 
100  struct EnumDef *ed = (struct EnumDef *) cdef->data;
101  if (!ed || !ed->lookup)
102  return CSR_ERR_CODE;
103 
104  const char *name = mutt_map_get_name(value, ed->lookup);
105  if (!name)
106  {
107  mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
108  return (CSR_ERR_INVALID | CSR_INV_TYPE);
109  }
110 
111  mutt_buffer_addstr(result, name);
112  return CSR_SUCCESS;
113 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
An enumeration.
Definition: enum.h:31
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
intptr_t initial
Initial value.
Definition: set.h:66
intptr_t data
Extra variable data.
Definition: set.h:67
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
Log at debug level 1.
Definition: logging.h:40
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
const char * name
Definition: enum.h:33
+ Here is the call graph for this function:

◆ enum_native_set()

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

Set an Enumeration config item by int - Implements ConfigSetType::native_set()

Definition at line 118 of file enum.c.

120 {
121  if (!cs || !var || !cdef)
122  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
123 
124  struct EnumDef *ed = (struct EnumDef *) cdef->data;
125  if (!ed || !ed->lookup)
126  return CSR_ERR_CODE;
127 
128  const char *name = mutt_map_get_name(value, ed->lookup);
129  if (!name)
130  {
131  mutt_buffer_printf(err, _("Invalid enum value: %ld"), value);
132  return (CSR_ERR_INVALID | CSR_INV_TYPE);
133  }
134 
135  if (value == (*(unsigned char *) var))
136  return (CSR_SUCCESS | CSR_SUC_NO_CHANGE);
137 
138  if (cdef->validator)
139  {
140  int rc = cdef->validator(cs, cdef, value, err);
141 
142  if (CSR_RESULT(rc) != CSR_SUCCESS)
143  return (rc | CSR_INV_VALIDATOR);
144  }
145 
146  *(unsigned char *) var = value;
147  return CSR_SUCCESS;
148 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
An enumeration.
Definition: enum.h:31
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define _(a)
Definition: message.h:28
intptr_t data
Extra variable data.
Definition: set.h:67
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 * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
#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
struct Mapping * lookup
Definition: enum.h:35
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
const char * name
Definition: enum.h:33
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ enum_native_get()

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

Get an int object from an Enumeration config item - Implements ConfigSetType::native_get()

Definition at line 153 of file enum.c.

155 {
156  if (!cs || !var || !cdef)
157  return INT_MIN; /* LCOV_EXCL_LINE */
158 
159  return *(unsigned char *) var;
160 }

◆ enum_reset()

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

Reset an Enumeration to its initial value - Implements ConfigSetType::reset()

Definition at line 165 of file enum.c.

167 {
168  if (!cs || !var || !cdef)
169  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
170 
171  if (cdef->initial == (*(unsigned char *) var))
172  return (CSR_SUCCESS | CSR_SUC_NO_CHANGE);
173 
174  if (cdef->validator)
175  {
176  int rc = cdef->validator(cs, cdef, cdef->initial, err);
177 
178  if (CSR_RESULT(rc) != CSR_SUCCESS)
179  return (rc | CSR_INV_VALIDATOR);
180  }
181 
182  *(unsigned char *) var = cdef->initial;
183  return CSR_SUCCESS;
184 }
#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_ERR_CODE
Problem with the code.
Definition: set.h:36
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48

Variable Documentation

◆ cst_enum

const struct ConfigSetType cst_enum
Initial value:
= {
"enum",
NULL,
NULL,
NULL,
}
static intptr_t enum_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from an Enumeration config item - Implements ConfigSetType::native_get() ...
Definition: enum.c:153
static int enum_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an Enumeration by string - Implements ConfigSetType::string_set()
Definition: enum.c:44
static int enum_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an Enumeration config item by int - Implements ConfigSetType::native_set()
Definition: enum.c:118
#define DT_ENUM
an enumeration
Definition: types.h:31
static int enum_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get an Enumeration as a string - Implements ConfigSetType::string_get()
Definition: enum.c:87
static int enum_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Enumeration to its initial value - Implements ConfigSetType::reset()
Definition: enum.c:165

Config type representing an enumeration.

Definition at line 189 of file enum.c.