-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-extender.c
100 lines (75 loc) · 2.84 KB
/
c-extender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef __HELPER
#define __HELPER
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define return_value_if(value, condition) \
if (condition) \
return value;
#define return_if(condition) \
if (condition) \
return;
#define tif(condition) return_value_if(true, condition)
#define fif(condition) return_value_if(false, condition)
#define nif(condition) return_value_if(NULL, condition)
#define new(class, name, ...) \
s_##class *name = (s_##class *)malloc(sizeof(s_##class)); \
s_##class##_new(name, __VA_ARGS__); \
trash_can_register(name)
#define new_static(class, name, ...) \
s_##class name; \
s_##class##_new(&name, __VA_ARGS__);
#define new_static_np(class, name) \
s_##class name; \
s_##class##_new(&name);
#define class(name, property, parameter, constructor) \
typedef struct s_##name { \
property \
} s_##name; \
void s_##name##_new parameter constructor
#define loop while (true)
void trash_can_register(void *data);
class(list, struct s_list_item *start; struct s_list_item * end; int count;
, (s_list * list), // constructor
{ list->count = 0; });
class(list_item,
struct s_list_item *prev;
struct s_list_item * next; void *value;
, (s_list_item * list, s_list_item *prev), {
return_if(prev == NULL);
list->prev = prev;
prev->next = list;
});
void s_list_push(s_list *list, void *new) {
new (list_item, new_item, list->end);
if (list->start == NULL) {
list->start = new_item;
}
list->end = new_item;
list->count++;
}
s_list trash_can = {NULL, NULL, 0};
void trash_can_empty() {
return_if(trash_can.start == NULL);
s_list_item *item_start = trash_can.start;
s_list_item *item_temp;
while (true) {
free(item_start->value);
if (item_start->next == NULL) {
free(item_start);
break;
}
item_temp = item_start->next;
free(item_start);
item_start = item_temp;
}
}
void trash_can_register(void *data) { s_list_push(&trash_can, data); }
int main(int argc, char **argv) {
#include "main.c"
trash_can_empty();
signal(SIGINT, trash_can_empty);
return 0;
}
#endif