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

Type representing a list of strings. 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 slist.c:

Go to the source code of this file.

Functions

static void slist_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy an Slist object - Implements ConfigSetType::destroy() More...
 
static int slist_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Slist by string - Implements ConfigSetType::string_set() More...
 
static int slist_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Slist as a string - Implements ConfigSetType::string_get() More...
 
static int slist_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Slist config item by Slist - Implements ConfigSetType::native_set() More...
 
static intptr_t slist_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get a Slist from a Slist config item - Implements ConfigSetType::native_get() More...
 
static int slist_string_plus_equals (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Add to a Slist by string - Implements ConfigSetType::string_plus_equals() More...
 
static int slist_string_minus_equals (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Remove from a Slist by string - Implements ConfigSetType::string_plus_equals() More...
 
static int slist_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Slist to its initial value - Implements ConfigSetType::reset() More...
 

Variables

const struct ConfigSetType cst_slist
 Config type representing a list of strings. More...
 

Detailed Description

Type representing a list of strings.

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

Function Documentation

◆ slist_destroy()

static void slist_destroy ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef 
)
static

Destroy an Slist object - Implements ConfigSetType::destroy()

Definition at line 45 of file slist.c.

46 {
47  if (!cs || !var || !cdef)
48  return; /* LCOV_EXCL_LINE */
49 
50  struct Slist **l = (struct Slist **) var;
51  if (!*l)
52  return;
53 
54  slist_free(l);
55 }
String list.
Definition: slist.h:45
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_string_set()

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

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

Definition at line 60 of file slist.c.

62 {
63  if (!cs || !cdef)
64  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
65 
66  /* Store empty string list as NULL */
67  if (value && (value[0] == '\0'))
68  value = NULL;
69 
70  struct Slist *list = NULL;
71 
72  int rc = CSR_SUCCESS;
73 
74  if (var)
75  {
76  list = slist_parse(value, cdef->type);
77 
78  if (cdef->validator)
79  {
80  rc = cdef->validator(cs, cdef, (intptr_t) list, err);
81 
82  if (CSR_RESULT(rc) != CSR_SUCCESS)
83  {
84  slist_free(&list);
85  return (rc | CSR_INV_VALIDATOR);
86  }
87  }
88 
89  slist_destroy(cs, var, cdef);
90 
91  *(struct Slist **) var = list;
92 
93  if (!list)
94  rc |= CSR_SUC_EMPTY;
95  }
96  else
97  {
98  if (cdef->type & DT_INITIAL_SET)
99  FREE(&cdef->initial);
100 
101  cdef->type |= DT_INITIAL_SET;
102  cdef->initial = IP mutt_str_dup(value);
103  }
104 
105  return rc;
106 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define IP
Definition: set.h:54
static void slist_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Slist object - Implements ConfigSetType::destroy()
Definition: slist.c:45
intptr_t initial
Initial value.
Definition: set.h:66
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:81
struct Slist * slist_parse(const char *str, int flags)
Parse a list of strings into a list.
Definition: slist.c:200
String list.
Definition: slist.h:45
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
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 FREE(x)
Definition: memory.h:40
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ slist_string_get()

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

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

Definition at line 111 of file slist.c.

113 {
114  if (!cs || !cdef)
115  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
116 
117  if (var)
118  {
119  struct Slist *list = *(struct Slist **) var;
120  if (!list)
121  return (CSR_SUCCESS | CSR_SUC_EMPTY); /* empty string */
122 
123  slist_to_buffer(list, result);
124  }
125  else
126  {
127  mutt_buffer_addstr(result, (char *) cdef->initial);
128  }
129 
130  int rc = CSR_SUCCESS;
131  if (mutt_buffer_is_empty(result))
132  rc |= CSR_SUC_EMPTY;
133 
134  return rc;
135 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
String list.
Definition: slist.h:45
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
int slist_to_buffer(const struct Slist *list, struct Buffer *buf)
Export an Slist to a Buffer.
Definition: slist.c:292
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
+ Here is the call graph for this function:

◆ slist_native_set()

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

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

Definition at line 140 of file slist.c.

142 {
143  if (!cs || !var || !cdef)
144  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
145 
146  int rc;
147 
148  if (cdef->validator)
149  {
150  rc = cdef->validator(cs, cdef, value, err);
151 
152  if (CSR_RESULT(rc) != CSR_SUCCESS)
153  return (rc | CSR_INV_VALIDATOR);
154  }
155 
156  slist_free(var);
157 
158  struct Slist *list = slist_dup((struct Slist *) value);
159 
160  rc = CSR_SUCCESS;
161  if (!list)
162  rc |= CSR_SUC_EMPTY;
163 
164  *(struct Slist **) var = list;
165  return rc;
166 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:120
String list.
Definition: slist.h:45
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
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
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ slist_native_get()

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

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

Definition at line 171 of file slist.c.

173 {
174  if (!cs || !var || !cdef)
175  return INT_MIN; /* LCOV_EXCL_LINE */
176 
177  struct Slist *list = *(struct Slist **) var;
178 
179  return (intptr_t) list;
180 }
String list.
Definition: slist.h:45

◆ slist_string_plus_equals()

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

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

Definition at line 185 of file slist.c.

188 {
189  if (!cs || !cdef)
190  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
191 
192  int rc = CSR_SUCCESS;
193 
194  /* Store empty strings as NULL */
195  if (value && (value[0] == '\0'))
196  return rc |= CSR_SUC_NO_CHANGE;
197 
198  struct Slist *orig = *(struct Slist **) var;
199  if (slist_is_member(orig, value))
200  return rc |= CSR_SUC_NO_CHANGE;
201 
202  struct Slist *copy = slist_dup(orig);
203  if (!copy)
204  copy = slist_new(cdef->type & SLIST_SEP_MASK);
205 
206  slist_add_string(copy, value);
207 
208  if (cdef->validator)
209  {
210  rc = cdef->validator(cs, cdef, (intptr_t) copy, err);
211  if (CSR_RESULT(rc) != CSR_SUCCESS)
212  {
213  slist_free(&copy);
214  return (rc | CSR_INV_VALIDATOR);
215  }
216  }
217 
218  slist_free(&orig);
219  *(struct Slist **) var = copy;
220 
221  return rc;
222 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define SLIST_SEP_MASK
Definition: slist.h:36
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:120
struct Slist * slist_add_string(struct Slist *list, const char *str)
Add a string to a list.
Definition: slist.c:80
String list.
Definition: slist.h:45
#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
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:177
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
struct Slist * slist_new(int flags)
Create a new string list.
Definition: slist.c:43
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ slist_string_minus_equals()

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

Remove from a Slist by string - Implements ConfigSetType::string_plus_equals()

Definition at line 227 of file slist.c.

230 {
231  if (!cs || !cdef)
232  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
233 
234  int rc = CSR_SUCCESS;
235 
236  /* Store empty strings as NULL */
237  if (value && (value[0] == '\0'))
238  return rc |= CSR_SUC_NO_CHANGE;
239 
240  struct Slist *orig = *(struct Slist **) var;
241  if (!slist_is_member(orig, value))
242  return rc |= CSR_SUC_NO_CHANGE;
243 
244  struct Slist *copy = slist_dup(orig);
245  slist_remove_string(copy, value);
246 
247  if (cdef->validator)
248  {
249  rc = cdef->validator(cs, cdef, (intptr_t) copy, err);
250  if (CSR_RESULT(rc) != CSR_SUCCESS)
251  {
252  slist_free(&copy);
253  return (rc | CSR_INV_VALIDATOR);
254  }
255  }
256 
257  slist_free(&orig);
258  *(struct Slist **) var = copy;
259 
260  return rc;
261 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:120
String list.
Definition: slist.h:45
#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
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:177
struct Slist * slist_remove_string(struct Slist *list, const char *str)
Remove a string from a list.
Definition: slist.c:258
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ slist_reset()

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

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

Definition at line 266 of file slist.c.

268 {
269  if (!cs || !var || !cdef)
270  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
271 
272  struct Slist *list = NULL;
273  const char *initial = (const char *) cdef->initial;
274 
275  if (initial)
276  list = slist_parse(initial, cdef->type);
277 
278  int rc = CSR_SUCCESS;
279 
280  if (cdef->validator)
281  {
282  rc = cdef->validator(cs, cdef, (intptr_t) list, err);
283 
284  if (CSR_RESULT(rc) != CSR_SUCCESS)
285  {
286  slist_destroy(cs, &list, cdef);
287  return (rc | CSR_INV_VALIDATOR);
288  }
289  }
290 
291  if (!list)
292  rc |= CSR_SUC_EMPTY;
293 
294  slist_destroy(cs, var, cdef);
295 
296  *(struct Slist **) var = list;
297  return rc;
298 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
static void slist_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Slist object - Implements ConfigSetType::destroy()
Definition: slist.c:45
intptr_t initial
Initial value.
Definition: set.h:66
struct Slist * slist_parse(const char *str, int flags)
Parse a list of strings into a list.
Definition: slist.c:200
String list.
Definition: slist.h:45
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
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_ERR_CODE
Problem with the code.
Definition: set.h:36
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

Variable Documentation

◆ cst_slist

const struct ConfigSetType cst_slist
Initial value:
= {
"slist",
}
static void slist_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Slist object - Implements ConfigSetType::destroy()
Definition: slist.c:45
static int slist_string_minus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Remove from a Slist by string - Implements ConfigSetType::string_plus_equals()
Definition: slist.c:227
static int slist_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Slist as a string - Implements ConfigSetType::string_get()
Definition: slist.c:111
static int slist_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a Slist by string - Implements ConfigSetType::string_plus_equals()
Definition: slist.c:185
#define DT_SLIST
a list of strings
Definition: types.h:39
static int slist_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Slist to its initial value - Implements ConfigSetType::reset()
Definition: slist.c:266
static intptr_t slist_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a Slist from a Slist config item - Implements ConfigSetType::native_get()
Definition: slist.c:171
static int slist_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Slist by string - Implements ConfigSetType::string_set()
Definition: slist.c:60
static int slist_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Slist config item by Slist - Implements ConfigSetType::native_set()
Definition: slist.c:140

Config type representing a list of strings.

Definition at line 303 of file slist.c.