NeoMutt  2019-12-07
Teaching an old dog new tricks
DOXYGEN
parameter.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <stdbool.h>
32 #include "mutt/mutt.h"
33 #include "parameter.h"
34 
40 {
41  return mutt_mem_calloc(1, sizeof(struct Parameter));
42 }
43 
49 {
50  if (!p || !*p)
51  return;
52  FREE(&(*p)->attribute);
53  FREE(&(*p)->value);
54  FREE(p);
55 }
56 
61 void mutt_param_free(struct ParameterList *pl)
62 {
63  if (!pl)
64  return;
65 
66  struct Parameter *np = TAILQ_FIRST(pl);
67  struct Parameter *next = NULL;
68  while (np)
69  {
70  next = TAILQ_NEXT(np, entries);
72  np = next;
73  }
74  TAILQ_INIT(pl);
75 }
76 
84 char *mutt_param_get(const struct ParameterList *pl, const char *s)
85 {
86  if (!pl)
87  return NULL;
88 
89  struct Parameter *np = NULL;
90  TAILQ_FOREACH(np, pl, entries)
91  {
92  if (mutt_str_strcasecmp(s, np->attribute) == 0)
93  return np->value;
94  }
95 
96  return NULL;
97 }
98 
110 void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
111 {
112  if (!pl)
113  return;
114 
115  if (!value)
116  {
117  mutt_param_delete(pl, attribute);
118  return;
119  }
120 
121  struct Parameter *np = NULL;
122  TAILQ_FOREACH(np, pl, entries)
123  {
124  if (mutt_str_strcasecmp(attribute, np->attribute) == 0)
125  {
126  mutt_str_replace(&np->value, value);
127  return;
128  }
129  }
130 
131  np = mutt_param_new();
132  np->attribute = mutt_str_strdup(attribute);
133  np->value = mutt_str_strdup(value);
134  TAILQ_INSERT_HEAD(pl, np, entries);
135 }
136 
142 void mutt_param_delete(struct ParameterList *pl, const char *attribute)
143 {
144  if (!pl)
145  return;
146 
147  struct Parameter *np = NULL;
148  TAILQ_FOREACH(np, pl, entries)
149  {
150  if (mutt_str_strcasecmp(attribute, np->attribute) == 0)
151  {
152  TAILQ_REMOVE(pl, np, entries);
153  mutt_param_free_one(&np);
154  return;
155  }
156  }
157 }
158 
165 bool mutt_param_cmp_strict(const struct ParameterList *pl1, const struct ParameterList *pl2)
166 {
167  if (!pl1 && !pl2)
168  return false;
169 
170  if ((pl1 == NULL) ^ (pl2 == NULL))
171  return true;
172 
173  struct Parameter *np1 = TAILQ_FIRST(pl1);
174  struct Parameter *np2 = TAILQ_FIRST(pl2);
175 
176  while (np1 && np2)
177  {
178  if ((mutt_str_strcmp(np1->attribute, np2->attribute) != 0) ||
179  (mutt_str_strcmp(np1->value, np2->value) != 0))
180  {
181  return false;
182  }
183 
184  np1 = TAILQ_NEXT(np1, entries);
185  np2 = TAILQ_NEXT(np2, entries);
186  }
187 
188  if (np1 || np2)
189  return false;
190 
191  return true;
192 }
char * attribute
Parameter name.
Definition: parameter.h:34
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define TAILQ_FIRST(head)
Definition: queue.h:717
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:719
struct Parameter * mutt_param_new(void)
Create a new Parameter.
Definition: parameter.c:39
#define TAILQ_INIT(head)
Definition: queue.h:759
void mutt_param_free(struct ParameterList *pl)
Free a ParameterList.
Definition: parameter.c:61
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:821
void mutt_param_delete(struct ParameterList *pl, const char *attribute)
Delete a matching Parameter.
Definition: parameter.c:142
void mutt_param_free_one(struct Parameter **p)
Free a Parameter.
Definition: parameter.c:48
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:453
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:790
char * value
Parameter value.
Definition: parameter.h:35
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
Attribute associated with a MIME part.
Definition: parameter.h:32
#define FREE(x)
Definition: memory.h:40
char * mutt_param_get(const struct ParameterList *pl, const char *s)
Find a matching Parameter.
Definition: parameter.c:84
#define TAILQ_NEXT(elm, field)
Definition: queue.h:816
bool mutt_param_cmp_strict(const struct ParameterList *pl1, const struct ParameterList *pl2)
Strictly compare two ParameterLists.
Definition: parameter.c:165
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
Store attributes associated with a MIME part.
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:110