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

Nilorea Library N_FILE api test.

Nilorea Library N_FILE api test

Author
Castagnier Mickael
Version
1.0
Date
03/01/2019
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#ifndef __windows__
#include <sys/wait.h>
#endif
void usage(void) {
fprintf(stderr,
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
void process_args(int argc, char** argv) {
int getoptret = 0,
log_level = LOG_DEBUG; /* default log level */
/* Arguments optionnels */
/* -v version
* -V log level
* -h help
*/
while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
switch (getoptret) {
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
exit(-1);
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, "\n Missing log level\n");
} else if (optopt == 'p') {
fprintf(stderr, "\n Missing port\n");
} else if (optopt != 's') {
fprintf(stderr, "\n Unknow missing option %c\n", optopt);
}
usage();
exit(1);
}
case 'h': {
usage();
exit(1);
}
}
}
} /* void process_args( ... ) */
n_log(LOG_DEBUG, "Inlined func");
}
int main(int argc, char** argv) {
/* processing args and set log_level */
process_args(argc, argv);
n_log(LOG_INFO, "TRUE,true values: %d,%d / FALSE,false value: %d,%d", TRUE, true, FALSE, false);
n_log(LOG_INFO, "_str(\"TEST\")=\"%s\" , _str(NULL)=\"%s\"", _str("TEST"), _str(NULL));
n_log(LOG_INFO, "_strw(\"TEST\")=\"%s\" , _strw(NULL)=\"%s\"", _str("TEST"), _str(NULL));
N_STR* nstr = char_to_nstr("TEST");
n_log(LOG_INFO, "_nstr(nstrpointer)=\"%s\"", _nstr(nstr));
n_log(LOG_INFO, "_nstrp(nstrpointer)=\"%s\"", _nstrp(nstr));
free_nstr(&nstr);
n_log(LOG_INFO, "_nstr(nstrpointer_freed)=\"%s\"", _nstr(nstr));
n_log(LOG_INFO, "_nstrp(nstrpointer_freed)=\"%s\"", _nstrp(nstr));
char* data = NULL;
__n_assert(data, n_log(LOG_INFO, "data is NULL"));
Malloc(data, char, 1024);
__n_assert(data, n_log(LOG_INFO, "data is NULL"));
Free(data);
Malloc(data, char, 1024);
Realloc(data, char, 2048);
Free(data);
Malloc(data, char, 1024);
Reallocz(data, char, 1024, 2048);
FreeNoLog(data);
Alloca(data, 2048);
// Free( data ); alloca should not be free else it's double freeing on exit
n_log(LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd(10), next_odd(11));
n_log(LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even(10), next_even(11));
ifnull(data) endif;
ifnull(NULL) endif;
iffalse(FALSE) endif;
iftrue(TRUE) endif;
error:
if (get_error()) {
n_log(LOG_INFO, "got an error while processing test");
} else {
n_log(LOG_INFO, "All tests for checkerror() are OK");
}
if (file_exist(argv[0])) {
n_log(LOG_INFO, "%s exists !", argv[0]);
}
char *dir = NULL, *name = NULL;
dir = get_prog_dir();
name = get_prog_name();
n_log(LOG_INFO, "From %s/%s", dir, name);
Free(dir);
Free(name);
N_STR* out = NULL;
int ret = -1;
if (n_popen("ls -ltr", 2048, (void*)&out, &ret) == TRUE) {
n_log(LOG_INFO, "ls returned %d : %s", ret, _nstr(out));
} else {
n_log(LOG_ERR, "popen s returned an error");
}
free_nstr(&out);
char hidden_str[47] = "";
N_HIDE_STR(hidden_str, 'T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'w', 'i', 'l', 'l', ' ', 'b', 'e', ' ', 'h', 'i', 'd', 'd', 'e', 'n', ' ', 'i', 'n', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'n', 'a', 'l', ' ', 'b', 'i', 'n', 'a', 'r', 'y', '\0');
printf("hidden str:%s\n", hidden_str);
/* test n_scan_dir and n_free_file_info from n_files.h */
LIST* file_list = new_generic_list(0);
if (n_scan_dir(".", file_list, 0) == TRUE) {
n_log(LOG_INFO, "n_scan_dir found %zu files in current directory", file_list->nb_items);
list_foreach(fnode, file_list) {
N_FILE_INFO* fi = (N_FILE_INFO*)fnode->ptr;
if (fi && fi->name) {
n_log(LOG_DEBUG, " file: %s (time: %ld)", fi->name, (long)fi->filetime);
}
}
} else {
n_log(LOG_ERR, "n_scan_dir failed");
}
list_destroy(&file_list);
#ifndef __windows__
n_log(LOG_INFO, "before system_nb( sleep 3 )");
int pid = system_nb("sleep 3", NULL, NULL);
n_log(LOG_INFO, "after system_nb( sleep 3 )");
n_log(LOG_INFO, "wait for nb sys call");
wait(&pid);
n_log(LOG_INFO, "done");
#endif
n_abort("Testing abort before exit");
n_log(LOG_INFO, "abort done");
exit(0);
}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
void inlined_func(void)
Definition ex_common.c:98
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:271
char * get_prog_name(void)
get current program name
Definition n_common.c:144
#define get_error()
pop up errors if any
Definition n_common.h:330
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
Definition n_common.c:458
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define _str(__PTR)
define true
Definition n_common.h:192
#define ifzero
error checker type if( 0 != toto )
Definition n_common.h:308
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
Definition n_common.c:389
#define iffalse
error checker type if( toto == FALSE )
Definition n_common.h:311
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
Definition n_common.h:246
#define next_odd(__val)
next odd helper
Definition n_common.h:295
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:215
#define endif
close a ifwhatever block
Definition n_common.h:324
#define ifnull
error checker type if( !toto )
Definition n_common.h:305
#define _nstrp(__PTR)
N_STR or NULL pointer for testing purposes.
Definition n_common.h:200
#define next_even(__val)
next even helper
Definition n_common.h:298
char * get_prog_dir(void)
get current program running directory
Definition n_common.c:112
#define FORCE_INLINE
FORCE_INLINE portable macro.
Definition n_common.h:163
#define iftrue
error checker type if( toto == FALSE )
Definition n_common.h:314
#define checkerror()
check for errors
Definition n_common.h:317
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:52
int n_daemonize(void)
Daemonize program.
Definition n_common.c:283
#define init_error_check()
init error checking in a function
Definition n_common.h:301
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition n_common.h:230
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:99
#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
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command and return output and status
Definition n_common.c:179
size_t nb_items
number of item currently in the list
Definition n_list.h:60
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:88
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
#define LOG_ERR
error conditions
Definition n_log.h:75
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_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
int n_scan_dir(const char *dir, LIST *result, const int recurse)
Scan a directory and append only regular files into a sorted list (oldest first).
Definition n_files.c:180
common file information
Definition n_files.h:45
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
A box including a string and his lenght.
Definition n_str.h:60
Files configuration header.
Generic log system.
N_STR and string function declaration.