ztring.c: A few important extension for <string.h>
 All Classes Files Functions Variables Enumerator Friends Macros
ztring.h
Go to the documentation of this file.
1 /* ztring.h (C) 2014 adolfo@di-mare.com */
2 
3 /** \file ztring.h
4  \brief A few string functions to enhance C's <string.h> library.
5  \author Adolfo Di Mare <adolfo@di-mare.com>
6  \date 2014
7 */
8 
9 #ifndef ztring_h
10 #define ztring_h
11 
12 #ifdef __cplusplus
13  #include <cstring>
14  #include <climits> // CHAR_BIT
15  #include <cstddef> // size_t && NULL
16  extern "C" {
17 #else
18  #include <string.h>
19  #include <limits.h> /* CHAR_BIT */
20  #include <stddef.h> /* size_t && NULL */
21 #endif
22 
23 /* 'size' checked versions of 'strcpy()' && 'strcat()' */
24 char* ztrcpy( size_t size, char * dest, const char * src );
25 char* ztrcat( size_t size, char * dest, const char * src );
26 
27 /* insert, delete and substring (with 'size' check) */
28 char* ztrins( size_t size, char * dest, size_t i, const char *insert );
29 char* strdel( char * dest, size_t len );
30 char* ztrsub( size_t size, char * dest, const char *src, size_t len );
31 
32 /* trim 'str' left, right and both */
33 char* strltrim( const char * str , char tr );
34 char* strrtrim( char * str , char tr );
35 char* strtrim( char * str , char tr );
36 
37 /* remove character 'ch' from memory block 'mem' */
38 size_t memczap( size_t size,void *mem, int ch );
39 
40 /* string prefix and suffix (boolean) */
41 int strpfx( const char *str, const char *prefix );
42 int strsffx( const char *str, const char *suffix );
43 
44 /* Get span until character in character range '[a..z]' */
45 size_t strrspn( const char * str, char a, char z );
46 
47 /* transform to ASCII accented letters in Latin 1 alphabet */
48 char strxltn1( char accented_latin_1 );
49 char* strxacct( char* str );
50 #define removeAccented( str ) strxacct( str ) /* mnemonic name */
51 
52 #ifdef __cplusplus
53  #include <climits> /* CHAR_BIT */
54 #else
55  #include <limits.h> /* CHAR_BIT */
56  #if __STDC_VERSION__ < 199901L
57  #define inline /* nada nothing */
58  #endif
59 #endif
60 
61 /** Translation table for strxltn1(). */
62 #define strxltn1_translate \
63 /* "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ" */ \
64  "AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy"
65 
66 /** \fn inline char strxltn1( char accented_latin_1 );
67  \brief Translates characters in range [192<-->192+63] into letters
68  or ASCII symbols that look similar.
69  This has the effect of removing accents in many of those letters;
70  for example, 'á' is translated as 'a' and 'ÿ' as 'y'.
71  The translated characters are the upper 8 bit characters in the Latin 1
72  alphabet, also know as Windows-1252 and ISO/IEC 8859-1.
73  \see http://en.wikipedia.org/wiki/ISO/IEC_8859-1
74  \see http://en.wikipedia.org/wiki/Windows-1252
75 
76  This is the translation table used:
77  \code
78  ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
79  AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy
80  \endcode
81 \test_example{strxltn1}
82 */
83 
84 #ifdef __cplusplus
85  #include <climits> /* CHAR_BIT */
86  inline char strxltn1( char accented_latin_1 ) {
87  #if CHAR_BIT!=8
88  accented_latin_1 &= 0xFF; /* bitwise operator */
89  #endif
90  if ( (unsigned char)(accented_latin_1) >= 192 ) {
91  accented_latin_1 = strxltn1_translate[ (unsigned char)(accented_latin_1) - 192 ];
92  }
93  return accented_latin_1;
94  }
95 #else
96  #include <limits.h> /* CHAR_BIT */
97  #if CHAR_BIT==8
98  #define strxltn1( accented_latin_1 ) \
99  ( ( (unsigned char)(accented_latin_1) < 192 ) \
100  ? accented_latin_1 \
101  : strxltn1_translate[ (unsigned char)(accented_latin_1) - 192 ] \
102  )
103  #else
104  #define strxltn1( accented_latin_1 ) \
105  ( ( (unsigned char)(accented_latin_1) < 192 ) \
106  ? accented_latin_1 \
107  : 0xFF & strxltn1_translate[ (unsigned char)(accented_latin_1) - 192 ] \
108  )
109  #endif
110 #endif
111 
112 
113 /* wchar_t */
114 /* http://www1.kokusaika.jp/advisory/org/en/c_wchar.html */
115 
116 #ifdef __cplusplus
117  } /* extern "C" */
118 #endif
119 
120 #endif
121 
122 /* EOF: ztring.h */
123 
size_t memczap(size_t size, void *mem, int ch)
Removes every ocurrence of 'ch' from 'mem'.
Definition: ztring.c:306
char * strtrim(char *str, char tr)
return strrtrim( strltrim(s,tr),tr ).
Definition: ztring.c:288
char * strxacct(char *str)
Uses strxltn1() to convert all letters in 'str'.
Definition: ztring.c:445
int strsffx(const char *str, const char *suffix)
Returns '1' if 'suffix' is a suffix of 'str'.
Definition: ztring.c:393
size_t strrspn(const char *str, char a, char z)
Get span until character in character range '[a..z]'.
Definition: ztring.c:421
char * strrtrim(char *str, char tr)
Removes from 'str' all trailing characters that are equal to 'tr'.
Definition: ztring.c:260
#define strxltn1_translate
Translation table for strxltn1().
Definition: ztring.h:62
char * ztrcat(size_t size, char *dest, const char *src)
Append characters to a string.
Definition: ztring.c:95
char * strdel(char *dest, size_t len)
Deletes the leading 'len' characters from 'str'.
Definition: ztring.c:200
char * strltrim(const char *str, char tr)
Returns a pointer to the first character in 'str' different from 'tr'.
Definition: ztring.c:244
char * ztrsub(size_t size, char *dest, const char *src, size_t len)
Copies the first 'len' characters from 'src' to 'dest'.
Definition: ztring.c:222
int strpfx(const char *str, const char *prefix)
Returns '1' if 'prefix' is a prefix of 'str'.
Definition: ztring.c:380
char * ztrcpy(size_t size, char *dest, const char *src)
Copies up to 'size' characters from 'src' to 'dest'.
Definition: ztring.c:57
char strxltn1(char accented_latin_1)
Translates characters in range [192<–>192+63] into letters or ASCII symbols that look similar...
Definition: ztring.h:86
char * ztrins(size_t size, char *dest, size_t i, const char *insert)
Inserts string 'insert' into 'dest' at position 'n'.
Definition: ztring.c:144