Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_str.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26#ifndef N_STRFUNC
27#define N_STRFUNC
28
30#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
41#include "n_common.h"
42#include "n_list.h"
43
44/* forward declaration for n_str_template_expand (avoids pulling in n_hash.h) */
45struct HASH_TABLE;
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <strings.h>
51#include <math.h>
52#include <fcntl.h>
53#include <unistd.h>
54#include <inttypes.h>
55
57typedef size_t NSTRBYTE;
58
60typedef struct N_STR {
62 char* data;
64 size_t length;
66 size_t written;
67} N_STR;
68
70#define WILDMAT_ABORT -2
72#define WILDMAT_NEGATE_CLASS '^'
74#undef WILDMAT_MATCH_TAR_PATTERN
75
77#define local_strdup(__src_) \
78 ({ \
79 char* __local_strdup_str = NULL; \
80 size_t __local_strdup_len = strlen((__src_)); \
81 Malloc(__local_strdup_str, char, __local_strdup_len + 1); \
82 if (!__local_strdup_str) { \
83 n_log(LOG_ERR, "Couldn't allocate %d byte for duplicating \"%s\"", (int)strlen((__src_)) + 1, (__src_)); \
84 } else { \
85 for (size_t __local_strdup_it = 0; __local_strdup_it <= __local_strdup_len; __local_strdup_it++) { \
86 __local_strdup_str[__local_strdup_it] = (__src_)[__local_strdup_it]; \
87 } \
88 } \
89 __local_strdup_str; \
90 })
91
93#define strprintf(__n_var, ...) \
94 ({ \
95 char* __strprintf_ptr = NULL; \
96 if (!(__n_var)) { \
97 int __needed = snprintf(NULL, 0, __VA_ARGS__); \
98 if (__needed > 0) { \
99 Malloc(__n_var, char, (size_t)__needed + 1); \
100 if (__n_var) { \
101 snprintf(__n_var, (size_t)__needed + 1, __VA_ARGS__); \
102 __strprintf_ptr = __n_var; \
103 } else { \
104 n_log(LOG_ERR, "couldn't allocate %s with %d bytes", \
105 #__n_var, __needed + 1); \
106 } \
107 } \
108 } else { \
109 n_log(LOG_ERR, "%s is already allocated.", #__n_var); \
110 } \
111 __strprintf_ptr; \
112 })
113
115#define nstrprintf(__nstr_var, __format, ...) \
116 nstrprintf_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
117
119#define nstrprintf_cat(__nstr_var, __format, ...) \
120 nstrprintf_cat_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
121
123#define nstrcat(__nstr_dst, __nstr_src) \
124 ({ \
125 N_STR* __nstrcat_ret = NULL; \
126 if (__nstr_src && __nstr_src->data) { \
127 __nstrcat_ret = nstrcat_ex(&(__nstr_dst), __nstr_src->data, __nstr_src->written, 1); \
128 } \
129 __nstrcat_ret; \
130 })
131
133#define nstrcat_bytes(__nstr_dst, __void_src) \
134 nstrcat_bytes_ex(&(__nstr_dst), __void_src, strlen(__void_src))
135
137#define n_remove_ending_cr(__nstr_var) \
138 do { \
139 if (__nstr_var && __nstr_var->data && __nstr_var->written > 0 && __nstr_var->data[__nstr_var->written - 1] == '\r') { \
140 __nstr_var->data[__nstr_var->written - 1] = '\0'; \
141 __nstr_var->written--; \
142 } \
143 } while (0)
144
146#define n_replace_cr(__nstr_var, __replacement) \
147 do { \
148 if (__nstr_var && __nstr_var->data && __nstr_var->written > 0) { \
149 char* __replaced = str_replace(__nstr_var->data, "\r", __replacement); \
150 if (__replaced) { \
151 Free(__nstr_var->data); \
152 __nstr_var->data = __replaced; \
153 __nstr_var->written = strlen(__nstr_var->data); \
154 __nstr_var->length = __nstr_var->written + 1; \
155 } \
156 } \
157 } while (0)
158
159#ifdef __windows__
160const char* strcasestr(const char* s1, const char* s2);
161#endif
162
164char* trim_nocopy(char* s);
166char* trim(const char* s);
168char* nfgets(char* buffer, NSTRBYTE size, FILE* stream);
170N_STR* new_nstr(NSTRBYTE size);
172int empty_nstr(N_STR* nstr);
174N_STR* nstrdup(N_STR* msg);
176int char_to_nstr_ex(const char* from, NSTRBYTE nboct, N_STR** to);
178N_STR* char_to_nstr(const char* src);
180int char_to_nstr_nocopy_ex(char* from, NSTRBYTE nboct, N_STR** to);
182N_STR* char_to_nstr_nocopy(char* src);
184N_STR* nstrcat_ex(N_STR** dest, void* src, NSTRBYTE size, int resize_flag);
186N_STR* nstrcat_bytes_ex(N_STR** dest, void* src, NSTRBYTE size);
188int resize_nstr(N_STR* nstr, size_t size);
190N_STR* nstrprintf_ex(N_STR** nstr_var, const char* format, ...);
192N_STR* nstrprintf_cat_ex(N_STR** nstr_var, const char* format, ...);
194N_STR* file_to_nstr(char* filename);
196int nstr_to_fd(N_STR* str, FILE* out, int lock);
198int nstr_to_file(N_STR* n_str, char* filename);
199
201#define free_nstr(__ptr) \
202 { \
203 if ((*__ptr)) { \
204 _free_nstr(__ptr); \
205 } else { \
206 n_log(LOG_DEBUG, "%s is already NULL", #__ptr); \
207 } \
208 }
209
211int _free_nstr(N_STR** ptr);
213void free_nstr_ptr(void* ptr);
215int free_nstr_nolog(N_STR** ptr);
217void free_nstr_ptr_nolog(void* ptr);
219int str_to_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long int* i, const int base);
221int str_to_long(const char* s, long int* i, const int base);
223int str_to_long_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long long int* i, const int base);
225int str_to_long_long(const char* s, long long int* i, const int base);
227int str_to_int_ex(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base);
229int str_to_int_nolog(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base, N_STR** infos);
231int str_to_int(const char* s, int* i, const int base);
233int skipw(const char* string, char toskip, NSTRBYTE* iterator, int inc);
235int skipu(const char* string, char toskip, NSTRBYTE* iterator, int inc);
237int strup(const char* string, char* dest);
239int strlo(const char* string, char* dest);
241int strcpy_u(const char* from, char* to, NSTRBYTE to_size, char split, NSTRBYTE* it);
243char** split(const char* str, const char* delim, int empty);
245int split_count(char** split_result);
247int free_split_result(char*** tab);
249char* join(char** splitresult, const char* delim);
251int write_and_fit_ex(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src, NSTRBYTE src_size, NSTRBYTE additional_padding);
253int write_and_fit(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src);
255int scan_dir(const char* dir, LIST* result, const int recurse);
257int scan_dir_ex(const char* dir, const char* pattern, LIST* result, const int recurse, const int mode);
259int wildmat(register const char* text, register const char* p);
261int wildmatcase(register const char* text, register const char* p);
263char* str_replace(const char* string, const char* substr, const char* replacement);
265int str_sanitize_ex(char* string, const NSTRBYTE string_len, const char* mask, const NSTRBYTE masklen, const char replacement);
267int str_sanitize(char* string, const char* mask, const char replacement);
268
274N_STR* n_str_template_expand(const char* tmpl, struct HASH_TABLE* vars);
275
280#ifdef __cplusplus
281}
282#endif
283/* #ifndef N_STR*/
284#endif
static int mode
structure of a hash table
Definition n_hash.h:137
Structure of a generic LIST container.
Definition n_list.h:58
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
int str_to_long_long(const char *s, long long int *i, const int base)
string to long long integer, shorter version
Definition n_str.c:695
N_STR * n_str_template_expand(const char *tmpl, struct HASH_TABLE *vars)
Expand double-brace tokens in a template using a hash table.
Definition n_str.c:1652
char * trim_nocopy(char *s)
trim and put a \0 at the end, return new begin pointer
Definition n_str.c:122
void free_nstr_ptr(void *ptr)
free a N_STR without setting the pointer to NULL
Definition n_str.c:69
N_STR * nstrprintf_cat_ex(N_STR **nstr_var, const char *format,...)
concatenate printf on a N_STR
Definition n_str.c:1588
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:57
int split_count(char **split_result)
count split elements
Definition n_str.c:992
int str_sanitize_ex(char *string, const NSTRBYTE string_len, const char *mask, const NSTRBYTE masklen, const char replacement)
sanitize a string using a character mask
Definition n_str.c:1472
int nstr_to_file(N_STR *n_str, char *filename)
write a whole N_STR into a file
Definition n_str.c:416
int scan_dir_ex(const char *dir, const char *pattern, LIST *result, const int recurse, const int mode)
get a list of files in a directory, extended N_STR version
Definition n_str.c:1224
int strcpy_u(const char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
copy from string to dest until from[iterator] == split
Definition n_str.c:876
int str_to_long_long_ex(const char *s, NSTRBYTE start, NSTRBYTE end, long long int *i, const int base)
string to long long integer, with error checking
Definition n_str.c:623
char * join(char **splitresult, const char *delim)
join a split result into a string
Definition n_str.c:1029
N_STR * nstrdup(N_STR *msg)
make a copy of a N_STR
Definition n_str.c:708
int strlo(const char *string, char *dest)
lower case a string
Definition n_str.c:853
N_STR * nstrcat_ex(N_STR **dest, void *src, NSTRBYTE size, int resize_flag)
concatenate data inside a N_STR
Definition n_str.c:1081
int write_and_fit(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src)
write and fit into the char array
Definition n_str.c:1200
int str_to_int_ex(const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base)
string to integer, with error checking
Definition n_str.c:454
int str_to_int_nolog(const char *s, NSTRBYTE start, NSTRBYTE end, int *i, const int base, N_STR **infos)
string to integer, with error checking and no logging
Definition n_str.c:502
int resize_nstr(N_STR *nstr, size_t size)
resize a N_STR
Definition n_str.c:1505
int char_to_nstr_nocopy_ex(char *from, NSTRBYTE nboct, N_STR **to)
convert a char into a N_STR without copying
N_STR * char_to_nstr(const char *src)
convert a char into a N_STR, shorter version
Definition n_str.c:254
char * nfgets(char *buffer, NSTRBYTE size, FILE *stream)
N_STR wrapper around fgets.
Definition n_str.c:170
int skipu(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip characters in string until string[iterator] == toskip
Definition n_str.c:786
int empty_nstr(N_STR *nstr)
reinitialize a nstr
Definition n_str.c:191
int wildmatcase(register const char *text, register const char *p)
pattern matching, case insensitive
Definition n_str.c:1353
N_STR * new_nstr(NSTRBYTE size)
create a new string
Definition n_str.c:206
char * str_replace(const char *string, const char *substr, const char *replacement)
return a new string with all occurrences of substr replaced
Definition n_str.c:1419
char * trim(const char *s)
trim and put a \0 at the end, return new char *
Definition n_str.c:151
int skipw(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip characters in string while string[iterator] == toskip
Definition n_str.c:739
N_STR * nstrcat_bytes_ex(N_STR **dest, void *src, NSTRBYTE size)
wrapper to nstrcat_ex to concatenate void *data
Definition n_str.c:1146
int scan_dir(const char *dir, LIST *result, const int recurse)
get a list of files in a directory
Definition n_str.c:1211
int str_sanitize(char *string, const char *mask, const char replacement)
in-place substitution of a set of chars by a single one
Definition n_str.c:1495
char ** split(const char *str, const char *delim, int empty)
return an array of char pointers to the split sections
Definition n_str.c:912
int wildmat(register const char *text, register const char *p)
pattern matching
Definition n_str.c:1289
int free_nstr_nolog(N_STR **ptr)
free a N_STR and set to NULL without logging
Definition n_str.c:96
int str_to_long(const char *s, long int *i, const int base)
string to long integer, shorter version
Definition n_str.c:680
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
convert a char into a N_STR
Definition n_str.c:231
N_STR * char_to_nstr_nocopy(char *src)
convert a char into a N_STR without copying, shorter version
Definition n_str.c:267
int _free_nstr(N_STR **ptr)
free a N_STR and set the pointer to NULL
Definition n_str.c:82
int write_and_fit_ex(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src, NSTRBYTE src_size, NSTRBYTE additional_padding)
write and fit bytes into a dynamically sized buffer
Definition n_str.c:1167
N_STR * file_to_nstr(char *filename)
load a whole file into a N_STR
Definition n_str.c:286
int free_split_result(char ***tab)
free a char **tab and set it to NULL
Definition n_str.c:1008
N_STR * nstrprintf_ex(N_STR **nstr_var, const char *format,...)
printf on a N_STR
Definition n_str.c:1536
void free_nstr_ptr_nolog(void *ptr)
free a N_STR without logging or setting to NULL
Definition n_str.c:109
int str_to_int(const char *s, int *i, const int base)
string to integer, shorter version
Definition n_str.c:545
int nstr_to_fd(N_STR *str, FILE *out, int lock)
write a whole N_STR into an open file descriptor
Definition n_str.c:363
int str_to_long_ex(const char *s, NSTRBYTE start, NSTRBYTE end, long int *i, const int base)
string to long integer, with error checking
Definition n_str.c:564
int strup(const char *string, char *dest)
upper case a string
Definition n_str.c:832
A box including a string and his lenght.
Definition n_str.h:60
Common headers and low-level functions & define.
List structures and definitions.