NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mbtable.h File Reference

Type representing a multibyte character table. More...

#include <stdbool.h>
+ Include dependency graph for mbtable.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  MbTable
 Multibyte character table. More...
 

Functions

bool mbtable_equal (const struct MbTable *a, const struct MbTable *b)
 Compare two MbTables.
 
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.
 
struct MbTablembtable_parse (const char *str)
 Parse a multibyte string into a table.
 

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.h.

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:666
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_free()

void mbtable_free ( struct MbTable ** ptr)

Free an MbTable object.

Parameters
[out]ptrMbTable to free

Definition at line 318 of file mbtable.c.

319{
320 if (!ptr || !*ptr)
321 return;
322
323 struct MbTable *table = *ptr;
324 FREE(&table->orig_str);
325 FREE(&table->chars);
326 FREE(&table->segmented_str);
327
328 FREE(ptr);
329}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
Multibyte character table.
Definition mbtable.h:36
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 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 341 of file mbtable.c.

342{
343 if (!table || !table->chars || (index < 0) || (index >= table->len))
344 return " ";
345
346 if (table->chars[index][0] == '\r')
347 return "";
348
349 return table->chars[index];
350}
int len
Number of characters.
Definition mbtable.h:38
+ 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;
70 size_t k;
71 mbstate_t mbstate = { 0 };
72 char *d = NULL;
73
74 slen = mutt_str_len(s);
75 if (!slen)
76 return NULL;
77
78 t = MUTT_MEM_CALLOC(1, struct MbTable);
79
80 t->orig_str = mutt_str_dup(s);
81 /* This could be more space efficient. However, being used on tiny
82 * strings (`$to_chars` and `$status_chars`), the overhead is not great. */
83 t->chars = MUTT_MEM_CALLOC(slen, char *);
84 t->segmented_str = MUTT_MEM_CALLOC(slen * 2, char);
85 d = t->segmented_str;
86
87 while (slen && (k = mbrtowc(NULL, s, slen, &mbstate)))
88 {
89 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
90 {
91 /* XXX put message in err buffer; fail? warning? */
92 mutt_debug(LL_DEBUG1, "mbrtowc returned %zd converting %s in %s\n", k, s, t->orig_str);
93 if (k == ICONV_ILLEGAL_SEQ)
94 memset(&mbstate, 0, sizeof(mbstate));
95 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : slen;
96 }
97
98 slen -= k;
99 t->chars[t->len++] = d;
100 while (k-- > 0)
101 *d++ = *s++;
102 *d++ = '\0';
103 }
104
105 return t;
106}
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:116
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:114
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
+ Here is the call graph for this function:
+ Here is the caller graph for this function: