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

Type representing a sort option. More...

#include "config.h"
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include "mutt/lib.h"
#include "set.h"
#include "sort2.h"
#include "types.h"
+ Include dependency graph for sort.c:

Go to the source code of this file.

Macros

#define PREFIX_REVERSE   "reverse-"
 
#define PREFIX_LAST   "last-"
 

Functions

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() More...
 
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() More...
 
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() More...
 
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() More...
 
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() More...
 

Variables

const struct ConfigSetType cst_sort
 Config type representing a sort option. More...
 

Detailed Description

Type representing a sort option.

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

Macro Definition Documentation

◆ PREFIX_REVERSE

#define PREFIX_REVERSE   "reverse-"

Definition at line 41 of file sort.c.

◆ PREFIX_LAST

#define PREFIX_LAST   "last-"

Definition at line 42 of file sort.c.

Function Documentation

◆ sort_string_set()

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

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

Definition at line 47 of file sort.c.

49 {
50  intptr_t id = -1;
51  int 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  {
63  plen = mutt_str_startswith(value, PREFIX_REVERSE);
64  if (plen != 0)
65  {
66  flags |= SORT_REVERSE;
67  value += plen;
68  }
69  }
70 
71  if (cdef->type & DT_SORT_LAST)
72  {
73  plen = mutt_str_startswith(value, PREFIX_LAST);
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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
intptr_t initial
Initial value.
Definition: set.h:66
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
#define _(a)
Definition: message.h:28
intptr_t data
Extra variable data.
Definition: set.h:67
#define DT_SORT_REVERSE
Sort flag for -reverse prefix.
Definition: sort2.h:38
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 DT_SORT_LAST
Sort flag for -last prefix.
Definition: sort2.h:37
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
User-visible name.
Definition: set.h:63
#define PREFIX_LAST
Definition: sort.c:42
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:160
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 PREFIX_REVERSE
Definition: sort.c:41
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
Mapping between user-readable string and a constant.
Definition: mapping.h:31
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:81
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort2.h:82
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
+ Here is the call graph for this function:

◆ sort_string_get()

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

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

Definition at line 117 of file sort.c.

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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
intptr_t initial
Initial value.
Definition: set.h:66
#define DT_SUBTYPE_MASK
Mask for the Data Subtype.
Definition: types.h:61
intptr_t data
Extra variable data.
Definition: set.h:67
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define PREFIX_LAST
Definition: sort.c:42
#define PREFIX_REVERSE
Definition: sort.c:41
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
Log at debug level 1.
Definition: logging.h:40
Mapping between user-readable string and a constant.
Definition: mapping.h:31
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:81
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort2.h:82
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:80
+ Here is the call graph for this function:

◆ sort_native_set()

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

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

Definition at line 152 of file sort.c.

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 }
#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
intptr_t data
Extra variable data.
Definition: set.h:67
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
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
#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
Mapping between user-readable string and a constant.
Definition: mapping.h:31
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:80
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ sort_native_get()

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

Get an int from a Sort config item - Implements ConfigSetType::native_get()

Definition at line 183 of file sort.c.

185 {
186  return *(short *) var;
187 }

◆ sort_reset()

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

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

Definition at line 192 of file sort.c.

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 }
#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

Variable Documentation

◆ cst_sort

const struct ConfigSetType cst_sort
Initial value:
= {
"sort",
NULL,
NULL,
NULL,
}
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
#define DT_SORT
sorting methods
Definition: types.h:40
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_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
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_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

Config type representing a sort option.

Definition at line 213 of file sort.c.