NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
mbtable.c File Reference

Type representing a multibyte character table. More...

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

Go to the source code of this file.

Functions

struct MbTablembtable_parse (const char *s)
 Parse a multibyte string into a table. More...
 
static void mbtable_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy an MbTable object - Implements ConfigSetType::destroy() -. More...
 
static int mbtable_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set an MbTable by string - Implements ConfigSetType::string_set() -. More...
 
static int mbtable_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a MbTable as a string - Implements ConfigSetType::string_get() -. More...
 
static struct MbTablembtable_dup (struct MbTable *table)
 Create a copy of an MbTable object. More...
 
static int mbtable_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set an MbTable config item by MbTable object - Implements ConfigSetType::native_set() -. More...
 
static intptr_t mbtable_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get an MbTable object from a MbTable config item - Implements ConfigSetType::native_get() -. More...
 
static int mbtable_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset an MbTable to its initial value - Implements ConfigSetType::reset() -. More...
 
void mbtable_free (struct MbTable **table)
 Free an MbTable object. More...
 

Variables

const struct ConfigSetType CstMbtable
 Config type representing a multi-byte table. More...
 

Detailed Description

Type representing a multibyte character table.

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

Function Documentation

◆ mbtable_parse()

struct MbTable * mbtable_parse ( const char *  s)

Parse a multibyte string into a table.

Parameters
sString of multibyte characters
Return values
ptrNew MbTable object

Definition at line 49 of file mbtable.c.

50{
51 struct MbTable *t = NULL;
52 size_t slen, k;
53 mbstate_t mbstate = { 0 };
54 char *d = NULL;
55
56 slen = mutt_str_len(s);
57 if (!slen)
58 return NULL;
59
60 t = mutt_mem_calloc(1, sizeof(struct MbTable));
61
62 t->orig_str = mutt_str_dup(s);
63 /* This could be more space efficient. However, being used on tiny
64 * strings (`$to_chars` and `$status_chars`), the overhead is not great. */
65 t->chars = mutt_mem_calloc(slen, sizeof(char *));
66 t->segmented_str = mutt_mem_calloc(slen * 2, sizeof(char));
67 d = t->segmented_str;
68
69 while (slen && (k = mbrtowc(NULL, s, slen, &mbstate)))
70 {
71 if ((k == (size_t) (-1)) || (k == (size_t) (-2)))
72 {
73 /* XXX put message in err buffer; fail? warning? */
74 mutt_debug(LL_DEBUG1, "mbrtowc returned %d converting %s in %s\n",
75 (k == (size_t) (-1)) ? -1 : -2, s, t->orig_str);
76 if (k == (size_t) (-1))
77 memset(&mbstate, 0, sizeof(mbstate));
78 k = (k == (size_t) (-1)) ? 1 : slen;
79 }
80
81 slen -= k;
82 t->chars[t->len++] = d;
83 while (k--)
84 *d++ = *s++;
85 *d++ = '\0';
86 }
87
88 return t;
89}
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:567
Multibyte character table.
Definition: mbtable.h:34
char * orig_str
Original string used to generate this object.
Definition: mbtable.h:35
int len
Number of characters.
Definition: mbtable.h:36
char ** chars
The array of multibyte character strings.
Definition: mbtable.h:37
char * segmented_str
Each chars entry points inside this string.
Definition: mbtable.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbtable_dup()

static struct MbTable * mbtable_dup ( struct MbTable table)
static

Create a copy of an MbTable object.

Parameters
tableMbTable to duplicate
Return values
ptrNew MbTable object

Definition at line 184 of file mbtable.c.

185{
186 if (!table)
187 return NULL; /* LCOV_EXCL_LINE */
188
189 struct MbTable *m = mutt_mem_calloc(1, sizeof(*m));
190 m->orig_str = mutt_str_dup(table->orig_str);
191 return m;
192}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbtable_free()

void mbtable_free ( struct MbTable **  table)

Free an MbTable object.

Parameters
[out]tableMbTable to free

Definition at line 280 of file mbtable.c.

281{
282 if (!table || !*table)
283 return;
284
285 FREE(&(*table)->orig_str);
286 FREE(&(*table)->chars);
287 FREE(&(*table)->segmented_str);
288 FREE(table);
289}
#define FREE(x)
Definition: memory.h:43
+ Here is the caller graph for this function:

Variable Documentation

◆ CstMbtable

const struct ConfigSetType CstMbtable
Initial value:
= {
"mbtable",
NULL,
NULL,
}
static void mbtable_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an MbTable object - Implements ConfigSetType::destroy() -.
Definition: mbtable.c:94
static intptr_t mbtable_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an MbTable object from a MbTable config item - Implements ConfigSetType::native_get() -.
Definition: mbtable.c:226
static int mbtable_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an MbTable config item by MbTable object - Implements ConfigSetType::native_set() -.
Definition: mbtable.c:197
static int mbtable_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an MbTable to its initial value - Implements ConfigSetType::reset() -.
Definition: mbtable.c:237
static int mbtable_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a MbTable as a string - Implements ConfigSetType::string_get() -.
Definition: mbtable.c:158
static int mbtable_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an MbTable by string - Implements ConfigSetType::string_set() -.
Definition: mbtable.c:106
#define DT_MBTABLE
multibyte char table
Definition: types.h:34

Config type representing a multi-byte table.

Definition at line 294 of file mbtable.c.