NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
myvar.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stddef.h>
31#include "mutt/lib.h"
32#include "myvar.h"
33
35
44static struct MyVar *myvar_new(const char *name, const char *value)
45{
46 struct MyVar *myv = mutt_mem_calloc(1, sizeof(struct MyVar));
47 myv->name = mutt_str_dup(name);
48 myv->value = mutt_str_dup(value);
49 return myv;
50}
51
56static void myvar_free(struct MyVar **ptr)
57{
58 if (!ptr || !*ptr)
59 return;
60
61 struct MyVar *myv = *ptr;
62 FREE(&myv->name);
63 FREE(&myv->value);
64 FREE(ptr);
65}
66
73static struct MyVar *myvar_find(const char *var)
74{
75 struct MyVar *myv = NULL;
76
77 TAILQ_FOREACH(myv, &MyVars, entries)
78 {
79 if (mutt_str_equal(myv->name, var))
80 return myv;
81 }
82
83 return NULL;
84}
85
92const char *myvar_get(const char *var)
93{
94 struct MyVar *myv = myvar_find(var);
95
96 if (myv)
97 {
98 return NONULL(myv->value);
99 }
100
101 return NULL;
102}
103
109void myvar_set(const char *var, const char *val)
110{
111 struct MyVar *myv = myvar_find(var);
112
113 if (myv)
114 {
115 mutt_str_replace(&myv->value, val);
116 return;
117 }
118
119 myv = myvar_new(var, val);
120 TAILQ_INSERT_TAIL(&MyVars, myv, entries);
121}
122
128void myvar_append(const char *var, const char *val)
129{
130 struct MyVar *myv = myvar_find(var);
131
132 if (myv)
133 {
134 mutt_str_append_item(&myv->value, val, '\0');
135 return;
136 }
137
138 myv = myvar_new(var, val);
139 TAILQ_INSERT_TAIL(&MyVars, myv, entries);
140}
141
146void myvar_del(const char *var)
147{
148 struct MyVar *myv = myvar_find(var);
149
150 if (myv)
151 {
152 TAILQ_REMOVE(&MyVars, myv, entries);
153 myvar_free(&myv);
154 }
155}
156
161void myvarlist_free(struct MyVarList *list)
162{
163 if (!list)
164 return;
165
166 struct MyVar *myv = NULL;
167 struct MyVar *tmp = NULL;
168 TAILQ_FOREACH_SAFE(myv, list, entries, tmp)
169 {
170 TAILQ_REMOVE(list, myv, entries);
171 myvar_free(&myv);
172 }
173}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
void mutt_str_append_item(char **str, const char *item, char sep)
Add string to another separated by sep.
Definition: string.c:346
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
void myvar_del(const char *var)
Unset the value of a "my_" variable.
Definition: myvar.c:146
struct MyVarList MyVars
List of all the user's custom config variables.
Definition: myvar.c:34
static struct MyVar * myvar_find(const char *var)
Locate a "my_" variable.
Definition: myvar.c:73
static struct MyVar * myvar_new(const char *name, const char *value)
Create a new MyVar.
Definition: myvar.c:44
void myvar_set(const char *var, const char *val)
Set the value of a "my_" variable.
Definition: myvar.c:109
static void myvar_free(struct MyVar **ptr)
Free a MyVar.
Definition: myvar.c:56
void myvar_append(const char *var, const char *val)
Append to the value of a "my_" variable.
Definition: myvar.c:128
const char * myvar_get(const char *var)
Get the value of a "my_" variable.
Definition: myvar.c:92
void myvarlist_free(struct MyVarList *list)
Free a List of MyVars.
Definition: myvar.c:161
Handling of personal config ('my' variables)
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
#define NONULL(x)
Definition: string2.h:37
A user-set variable.
Definition: myvar.h:32
char * name
Name of user variable.
Definition: myvar.h:33
char * value
Value of user variable.
Definition: myvar.h:34