Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_nstr.c
Go to the documentation of this file.
1
9#include "nilorea/n_str.h"
10#include "nilorea/n_log.h"
11
12int main(void) {
14
15 char* chardest = NULL;
16 NSTRBYTE written = 0,
17 length = 0;
18
19 write_and_fit(&chardest, &length, &written, "Hello");
20 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
21 write_and_fit(&chardest, &length, &written, " ");
22 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
23 write_and_fit(&chardest, &length, &written, "world !");
24 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
25 write_and_fit(&chardest, &length, &written, "world ! ");
26 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
27 write_and_fit(&chardest, &length, &written, "world ! ");
28 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
29 write_and_fit(&chardest, &length, &written, "world ! ");
30 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
31
32 Free(chardest);
33 written = length = 0;
34
35 write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
36 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
37 write_and_fit_ex(&chardest, &length, &written, " ", 1, 0);
38 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
39 write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
40 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
41 write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
42 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
43 write_and_fit_ex(&chardest, &length, &written, " ", 1, 10); // alocate 10 more byte if resize needed
44 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
45 write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
46 n_log(LOG_INFO, "charstr (%d/%d): %s", written, length, chardest);
47
48 Free(chardest);
49
50 N_STR* nstr = NULL;
51
52 n_log(LOG_INFO, "NULL str:%s", _nstr(nstr));
53
54 nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
55
56 n_log(LOG_INFO, "str:%s", _nstr(nstr));
57
58 nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
59
60 n_log(LOG_INFO, "str:%s", _nstr(nstr));
61
62 free_nstr(&nstr);
63
64 nstr = new_nstr(0);
65
66 n_log(LOG_INFO, "EMPTY str:%s", _nstr(nstr));
67
68 nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
69
70 n_log(LOG_INFO, "str:%s", _nstr(nstr));
71
72 nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
73
74 n_log(LOG_INFO, "str:%s", _nstr(nstr));
75
76 nstrprintf_cat(nstr, " - some more texte");
77
78 N_STR* nstr2 = nstrdup(nstr);
79
80 n_log(LOG_INFO, "str: %s\n str2: %s", _nstr(nstr), _nstr(nstr2));
81
82 N_STR* nstr3 = NULL;
83
84 nstrcat(nstr3, nstr);
85 nstrcat(nstr3, nstr2);
86
87 n_log(LOG_INFO, "str:%s", _nstr(nstr3));
88
89 free_nstr(&nstr3);
90
91 nstr3 = new_nstr(10);
92
93 nstrcat(nstr3, nstr);
94 nstrcat(nstr3, nstr2);
95
96 n_log(LOG_INFO, "str:%s", _nstr(nstr3));
97
98 free_nstr(&nstr);
99 free_nstr(&nstr2);
100 free_nstr(&nstr3);
101
102 nstr = new_nstr(128);
103 char data[1048576] = "";
104
105 for (int it = 0; it < 1048575; it++) {
106 data[it] = 32 + rand() % 63;
107 }
108 data[1048574] = '\0';
109
110 for (int it = 0; it < 100; it++) {
111 write_and_fit(&nstr->data, &nstr->length, &nstr->written, data);
112 }
113
114 free_nstr(&nstr);
115
116 exit(0);
117}
int main(void)
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:240
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:180
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_INFO
informational
Definition n_log.h:63
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
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:36
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
#define nstrcat(__nstr_dst, __nstr_src)
Macro to quickly concatenate two N_STR.
Definition n_str.h:102
N_STR * nstrdup(N_STR *str)
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
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
Definition n_str.h:98
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:180
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
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
A box including a string and his lenght.
Definition n_str.h:39
Generic log system.
N_STR and string function declaration.