12#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
37#define local_strdup( __src_ )\
40 int len = strlen( (__src_) );\
41 Malloc( str , char , len + 5 );\
44 n_log( LOG_ERR , "Couldn't allocate %d byte for duplicating \"%s\"" , (__src_) );\
48 for( int it = 0 ; it <= len ; it ++ )\
50 str[ it ] = (__src_)[ it ] ;\
58#define WILDMAT_ABORT -2
60#define WILDMAT_NEGATE_CLASS '^'
62#undef WILDMAT_MATCH_TAR_PATTERN
65#define resize_nstr( __nstr , new_size ) \
67 if( __nstr && __nstr -> data ) \
69 Reallocz( __nstr -> data , char , __nstr -> length , new_size ); \
70 __nstr -> length = new_size ; \
77#define strprintf( __n_var , ... ) \
81 NSTRBYTE needed = 0 ; \
82 needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
83 Malloc( __n_var , char , needed + 2 );\
86 snprintf( __n_var , needed + 1 , __VA_ARGS__ ); \
91 n_log( LOG_ERR , "%s is already allocated." , "#__n_var" ); \
97#define nstrprintf( __nstr_var , ... ) \
101 free_nstr( &__nstr_var ); \
103 NSTRBYTE needed = 0 ; \
104 needed = snprintf( NULL , 0 , __VA_ARGS__ ); \
105 __nstr_var = new_nstr( needed + 1 ); \
108 snprintf( __nstr_var -> data , needed + 1 , __VA_ARGS__ ); \
109 __nstr_var -> written = needed ; \
117#define nstrprintf_cat( __nstr_var , ... ) \
121 int needed_size = 0 ; \
122 needed_size = snprintf( NULL , 0 , __VA_ARGS__ ); \
123 if( needed_size > 0 ) \
125 NSTRBYTE needed = needed_size + 1 ; \
126 if( ( __nstr_var -> written + needed ) > __nstr_var -> length ) \
128 resize_nstr( __nstr_var , __nstr_var -> length + needed ); \
130 snprintf( __nstr_var -> data + __nstr_var -> written , needed , __VA_ARGS__ ); \
131 __nstr_var -> written += needed_size ; \
135 nstrprintf( __nstr_var , __VA_ARGS__ ); \
144#define n_remove_ending_cr( __nstr_var ) \
145 if( __nstr_var && __nstr_var -> data && __nstr_var -> data[ __nstr_var -> written ] == '\r' ) \
147 __nstr_var -> data[ __nstr_var -> written ] = '\0' ; \
148 __nstr_var -> written -- ; \
154#define n_replace_cr( __nstr_var , __replacement ) \
155 if( __nstr_var && __nstr_var -> data && __nstr_var -> written > 0 ) \
157 char *__replaced = str_replace( __nstr_var -> data , "\r" , __replacement ); \
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 ; \
183const char *strcasestr(
const char *s1,
const char *s2);
222#define free_nstr( __ptr ) \
226 _free_nstr( __ptr ); \
230 n_log( LOG_DEBUG , "%s is already NULL" , #__ptr ); \
245int str_to_long(
const char *s,
long int *i,
const int base);
255int str_to_int(
const char *s,
int *i,
const int base);
257int skipw(
char *
string,
char toskip,
NSTRBYTE *iterator,
int inc );
259int skipu(
char *
string,
char toskip,
NSTRBYTE *iterator,
int inc );
261int strup(
char *
string,
char *dest );
263int strlo(
char *
string,
char *dest );
267char **
split(
const char* str,
const char* delim,
int empty );
273char*
join(
char** splitresult ,
char *delim );
279int scan_dir(
const char *dir,
LIST *result,
const int recurse );
281int scan_dir_ex(
const char *dir,
const char *pattern,
LIST *result,
const int recurse,
const int mode );
283int wildmat(
register const char *text,
register const char *p );
285int wildmatcase(
register const char *text,
register const char *p );
287char *
str_replace (
const char *
string,
const char *substr,
const char *replacement );
291int str_sanitize(
char *
string,
const char *mask,
const char replacement );
Structure of a generic LIST container.
size_t written
size of the written data inside the string
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
int str_to_long_long(const char *s, long long int *i, const int base)
Helper for string to integer.
int strcpy_u(char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
Copy from start to dest until from[ iterator ] == split.
int nstrcat(N_STR *dst, N_STR *src)
Add N_STR *src content to N_STR *dst, resizing it if needed.
char * trim_nocopy(char *s)
trim and zero end the string, WARNING: keep and original pointer to delete the string correctly
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
size_t NSTRBYTE
N_STR base unit.
int split_count(char **split_result)
Count split elements.
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
int nstr_to_file(N_STR *n_str, char *filename)
Write a N_STR content into a file.
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.
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.
N_STR * nstrdup(N_STR *msg)
Duplicate a N_STR.
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 ],...
int nstrcat_bytes_ex(N_STR *dest, void *src, NSTRBYTE size)
Append data into N_STR using internal N_STR size and cursor position.
int nstrcat_bytes(N_STR *dest, void *src)
Append data into N_STR using internal N_STR size and cursor position.
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.
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.
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.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
char * nfgets(char *buffer, NSTRBYTE size, FILE *stream)
try to fgets
int skipw(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
int empty_nstr(N_STR *nstr)
empty a N_STR string
int wildmatcase(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
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...
int scan_dir(const char *dir, LIST *result, const int recurse)
Scan a list of directory and return a list of char *file.
int str_sanitize(char *string, const char *mask, const char replacement)
clean a string by replacing evil characteres
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.
int strup(char *string, char *dest)
Upper case a string.
int wildmat(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
int free_nstr_nolog(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
int str_to_long(const char *s, long int *i, const int base)
Helper for string to integer.
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.
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, extended version.
N_STR * char_to_nstr_nocopy(char *src)
Convert a char into a N_STR, direct use of linked source pointer, short version.
int _free_nstr(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
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...
char * join(char **splitresult, char *delim)
join the array into a string
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
int free_split_result(char ***tab)
Free a split result allocated array.
void free_nstr_ptr_nolog(void *ptr)
Free a N_STR pointer structure.
int str_to_int(const char *s, int *i, const int base)
Helper for string to integer.
int skipu(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
char * trim(char *s)
trim and put a \0 at the end, return new char *
int strlo(char *string, char *dest)
Upper case a string.
int nstr_to_fd(N_STR *str, FILE *out, int lock)
Write a N_STR content into a file.
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.
A box including a string and his lenght.
Common headers and low-level hugly functions & define.
List structures and definitions.