NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
sort.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <stdint.h>
35 #include <string.h>
36 #include "mutt/lib.h"
37 #include "set.h"
38 #include "sort2.h"
39 #include "types.h"
40 
41 #define PREFIX_REVERSE "reverse-"
42 #define PREFIX_LAST "last-"
43 
47 static int sort_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
48  const char *value, struct Buffer *err)
49 {
50  intptr_t id = -1;
51  uint16_t flags = 0;
52 
53  if (!value || (value[0] == '\0'))
54  {
55  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
57  }
58 
59  size_t plen = 0;
60 
61  if (cdef->type & DT_SORT_REVERSE)
62  {
64  if (plen != 0)
65  {
66  flags |= SORT_REVERSE;
67  value += plen;
68  }
69  }
70 
71  if (cdef->type & DT_SORT_LAST)
72  {
74  if (plen != 0)
75  {
76  flags |= SORT_LAST;
77  value += plen;
78  }
79  }
80 
81  id = mutt_map_get_value(value, (struct Mapping *) cdef->data);
82 
83  if (id < 0)
84  {
85  mutt_buffer_printf(err, _("Invalid sort name: %s"), value);
87  }
88 
89  id |= flags;
90 
91  if (var)
92  {
93  if (id == (*(short *) var))
95 
96  if (cdef->validator)
97  {
98  int rc = cdef->validator(cs, cdef, (intptr_t) id, err);
99 
100  if (CSR_RESULT(rc) != CSR_SUCCESS)
101  return rc | CSR_INV_VALIDATOR;
102  }
103 
104  *(short *) var = id;
105  }
106  else
107  {
108  cdef->initial = id;
109  }
110 
111  return CSR_SUCCESS;
112 }
113 
117 static int sort_string_get(const struct ConfigSet *cs, void *var,
118  const struct ConfigDef *cdef, struct Buffer *result)
119 {
120  int sort;
121 
122  if (var)
123  sort = *(short *) var;
124  else
125  sort = (int) cdef->initial;
126 
127  if (sort & SORT_REVERSE)
129  if (sort & SORT_LAST)
131 
132  sort &= SORT_MASK;
133 
134  const char *str = NULL;
135 
136  str = mutt_map_get_name(sort, (struct Mapping *) cdef->data);
137 
138  if (!str)
139  {
140  mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d/%d\n",
141  cdef->type & DT_SUBTYPE_MASK, sort);
142  return CSR_ERR_INVALID | CSR_INV_TYPE;
143  }
144 
145  mutt_buffer_addstr(result, str);
146  return CSR_SUCCESS;
147 }
148 
152 static int sort_native_set(const struct ConfigSet *cs, void *var,
153  const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
154 {
155  const char *str = NULL;
156 
157  str = mutt_map_get_name((value & SORT_MASK), (struct Mapping *) cdef->data);
158 
159  if (!str)
160  {
161  mutt_buffer_printf(err, _("Invalid sort type: %ld"), value);
162  return CSR_ERR_INVALID | CSR_INV_TYPE;
163  }
164 
165  if (value == (*(short *) var))
167 
168  if (cdef->validator)
169  {
170  int rc = cdef->validator(cs, cdef, value, err);
171 
172  if (CSR_RESULT(rc) != CSR_SUCCESS)
173  return rc | CSR_INV_VALIDATOR;
174  }
175 
176  *(short *) var = value;
177  return CSR_SUCCESS;
178 }
179 
183 static intptr_t sort_native_get(const struct ConfigSet *cs, void *var,
184  const struct ConfigDef *cdef, struct Buffer *err)
185 {
186  return *(short *) var;
187 }
188 
192 static int sort_reset(const struct ConfigSet *cs, void *var,
193  const struct ConfigDef *cdef, struct Buffer *err)
194 {
195  if (cdef->initial == (*(short *) var))
197 
198  if (cdef->validator)
199  {
200  int rc = cdef->validator(cs, cdef, cdef->initial, err);
201 
202  if (CSR_RESULT(rc) != CSR_SUCCESS)
203  return rc | CSR_INV_VALIDATOR;
204  }
205 
206  *(short *) var = cdef->initial;
207  return CSR_SUCCESS;
208 }
209 
213 const struct ConfigSetType CstSort = {
214  DT_SORT,
215  "sort",
220  NULL, // string_plus_equals
221  NULL, // string_minus_equals
222  sort_reset,
223  NULL, // destroy
224 };
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:223
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
#define PREFIX_REVERSE
Definition: sort.c:41
#define PREFIX_LAST
Definition: sort.c:42
const struct ConfigSetType CstSort
Config type representing a sort option.
Definition: sort.c:213
static intptr_t sort_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int from a Sort config item - Implements ConfigSetType::native_get() -.
Definition: sort.c:183
static int sort_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Sort config item by int - Implements ConfigSetType::native_set() -.
Definition: sort.c:152
static int sort_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Sort to its initial value - Implements ConfigSetType::reset() -.
Definition: sort.c:192
static int sort_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Sort as a string - Implements ConfigSetType::string_get() -.
Definition: sort.c:117
static int sort_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Sort by string - Implements ConfigSetType::string_set() -.
Definition: sort.c:47
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:227
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
A collection of config items.
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#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
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
Type representing a sort option.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
#define DT_SORT_REVERSE
Sort flag for -reverse prefix.
Definition: sort2.h:36
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort2.h:80
#define DT_SORT_LAST
Sort flag for -last prefix.
Definition: sort2.h:35
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:79
String manipulation buffer.
Definition: buffer.h:34
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t data
Extra variable data.
Definition: set.h:68
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:260
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Constants for all the config types.
#define DT_SORT
sorting methods
Definition: types.h:40
#define DT_SUBTYPE_MASK
Mask for the Data Subtype.
Definition: types.h:61