Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_str.h
Go to the documentation of this file.
1
8#ifndef N_STRFUNC
9#define N_STRFUNC
10
12#define BAD_METACHARS "/-+&;`'\\\"|*?~<>^()[]{}$\n\r\t "
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
23#include "n_common.h"
24#include "n_list.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <strings.h>
30#include <math.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <inttypes.h>
34
36typedef size_t NSTRBYTE;
37
39typedef struct N_STR {
41 char* data;
43 size_t length;
45 size_t written;
46} N_STR;
47
49#define WILDMAT_ABORT -2
51#define WILDMAT_NEGATE_CLASS '^'
53#undef WILDMAT_MATCH_TAR_PATTERN
54
56#define local_strdup(__src_) \
57 ({ \
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_)); \
63 } else { \
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]; \
66 } \
67 } \
68 __local_strdup_str; \
69 })
70
72#define strprintf(__n_var, ...) \
73 ({ \
74 char* __strprintf_ptr = NULL; \
75 if (!(__n_var)) { \
76 int __needed = snprintf(NULL, 0, __VA_ARGS__); \
77 if (__needed > 0) { \
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; \
81 } \
82 else { \
83 n_log(LOG_ERR, "couldn't allocate %s with %d bytes", \
84 #__n_var, __needed + 1); \
85 } \
86 } \
87 } else { \
88 n_log(LOG_ERR, "%s is already allocated.", #__n_var); \
89 } \
90 __strprintf_ptr; \
91 })
92
94#define nstrprintf(__nstr_var, __format, ...) \
95 nstrprintf_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
96
98#define nstrprintf_cat(__nstr_var, __format, ...) \
99 nstrprintf_cat_ex(&(__nstr_var), (__format), ##__VA_ARGS__)
100
102#define nstrcat(__nstr_dst, __nstr_src) \
103 ({ \
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); \
107 } \
108 __nstrcat_ret; \
109 })
110
112#define nstrcat_bytes(__nstr_dst, __void_src) \
113 nstrcat_bytes_ex(&(__nstr_dst), __void_src, strlen(__void_src))
114
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--; \
120 }
121
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); \
126 if (__replaced) { \
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; \
131 } \
132 }
133
134#ifdef __windows__
135const char* strcasestr(const char* s1, const char* s2);
136#endif
137
138/* trim and put a \0 at the end, return new begin pointer */
139char* trim_nocopy(char* s);
140/* trim and put a \0 at the end, return new char * */
141char* trim(char* s);
142/* N_STR wrapper around fgets */
143char* nfgets(char* buffer, NSTRBYTE size, FILE* stream);
144/* create a new string */
145N_STR* new_nstr(NSTRBYTE size);
146/* reinitialize a nstr */
147int empty_nstr(N_STR* nstr);
148/* Make a copy of a N_STR */
149N_STR* nstrdup(N_STR* msg);
150/* Convert a char into a N_STR */
151int char_to_nstr_ex(const char* from, NSTRBYTE nboct, N_STR** to);
152/* Convert a char into a N_STR, shorter version */
153N_STR* char_to_nstr(const char* src);
154/* Convert a char into a N_STR */
155int char_to_nstr_nocopy_ex(char* from, NSTRBYTE nboct, N_STR** to);
156/* Convert a char into a N_STR, shorter version */
157N_STR* char_to_nstr_nocopy(char* src);
158/* cat data inside a N8STR */
159N_STR* nstrcat_ex(N_STR** dest, void* src, NSTRBYTE size, int resize_flag);
160/* Wrapper to nstrcat_ex to concatenate void *data */
161N_STR* nstrcat_bytes_ex(N_STR** dest, void* src, NSTRBYTE size);
162/* Resize a N_STR */
163int resize_nstr(N_STR* nstr, size_t size);
164/* printf on a N_STR */
165N_STR* nstrprintf_ex(N_STR** nstr_var, const char* format, ...);
166/* concatenate printf on a N_STR */
167N_STR* nstrprintf_cat_ex(N_STR** nstr_var, const char* format, ...);
168/* Load a whole file into a N_STR. Be aware of the (4GB ||System Memory) limit */
169N_STR* file_to_nstr(char* filename);
170/* Write a whole N_STR into an open file descriptor */
171int nstr_to_fd(N_STR* str, FILE* out, int lock);
172/* Write a whole N_STR into a file */
173int nstr_to_file(N_STR* n_str, char* filename);
174
176#define free_nstr(__ptr) \
177 { \
178 if ((*__ptr)) { \
179 _free_nstr(__ptr); \
180 } else { \
181 n_log(LOG_DEBUG, "%s is already NULL", #__ptr); \
182 } \
183 }
184
185/* free NSTR and set it to NULL */
186int _free_nstr(N_STR** ptr);
187/* just free NSTR */
188void free_nstr_ptr(void* ptr);
189/* Do not warn and Free + set to NULL */
190int free_nstr_nolog(N_STR** ptr);
191/* Do not warn and just Free */
192void free_nstr_ptr_nolog(void* ptr);
193/* String to long integer, with error checking */
194int str_to_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long int* i, const int base);
195/* String to long integer, shorter version */
196int str_to_long(const char* s, long int* i, const int base);
197/* String to long long integer */
198int str_to_long_long_ex(const char* s, NSTRBYTE start, NSTRBYTE end, long long int* i, const int base);
199/* String to long long integer, shorter version */
200int str_to_long_long(const char* s, long long int* i, const int base);
201/* String to integer, with error checking */
202int str_to_int_ex(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base);
203/* String to integer, with error checking */
204int str_to_int_nolog(const char* s, NSTRBYTE start, NSTRBYTE end, int* i, const int base, N_STR** infos);
205/* String to integer, shorter version */
206int str_to_int(const char* s, int* i, const int base);
207/* Skip character from string while string[iterator] == toskip step inc */
208int skipw(char* string, char toskip, NSTRBYTE* iterator, int inc);
209/* Skip character from string until string[iterator] == toskip step inc */
210int skipu(char* string, char toskip, NSTRBYTE* iterator, int inc);
211/* Upper case a string */
212int strup(char* string, char* dest);
213/* Lower case a string */
214int strlo(char* string, char* dest);
215/* Copy from string to dest until from[ iterator ] == split */
216int strcpy_u(char* from, char* to, NSTRBYTE to_size, char split, NSTRBYTE* it);
217/* Return an array of char pointer to the splitted section */
218char** split(const char* str, const char* delim, int empty);
219/* Count split elements */
220int split_count(char** split_result);
221/* Free a char **tab and set it to NULL */
222int free_split_result(char*** tab);
223/* join a split result into a string */
224char* join(char** splitresult, char* delim);
225/* Write and fit bytes */
226int write_and_fit_ex(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src, NSTRBYTE src_size, NSTRBYTE additional_padding);
227/* Write and fit into the char array */
228int write_and_fit(char** dest, NSTRBYTE* size, NSTRBYTE* written, const char* src);
229/* get a list of the file in a directory */
230int scan_dir(const char* dir, LIST* result, const int recurse);
231/* get a list of the file in a directory, extented N_STR version */
232int scan_dir_ex(const char* dir, const char* pattern, LIST* result, const int recurse, const int mode);
233/* pattern matching */
234int wildmat(register const char* text, register const char* p);
235/* pattern matching case insensitive */
236int wildmatcase(register const char* text, register const char* p);
237/* return a replaced string */
238char* str_replace(const char* string, const char* substr, const char* replacement);
239/* sanitize string */
240int str_sanitize_ex(char* string, const NSTRBYTE string_len, const char* mask, const NSTRBYTE masklen, const char replacement);
241/* in-place substitution of a set of chars by a single one */
242int str_sanitize(char* string, const char* mask, const char replacement);
243
248#ifdef __cplusplus
249}
250#endif
251/* #ifndef N_STR*/
252#endif
Structure of a generic LIST container.
Definition n_list.h:39
size_t written
size of the written data inside the string
Definition n_str.h:45
char * data
the string
Definition n_str.h:41
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
int str_to_long_long(const char *s, long long int *i, const int base)
Helper for string to integer.
Definition n_str.c:657
int strcpy_u(char *from, char *to, NSTRBYTE to_size, char split, NSTRBYTE *it)
Copy from start to dest until from[ iterator ] == split.
Definition n_str.c:838
char * trim_nocopy(char *s)
trim and zero end the string, WARNING: keep and original pointer to delete the string correctly
Definition n_str.c:102
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
Definition n_str.c:49
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:36
int split_count(char **split_result)
Count split elements.
Definition n_str.c:954
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
Definition n_str.c:1380
int nstr_to_file(N_STR *n_str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:386
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.
Definition n_str.c:1150
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.
Definition n_str.c:587
N_STR * nstrdup(N_STR *msg)
Duplicate a N_STR.
Definition n_str.c:670
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 ],...
Definition n_str.c:1126
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.
Definition n_str.c:424
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.
Definition n_str.c:470
int resize_nstr(N_STR *nstr, size_t size)
reallocate a nstr internal buffer.
Definition n_str.c:1413
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
char * nfgets(char *buffer, NSTRBYTE size, FILE *stream)
try to fgets
Definition n_str.c:144
int skipw(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
Definition n_str.c:701
int empty_nstr(N_STR *nstr)
empty a N_STR string
Definition n_str.c:165
int wildmatcase(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
Definition n_str.c:1275
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:180
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...
Definition n_str.c:1341
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.
Definition n_str.c:1072
int scan_dir(const char *dir, LIST *result, const int recurse)
Scan a list of directory and return a list of char *file.
Definition n_str.c:1137
int str_sanitize(char *string, const char *mask, const char replacement)
clean a string by replacing evil characteres
Definition n_str.c:1403
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.
Definition n_str.c:874
int strup(char *string, char *dest)
Upper case a string.
Definition n_str.c:798
int wildmat(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
Definition n_str.c:1211
int free_nstr_nolog(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
Definition n_str.c:76
int str_to_long(const char *s, long int *i, const int base)
Helper for string to integer.
Definition n_str.c:642
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, extended version.
Definition n_str.c:205
N_STR * char_to_nstr_nocopy(char *src)
Convert a char into a N_STR, direct use of linked source pointer.
Definition n_str.c:241
int _free_nstr(N_STR **ptr)
Free a N_STR structure and set the pointer to NULL.
Definition n_str.c:62
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...
Definition n_str.c:1093
char * join(char **splitresult, char *delim)
join the array into a string
Definition n_str.c:990
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:260
int free_split_result(char ***tab)
Free a split result allocated array.
Definition n_str.c:970
void free_nstr_ptr_nolog(void *ptr)
Free a N_STR pointer structure.
Definition n_str.c:89
int str_to_int(const char *s, int *i, const int base)
Helper for string to integer.
Definition n_str.c:511
int skipu(char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
Definition n_str.c:750
char * trim(char *s)
trim and put a \0 at the end, return new char *
Definition n_str.c:131
int strlo(char *string, char *dest)
Upper case a string.
Definition n_str.c:817
int nstr_to_fd(N_STR *str, FILE *out, int lock)
Write a N_STR content into a file.
Definition n_str.c:336
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.
Definition n_str.c:530
A box including a string and his lenght.
Definition n_str.h:39
Common headers and low-level functions & define.
List structures and definitions.