Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_nstr.c
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
27#include "nilorea/n_str.h"
28#include "nilorea/n_log.h"
29
30int main(void) {
32
33 char* chardest = NULL;
34 NSTRBYTE written = 0,
35 length = 0;
36
37 write_and_fit(&chardest, &length, &written, "Hello");
38 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
39 write_and_fit(&chardest, &length, &written, " ");
40 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
41 write_and_fit(&chardest, &length, &written, "world !");
42 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
43 write_and_fit(&chardest, &length, &written, "world ! ");
44 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
45 write_and_fit(&chardest, &length, &written, "world ! ");
46 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
47 write_and_fit(&chardest, &length, &written, "world ! ");
48 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
49
50 Free(chardest);
51 written = length = 0;
52
53 write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
54 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
55 write_and_fit_ex(&chardest, &length, &written, " ", 1, 0);
56 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
57 write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
58 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
59 write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
60 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
61 write_and_fit_ex(&chardest, &length, &written, " ", 1, 10); // alocate 10 more byte if resize needed
62 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
63 write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
64 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
65
66 Free(chardest);
67
68 N_STR* nstr = NULL;
69
70 n_log(LOG_INFO, "NULL str:%s", _nstr(nstr));
71
72 nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
73
74 n_log(LOG_INFO, "str:%s", _nstr(nstr));
75
76 nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
77
78 n_log(LOG_INFO, "str:%s", _nstr(nstr));
79
80 free_nstr(&nstr);
81
82 nstr = new_nstr(0);
83
84 n_log(LOG_INFO, "EMPTY str:%s", _nstr(nstr));
85
86 nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
87
88 n_log(LOG_INFO, "str:%s", _nstr(nstr));
89
90 nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
91
92 n_log(LOG_INFO, "str:%s", _nstr(nstr));
93
94 nstrprintf_cat(nstr, " - some more texte");
95
96 N_STR* nstr2 = nstrdup(nstr);
97
98 n_log(LOG_INFO, "str: %s\n str2: %s", _nstr(nstr), _nstr(nstr2));
99
100 N_STR* nstr3 = NULL;
101
102 nstrcat(nstr3, nstr);
103 nstrcat(nstr3, nstr2);
104
105 n_log(LOG_INFO, "str:%s", _nstr(nstr3));
106
107 free_nstr(&nstr3);
108
109 nstr3 = new_nstr(10);
110
111 nstrcat(nstr3, nstr);
112 nstrcat(nstr3, nstr2);
113
114 n_log(LOG_INFO, "str:%s", _nstr(nstr3));
115
116 free_nstr(&nstr);
117 free_nstr(&nstr2);
118 free_nstr(&nstr3);
119
120 nstr = new_nstr(128);
121 char data[1048576] = "";
122
123 for (int it = 0; it < 1048575; it++) {
124 data[it] = (char)(32 + rand() % 63);
125 }
126 data[1048574] = '\0';
127
128 for (int it = 0; it < 100; it++) {
129 write_and_fit(&nstr->data, &nstr->length, &nstr->written, data);
130 }
131
132 free_nstr(&nstr);
133
134 /* test char_to_nstr and empty_nstr */
135 nstr = char_to_nstr("char_to_nstr test");
136 n_log(LOG_INFO, "char_to_nstr: %s", _nstr(nstr));
137 empty_nstr(nstr);
138 n_log(LOG_INFO, "after empty_nstr: '%s' written=%zu", _nstr(nstr), nstr->written);
139 free_nstr(&nstr);
140
141 /* test trim */
142 char* trimmed = trim(" spaces around ");
143 if (trimmed) {
144 n_log(LOG_INFO, "trim result: '%s'", trimmed);
145 Free(trimmed);
146 }
147 /* test trim_nocopy */
148 char trimbuf[] = " hello ";
149 char* trimptr = trim_nocopy(trimbuf);
150 n_log(LOG_INFO, "trim_nocopy result: '%s'", trimptr);
151
152 /* test str_to_int, str_to_long, str_to_long_long */
153 int ival = 0;
154 if (str_to_int("12345", &ival, 10) == TRUE) {
155 n_log(LOG_INFO, "str_to_int: %d", ival);
156 }
157 long int lval = 0;
158 if (str_to_long("9876543210", &lval, 10) == TRUE) {
159 n_log(LOG_INFO, "str_to_long: %ld", lval);
160 }
161 long long int llval = 0;
162 if (str_to_long_long("123456789012345", &llval, 10) == TRUE) {
163 n_log(LOG_INFO, "str_to_long_long: %lld", llval);
164 }
165
166 /* test strup, strlo */
167 char upper[32] = "";
168 char lower[32] = "";
169 strup("hello world", upper);
170 n_log(LOG_INFO, "strup: %s", upper);
171 strlo("HELLO WORLD", lower);
172 n_log(LOG_INFO, "strlo: %s", lower);
173
174 /* test split, split_count, join, free_split_result */
175 char** parts = split("one:two:three:four", ":", 0);
176 if (parts) {
177 int count = split_count(parts);
178 n_log(LOG_INFO, "split count: %d", count);
179 for (int it = 0; it < count; it++) {
180 n_log(LOG_INFO, " split[%d]: %s", it, parts[it]);
181 }
182 char* joined = join(parts, " - ");
183 if (joined) {
184 n_log(LOG_INFO, "join result: %s", joined);
185 Free(joined);
186 }
187 free_split_result(&parts);
188 }
189
190 /* test wildmat, wildmatcase */
191 n_log(LOG_INFO, "wildmat(\"hello.txt\", \"*.txt\"): %d", wildmat("hello.txt", "*.txt"));
192 n_log(LOG_INFO, "wildmat(\"hello.txt\", \"*.csv\"): %d", wildmat("hello.txt", "*.csv"));
193 n_log(LOG_INFO, "wildmatcase(\"Hello.TXT\", \"*.txt\"): %d", wildmatcase("Hello.TXT", "*.txt"));
194
195 /* test str_replace */
196 char* replaced = str_replace("hello world hello", "hello", "bye");
197 if (replaced) {
198 n_log(LOG_INFO, "str_replace: %s", replaced);
199 Free(replaced);
200 }
201
202 /* test str_sanitize */
203 char sanitize_buf[] = "hello; rm -rf /";
204 str_sanitize(sanitize_buf, ";/", '_');
205 n_log(LOG_INFO, "str_sanitize: %s", sanitize_buf);
206
207 /* test file_to_nstr and nstr_to_file */
208 N_STR* file_content = char_to_nstr("file content test\nsecond line\n");
209 nstr_to_file(file_content, "nilorea_nstr_test.txt");
210 free_nstr(&file_content);
211 file_content = file_to_nstr("nilorea_nstr_test.txt");
212 if (file_content) {
213 n_log(LOG_INFO, "file_to_nstr: %s", _nstr(file_content));
214 free_nstr(&file_content);
215 }
216
217 /* test skipw, skipu */
218 char skipbuf[] = " hello";
219 NSTRBYTE it = 0;
220 skipw(skipbuf, ' ', &it, 1);
221 n_log(LOG_INFO, "skipw past spaces: pos=%zu char='%c'", it, skipbuf[it]);
222 it = 0;
223 skipu(skipbuf, 'h', &it, 1);
224 n_log(LOG_INFO, "skipu to 'h': pos=%zu char='%c'", it, skipbuf[it]);
225
226 /* test resize_nstr */
227 nstr = new_nstr(16);
228 n_log(LOG_INFO, "before resize: length=%zu", nstr->length);
229 resize_nstr(nstr, 256);
230 n_log(LOG_INFO, "after resize: length=%zu", nstr->length);
231 free_nstr(&nstr);
232
233 /* test scan_dir */
234 LIST* dir_list = new_generic_list(0);
235 if (scan_dir(".", dir_list, 0) == TRUE) {
236 n_log(LOG_INFO, "scan_dir found %zu entries", dir_list->nb_items);
237 }
238 list_destroy(&dir_list);
239
240 exit(0);
241}
int main(void)
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
size_t nb_items
number of item currently in the list
Definition n_list.h:60
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:547
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:36
Structure of a generic LIST container.
Definition n_list.h:58
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_INFO
informational
Definition n_log.h:81
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)
Helper for string to integer.
Definition n_str.c:695
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:122
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:57
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
int split_count(char **split_result)
Count split elements.
Definition n_str.c:992
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:416
char * join(char **splitresult, const char *delim)
join the array into a string
Definition n_str.c:1029
#define nstrcat(__nstr_dst, __nstr_src)
Macro to quickly concatenate two N_STR.
Definition n_str.h:123
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition n_str.c:708
int strlo(const char *string, char *dest)
Lower case a string.
Definition n_str.c:853
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:1200
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
Definition n_str.h:119
int resize_nstr(N_STR *nstr, size_t size)
reallocate a nstr internal buffer.
Definition n_str.c:1505
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
int skipu(const 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:786
int empty_nstr(N_STR *nstr)
empty a N_STR string
Definition n_str.c:191
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:1353
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:206
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: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 while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
Definition n_str.c:739
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
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:1211
int str_sanitize(char *string, const char *mask, const char replacement)
clean a string by replacing evil characteres
Definition n_str.c:1495
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:912
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:1289
int str_to_long(const char *s, long int *i, const int base)
Helper for string to integer.
Definition n_str.c:680
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: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 split result allocated array.
Definition n_str.c:1008
int str_to_int(const char *s, int *i, const int base)
Helper for string to integer.
Definition n_str.c:545
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
Generic log system.
N_STR and string function declaration.