Nilorea Library
C utilities for networking, threading, graphics
n_str.h
Go to the documentation of this file.
1
8#ifndef N_STRFUNC
9#define N_STRFUNC
10
12#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
13
14#ifdef __cplusplus
15extern "C"
16{
17#endif
18
24#include "n_common.h"
25#include "n_list.h"
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <strings.h>
31#include <math.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35
37#define local_strdup( __src_ )\
38 ({ \
39 char *str = NULL ;\
40 int len = strlen( (__src_) );\
41 Malloc( str , char , len + 5 );\
42 if( !str )\
43 { \
44 n_log( LOG_ERR , "Couldn't allocate %d byte for duplicating \"%s\"" , (__src_) );\
45 }\
46 else\
47 {\
48 for( int it = 0 ; it <= len ; it ++ )\
49 {\
50 str[ it ] = (__src_)[ it ] ;\
51 }\
52 }\
53 str ;\
54 })
55
56
58#define WILDMAT_ABORT -2
60#define WILDMAT_NEGATE_CLASS '^'
62#undef WILDMAT_MATCH_TAR_PATTERN
63
65#define resize_nstr( __nstr , new_size ) \
66 ({ \
67 if( __nstr && __nstr -> data ) \
68 { \
69 Reallocz( __nstr -> data , char , __nstr -> length , new_size ); \
70 __nstr -> length = new_size ; \
71 } \
72 })
73
74
75
77#define strprintf( __n_var , ... ) \
78 ({ \
79 if( !__n_var ) \
80 { \
81 NSTRBYTE needed = 0 ; \
82 needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
83 Malloc( __n_var , char , needed + 2 );\
84 if( __n_var )\
85 { \
86 snprintf( __n_var , needed + 1 , __VA_ARGS__ ); \
87 } \
88 } \
89 else \
90 { \
91 n_log( LOG_ERR , "%s is already allocated." , "#__n_var" ); \
92 } \
93 __n_var ; \
94 })
95
97#define nstrprintf( __nstr_var , ... ) \
98 ({ \
99 if( __nstr_var ) \
100 { \
101 free_nstr( &__nstr_var ); \
102 }\
103 NSTRBYTE needed = 0 ; \
104 needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
105 __nstr_var = new_nstr( needed + 1 ); \
106 if( __nstr_var )\
107 { \
108 snprintf( __nstr_var -> data , needed + 1 , __VA_ARGS__ ); \
109 __nstr_var -> written = needed ; \
110 } \
111 __nstr_var ; \
112 })
113
114
115
117#define nstrprintf_cat( __nstr_var , ... ) \
118 ({ \
119 if( __nstr_var ) \
120 { \
121 int needed_size = 0 ; \
122 needed_size = snprintf( NULL , 0 , __VA_ARGS__ ); \
123 if( needed_size > 0 ) \
124 { \
125 NSTRBYTE needed = needed_size + 1 ; \
126 if( ( __nstr_var -> written + needed ) > __nstr_var -> length ) \
127 { \
128 resize_nstr( __nstr_var , __nstr_var -> length + needed ); \
129 } \
130 snprintf( __nstr_var -> data + __nstr_var -> written , needed , __VA_ARGS__ ); \
131 __nstr_var -> written += needed_size ; \
132 } \
133 else \
134 { \
135 nstrprintf( __nstr_var , __VA_ARGS__ ); \
136 } \
137 } \
138 __nstr_var ; \
139 } )
140
141
142
144#define n_remove_ending_cr( __nstr_var ) \
145 if( __nstr_var && __nstr_var -> data && __nstr_var -> data[ __nstr_var -> written ] == '\r' ) \
146 { \
147 __nstr_var -> data[ __nstr_var -> written ] = '\0' ; \
148 __nstr_var -> written -- ; \
149 }
150
151
152
154#define n_replace_cr( __nstr_var , __replacement ) \
155 if( __nstr_var && __nstr_var -> data && __nstr_var -> written > 0 ) \
156 { \
157 char *__replaced = str_replace( __nstr_var -> data , "\r" , __replacement ); \
158 if( __replaced ) \
159 { \
160 Free( __nstr_var -> data ); \
161 __nstr_var -> data = __replaced ; \
162 __nstr_var -> written = strlen( __nstr_var -> data ); \
163 __nstr_var -> length = __nstr_var -> written + 1 ; \
164 } \
165 }
166
167#include <inttypes.h>
169typedef size_t NSTRBYTE;
170
172typedef struct N_STR
173{
175 char *data;
177 size_t length ;
179 size_t written ;
180} N_STR;
181
182#ifdef __windows__
183const char *strcasestr(const char *s1, const char *s2);
184#endif
185
186/* trim and put a \0 at the end, return new begin pointer */
187char *trim_nocopy(char *s);
188/* trim and put a \0 at the end, return new char * */
189char *trim(char *s);
190/* N_STR wrapper around fgets */
191char *nfgets( char *buffer, NSTRBYTE size, FILE *stream );
192/* create a new string */
193N_STR *new_nstr( NSTRBYTE size );
194/* reinitialize a nstr */
195int empty_nstr( N_STR *nstr );
196/* Make a copy of a N_STR */
197N_STR *nstrdup( N_STR *msg );
198/* Convert a char into a N_STR */
199int char_to_nstr_ex( const char *from, NSTRBYTE nboct, N_STR **to );
200/* Convert a char into a N_STR, shorter version */
201N_STR *char_to_nstr( const char *src );
202/* Convert a char into a N_STR */
203int char_to_nstr_nocopy_ex( char *from, NSTRBYTE nboct, N_STR **to );
204/* Convert a char into a N_STR, shorter version */
205N_STR *char_to_nstr_nocopy( char *src );
206/* cat data inside a N8STR */
207int nstrcat_ex( N_STR *dest, void *src, NSTRBYTE size ,int resize_flag );
208/* Wrapper to nstrcat_ex to concatenate N_STR *datas */
209int nstrcat( N_STR *dst, N_STR *src );
210/* Wrapper to nstrcat_ex to concatenate void *data */
211int nstrcat_bytes_ex( N_STR *dest, void *src, NSTRBYTE size );
212/* Wrapper to nstrcat_bytes_ex to cat null termined data streams */
213int nstrcat_bytes( N_STR *dest, void *src );
214/* Load a whole file into a N_STR. Be aware of the (4GB ||System Memory) limit */
215N_STR *file_to_nstr( char *filename );
216/* Write a whole N_STR into an open file descriptor */
217int nstr_to_fd( N_STR *str, FILE *out, int lock );
218/* Write a whole N_STR into a file */
219int nstr_to_file( N_STR *n_str, char *filename );
220
222#define free_nstr( __ptr ) \
223 { \
224 if( (*__ptr ) ) \
225 { \
226 _free_nstr( __ptr ); \
227 } \
228 else \
229 { \
230 n_log( LOG_DEBUG , "%s is already NULL" , #__ptr ); \
231 } \
232 }
233
234/* free NSTR and set it to NULL */
235int _free_nstr( N_STR **ptr );
236/* just free NSTR */
237void free_nstr_ptr( void *ptr );
238/* Do not warn and Free + set to NULL */
239int free_nstr_nolog( N_STR **ptr );
240/* Do not warn and just Free */
241void free_nstr_ptr_nolog( void *ptr );
242/* String to long integer, with error checking */
243int str_to_long_ex( const char *s, NSTRBYTE start, NSTRBYTE end, long int *i, const int base );
244/* String to long integer, shorter version */
245int str_to_long( const char *s, long int *i, const int base);
246/* String to long long integer */
247int str_to_long_long_ex( const char *s, NSTRBYTE start, NSTRBYTE end, long long int *i, const int base );
248/* String to long long integer, shorter version */
249int str_to_long_long( const char *s, long long int *i, const int base);
250/* String to integer, with error checking */
251int str_to_int_ex( const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base );
252/* String to integer, with error checking */
253int str_to_int_nolog( const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base, N_STR **infos );
254/* String to integer, shorter version */
255int str_to_int( const char *s, int *i, const int base);
256/* Skip character from string while string[iterator] == toskip step inc */
257int skipw( char *string, char toskip, NSTRBYTE *iterator, int inc );
258/* Skip character from string until string[iterator] == toskip step inc */
259int skipu( char *string, char toskip, NSTRBYTE *iterator, int inc );
260/* Upper case a string */
261int strup( char *string, char *dest );
262/* Lower case a string */
263int strlo( char *string, char *dest );
264/* Copy from string to dest until from[ iterator ] == split */
265int strcpy_u( char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it );
266/* Return an array of char pointer to the splitted section */
267char **split( const char* str, const char* delim, int empty );
268/* Count split elements */
269int split_count( char **split_result );
270/* Free a char **tab and set it to NULL */
271int free_split_result( char ***tab );
272/* join a split result into a string */
273char* join( char** splitresult , char *delim );
274/* Write and fit bytes */
275int write_and_fit_ex( char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src, NSTRBYTE src_size, NSTRBYTE additional_padding );
276/* Write and fit into the char array */
277int write_and_fit( char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src );
278/* get a list of the file in a directory */
279int scan_dir( const char *dir, LIST *result, const int recurse );
280/* get a list of the file in a directory, extented N_STR version */
281int scan_dir_ex( const char *dir, const char *pattern, LIST *result, const int recurse, const int mode );
282/* pattern matching */
283int wildmat( register const char *text, register const char *p );
284/* pattern matching case insensitive */
285int wildmatcase( register const char *text, register const char *p );
286/* return a replaced string */
287char *str_replace ( const char *string, const char *substr, const char *replacement );
288/* sanitize string */
289int str_sanitize_ex( char *string, const NSTRBYTE string_len, const char *mask, const NSTRBYTE masklen, const char replacement );
290/* in-place substitution of a set of chars by a single one */
291int str_sanitize( char *string, const char *mask, const char replacement );
292
297#ifdef __cplusplus
298}
299#endif
300/* #ifndef N_STR*/
301#endif
Structure of a generic LIST container.
Definition: n_list.h:45
size_t written
size of the written data inside the string
Definition: n_str.h:179
char * data
the string
Definition: n_str.h:175
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition: n_str.h:177
int str_to_long_long(const char *s, long long int *i, const int base)
Helper for string to integer.
Definition: n_str.c:778
int strcpy_u(char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
Copy from start to dest until from[ iterator ] == split.
Definition: n_str.c:990
int nstrcat(N_STR *dst, N_STR *src)
Add N_STR *src content to N_STR *dst, resizing it if needed.
Definition: n_str.c:1246
char * trim_nocopy(char *s)
trim and zero end the string, WARNING: keep and original pointer to delete the string correctly
Definition: n_str.c:124
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
Definition: n_str.c:55
size_t NSTRBYTE
N_STR base unit.
Definition: n_str.h:169
int split_count(char **split_result)
Count split elements.
Definition: n_str.c:1111
int str_sanitize_ex(char *string, const NSTRBYTE string_len, const char *mask, const NSTRBYTE masklen, const char replacement)
clean a string by replacing evil characteres
Definition: n_str.c:1640
int nstr_to_file(N_STR *n_str, char *filename)
Write a N_STR content into a file.
Definition: n_str.c:459
int scan_dir_ex(const char *dir, const char *pattern, LIST *result, const int recurse, const int mode)
Scan a list of directory and return a list of char *file.
Definition: n_str.c:1379
int str_to_long_long_ex(const char *s, NSTRBYTE start, NSTRBYTE end, long long int *i, const int base)
Helper for string[start to end] to long long integer.
Definition: n_str.c:688
N_STR * nstrdup(N_STR *msg)
Duplicate a N_STR.
Definition: n_str.c:795
int write_and_fit(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src)
concatenate a copy of src of size strlen( src ) to dest, starting at dest[ written ],...
Definition: n_str.c:1349
int nstrcat_bytes_ex(N_STR *dest, void *src, NSTRBYTE size)
Append data into N_STR using internal N_STR size and cursor position.
Definition: n_str.c:1260
int nstrcat_bytes(N_STR *dest, void *src)
Append data into N_STR using internal N_STR size and cursor position.
Definition: n_str.c:1281
int str_to_int_ex(const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base)
Helper for string[start to end] to integer.
Definition: n_str.c:492
int str_to_int_nolog(const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base, N_STR **infos)
Helper for string[start to end] to integer.
Definition: n_str.c:545
int char_to_nstr_nocopy_ex(char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, direct use of linked source pointer, extended version.
Definition: n_str.c:289
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition: n_str.c:273
char * nfgets(char *buffer, NSTRBYTE size, FILE *stream)
try to fgets
Definition: n_str.c:172
int skipw(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
Definition: n_str.c:835
int empty_nstr(N_STR *nstr)
empty a N_STR string
Definition: n_str.c:199
int wildmatcase(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
Definition: n_str.c:1527
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition: n_str.c:215
char * str_replace(const char *string, const char *substr, const char *replacement)
Replace "substr" by "replacement" inside string taken from http://coding.debuntu.org/c-implementing-s...
Definition: n_str.c:1598
int scan_dir(const char *dir, LIST *result, const int recurse)
Scan a list of directory and return a list of char *file.
Definition: n_str.c:1363
int str_sanitize(char *string, const char *mask, const char replacement)
clean a string by replacing evil characteres
Definition: n_str.c:1667
char ** split(const char *str, const char *delim, int empty)
split the strings into a an array of char *pointer , ended by a NULL one.
Definition: n_str.c:1032
int strup(char *string, char *dest)
Upper case a string.
Definition: n_str.c:942
int wildmat(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
Definition: n_str.c:1459
int free_nstr_nolog(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
Definition: n_str.c:89
int str_to_long(const char *s, long int *i, const int base)
Helper for string to integer.
Definition: n_str.c:759
int nstrcat_ex(N_STR *dest, void *src, NSTRBYTE size, int resize_flag)
Append data into N_STR using internal N_STR size and cursor position.
Definition: n_str.c:1198
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, extended version.
Definition: n_str.c:246
N_STR * char_to_nstr_nocopy(char *src)
Convert a char into a N_STR, direct use of linked source pointer, short version.
Definition: n_str.c:318
int _free_nstr(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
Definition: n_str.c:72
int write_and_fit_ex(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src, NSTRBYTE src_size, NSTRBYTE additional_padding)
concatenate a copy of src of size src_size to dest, starting at dest[ written ], updating written and...
Definition: n_str.c:1310
char * join(char **splitresult, char *delim)
join the array into a string
Definition: n_str.c:1153
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition: n_str.c:332
int free_split_result(char ***tab)
Free a split result allocated array.
Definition: n_str.c:1131
void free_nstr_ptr_nolog(void *ptr)
Free a N_STR pointer structure.
Definition: n_str.c:107
int str_to_int(const char *s, int *i, const int base)
Helper for string to integer.
Definition: n_str.c:592
int skipu(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
Definition: n_str.c:889
char * trim(char *s)
trim and put a \0 at the end, return new char *
Definition: n_str.c:156
int strlo(char *string, char *dest)
Upper case a string.
Definition: n_str.c:965
int nstr_to_fd(N_STR *str, FILE *out, int lock)
Write a N_STR content into a file.
Definition: n_str.c:402
int str_to_long_ex(const char *s, NSTRBYTE start, NSTRBYTE end, long int *i, const int base)
Helper for string[start to end] to long integer.
Definition: n_str.c:615
A box including a string and his lenght.
Definition: n_str.h:173
Common headers and low-level hugly functions & define.
List structures and definitions.