52const char* strcasestr(
const char* s1,
const char* s2) {
56 size_t n = strlen(s2);
58 if (!strnicmp(s1++, s2, n))
131 while (*start && isspace((
unsigned char)*start))
134 char* end = s + strlen(s) - 1;
136 while (*end && isspace((
unsigned char)*end) && end > s) {
154 char* copy = strdup(s);
158 char* result = strdup(trimmed);
174 if (size >= INT_MAX) {
175 n_log(
LOG_ERR,
"size of %zu too big, >INT_MAX for buffer %p", size, buffer);
179 if (!fgets(buffer, (
int)size, stream)) {
233 n_log(
LOG_ERR,
"destination N_STR **str is not NULL (%p), it contain (%s). You must provide an empty destination.", (*to), ((*to) && (*to)->data) ? (*to)->data :
"DATA_IS_NULL");
243 memcpy((*to)->data, from, nboct);
244 (*to)->written = nboct;
256 N_STR* strptr = NULL;
257 size_t length = strlen(src);
287 N_STR* tmpstr = NULL;
288 struct stat filestat;
294 int fd = open(filename, O_RDONLY);
297 n_log(
LOG_ERR,
"Unable to open %s for reading. Errno: %s", filename, strerror(error));
301 if (fstat(fd, &filestat) != 0) {
304 if (error == EOVERFLOW) {
305 n_log(
LOG_ERR,
"%s size is too big ,EOVERFLOW)", filename);
308 n_log(
LOG_ERR,
"Couldn't stat %s. Errno: %s", filename, strerror(error));
316 if ((uint64_t)filestat.st_size >= (uint64_t)UINT32_MAX) {
317 n_log(
LOG_ERR,
"file size >= 4GB is not possible yet. %s is %lld oct", filename, (
long long)filestat.st_size);
322 n_log(
LOG_DEBUG,
"%s file size is: %lld", filename, (
long long)filestat.st_size);
324 in = fdopen(fd,
"rb");
326 n_log(
LOG_ERR,
"fdopen failed on %s (fd=%d)", filename, fd);
331 tmpstr =
new_nstr((
size_t)filestat.st_size + 1);
332 __n_assert(tmpstr, fclose(in);
n_log(
LOG_ERR,
"Unable to get a new nstr of %ld octets", filestat.st_size + 1);
return NULL);
334 tmpstr->
written = (size_t)filestat.st_size;
336 if (fread(tmpstr->
data,
sizeof(
char), tmpstr->
written, in) == 0) {
337 n_log(
LOG_ERR,
"Couldn't read %s, fread return 0", filename);
343 n_log(
LOG_ERR,
"There were some errors when reading %s", filename);
365 struct flock out_lock;
374 memset(&out_lock, 0,
sizeof(out_lock));
375 out_lock.l_type = F_WRLCK;
377 fcntl(fileno(out), F_SETLKW, &out_lock);
384 size_t written_to_file = 0;
385 if ((written_to_file = fwrite(str->
data,
sizeof(
char), str->
written, out)) != (
size_t)str->
written) {
386 n_log(
LOG_ERR,
"Couldn't write file, fwrite %d of %d octets", written_to_file, str->
written);
390 n_log(
LOG_ERR,
"There were some errors when writing to %d", fileno(out));
396 memset(&out_lock, 0,
sizeof(out_lock));
397 out_lock.l_type = F_WRLCK;
399 fcntl(fileno(out), F_SETLKW, &out_lock);
420 int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600);
422 n_log(
LOG_ERR,
"Couldn't open %s for writing (0600). Errno: %s",
_str(filename), strerror(errno));
427 out = fdopen(fd,
"wb");
429 n_log(
LOG_ERR,
"fdopen failed for %s (fd=%d). Errno: %s",
_str(filename), fd, strerror(errno));
461 if (start > end)
return FALSE;
463 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
464 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
466 memcpy(tmpstr, s + start, end - start);
469 l = strtol(tmpstr, &endstr, base);
470 if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
471 n_log(
LOG_ERR,
"OVERFLOW reached when converting %s to int", tmpstr);
475 if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
476 n_log(
LOG_ERR,
"UNDERFLOW reached when converting %s to int", tmpstr);
480 if (*endstr !=
'\0' && *endstr !=
'\n') {
509 if (start > end)
return FALSE;
511 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
512 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
514 memcpy(tmpstr, s + start, end - start);
517 l = strtol(tmpstr, &endstr, base);
518 if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
519 nstrprintf((*infos),
"OVERFLOW reached when converting %s to int", tmpstr);
523 if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
524 nstrprintf((*infos),
"UNDERFLOW reached when converting %s to int", tmpstr);
528 if (*endstr !=
'\0' && *endstr !=
'\n') {
529 nstrprintf((*infos),
"Impossible conversion for %s", tmpstr);
571 if (start > end)
return FALSE;
573 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
574 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
576 memcpy(tmpstr, s + start, end - start);
579 l = strtol(tmpstr, &endstr, base);
583 if (tmpstr == endstr) {
584 n_log(
LOG_ERR,
" number : %ld invalid (no digits found, 0 returned)", l);
587 }
else if (error == ERANGE && l == LONG_MIN) {
588 n_log(
LOG_ERR,
" number : %ld invalid (underflow occurred)", l);
591 }
else if (error == ERANGE && l == LONG_MAX) {
592 n_log(
LOG_ERR,
" number : %ld invalid (overflow occurred)", l);
595 }
else if (error == EINVAL)
597 n_log(
LOG_ERR,
" number : %ld invalid (base contains unsupported value)", l);
600 }
else if (error != 0 && l == 0) {
601 n_log(
LOG_ERR,
" number : %ld invalid (unspecified error occurred)", l);
604 }
else if (error == 0 && tmpstr && !*endstr) {
605 n_log(
LOG_DEBUG,
" number : %ld valid (and represents all characters read)", l);
606 }
else if (error == 0 && tmpstr && *endstr != 0) {
607 n_log(
LOG_DEBUG,
" number : %ld valid (but additional characters remain", l);
630 if (start > end)
return FALSE;
632 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
633 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
635 memcpy(tmpstr, s + start, end - start);
638 l = strtoll(tmpstr, &endstr, base);
642 if (tmpstr == endstr) {
643 n_log(
LOG_ERR,
"number: '%s' invalid (no digits found, 0 returned)", s);
646 }
else if (error == ERANGE && l == LLONG_MIN) {
647 n_log(
LOG_ERR,
"number: '%s' invalid (underflow occurred)", s);
650 }
else if (error == ERANGE && l == LLONG_MAX) {
651 n_log(
LOG_ERR,
"number: 's' invalid (overflow occurred)", s);
654 }
else if (error == EINVAL)
656 n_log(
LOG_ERR,
"number: '%s' invalid (base contains unsupported value)", s);
659 }
else if (error != 0 && l == 0) {
660 n_log(
LOG_ERR,
"number: '%s' invalid (unspecified error occurred)", s);
663 }
else if (error == 0 && tmpstr && !*endstr) {
664 n_log(
LOG_DEBUG,
"number : '%llu' valid (and represents all characters read)", l);
665 }
else if (error == 0 && tmpstr && *endstr != 0) {
666 n_log(
LOG_DEBUG,
" number : '%llu' valid (remaining characters: '%s')", l, s);
709 N_STR* new_str = NULL;
722 n_log(
LOG_ERR,
"Error duplicating N_STR %p -> data", str);
746 while (*iterator <= slen && isspace((
unsigned char)
string[*iterator])) {
747 if (inc < 0 && *iterator == 0) {
752 *iterator = *iterator + (
NSTRBYTE)inc;
754 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
758 while (*iterator <= slen &&
string[*iterator] == toskip) {
759 if (inc < 0 && *iterator == 0) {
764 *iterator = *iterator + (
NSTRBYTE)inc;
766 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
770 if (error_flag == 1 || *iterator > slen) {
793 while (*iterator <= slen && !isspace((
unsigned char)
string[*iterator])) {
794 if (inc < 0 && *iterator == 0) {
799 *iterator = *iterator + (
NSTRBYTE)inc;
801 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
805 while (*iterator <= slen &&
string[*iterator] != toskip) {
806 if (inc < 0 && *iterator == 0) {
811 *iterator = *iterator + (
NSTRBYTE)inc;
813 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
818 if (error_flag == 1 || *iterator > slen) {
832int strup(
const char*
string,
char* dest) {
838 for (it = 0; it < (
NSTRBYTE)strlen(
string); it++)
839 dest[it] = (
char)toupper(
string[it]);
841 dest[strlen(
string)] =
'\0';
853int strlo(
const char*
string,
char* dest) {
859 for (it = 0; it < (
NSTRBYTE)strlen(
string); it++)
860 dest[it] = (
char)tolower(
string[it]);
862 dest[strlen(
string)] =
'\0';
883 while (_it < to_size && from[(*it)] !=
'\0' && from[(*it)] !=
split) {
884 to[_it] = from[(*it)];
889 if (_it == to_size) {
892 n_log(
LOG_DEBUG,
"strcpy_u: not enough space to write %d octet to dest (%d max) , %s: %d", _it, to_size, __FILE__, __LINE__);
898 if (from[(*it)] !=
split) {
899 n_log(
LOG_DEBUG,
"strcpy_u: split value not found, written %d octet to dest (%d max) , %s: %d", _it, to_size, __FILE__, __LINE__);
912char**
split(
const char* str,
const char* delim,
int empty) {
918 const char* largestring = NULL;
923 size_t sizeDelim = strlen(delim);
926 while ((ptr = strstr(largestring, delim)) != NULL) {
927 sizeStr = (size_t)(ptr - largestring);
928 if (empty == 1 || sizeStr != 0) {
930 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
934 Malloc(tab[sizeTab - 1],
char,
sizeof(
char) * (sizeStr + 1));
937 memcpy(tab[sizeTab - 1], largestring, sizeStr);
938 tab[sizeTab - 1][sizeStr] =
'\0';
940 ptr = ptr + sizeDelim;
945 if (strlen(largestring) != 0) {
946 sizeStr = strlen(largestring);
949 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
953 Malloc(tab[sizeTab - 1],
char,
sizeof(
char) * (sizeStr + 1));
956 memcpy(tab[sizeTab - 1], largestring, sizeStr);
957 tab[sizeTab - 1][sizeStr] =
'\0';
962 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
966 Malloc(tab[sizeTab - 1],
char, (
int)(
sizeof(
char) * 1));
969 tab[sizeTab - 1][0] =
'\0';
975 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
978 tab[sizeTab - 1] = NULL;
997 while (split_result[it]) {
1013 while ((*tab)[it]) {
1029char*
join(
char** splitresult,
const char* delim) {
1030 size_t delim_length = 0;
1032 delim_length = strlen(delim);
1033 size_t total_length = 0;
1035 while (splitresult[it]) {
1037 size_t entry_length = strlen(splitresult[it]);
1038 if (entry_length >= SIZE_MAX - total_length) {
1039 n_log(
LOG_ERR,
"join: total_length overflow accumulating entry %d", it);
1042 total_length += entry_length;
1044 if (delim && splitresult[it + 1]) {
1045 if (delim_length >= SIZE_MAX - total_length) {
1046 n_log(
LOG_ERR,
"join: total_length overflow accumulating delimiter at entry %d", it);
1049 total_length += delim_length;
1053 char* result = NULL;
1054 Malloc(result,
char, total_length + 1);
1056 size_t position = 0;
1058 while (splitresult[it]) {
1059 size_t copy_size = strlen(splitresult[it]);
1060 memcpy(&result[position], splitresult[it], copy_size);
1061 position += copy_size;
1063 if (delim && splitresult[it + 1]) {
1064 memcpy(&result[position], delim, delim_length);
1065 position += delim_length;
1069 result[position] =
'\0';
1086 if (size >= SIZE_MAX - 1) {
1087 n_log(
LOG_ERR,
"size too large in nstrcat_ex: %zu", size);
1091 if (resize_flag == 0) {
1094 if (size > SIZE_MAX - (*dest)->written - 1) {
1095 n_log(
LOG_ERR,
"integer overflow in nstrcat_ex size calculation");
1098 if (((*dest)->written + size + 1) > (*dest)->length) {
1099 n_log(
LOG_ERR,
"%p to %p: not enough space. Resize forbidden. %zu needed, %zu available", src, (*dest), (*dest)->written + size + 1, (*dest)->length);
1103 n_log(
LOG_ERR,
"%p to %p: not enough space. Resize forbidden. %zu needed, destination is NULL", src, (*dest), size + 1);
1111 n_log(
LOG_ERR,
"could not allocate new N_STR of size %zu", size + 1);
1117 if (size > SIZE_MAX - (*dest)->written - 1) {
1118 n_log(
LOG_ERR,
"integer overflow in nstrcat_ex size calculation");
1122 if ((*dest)->length < (*dest)->written + size + 1) {
1123 (*dest)->length = (*dest)->written + size + 1;
1124 if (
Reallocz((*dest)->data,
char, (*dest)->written, (*dest)->length) == FALSE) {
1130 ptr = (*dest)->data + (*dest)->written;
1131 memcpy(ptr, src, size);
1132 (*dest)->written += size;
1134 (*dest)->data[(*dest)->written] =
'\0';
1150 n_log(
LOG_ERR,
"Could not copy 0 or less (%ld) octet!", size);
1169 if (src_size == 0)
return TRUE;
1170 NSTRBYTE needed_size = (*written) + src_size + 1;
1173 if ((needed_size >= (*size)) || !(*dest)) {
1178 if (
Reallocz((*dest),
char, (*size), needed_size + additional_padding) == FALSE) {
1182 (*size) = needed_size;
1184 ptr = (*dest) + (*written);
1185 memcpy(ptr, src, src_size);
1186 (*written) += src_size;
1187 (*dest)[(*written)] =
'\0';
1226 struct dirent* entry = NULL;
1227 struct stat statbuf;
1232 if ((dp = opendir(dir)) == NULL) {
1237 N_STR* newname = NULL;
1238 while ((entry = readdir(dp)) != NULL) {
1239 if (!
nstrprintf(newname,
"%s/%s", dir, entry->d_name)) {
1240 n_log(
LOG_ERR,
"could not allocate newname for %s/%s", dir, entry->d_name);
1245 if (stat(newname->
data, &statbuf) >= 0) {
1246 if (S_ISDIR(statbuf.st_mode) != 0) {
1247 if (strcmp(
".", entry->d_name) == 0 || strcmp(
"..", entry->d_name) == 0) {
1253 if (recurse != FALSE) {
1255 n_log(
LOG_ERR,
"scan_dir_ex( %s , %s , %p , %d , %d ) returned FALSE !", newname->
data, pattern, result, recurse,
mode);
1260 }
else if (S_ISREG(statbuf.st_mode) != 0) {
1263 char* file = strdup(newname->
data);
1267 n_log(
LOG_ERR,
"Error adding %s/%s to list", dir, entry->d_name);
1269 }
else if (
mode == 1) {
1289int wildmat(
register const char* text,
register const char* p) {
1290 register int last = 0;
1291 register int matched = 0;
1292 register int reverse = 0;
1294 for (; *p; text++, p++) {
1295 if (*text ==
'\0' && *p !=
'*')
1317 if ((matched =
wildmat(text++, p)) != FALSE)
1326 if (p[1] ==
']' || p[1] ==
'-')
1329 for (last = *p; *++p && *p !=
']'; last = *p)
1331 if (*p ==
'-' && p[1] !=
']'
1332 ? *text <= *++p && *text >= last
1335 if (matched == reverse)
1340#ifdef WILDMAT_MATCH_TAR_PATTERN
1344 return *text ==
'\0';
1355 register int matched;
1356 register int reverse;
1358 for (; *p; text++, p++) {
1359 if (*text ==
'\0' && *p !=
'*')
1367 if (toupper(*text) != toupper(*p))
1390 if (p[1] ==
']' || p[1] ==
'-')
1391 if (toupper(*++p) == toupper(*text))
1393 for (last = toupper(*p); *++p && *p !=
']'; last = toupper(*p))
1394 if (*p ==
'-' && p[1] !=
']'
1395 ? toupper(*text) <= toupper(*++p) && toupper(*text) >= last
1396 : toupper(*text) == toupper(*p))
1398 if (matched == reverse)
1403#ifdef WILDMAT_MATCH_TAR_PATTERN
1407 return *text ==
'\0';
1419char*
str_replace(
const char*
string,
const char* substr,
const char* replacement) {
1420 const char* tok = NULL;
1421 char* newstr = NULL;
1424 if (substr == NULL || substr[0] ==
'\0' || replacement == NULL)
1425 return strdup(
string);
1427 size_t substr_len = strlen(substr);
1428 size_t replacement_len = strlen(replacement);
1430 newstr = strdup(
string);
1431 if (!newstr)
return NULL;
1433 while ((tok = strstr(head, substr))) {
1434 char* oldstr = newstr;
1435 size_t oldstr_len = strlen(oldstr);
1436 size_t base_len = oldstr_len - substr_len;
1438 if (base_len > SIZE_MAX - 8 ||
1439 replacement_len > SIZE_MAX - base_len - 8) {
1440 n_log(
LOG_ERR,
"str_replace: allocation size overflow (base_len=%zu, replacement_len=%zu)", base_len, replacement_len);
1444 size_t newlen = base_len + replacement_len + 8;
1445 Malloc(newstr,
char, newlen);
1447 if (newstr == NULL) {
1448 n_log(
LOG_ERR,
"str_replace: could not allocate newstr of size %zu", newlen);
1452 memcpy(newstr, oldstr, (
size_t)(tok - oldstr));
1453 memcpy(newstr + (tok - oldstr), replacement, replacement_len);
1454 memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (
size_t)(tok - oldstr));
1455 memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);
1457 head = newstr + (tok - oldstr) + replacement_len;
1477 for (it = 0; it < string_len; it++) {
1479 while (mask_it < masklen && mask[mask_it] !=
'\0') {
1480 if (
string[it] == mask[mask_it])
1481 string[it] = replacement;
1496 return str_sanitize_ex(
string, strlen(
string), mask, strlen(mask), replacement);
1541 int needed_size = 0;
1544 va_start(args, format);
1545 va_copy(args_copy, args);
1546 needed_size = vsnprintf(NULL, 0, format, args);
1549 if (needed_size < 0) {
1551 n_log(
LOG_ERR,
"there was an error while computing the new size according to format \"%s\" for N_STR %p", format, (*nstr_var));
1555 size_t needed = (size_t)(needed_size + 1);
1560 if (!(*nstr_var) || !(*nstr_var)->data) {
1562 n_log(
LOG_ERR,
"could not allocate N_STR of size %zu", needed);
1565 }
else if (needed > (*nstr_var)->length) {
1568 n_log(
LOG_ERR,
"could not resize N_STR %p to size %zu", (*nstr_var), needed);
1574 vsnprintf((*nstr_var)->data, needed, format, args_copy);
1577 (*nstr_var)->written = (size_t)needed_size;
1592 va_start(args, format);
1596 va_copy(args_copy, args);
1599 int needed_size = vsnprintf(NULL, 0, format, args_copy);
1602 if (needed_size < 0) {
1603 n_log(
LOG_ERR,
"vsnprintf size estimation failed for format \"%s\"", format);
1608 size_t needed = (size_t)(needed_size + 1);
1620 if (needed > SIZE_MAX - (*nstr_var)->written) {
1622 n_log(
LOG_ERR,
"integer overflow computing total_needed in nstrprintf_cat_ex");
1627 size_t total_needed = (*nstr_var)->written + needed;
1628 if (total_needed > (*nstr_var)->length) {
1629 if (
resize_nstr((*nstr_var), total_needed) == FALSE) {
1631 n_log(
LOG_ERR,
"could not resize N_STR %p to size %zu", (*nstr_var), total_needed);
1637 char* write_ptr = (*nstr_var)->data + (*nstr_var)->written;
1638 int written_now = vsnprintf(write_ptr, needed, format, args);
1641 if (written_now < 0) {
1642 n_log(
LOG_ERR,
"vsnprintf failed while writing at offset %zu in N_STR %p", (*nstr_var)->written, (*nstr_var));
1646 (*nstr_var)->written += (size_t)written_now;
1647 (*nstr_var)->data[(*nstr_var)->written] =
'\0';
1653 if (!tmpl)
return NULL;
1655 if (!result)
return NULL;
1656 const char* p = tmpl;
1658 if (p[0] ==
'{' && p[1] ==
'{') {
1659 const char* end = strstr(p + 2,
"}}");
1661 size_t klen = (size_t)(end - (p + 2));
1663 if (klen <
sizeof(
key)) {
1664 memcpy(
key, p + 2, klen);
1677 char ch[2] = {*p,
'\0'};
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
#define Free(__ptr)
Free Handler to get errors.
int ht_get_string(HASH_TABLE *table, const char *key, char **val)
get string at 'key' from 'table'
structure of a hash table
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
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.
N_STR * n_str_template_expand(const char *tmpl, HASH_TABLE *vars)
Expand double-brace tokens in a template using a hash table.
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.
N_STR * nstrprintf_cat_ex(N_STR **nstr_var, const char *format,...)
Function to allocate, format, and concatenate a string into an N_STR object.
size_t NSTRBYTE
N_STR base unit.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
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 *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 strcpy_u(const char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
Copy from start to dest until from[ iterator ] == split.
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.
char * join(char **splitresult, const char *delim)
join the array into a string
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
int strlo(const char *string, char *dest)
Lower case a string.
N_STR * 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 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 ],...
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
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 skipu(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
#define WILDMAT_ABORT
Abort code to sped up pattern matching.
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.
#define WILDMAT_NEGATE_CLASS
What character marks an inverted character class?
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...
char * trim(const char *s)
trim and put a \0 at the end, return new char *
int skipw(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
N_STR * nstrcat_bytes_ex(N_STR **dest, void *data, 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 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...
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.
N_STR * nstrprintf_ex(N_STR **nstr_var, const char *format,...)
Function to allocate and format a string into an N_STR object.
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 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.
int strup(const char *string, char *dest)
Upper case a string.
A box including a string and his lenght.
Common headers and low-level functions & define.
Hash functions and table.
N_STR and string function declaration.