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