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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#ifndef N_STRFUNC
28#define N_STRFUNC
29
31#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
42#include "n_common.h"
43#include "n_list.h"
44
45/* forward declaration for n_str_template_expand (avoids pulling in n_hash.h) */
46struct HASH_TABLE;
47
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <strings.h>
52#include <math.h>
53#include <fcntl.h>
54#include <unistd.h>
55#include <inttypes.h>
56
58typedef size_t NSTRBYTE;
59
61typedef struct N_STR {
63 char* data;
65 size_t length;
68 size_t written;
69} N_STR;
70
72#define WILDMAT_ABORT -2
74#define WILDMAT_NEGATE_CLASS '^'
76#undef WILDMAT_MATCH_TAR_PATTERN
77
79#define local_strdup(__src_) \
80 ({ \
81 char* __local_strdup_str = NULL; \
82 size_t __local_strdup_len = strlen((__src_)); \
83 Malloc(__local_strdup_str, char, __local_strdup_len + 1); \
84 if (!__local_strdup_str) { \
85 n_log(LOG_ERR, "Couldn't allocate %d byte for duplicating \"%s\"", (int)strlen((__src_)) + 1, (__src_)); \
86 } else { \
87 for (size_t __local_strdup_it = 0; __local_strdup_it <= __local_strdup_len; __local_strdup_it++) { \
88 __local_strdup_str[__local_strdup_it] = (__src_)[__local_strdup_it]; \
89 } \
90 } \
91 __local_strdup_str; \
92 })
93
95#define strprintf(__n_var, ...) \
96 ({ \
97 char* __strprintf_ptr = NULL; \
98 if (!(__n_var)) { \
99 int __needed = snprintf(NULL, 0, __VA_ARGS__); \
100 if (__needed > 0) { \
101 Malloc(__n_var, char, (size_t)__needed + 1); \
102 if (__n_var) { \
103 snprintf(__n_var, (size_t)__needed + 1, __VA_ARGS__); \
104 __strprintf_ptr = __n_var; \
105 } else { \
106 n_log(LOG_ERR, "couldn't allocate %s with %d bytes", \
107 #__n_var, __needed + 1); \
108 } \
109 } \
110 } else { \
111 n_log(LOG_ERR, "%s is already allocated.", #__n_var); \
112 } \
113 __strprintf_ptr; \
114 })
115
117#define nstrprintf(__nstr_var, __format, ...) \
118 nstrprintf_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
119
121#define nstrprintf_cat(__nstr_var, __format, ...) \
122 nstrprintf_cat_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
123
125#define nstrcat(__nstr_dst, __nstr_src) \
126 ({ \
127 N_STR* __nstrcat_ret = NULL; \
128 if (__nstr_src && __nstr_src->data) { \
129 __nstrcat_ret = nstrcat_ex(&(__nstr_dst), __nstr_src->data, __nstr_src->written, 1); \
130 } \
131 __nstrcat_ret; \
132 })
133
135#define nstrcat_bytes(__nstr_dst, __void_src) \
136 nstrcat_bytes_ex(&(__nstr_dst), __void_src, strlen(__void_src))
137
139#define n_remove_ending_cr(__nstr_var) \
140 do { \
141 if (__nstr_var && __nstr_var->data && __nstr_var->written > 0 && __nstr_var->data[__nstr_var->written - 1] == '\r') { \
142 __nstr_var->data[__nstr_var->written - 1] = '\0'; \
143 __nstr_var->written--; \
144 } \
145 } while (0)
146
148#define n_replace_cr(__nstr_var, __replacement) \
149 do { \
150 if (__nstr_var && __nstr_var->data && __nstr_var->written > 0) { \
151 char* __replaced = str_replace(__nstr_var->data, "\r", __replacement); \
152 if (__replaced) { \
153 Free(__nstr_var->data); \
154 __nstr_var->data = __replaced; \
155 __nstr_var->written = strlen(__nstr_var->data); \
156 __nstr_var->length = __nstr_var->written + 1; \
157 } \
158 } \
159 } while (0)
160
161#ifdef __windows__
162const char* strcasestr(const char* s1, const char* s2);
163#endif
164
166char* trim_nocopy(char* s);
168char* trim(const char* s);
170char* nfgets(char* buffer, NSTRBYTE size, FILE* stream);
172N_STR* new_nstr(NSTRBYTE size);
174int empty_nstr(N_STR* nstr);
176N_STR* nstrdup(N_STR* msg);
178int char_to_nstr_ex(const char* from, NSTRBYTE nboct, N_STR** to);
180N_STR* char_to_nstr(const char* src);
182int char_to_nstr_nocopy_ex(char* from, NSTRBYTE nboct, N_STR** to);
184N_STR* char_to_nstr_nocopy(char* src);
186N_STR* nstrcat_ex(N_STR** dest, void* src, NSTRBYTE size, int resize_flag);
188N_STR* nstrcat_bytes_ex(N_STR** dest, void* src, NSTRBYTE size);
190int resize_nstr(N_STR* nstr, size_t size);
192N_STR* nstrprintf_ex(N_STR** nstr_var, const char* format, ...);
194N_STR* nstrprintf_cat_ex(N_STR** nstr_var, const char* format, ...);
196N_STR* file_to_nstr(char* filename);
198int nstr_to_fd(N_STR* str, FILE* out, int lock);
200int nstr_to_file(N_STR* n_str, char* filename);
201
203#define free_nstr(__ptr) \
204 { \
205 if ((*__ptr)) { \
206 _free_nstr(__ptr); \
207 } else { \
208 n_log(LOG_DEBUG, "%s is already NULL", #__ptr); \
209 } \
210 }
211
213int _free_nstr(N_STR** ptr);
215void free_nstr_ptr(void* ptr);
217int free_nstr_nolog(N_STR** ptr);
219void free_nstr_ptr_nolog(void* ptr);
221int str_to_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long int* i, const int base);
223int str_to_long(const char* s, long int* i, const int base);
225int str_to_long_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long long int* i, const int base);
227int str_to_long_long(const char* s, long long int* i, const int base);
229int str_to_int_ex(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base);
231int str_to_int_nolog(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base, N_STR** infos);
233int str_to_int(const char* s, int* i, const int base);
235int skipw(const char* string, char toskip, NSTRBYTE* iterator, int inc);
237int skipu(const char* string, char toskip, NSTRBYTE* iterator, int inc);
239int strup(const char* string, char* dest);
241int strlo(const char* string, char* dest);
243int strcpy_u(const char* from, char* to, NSTRBYTE to_size, char delim, NSTRBYTE* it);
245char** split(const char* str, const char* delim, int empty);
247int split_count(char** split_result);
249int free_split_result(char*** tab);
251char* join(char** splitresult, const char* delim);
253int write_and_fit_ex(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src, NSTRBYTE src_size, NSTRBYTE additional_padding);
255int write_and_fit(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src);
257int scan_dir(const char* dir, LIST* result, const int recurse);
259int scan_dir_ex(const char* dir, const char* pattern, LIST* result, const int recurse, const int mode);
261int wildmat(register const char* text, register const char* p);
263int wildmatcase(register const char* text, register const char* p);
265char* str_replace(const char* string, const char* substr, const char* replacement);
267int str_sanitize_ex(char* string, const NSTRBYTE string_len, const char* mask, const NSTRBYTE masklen, const char replacement);
269int str_sanitize(char* string, const char* mask, const char replacement);
270
276N_STR* n_str_template_expand(const char* tmpl, struct HASH_TABLE* vars);
277
284N_STR* n_str_url_encode(const char* src);
285
290#ifdef __cplusplus
291}
292#endif
293/* #ifndef N_STR*/
294#endif
static int mode
structure of a hash table
Definition n_hash.h:138
Structure of a generic LIST container.
Definition n_list.h:59
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
size_t length
total allocation (in bytes) of the data buffer, padding included
Definition n_str.h:65
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:703
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:1681
char * trim_nocopy(char *s)
trim and put a \0 at the end, return new begin pointer
Definition n_str.c:123
void free_nstr_ptr(void *ptr)
free a N_STR without setting the pointer to NULL
Definition n_str.c:70
N_STR * nstrprintf_cat_ex(N_STR **nstr_var, const char *format,...)
concatenate printf on a N_STR
Definition n_str.c:1617
N_STR * n_str_url_encode(const char *src)
Percent-encode a string per RFC 3986 (unreserved chars kept).
Definition n_str.c:1725
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:58
int split_count(char **split_result)
count split elements
Definition n_str.c:1000
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:1501
int nstr_to_file(N_STR *n_str, char *filename)
write a whole N_STR into a file
Definition n_str.c:424
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:1253
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:631
char * join(char **splitresult, const char *delim)
join a split result into a string
Definition n_str.c:1037
N_STR * nstrdup(N_STR *msg)
make a copy of a N_STR
Definition n_str.c:716
int strlo(const char *string, char *dest)
lower case a string
Definition n_str.c:861
N_STR * nstrcat_ex(N_STR **dest, void *src, NSTRBYTE size, int resize_flag)
concatenate data inside a N_STR
Definition n_str.c:1089
int write_and_fit(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src)
write and fit into the char array
Definition n_str.c:1229
int strcpy_u(const char *from, char *to, NSTRBYTE to_size, char delim, NSTRBYTE *it)
copy from string to dest until from[iterator] == delim
Definition n_str.c:884
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:462
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:510
int resize_nstr(N_STR *nstr, size_t size)
resize a N_STR
Definition n_str.c:1534
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:255
char * nfgets(char *buffer, NSTRBYTE size, FILE *stream)
N_STR wrapper around fgets.
Definition n_str.c:171
int skipu(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip characters in string until string[iterator] == toskip
Definition n_str.c:794
int empty_nstr(N_STR *nstr)
reinitialize a nstr
Definition n_str.c:192
int wildmatcase(register const char *text, register const char *p)
pattern matching, case insensitive
Definition n_str.c:1382
N_STR * new_nstr(NSTRBYTE size)
create a new string
Definition n_str.c:207
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:1448
char * trim(const char *s)
trim and put a \0 at the end, return new char *
Definition n_str.c:152
int skipw(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip characters in string while string[iterator] == toskip
Definition n_str.c:747
N_STR * nstrcat_bytes_ex(N_STR **dest, void *src, NSTRBYTE size)
wrapper to nstrcat_ex to concatenate void *data
Definition n_str.c:1154
int scan_dir(const char *dir, LIST *result, const int recurse)
get a list of files in a directory
Definition n_str.c:1240
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:1524
char ** split(const char *str, const char *delim, int empty)
return an array of char pointers to the split sections
Definition n_str.c:920
int wildmat(register const char *text, register const char *p)
pattern matching
Definition n_str.c:1318
int free_nstr_nolog(N_STR **ptr)
free a N_STR and set to NULL without logging
Definition n_str.c:97
int str_to_long(const char *s, long int *i, const int base)
string to long integer, shorter version
Definition n_str.c:688
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
convert a char into a N_STR
Definition n_str.c:232
N_STR * char_to_nstr_nocopy(char *src)
convert a char into a N_STR without copying, shorter version
Definition n_str.c:268
int _free_nstr(N_STR **ptr)
free a N_STR and set the pointer to NULL
Definition n_str.c:83
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:1187
N_STR * file_to_nstr(char *filename)
load a whole file into a N_STR
Definition n_str.c:288
int free_split_result(char ***tab)
free a char **tab and set it to NULL
Definition n_str.c:1016
N_STR * nstrprintf_ex(N_STR **nstr_var, const char *format,...)
printf on a N_STR
Definition n_str.c:1565
void free_nstr_ptr_nolog(void *ptr)
free a N_STR without logging or setting to NULL
Definition n_str.c:110
int str_to_int(const char *s, int *i, const int base)
string to integer, shorter version
Definition n_str.c:553
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:371
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:572
int strup(const char *string, char *dest)
upper case a string
Definition n_str.c:840
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
List structures and definitions.