53const char* strcasestr(
const char* s1,
const char* s2) {
57 size_t n = strlen(s2);
59 if (!strnicmp(s1++, s2, n))
132 while (*start && isspace((
unsigned char)*start))
135 char* end = s + strlen(s) - 1;
137 while (*end && isspace((
unsigned char)*end) && end > s) {
155 char* copy = strdup(s);
159 char* result = strdup(trimmed);
175 if (size >= INT_MAX) {
176 n_log(
LOG_ERR,
"size of %zu too big, >INT_MAX for buffer %p", size, buffer);
180 if (!fgets(buffer, (
int)size, stream)) {
234 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");
244 memcpy((*to)->data, from, nboct);
245 (*to)->written = nboct;
257 N_STR* strptr = NULL;
258 size_t length = strlen(src);
289 N_STR* tmpstr = NULL;
290 struct stat filestat;
296 int fd = open(filename, O_RDONLY);
299 n_log(
LOG_ERR,
"Unable to open %s for reading. Errno: %s", filename, strerror(error));
303 if (fstat(fd, &filestat) != 0) {
306 if (error == EOVERFLOW) {
307 n_log(
LOG_ERR,
"%s size is too big ,EOVERFLOW)", filename);
310 n_log(
LOG_ERR,
"Couldn't stat %s. Errno: %s", filename, strerror(error));
318 if ((uint64_t)filestat.st_size >= (uint64_t)UINT32_MAX) {
319 n_log(
LOG_ERR,
"file size >= 4GB is not possible yet. %s is %lld oct", filename, (
long long)filestat.st_size);
324 n_log(
LOG_DEBUG,
"%s file size is: %lld", filename, (
long long)filestat.st_size);
326 in = fdopen(fd,
"rb");
328 n_log(
LOG_ERR,
"fdopen failed on %s (fd=%d)", filename, fd);
333 tmpstr =
new_nstr((
size_t)filestat.st_size + 1);
334 __n_assert(tmpstr, fclose(in);
n_log(
LOG_ERR,
"Unable to get a new nstr of %ld octets", filestat.st_size + 1);
return NULL);
336 tmpstr->
written = (size_t)filestat.st_size;
343 if (fread(tmpstr->
data,
sizeof(
char), tmpstr->
written, in) == 0) {
344 n_log(
LOG_ERR,
"Couldn't read %s, fread return 0", filename);
350 n_log(
LOG_ERR,
"There were some errors when reading %s", filename);
373 struct flock out_lock;
382 memset(&out_lock, 0,
sizeof(out_lock));
383 out_lock.l_type = F_WRLCK;
385 fcntl(fileno(out), F_SETLKW, &out_lock);
392 size_t written_to_file = 0;
393 if ((written_to_file = fwrite(str->
data,
sizeof(
char), str->
written, out)) != (
size_t)str->
written) {
394 n_log(
LOG_ERR,
"Couldn't write file, fwrite %d of %d octets", written_to_file, str->
written);
398 n_log(
LOG_ERR,
"There were some errors when writing to %d", fileno(out));
404 memset(&out_lock, 0,
sizeof(out_lock));
405 out_lock.l_type = F_WRLCK;
407 fcntl(fileno(out), F_SETLKW, &out_lock);
428 int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600);
430 n_log(
LOG_ERR,
"Couldn't open %s for writing (0600). Errno: %s",
_str(filename), strerror(errno));
435 out = fdopen(fd,
"wb");
437 n_log(
LOG_ERR,
"fdopen failed for %s (fd=%d). Errno: %s",
_str(filename), fd, strerror(errno));
469 if (start > end)
return FALSE;
471 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
472 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
474 memcpy(tmpstr, s + start, end - start);
477 l = strtol(tmpstr, &endstr, base);
478 if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
479 n_log(
LOG_ERR,
"OVERFLOW reached when converting %s to int", tmpstr);
483 if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
484 n_log(
LOG_ERR,
"UNDERFLOW reached when converting %s to int", tmpstr);
488 if (*endstr !=
'\0' && *endstr !=
'\n') {
517 if (start > end)
return FALSE;
519 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
520 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
522 memcpy(tmpstr, s + start, end - start);
525 l = strtol(tmpstr, &endstr, base);
526 if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
527 nstrprintf((*infos),
"OVERFLOW reached when converting %s to int", tmpstr);
531 if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
532 nstrprintf((*infos),
"UNDERFLOW reached when converting %s to int", tmpstr);
536 if (*endstr !=
'\0' && *endstr !=
'\n') {
537 nstrprintf((*infos),
"Impossible conversion for %s", tmpstr);
579 if (start > end)
return FALSE;
581 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
582 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
584 memcpy(tmpstr, s + start, end - start);
587 l = strtol(tmpstr, &endstr, base);
591 if (tmpstr == endstr) {
592 n_log(
LOG_ERR,
" number : %ld invalid (no digits found, 0 returned)", l);
595 }
else if (error == ERANGE && l == LONG_MIN) {
596 n_log(
LOG_ERR,
" number : %ld invalid (underflow occurred)", l);
599 }
else if (error == ERANGE && l == LONG_MAX) {
600 n_log(
LOG_ERR,
" number : %ld invalid (overflow occurred)", l);
603 }
else if (error == EINVAL)
605 n_log(
LOG_ERR,
" number : %ld invalid (base contains unsupported value)", l);
608 }
else if (error != 0 && l == 0) {
609 n_log(
LOG_ERR,
" number : %ld invalid (unspecified error occurred)", l);
612 }
else if (error == 0 && !*endstr) {
613 n_log(
LOG_DEBUG,
" number : %ld valid (and represents all characters read)", l);
614 }
else if (error == 0 && *endstr != 0) {
615 n_log(
LOG_DEBUG,
" number : %ld valid (but additional characters remain", l);
638 if (start > end)
return FALSE;
640 Malloc(tmpstr,
char,
sizeof(
int) + end - start + 8);
641 __n_assert(tmpstr,
n_log(
LOG_ERR,
"Unable to Malloc( tmpstr , char , sizeof( int ) + %d - %d )", end, start);
return FALSE);
643 memcpy(tmpstr, s + start, end - start);
646 l = strtoll(tmpstr, &endstr, base);
650 if (tmpstr == endstr) {
651 n_log(
LOG_ERR,
"number: '%s' invalid (no digits found, 0 returned)", s);
654 }
else if (error == ERANGE && l == LLONG_MIN) {
655 n_log(
LOG_ERR,
"number: '%s' invalid (underflow occurred)", s);
658 }
else if (error == ERANGE && l == LLONG_MAX) {
659 n_log(
LOG_ERR,
"number: 's' invalid (overflow occurred)", s);
662 }
else if (error == EINVAL)
664 n_log(
LOG_ERR,
"number: '%s' invalid (base contains unsupported value)", s);
667 }
else if (error != 0 && l == 0) {
668 n_log(
LOG_ERR,
"number: '%s' invalid (unspecified error occurred)", s);
671 }
else if (error == 0 && !*endstr) {
672 n_log(
LOG_DEBUG,
"number : '%llu' valid (and represents all characters read)", l);
673 }
else if (error == 0 && *endstr != 0) {
674 n_log(
LOG_DEBUG,
" number : '%llu' valid (remaining characters: '%s')", l, s);
717 N_STR* new_str = NULL;
730 n_log(
LOG_ERR,
"Error duplicating N_STR %p -> data", str);
754 while (*iterator <= slen && isspace((
unsigned char)
string[*iterator])) {
755 if (inc < 0 && *iterator == 0) {
760 *iterator = *iterator + (
NSTRBYTE)inc;
762 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
766 while (*iterator <= slen &&
string[*iterator] == toskip) {
767 if (inc < 0 && *iterator == 0) {
772 *iterator = *iterator + (
NSTRBYTE)inc;
774 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
778 if (error_flag == 1 || *iterator > slen) {
801 while (*iterator <= slen && !isspace((
unsigned char)
string[*iterator])) {
802 if (inc < 0 && *iterator == 0) {
807 *iterator = *iterator + (
NSTRBYTE)inc;
809 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
813 while (*iterator <= slen &&
string[*iterator] != toskip) {
814 if (inc < 0 && *iterator == 0) {
819 *iterator = *iterator + (
NSTRBYTE)inc;
821 *iterator = (*iterator > (
NSTRBYTE)(-inc)) ? (*iterator - (
NSTRBYTE)(-inc)) : 0;
826 if (error_flag == 1 || *iterator > slen) {
840int strup(
const char*
string,
char* dest) {
846 for (it = 0; it < (
NSTRBYTE)strlen(
string); it++)
847 dest[it] = (
char)toupper(
string[it]);
849 dest[strlen(
string)] =
'\0';
861int strlo(
const char*
string,
char* dest) {
867 for (it = 0; it < (
NSTRBYTE)strlen(
string); it++)
868 dest[it] = (
char)tolower(
string[it]);
870 dest[strlen(
string)] =
'\0';
891 while (_it < to_size && from[(*it)] !=
'\0' && from[(*it)] != delim) {
892 to[_it] = from[(*it)];
897 if (_it == to_size) {
900 n_log(
LOG_DEBUG,
"strcpy_u: not enough space to write %d octet to dest (%d max) , %s: %d", _it, to_size, __FILE__, __LINE__);
906 if (from[(*it)] != delim) {
907 n_log(
LOG_DEBUG,
"strcpy_u: delim value not found, written %d octet to dest (%d max) , %s: %d", _it, to_size, __FILE__, __LINE__);
920char**
split(
const char* str,
const char* delim,
int empty) {
926 const char* largestring = NULL;
931 size_t sizeDelim = strlen(delim);
934 while ((ptr = strstr(largestring, delim)) != NULL) {
935 sizeStr = (size_t)(ptr - largestring);
936 if (empty == 1 || sizeStr != 0) {
938 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
942 Malloc(tab[sizeTab - 1],
char,
sizeof(
char) * (sizeStr + 1));
945 memcpy(tab[sizeTab - 1], largestring, sizeStr);
946 tab[sizeTab - 1][sizeStr] =
'\0';
948 ptr = ptr + sizeDelim;
953 if (strlen(largestring) != 0) {
954 sizeStr = strlen(largestring);
957 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
961 Malloc(tab[sizeTab - 1],
char,
sizeof(
char) * (sizeStr + 1));
964 memcpy(tab[sizeTab - 1], largestring, sizeStr);
965 tab[sizeTab - 1][sizeStr] =
'\0';
970 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
974 Malloc(tab[sizeTab - 1],
char, (
int)(
sizeof(
char) * 1));
977 tab[sizeTab - 1][0] =
'\0';
983 tmp = (
char**)realloc(tab,
sizeof(
char*) * sizeTab);
986 tab[sizeTab - 1] = NULL;
1005 while (split_result[it]) {
1021 while ((*tab)[it]) {
1037char*
join(
char** splitresult,
const char* delim) {
1038 size_t delim_length = 0;
1040 delim_length = strlen(delim);
1041 size_t total_length = 0;
1043 while (splitresult[it]) {
1045 size_t entry_length = strlen(splitresult[it]);
1046 if (entry_length >= SIZE_MAX - total_length) {
1047 n_log(
LOG_ERR,
"join: total_length overflow accumulating entry %d", it);
1050 total_length += entry_length;
1052 if (delim && splitresult[it + 1]) {
1053 if (delim_length >= SIZE_MAX - total_length) {
1054 n_log(
LOG_ERR,
"join: total_length overflow accumulating delimiter at entry %d", it);
1057 total_length += delim_length;
1061 char* result = NULL;
1062 Malloc(result,
char, total_length + 1);
1064 size_t position = 0;
1066 while (splitresult[it]) {
1067 size_t copy_size = strlen(splitresult[it]);
1068 memcpy(&result[position], splitresult[it], copy_size);
1069 position += copy_size;
1071 if (delim && splitresult[it + 1]) {
1072 memcpy(&result[position], delim, delim_length);
1073 position += delim_length;
1077 result[position] =
'\0';
1094 if (size >= SIZE_MAX - 1) {
1095 n_log(
LOG_ERR,
"size too large in nstrcat_ex: %zu", size);
1099 if (resize_flag == 0) {
1102 if (size > SIZE_MAX - (*dest)->written - 1) {
1103 n_log(
LOG_ERR,
"integer overflow in nstrcat_ex size calculation");
1106 if (((*dest)->written + size + 1) > (*dest)->length) {
1107 n_log(
LOG_ERR,
"%p to %p: not enough space. Resize forbidden. %zu needed, %zu available", src, (*dest), (*dest)->written + size + 1, (*dest)->length);
1111 n_log(
LOG_ERR,
"%p to %p: not enough space. Resize forbidden. %zu needed, destination is NULL", src, (*dest), size + 1);
1119 n_log(
LOG_ERR,
"could not allocate new N_STR of size %zu", size + 1);
1125 if (size > SIZE_MAX - (*dest)->written - 1) {
1126 n_log(
LOG_ERR,
"integer overflow in nstrcat_ex size calculation");
1130 if ((*dest)->length < (*dest)->written + size + 1) {
1131 (*dest)->length = (*dest)->written + size + 1;
1132 if (
Reallocz((*dest)->data,
char, (*dest)->written, (*dest)->length) == FALSE) {
1138 ptr = (*dest)->data + (*dest)->written;
1139 memcpy(ptr, src, size);
1140 (*dest)->written += size;
1142 (*dest)->data[(*dest)->written] =
'\0';
1158 n_log(
LOG_ERR,
"Could not copy 0 or less (%ld) octet!", size);
1188 if (src_size == 0)
return TRUE;
1189 NSTRBYTE needed_size = (*written) + src_size + 1;
1190 NSTRBYTE alloc_size = needed_size + additional_padding;
1193 if (alloc_size < needed_size)
return FALSE;
1196 if ((needed_size >= (*size)) || !(*dest)) {
1201 if (
Reallocz((*dest),
char, (*size), alloc_size) == FALSE) {
1207 (*size) = alloc_size;
1209 char* ptr = (*dest) + (*written);
1210 memcpy(ptr, src, src_size);
1211 (*written) += src_size;
1212 (*dest)[(*written)] =
'\0';
1255 struct dirent* entry = NULL;
1256 struct stat statbuf;
1261 if ((dp = opendir(dir)) == NULL) {
1266 N_STR* newname = NULL;
1267 while ((entry = readdir(dp)) != NULL) {
1268 if (!
nstrprintf(newname,
"%s/%s", dir, entry->d_name)) {
1269 n_log(
LOG_ERR,
"could not allocate newname for %s/%s", dir, entry->d_name);
1274 if (stat(newname->
data, &statbuf) >= 0) {
1275 if (S_ISDIR(statbuf.st_mode) != 0) {
1276 if (strcmp(
".", entry->d_name) == 0 || strcmp(
"..", entry->d_name) == 0) {
1282 if (recurse != FALSE) {
1284 n_log(
LOG_ERR,
"scan_dir_ex( %s , %s , %p , %d , %d ) returned FALSE !", newname->
data, pattern, result, recurse,
mode);
1289 }
else if (S_ISREG(statbuf.st_mode) != 0) {
1292 char* file = strdup(newname->
data);
1296 n_log(
LOG_ERR,
"Error adding %s/%s to list", dir, entry->d_name);
1298 }
else if (
mode == 1) {
1318int wildmat(
register const char* text,
register const char* p) {
1319 register int last = 0;
1320 register int matched = 0;
1321 register int reverse = 0;
1323 for (; *p; text++, p++) {
1324 if (*text ==
'\0' && *p !=
'*')
1346 if ((matched =
wildmat(text++, p)) != FALSE)
1355 if (p[1] ==
']' || p[1] ==
'-')
1358 for (last = *p; *++p && *p !=
']'; last = *p)
1360 if (*p ==
'-' && p[1] !=
']'
1361 ? *text <= *++p && *text >= last
1364 if (matched == reverse)
1369#ifdef WILDMAT_MATCH_TAR_PATTERN
1373 return *text ==
'\0';
1384 register int matched;
1385 register int reverse;
1387 for (; *p; text++, p++) {
1388 if (*text ==
'\0' && *p !=
'*')
1396 if (toupper(*text) != toupper(*p))
1419 if (p[1] ==
']' || p[1] ==
'-')
1420 if (toupper(*++p) == toupper(*text))
1422 for (last = toupper(*p); *++p && *p !=
']'; last = toupper(*p))
1423 if (*p ==
'-' && p[1] !=
']'
1424 ? toupper(*text) <= toupper(*++p) && toupper(*text) >= last
1425 : toupper(*text) == toupper(*p))
1427 if (matched == reverse)
1432#ifdef WILDMAT_MATCH_TAR_PATTERN
1436 return *text ==
'\0';
1448char*
str_replace(
const char*
string,
const char* substr,
const char* replacement) {
1449 const char* tok = NULL;
1450 char* newstr = NULL;
1453 if (substr == NULL || substr[0] ==
'\0' || replacement == NULL)
1454 return strdup(
string);
1456 size_t substr_len = strlen(substr);
1457 size_t replacement_len = strlen(replacement);
1459 newstr = strdup(
string);
1460 if (!newstr)
return NULL;
1462 while ((tok = strstr(head, substr))) {
1463 char* oldstr = newstr;
1464 size_t oldstr_len = strlen(oldstr);
1465 size_t base_len = oldstr_len - substr_len;
1467 if (base_len > SIZE_MAX - 8 ||
1468 replacement_len > SIZE_MAX - base_len - 8) {
1469 n_log(
LOG_ERR,
"str_replace: allocation size overflow (base_len=%zu, replacement_len=%zu)", base_len, replacement_len);
1473 size_t newlen = base_len + replacement_len + 8;
1474 Malloc(newstr,
char, newlen);
1476 if (newstr == NULL) {
1477 n_log(
LOG_ERR,
"str_replace: could not allocate newstr of size %zu", newlen);
1481 memcpy(newstr, oldstr, (
size_t)(tok - oldstr));
1482 memcpy(newstr + (tok - oldstr), replacement, replacement_len);
1483 memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (
size_t)(tok - oldstr));
1484 memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);
1486 head = newstr + (tok - oldstr) + replacement_len;
1506 for (it = 0; it < string_len; it++) {
1508 while (mask_it < masklen && mask[mask_it] !=
'\0') {
1509 if (
string[it] == mask[mask_it])
1510 string[it] = replacement;
1525 return str_sanitize_ex(
string, strlen(
string), mask, strlen(mask), replacement);
1570 int needed_size = 0;
1573 va_start(args, format);
1574 va_copy(args_copy, args);
1575 needed_size = vsnprintf(NULL, 0, format, args);
1578 if (needed_size < 0) {
1580 n_log(
LOG_ERR,
"there was an error while computing the new size according to format \"%s\" for N_STR %p", format, (*nstr_var));
1584 size_t needed = (size_t)(needed_size + 1);
1589 if (!(*nstr_var) || !(*nstr_var)->data) {
1591 n_log(
LOG_ERR,
"could not allocate N_STR of size %zu", needed);
1594 }
else if (needed > (*nstr_var)->length) {
1597 n_log(
LOG_ERR,
"could not resize N_STR %p to size %zu", (*nstr_var), needed);
1603 vsnprintf((*nstr_var)->data, needed, format, args_copy);
1606 (*nstr_var)->written = (size_t)needed_size;
1621 va_start(args, format);
1625 va_copy(args_copy, args);
1628 int needed_size = vsnprintf(NULL, 0, format, args_copy);
1631 if (needed_size < 0) {
1632 n_log(
LOG_ERR,
"vsnprintf size estimation failed for format \"%s\"", format);
1637 size_t needed = (size_t)(needed_size + 1);
1649 if (needed > SIZE_MAX - (*nstr_var)->written) {
1651 n_log(
LOG_ERR,
"integer overflow computing total_needed in nstrprintf_cat_ex");
1656 size_t total_needed = (*nstr_var)->written + needed;
1657 if (total_needed > (*nstr_var)->length) {
1658 if (
resize_nstr((*nstr_var), total_needed) == FALSE) {
1660 n_log(
LOG_ERR,
"could not resize N_STR %p to size %zu", (*nstr_var), total_needed);
1666 char* write_ptr = (*nstr_var)->data + (*nstr_var)->written;
1667 int written_now = vsnprintf(write_ptr, needed, format, args);
1670 if (written_now < 0) {
1671 n_log(
LOG_ERR,
"vsnprintf failed while writing at offset %zu in N_STR %p", (*nstr_var)->written, (*nstr_var));
1675 (*nstr_var)->written += (size_t)written_now;
1676 (*nstr_var)->data[(*nstr_var)->written] =
'\0';
1682 if (!tmpl)
return NULL;
1684 if (!result)
return NULL;
1685 const char* p = tmpl;
1687 if (p[0] ==
'{' && p[1] ==
'{') {
1688 const char* end = strstr(p + 2,
"}}");
1690 size_t klen = (size_t)(end - (p + 2));
1692 if (klen <
sizeof(
key)) {
1693 memcpy(
key, p + 2, klen);
1706 char ch[2] = {*p,
'\0'};
1726 if (!src)
return NULL;
1727 size_t in_len = strlen(src);
1729 if (in_len > (SIZE_MAX - 1) / 3)
return NULL;
1731 if (!out)
return NULL;
1732 for (
const unsigned char* p = (
const unsigned char*)src; *p; p++) {
1733 unsigned char c = *p;
1734 if ((c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') ||
1735 (c >=
'0' && c <=
'9') ||
1736 c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
1737 char ch[2] = {(char)c,
'\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
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
size_t length
total allocation (in bytes) of the data buffer, padding included
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.
N_STR * n_str_url_encode(const char *src)
Percent-encode a C string per RFC 3986 (unreserved set kept as-is).
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 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)
Append src to *dest.
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
int strcpy_u(const char *from, char *to, NSTRBYTE to_size, char delim, NSTRBYTE *it)
Copy from start to dest until from[ iterator ] == delim.
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)
Append src_size bytes from src to *dest, updating size/written, growing the buffer if needed.
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.