NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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

bool mbtable_equal (const struct MbTable *a, const struct MbTable *b)
 Compare two MbTables.
 
struct MbTablembtable_parse (const char *s)
 Parse a multibyte string into a table.
 
static void mbtable_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy an MbTable object - Implements ConfigSetType::destroy() -.
 
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() -.
 
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() -.
 
static struct MbTablembtable_dup (struct MbTable *table)
 Create a copy of an MbTable object.
 
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() -.
 
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() -.
 
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() -.
 
void mbtable_free (struct MbTable **ptr)
 Free an MbTable object.
 
const char * mbtable_get_nth_wchar (const struct MbTable *table, int index)
 Extract one char from a multi-byte table.
 

Variables

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

Detailed Description

Type representing a multibyte character table.

Authors
  • Richard Russon
  • Pietro Cerutti

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_equal()

bool mbtable_equal ( const struct MbTable a,
const struct MbTable b 
)

Compare two MbTables.

Parameters
aFirst MbTable
bSecond MbTable
Return values
trueThey are identical

Definition at line 51 of file mbtable.c.

52{
53 if (!a && !b) /* both empty */
54 return true;
55 if (!a ^ !b) /* one is empty, but not the other */
56 return false;
57
58 return mutt_str_equal(a->orig_str, b->orig_str);
59}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
char * orig_str
Original string used to generate this object.
Definition: mbtable.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 66 of file mbtable.c.

67{
68 struct MbTable *t = NULL;
69 size_t slen, k;
70 mbstate_t mbstate = { 0 };
71 char *d = NULL;
72
73 slen = mutt_str_len(s);
74 if (!slen)
75 return NULL;
76
77 t = mutt_mem_calloc(1, sizeof(struct MbTable));
78
79 t->orig_str = mutt_str_dup(s);
80 /* This could be more space efficient. However, being used on tiny
81 * strings (`$to_chars` and `$status_chars`), the overhead is not great. */
82 t->chars = mutt_mem_calloc(slen, sizeof(char *));
83 t->segmented_str = mutt_mem_calloc(slen * 2, sizeof(char));
84 d = t->segmented_str;
85
86 while (slen && (k = mbrtowc(NULL, s, slen, &mbstate)))
87 {
88 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
89 {
90 /* XXX put message in err buffer; fail? warning? */
91 mutt_debug(LL_DEBUG1, "mbrtowc returned %zd converting %s in %s\n", k, s, t->orig_str);
92 if (k == ICONV_ILLEGAL_SEQ)
93 memset(&mbstate, 0, sizeof(mbstate));
94 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : slen;
95 }
96
97 slen -= k;
98 t->chars[t->len++] = d;
99 while (k--)
100 *d++ = *s++;
101 *d++ = '\0';
102 }
103
104 return t;
105}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:106
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:104
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:490
Multibyte character table.
Definition: mbtable.h:36
int len
Number of characters.
Definition: mbtable.h:38
char ** chars
The array of multibyte character strings.
Definition: mbtable.h:39
char * segmented_str
Each chars entry points inside this string.
Definition: mbtable.h:40
+ 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 203 of file mbtable.c.

204{
205 if (!table)
206 return NULL; /* LCOV_EXCL_LINE */
207
208 struct MbTable *m = mutt_mem_calloc(1, sizeof(*m));
209 m->orig_str = mutt_str_dup(table->orig_str);
210 return m;
211}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mbtable_free()

void mbtable_free ( struct MbTable **  ptr)

Free an MbTable object.

Parameters
[out]ptrMbTable to free

Definition at line 308 of file mbtable.c.

309{
310 if (!ptr || !*ptr)
311 return;
312
313 struct MbTable *table = *ptr;
314 FREE(&table->orig_str);
315 FREE(&table->chars);
316 FREE(&table->segmented_str);
317
318 FREE(ptr);
319}
#define FREE(x)
Definition: memory.h:45
+ Here is the caller graph for this function:

◆ mbtable_get_nth_wchar()

const char * mbtable_get_nth_wchar ( const struct MbTable table,
int  index 
)

Extract one char from a multi-byte table.

Parameters
tableMulti-byte table
indexSelect this character
Return values
ptrString pointer to the character

Extract one multi-byte character from a string table. If the index is invalid, then a space character will be returned. If the character selected is '
' (Ctrl-M), then "" will be returned.

Definition at line 331 of file mbtable.c.

332{
333 if (!table || !table->chars || (index < 0) || (index >= table->len))
334 return " ";
335
336 if (table->chars[index][0] == '\r')
337 return "";
338
339 return table->chars[index];
340}
+ 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:110
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:251
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:216
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:262
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:177
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:122
@ DT_MBTABLE
multibyte char table
Definition: types.h:37

Config type representing a multi-byte table.

Definition at line 345 of file mbtable.c.