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

Type representing a quad-option. More...

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

Go to the source code of this file.

Functions

static int quad_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Quad-option by string - Implements ConfigSetType::string_set() More...
 
static int quad_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Quad-option as a string - Implements ConfigSetType::string_get() More...
 
static int quad_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Quad-option config item by int - Implements ConfigSetType::native_set() More...
 
static intptr_t quad_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() More...
 
static int quad_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Quad-option to its initial value - Implements ConfigSetType::reset() More...
 
static int quad_toggle (int opt)
 Toggle (invert) the value of a quad option. More...
 
int quad_he_toggle (struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
 Toggle the value of a quad. More...
 
int quad_str_toggle (struct ConfigSubset *sub, const char *name, struct Buffer *err)
 Toggle the value of a quad. More...
 

Variables

const char * QuadValues []
 Valid strings for creating a QuadValue. More...
 
const struct ConfigSetType cst_quad
 Config type representing a quad-option. More...
 

Detailed Description

Type representing a quad-option.

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

Function Documentation

◆ quad_string_set()

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

Set a Quad-option by string - Implements ConfigSetType::string_set()

Definition at line 55 of file quad.c.

57 {
58  if (!value)
59  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
60 
61  int num = -1;
62  for (size_t i = 0; QuadValues[i]; i++)
63  {
64  if (mutt_istr_equal(QuadValues[i], value))
65  {
66  num = i;
67  break;
68  }
69  }
70 
71  if (num < 0)
72  {
73  mutt_buffer_printf(err, _("Invalid quad value: %s"), value);
75  }
76 
77  if (var)
78  {
79  if (num == (*(char *) var))
81 
82  if (cdef->validator)
83  {
84  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
85 
86  if (CSR_RESULT(rc) != CSR_SUCCESS)
87  return rc | CSR_INV_VALIDATOR;
88  }
89 
90  *(char *) var = num;
91  }
92  else
93  {
94  cdef->initial = num;
95  }
96 
97  return CSR_SUCCESS;
98 }
#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
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define _(a)
Definition: message.h:28
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
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:883
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_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:

◆ quad_string_get()

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

Get a Quad-option as a string - Implements ConfigSetType::string_get()

Definition at line 103 of file quad.c.

105 {
106  unsigned int value;
107 
108  if (var)
109  value = *(char *) var;
110  else
111  value = (int) cdef->initial;
112 
113  if (value >= (mutt_array_size(QuadValues) - 1))
114  {
115  mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
116  return CSR_ERR_INVALID | CSR_INV_TYPE;
117  }
118 
119  mutt_buffer_addstr(result, QuadValues[value]);
120  return CSR_SUCCESS;
121 }
#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
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define mutt_array_size(x)
Definition: memory.h:33
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 mutt_debug(LEVEL,...)
Definition: logging.h:81
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
+ Here is the call graph for this function:

◆ quad_native_set()

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

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

Definition at line 126 of file quad.c.

128 {
129  if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
130  {
131  mutt_buffer_printf(err, _("Invalid quad value: %ld"), value);
132  return CSR_ERR_INVALID | CSR_INV_TYPE;
133  }
134 
135  if (value == (*(char *) var))
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  *(char *) var = value;
147  return CSR_SUCCESS;
148 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define _(a)
Definition: message.h:28
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 mutt_array_size(x)
Definition: memory.h:33
#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_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:

◆ quad_native_get()

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

Get an int object from a Quad-option config item - Implements ConfigSetType::native_get()

Definition at line 153 of file quad.c.

155 {
156  return *(char *) var;
157 }

◆ quad_reset()

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

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

Definition at line 162 of file quad.c.

164 {
165  if (cdef->initial == (*(char *) var))
167 
168  if (cdef->validator)
169  {
170  int rc = cdef->validator(cs, cdef, cdef->initial, err);
171 
172  if (CSR_RESULT(rc) != CSR_SUCCESS)
173  return rc | CSR_INV_VALIDATOR;
174  }
175 
176  *(char *) var = cdef->initial;
177  return CSR_SUCCESS;
178 }
#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

◆ quad_toggle()

static int quad_toggle ( int  opt)
static

Toggle (invert) the value of a quad option.

Parameters
optValue to toggle

By toggling the low bit, the following are swapped:

Definition at line 188 of file quad.c.

189 {
190  return opt ^= 1;
191 }
+ Here is the caller graph for this function:

◆ quad_he_toggle()

int quad_he_toggle ( struct ConfigSubset sub,
struct HashElem he,
struct Buffer err 
)

Toggle the value of a quad.

Parameters
subConfig subset
heHashElem representing config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS
See also
quad_toggle()

Definition at line 202 of file quad.c.

203 {
204  if (!sub || !he || !he->data)
205  return CSR_ERR_CODE;
206 
207  struct HashElem *he_base = cs_get_base(he);
208  if (DTYPE(he_base->type) != DT_QUAD)
209  return CSR_ERR_CODE;
210 
211  intptr_t value = cs_he_native_get(sub->cs, he, err);
212  if (value == INT_MIN)
213  return CSR_ERR_CODE;
214 
215  value = quad_toggle(value);
216  int rc = cs_he_native_set(sub->cs, he, value, err);
217 
218  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
220 
221  return rc;
222 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition: subset.c:218
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define CSR_RESULT(x)
Definition: set.h:52
intptr_t cs_he_native_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition: set.c:817
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:188
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:199
Config item has been set.
Definition: subset.h:62
void * data
User-supplied data.
Definition: hash.h:47
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
The item stored in a Hash Table.
Definition: hash.h:43
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
int cs_he_native_set(const struct ConfigSet *cs, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: set.c:706
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quad_str_toggle()

int quad_str_toggle ( struct ConfigSubset sub,
const char *  name,
struct Buffer err 
)

Toggle the value of a quad.

Parameters
subConfig subset
nameHashElem representing config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS
See also
quad_toggle()

Definition at line 233 of file quad.c.

234 {
235  struct HashElem *he = cs_subset_create_inheritance(sub, name);
236 
237  return quad_he_toggle(sub, he, err);
238 }
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:202
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:189
The item stored in a Hash Table.
Definition: hash.h:43
+ Here is the call graph for this function:

Variable Documentation

◆ QuadValues

const char* QuadValues[]
Initial value:
= {
"no", "yes", "ask-no", "ask-yes", NULL,
}

Valid strings for creating a QuadValue.

These strings are case-insensitive.

Definition at line 48 of file quad.c.

◆ cst_quad

const struct ConfigSetType cst_quad
Initial value:
= {
"quad",
NULL,
NULL,
NULL,
}
static int quad_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Quad-option config item by int - Implements ConfigSetType::native_set()
Definition: quad.c:126
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
static int quad_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Quad-option to its initial value - Implements ConfigSetType::reset()
Definition: quad.c:162
static int quad_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Quad-option as a string - Implements ConfigSetType::string_get()
Definition: quad.c:103
static int quad_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Quad-option by string - Implements ConfigSetType::string_set()
Definition: quad.c:55
static intptr_t quad_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() ...
Definition: quad.c:153

Config type representing a quad-option.

Definition at line 243 of file quad.c.