Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_nstr.c

Nilorea Library string api test.

Nilorea Library string api test

Author
Castagnier Mickael
Version
1.0
Date
26/05/2015
#include "nilorea/n_str.h"
#include "nilorea/n_log.h"
int main(void) {
char* chardest = NULL;
NSTRBYTE written = 0,
length = 0;
write_and_fit(&chardest, &length, &written, "Hello");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit(&chardest, &length, &written, " ");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit(&chardest, &length, &written, "world !");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit(&chardest, &length, &written, "world ! ");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit(&chardest, &length, &written, "world ! ");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit(&chardest, &length, &written, "world ! ");
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
Free(chardest);
written = length = 0;
write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit_ex(&chardest, &length, &written, " ", 1, 0);
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit_ex(&chardest, &length, &written, "Hello", 5, 0);
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit_ex(&chardest, &length, &written, " ", 1, 10); // alocate 10 more byte if resize needed
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
write_and_fit_ex(&chardest, &length, &written, "world !", 7, 0);
n_log(LOG_INFO, "charstr (%d/%d): %s\n", written, length, chardest);
Free(chardest);
N_STR* nstr = NULL;
n_log(LOG_INFO, "NULL str:%s\n", _nstr(nstr));
nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr));
nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr));
free_nstr(&nstr);
nstr = new_nstr(0);
n_log(LOG_INFO, "EMPTY str:%s\n", _nstr(nstr));
nstrprintf(nstr, "Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr));
nstrprintf_cat(nstr, " - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr));
nstrprintf_cat(nstr, " - some more texte");
N_STR* nstr2 = nstrdup(nstr);
n_log(LOG_INFO, "str: %s\n str2: %s\n", _nstr(nstr), _nstr(nstr2));
N_STR* nstr3 = NULL;
nstrcat(nstr3, nstr);
nstrcat(nstr3, nstr2);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr3));
free_nstr(&nstr3);
nstr3 = new_nstr(10);
nstrcat(nstr3, nstr);
nstrcat(nstr3, nstr2);
n_log(LOG_INFO, "str:%s\n", _nstr(nstr3));
free_nstr(&nstr);
free_nstr(&nstr2);
free_nstr(&nstr3);
nstr = new_nstr(128);
char data[1048576] = "";
for (int it = 0; it < 1048575; it++) {
data[it] = 32 + rand() % 63;
}
data[1048574] = '\0';
for (int it = 0; it < 100; it++) {
write_and_fit(&nstr->data, &nstr->length, &nstr->written, data);
}
free_nstr(&nstr);
exit(0);
}
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:182
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_INFO
informational
Definition n_log.h:62
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.