Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_stack.c
1
7#include <stdio.h>
8#include <errno.h>
9#include <string.h>
10#include <sys/types.h>
11
12#include "nilorea/n_common.h"
13#include "nilorea/n_log.h"
14#include "nilorea/n_str.h"
15#include "nilorea/n_stack.h"
16
17void usage(void) {
18 fprintf(stderr,
19 " -v version\n"
20 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
21 " -h help\n");
22}
23
24void process_args(int argc, char** argv) {
25 int getoptret = 0,
26 log_level = LOG_DEBUG; /* default log level */
27
28 /* Arguments optionnels */
29 /* -v version
30 * -V log level
31 * -h help
32 */
33 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
34 switch (getoptret) {
35 case 'v':
36 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
37 exit(1);
38 case 'V':
39 if (!strncmp("LOG_NULL", optarg, 5)) {
40 log_level = LOG_NULL;
41 } else {
42 if (!strncmp("LOG_NOTICE", optarg, 6)) {
43 log_level = LOG_NOTICE;
44 } else {
45 if (!strncmp("LOG_INFO", optarg, 7)) {
46 log_level = LOG_INFO;
47 } else {
48 if (!strncmp("LOG_ERR", optarg, 5)) {
49 log_level = LOG_ERR;
50 } else {
51 if (!strncmp("LOG_DEBUG", optarg, 5)) {
52 log_level = LOG_DEBUG;
53 } else {
54 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
55 exit(-1);
56 }
57 }
58 }
59 }
60 }
61 break;
62 default:
63 case '?': {
64 if (optopt == 'V') {
65 fprintf(stderr, "\n Missing log level\n");
66 } else if (optopt == 'p') {
67 fprintf(stderr, "\n Missing port\n");
68 } else if (optopt != 's') {
69 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
70 }
71 usage();
72 exit(1);
73 }
74 case 'h': {
75 usage();
76 exit(1);
77 }
78 }
79 }
80 set_log_level(log_level);
81} /* void process_args( ... ) */
82
83int main(int argc, char** argv) {
85
86 /* processing args and set log_level */
87 process_args(argc, argv);
88
89 STACK* stack = new_stack(16);
90 n_log(LOG_INFO, "created stack of 16 elements at %p", stack);
91
92 for (int it = 0; it < 20; it++) {
93 int32_t nb = rand() % 10;
94 bool btest = rand() % 1;
95
96 stack_push(stack, nb);
97 stack_push(stack, btest);
98 }
99
100 for (int it = 0; it < 20; it++) {
101 STACK_ITEM* item = NULL;
102 item = stack_peek(stack, stack->tail);
103 if (item) {
104 uint8_t status = STACK_IS_UNDEFINED;
105 switch (item->v_type) {
106 case STACK_ITEM_BOOL: {
107 bool bval = stack_pop_b(stack, &status);
108 n_log(LOG_INFO, "got bool: %d", bval);
109 status = STACK_ITEM_OK;
110 } break;
111 case STACK_ITEM_INT32: {
112 int32_t val = stack_pop_i32(stack, &status);
113 n_log(LOG_INFO, "got int32_t: %d", val);
114 status = STACK_ITEM_OK;
115 } break;
116 default:
117 n_log(LOG_ERR, "uknown type %d", item->v_type);
118 break;
119 }
120 if (status != STACK_ITEM_OK) {
121 n_log(LOG_ERR, "error popping value ! status: %d", status);
122 }
123 }
124 }
125
126 delete_stack(&stack);
127
128 exit(0);
129}
#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
#define LOG_ERR
error conditions
Definition n_log.h:56
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_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_NULL
no log output
Definition n_log.h:26
#define LOG_INFO
informational
Definition n_log.h:62
size_t tail
position of tail
Definition n_stack.h:107
uint8_t v_type
type of the item
Definition n_stack.h:93
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:176
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack with un stacking the stack item
Definition n_stack.c:65
#define STACK_ITEM_INT32
v_type value for a int32_t
Definition n_stack.h:33
#define STACK_ITEM_OK
code for a successfully retrieved item
Definition n_stack.h:54
#define STACK_ITEM_BOOL
v_type value for a bool
Definition n_stack.h:23
#define STACK_IS_UNDEFINED
code for a NULL stack state
Definition n_stack.h:50
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:396
#define stack_push(__STACK, __VAL)
stack push helper with automatic value selection
Definition n_stack.h:129
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:34
STACK * new_stack(size_t size)
allocate a new STACK
Definition n_stack.c:15
STACK structure.
Definition n_stack.h:99
structure of a STACK item
Definition n_stack.h:85
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.