forked from mailru/confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h_dump.c
207 lines (181 loc) · 5.12 KB
/
h_dump.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <prscfl.h>
void
dumpStructName(FILE *fh, ParamDef *def, char *delim) {
if (def && def->parent) {
ParamDef *p = def->parent;
if (p->name == NULL)
p = p->parent;
if (p != NULL) {
dumpStructName(fh, p, delim);
fputs(delim, fh);
fputs(p->name, fh);
}
}
}
static void
dumpComment(FILE *fh, ParamDef *def, int istab) {
if (def->comment) {
ParamDef *i = def->comment;
if (i->next) {
/* multiline comment */
fprintf(fh, "\n%s/*\n", istab ? "\t" : "");
while(i) {
fprintf(fh, "%s * %s\n", istab ? "\t" : "", i->paramValue.commentval);
i = i->next;
}
fprintf(fh, "%s */\n", istab ? "\t" : "");
} else {
/* single line comment */
fprintf(fh, "\n%s/* %s */\n", istab ? "\t" : "", i->paramValue.commentval);
}
}
}
static void
dumpParamDef(FILE *fh, char* name, ParamDef *def) {
dumpComment(fh, def, 1);
switch(def->paramType) {
case int32Type:
fprintf(fh, "\tint32_t\t%s;\n", def->name);
break;
case uint32Type:
fprintf(fh, "\tu_int32_t\t%s;\n", def->name);
break;
case int64Type:
fprintf(fh, "\tint64_t\t%s;\n", def->name);
break;
case uint64Type:
fprintf(fh, "\tu_int64_t\t%s;\n", def->name);
break;
case doubleType:
fprintf(fh, "\tdouble\t%s;\n", def->name);
break;
case stringType:
fprintf(fh, "\tchar*\t%s;\n", def->name);
break;
case boolType:
fprintf(fh, "\tconfetti_bool_t\t%s;\n", def->name);
break;
case commentType:
fprintf(stderr, "Unexpected comment");
break;
case structType:
fprintf(fh, "\t%s", name);
dumpStructName(fh, def->paramValue.structval, "_");
fprintf(fh, "*\t%s;\n", def->name);
break;
case arrayType:
fprintf(fh, "\t%s", name);
dumpStructName(fh, def->paramValue.arrayval->paramValue.structval, "_");
fprintf(fh, "**\t%s;\n", def->name);
break;
case builtinType:
break;
default:
fprintf(stderr,"Unknown paramType (%d)\n", def->paramType);
exit(1);
}
}
static void
dumpParamList(FILE *fh, char* name, ParamDef *def) {
while(def) {
dumpParamDef(fh, name, def);
def = def->next;
}
}
static void
dumpStruct(FILE *fh, char* name, ParamDef *def) {
ParamDef *list = NULL;
switch(def->paramType) {
case structType:
list = def->paramValue.structval;
break;
case arrayType:
list = def->paramValue.arrayval->paramValue.structval;
break;
default:
fprintf(stderr,"Non-struct paramType (%d)\n", def->paramType);
exit(1);
break;
}
fprintf(fh, "typedef struct %s", name);
dumpStructName(fh, list, "_");
fputs(" {\n", fh);
fputs("\tunsigned char __confetti_flags;\n\n", fh);
dumpParamList(fh, name, list);
fprintf(fh, "} %s", name);
dumpStructName(fh, list, "_");
fputs(";\n\n", fh);
}
static void
dumpRecursive(FILE *fh, char* name, ParamDef *def) {
while(def) {
switch(def->paramType) {
case structType:
dumpRecursive(fh, name, def->paramValue.structval);
dumpStruct(fh, name, def);
break;
case arrayType:
dumpComment(fh, def->paramValue.arrayval, 0);
dumpRecursive(fh, name, def->paramValue.arrayval->paramValue.structval);
dumpStruct(fh, name, def);
break;
default:
break;
}
def = def->next;
}
}
void
hDump(FILE *fh, char* name, ParamDef *def) {
ParamDef root;
root.paramType = structType;
root.paramValue.structval = def;
root.name = NULL;
root.parent = NULL;
root.next = NULL;
def->parent = &root;
fprintf(fh, "#ifndef %s_CFG_H\n", name);
fprintf(fh, "#define %s_CFG_H\n\n", name);
fputs(
"#include <stdio.h>\n"
"#include <stdbool.h>\n"
"#include <sys/types.h>\n\n"
"#ifndef confetti_bool_t\n"
"#define confetti_bool_t char\n"
"#endif\n\n"
"/*\n"
" * Autogenerated file, do not edit it!\n"
" */\n\n",
fh
);
dumpRecursive(fh, name, &root);
fprintf(fh,
"#ifndef CNF_FLAG_STRUCT_NEW\n"
"#define CNF_FLAG_STRUCT_NEW\t0x01\n"
"#endif\n"
"#ifndef CNF_FLAG_STRUCT_NOTSET\n"
"#define CNF_FLAG_STRUCT_NOTSET\t0x02\n"
"#endif\n"
"#ifndef CNF_STRUCT_DEFINED\n"
"#define CNF_STRUCT_DEFINED(s) ((s) != NULL && ((s)->__confetti_flags & CNF_FLAG_STRUCT_NOTSET) == 0)\n"
"#endif\n\n"
);
fprintf(fh, "void init_%s(%s *c);\n\n", name, name);
fprintf(fh, "int fill_default_%s(%s *c);\n\n", name, name);
fprintf(fh, "void swap_%s(struct %s *c1, struct %s *c2);\n\n",
name, name, name);
fprintf(fh, "int parse_cfg_file_%s(%s *c, FILE *fh, int check_rdonly, int *n_accepted, int *n_skipped, int *n_optional);\n\n", name, name);
fprintf(fh, "int parse_cfg_buffer_%s(%s *c, char *buffer, int check_rdonly, int *n_accepted, int *n_skipped, int *n_optional);\n\n", name, name);
fprintf(fh, "int check_cfg_%s(%s *c);\n\n", name, name);
fprintf(fh, "int dup_%s(%s *dst, %s *src);\n\n", name, name, name);
fprintf(fh, "void destroy_%s(%s *c);\n\n", name, name);
fprintf(fh, "char *cmp_%s(%s* c1, %s* c2, int only_check_rdonly);\n\n", name, name, name);
fprintf(fh, "typedef struct %s_iterator_t %s_iterator_t;\n", name, name);
fprintf(fh, "%s_iterator_t* %s_iterator_init();\n", name, name);
fprintf(fh, "char* %s_iterator_next(%s_iterator_t* i, %s *c, char **v);\n\n", name, name, name);
fputs("#endif\n", fh);
def->parent = NULL;
}