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