12#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
49#define WILDMAT_ABORT -2
51#define WILDMAT_NEGATE_CLASS '^'
53#undef WILDMAT_MATCH_TAR_PATTERN
56#define local_strdup(__src_) \
58 char* __local_strdup_str = NULL; \
59 size_t __local_strdup_len = strlen((__src_)); \
60 Malloc(__local_strdup_str, char, __local_strdup_len + 1); \
61 if (!__local_strdup_str) { \
62 n_log(LOG_ERR, "Couldn't allocate %d byte for duplicating \"%s\"", (__src_)); \
64 for (size_t __local_strdup_it = 0; __local_strdup_it <= __local_strdup_len; __local_strdup_it++) { \
65 __local_strdup_str[__local_strdup_it] = (__src_)[__local_strdup_it]; \
72#define strprintf(__n_var, ...) \
74 char* __strprintf_ptr = NULL; \
76 int __needed = snprintf(NULL, 0, __VA_ARGS__); \
78 Malloc(__n_var, char, (size_t)__needed + 1) if (__n_var) { \
79 snprintf(__n_var, (size_t)__needed + 1, __VA_ARGS__); \
80 __strprintf_ptr = __n_var; \
83 n_log(LOG_ERR, "couldn't allocate %s with %d bytes", \
84 #__n_var, __needed + 1); \
88 n_log(LOG_ERR, "%s is already allocated.", #__n_var); \
94#define nstrprintf(__nstr_var, __format, ...) \
95 nstrprintf_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
98#define nstrprintf_cat(__nstr_var, __format, ...) \
99 nstrprintf_cat_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
102#define nstrcat(__nstr_dst, __nstr_src) \
104 N_STR* __nstrcat_ret = NULL; \
105 if (__nstr_src && __nstr_src->data) { \
106 __nstrcat_ret = nstrcat_ex(&(__nstr_dst), __nstr_src->data, __nstr_src->written, 1); \
112#define nstrcat_bytes(__nstr_dst, __void_src) \
113 nstrcat_bytes_ex(&(__nstr_dst), __void_src, strlen(__void_src))
116#define n_remove_ending_cr(__nstr_var) \
117 if (__nstr_var && __nstr_var->data && __nstr_var->data[__nstr_var->written] == '\r') { \
118 __nstr_var->data[__nstr_var->written] = '\0'; \
119 __nstr_var->written--; \
123#define n_replace_cr(__nstr_var, __replacement) \
124 if (__nstr_var && __nstr_var->data && __nstr_var->written > 0) { \
125 char* __replaced = str_replace(__nstr_var->data, "\r", __replacement); \
127 Free(__nstr_var->data); \
128 __nstr_var->data = __replaced; \
129 __nstr_var->written = strlen(__nstr_var->data); \
130 __nstr_var->length = __nstr_var->written + 1; \
135const char* strcasestr(
const char* s1,
const char* s2);
155int char_to_nstr_nocopy_ex(
char* from,
NSTRBYTE nboct,
N_STR** to);
165N_STR* nstrprintf_ex(
N_STR** nstr_var,
const char* format, ...);
167N_STR* nstrprintf_cat_ex(
N_STR** nstr_var,
const char* format, ...);
176#define free_nstr(__ptr) \
181 n_log(LOG_DEBUG, "%s is already NULL", #__ptr); \
196int str_to_long(
const char* s,
long int* i,
const int base);
206int str_to_int(
const char* s,
int* i,
const int base);
208int skipw(
char*
string,
char toskip,
NSTRBYTE* iterator,
int inc);
210int skipu(
char*
string,
char toskip,
NSTRBYTE* iterator,
int inc);
212int strup(
char*
string,
char* dest);
214int strlo(
char*
string,
char* dest);
218char**
split(
const char* str,
const char* delim,
int empty);
224char*
join(
char** splitresult,
char* delim);
230int scan_dir(
const char* dir,
LIST* result,
const int recurse);
232int scan_dir_ex(
const char* dir,
const char* pattern,
LIST* result,
const int recurse,
const int mode);
234int wildmat(
register const char* text,
register const char* p);
236int wildmatcase(
register const char* text,
register const char* p);
238char*
str_replace(
const char*
string,
const char* substr,
const char* replacement);
242int 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.
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 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 resize_nstr(N_STR *nstr, size_t size)
reallocate a nstr internal buffer.
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...
N_STR * nstrcat_bytes_ex(N_STR **dest, void *src, NSTRBYTE size)
Append data into N_STR using internal N_STR size and cursor position.
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 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.
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 functions & define.
List structures and definitions.