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

Type representing a boolean. More...

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

Go to the source code of this file.

Functions

static int bool_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Bool by string - Implements ConfigSetType::string_set() More...
 
static int bool_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Bool as a string - Implements ConfigSetType::string_get() More...
 
static int bool_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Bool config item by bool - Implements ConfigSetType::native_set() More...
 
static intptr_t bool_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get a bool from a Bool config item - Implements ConfigSetType::native_get() More...
 
static int bool_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Bool to its initial value - Implements ConfigSetType::reset() More...
 
int bool_he_toggle (struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
 Toggle the value of a bool. More...
 
int bool_str_toggle (struct ConfigSubset *sub, const char *name, struct Buffer *err)
 Toggle the value of a bool. More...
 

Variables

const char * BoolValues []
 Valid strings for creating a Bool. More...
 
const struct ConfigSetType cst_bool
 Config type representing an boolean. More...
 

Detailed Description

Type representing a boolean.

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

Function Documentation

◆ bool_string_set()

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

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

Definition at line 56 of file bool.c.

58 {
59  if (!value)
60  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
61 
62  int num = -1;
63  for (size_t i = 0; BoolValues[i]; i++)
64  {
65  if (mutt_istr_equal(BoolValues[i], value))
66  {
67  num = i % 2;
68  break;
69  }
70  }
71 
72  if (num < 0)
73  {
74  mutt_buffer_printf(err, _("Invalid boolean value: %s"), value);
76  }
77 
78  if (var)
79  {
80  if (num == (*(bool *) var))
82 
83  if (cdef->validator)
84  {
85  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
86 
87  if (CSR_RESULT(rc) != CSR_SUCCESS)
88  return rc | CSR_INV_VALIDATOR;
89  }
90 
91  *(bool *) var = num;
92  }
93  else
94  {
95  cdef->initial = num;
96  }
97 
98  return CSR_SUCCESS;
99 }
#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 * BoolValues[]
Valid strings for creating a Bool.
Definition: bool.c:49
intptr_t initial
Initial value.
Definition: set.h:66
#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:

◆ bool_string_get()

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

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

Definition at line 104 of file bool.c.

106 {
107  int index;
108 
109  if (var)
110  index = *(bool *) var;
111  else
112  index = (int) cdef->initial;
113 
114  if (index > 1)
115  return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
116 
117  mutt_buffer_addstr(result, BoolValues[index]);
118  return CSR_SUCCESS;
119 }
#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 * BoolValues[]
Valid strings for creating a Bool.
Definition: bool.c:49
intptr_t initial
Initial value.
Definition: set.h:66
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
+ Here is the call graph for this function:

◆ bool_native_set()

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

Set a Bool config item by bool - Implements ConfigSetType::native_set()

Definition at line 124 of file bool.c.

126 {
127  if ((value < 0) || (value > 1))
128  {
129  mutt_buffer_printf(err, _("Invalid boolean value: %ld"), value);
130  return CSR_ERR_INVALID | CSR_INV_TYPE;
131  }
132 
133  if (value == (*(bool *) var))
135 
136  if (cdef->validator)
137  {
138  int rc = cdef->validator(cs, cdef, value, err);
139 
140  if (CSR_RESULT(rc) != CSR_SUCCESS)
141  return rc | CSR_INV_VALIDATOR;
142  }
143 
144  *(bool *) var = value;
145  return CSR_SUCCESS;
146 }
#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
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
#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:

◆ bool_native_get()

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

Get a bool from a Bool config item - Implements ConfigSetType::native_get()

Definition at line 151 of file bool.c.

153 {
154  return *(bool *) var;
155 }

◆ bool_reset()

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

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

Definition at line 160 of file bool.c.

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

◆ bool_he_toggle()

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

Toggle the value of a bool.

Parameters
subConfig Subset
heHashElem representing config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS

Definition at line 185 of file bool.c.

186 {
187  if (!sub || !he || !he->data)
188  return CSR_ERR_CODE;
189 
190  struct HashElem *he_base = cs_get_base(he);
191  if (DTYPE(he_base->type) != DT_BOOL)
192  return CSR_ERR_CODE;
193 
194  intptr_t value = cs_he_native_get(sub->cs, he, err);
195  if (value == INT_MIN)
196  return CSR_ERR_CODE;
197 
198  int rc = cs_he_native_set(sub->cs, he, !value, err);
199 
200  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
202 
203  return rc;
204 }
#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 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
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
#define DT_BOOL
boolean option
Definition: types.h:30
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bool_str_toggle()

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

Toggle the value of a bool.

Parameters
subConfig Subset
nameName of config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS

Definition at line 213 of file bool.c.

214 {
215  struct HashElem *he = cs_subset_create_inheritance(sub, name);
216 
217  return bool_he_toggle(sub, he, err);
218 }
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:185
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:
+ Here is the caller graph for this function:

Variable Documentation

◆ BoolValues

const char* BoolValues[]
Initial value:
= {
"no", "yes", "n", "y", "false", "true", "0", "1", "off", "on", NULL,
}

Valid strings for creating a Bool.

These strings are case-insensitive.

Definition at line 49 of file bool.c.

◆ cst_bool

const struct ConfigSetType cst_bool
Initial value:
= {
"boolean",
NULL,
NULL,
NULL,
}
static int bool_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Bool config item by bool - Implements ConfigSetType::native_set()
Definition: bool.c:124
static int bool_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Bool as a string - Implements ConfigSetType::string_get()
Definition: bool.c:104
static int bool_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Bool by string - Implements ConfigSetType::string_set()
Definition: bool.c:56
static intptr_t bool_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a bool from a Bool config item - Implements ConfigSetType::native_get()
Definition: bool.c:151
static int bool_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Bool to its initial value - Implements ConfigSetType::reset()
Definition: bool.c:160
#define DT_BOOL
boolean option
Definition: types.h:30

Config type representing an boolean.

Definition at line 223 of file bool.c.