ae2f_docs
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
submod
ae2f
Preproc
Inc
main.c
Go to the documentation of this file.
1
#
include
"./main.auto.h"
2
3
#
if
defined
(
NDEBUG
)
&&
NDEBUG
4
#
define
DEBUG
0
5
#
else
6
#
define
DEBUG
1
7
#
endif
8
9
#
include
<
stdio
.
h
>
10
#
include
<
string
.
h
>
11
#
include
<
assert
.
h
>
12
13
enum
STATE_t
{
14
STATE_GOOD
,
15
STATE_EOF
,
16
STATE_OPENING_UNKNOWN
,
17
STATE_STACK_SMASHED
,
18
STATE_ARGC_UNEXPECTED
,
19
STATE_FOPEN_BAD
,
20
STATE_UNEXPECTED
21
};
22
23
struct
STACK_T
{
24
FILE*
m_inp
;
25
char
m_dir
[
DIRLEN
+ 1];
26
char
m_current
[
DIRLEN
+
PATHLEN
+ 1];
27
} STACK[
STACKLEN
+ 1];
28
size_t
STACK_IDX
= 0;
29
size_t
STACK_IDX_BUFF
;
30
31
#
define
STACK_LAST
STACK
[
STACK_IDX
]
32
#
define
STACK_NXT
STACK
[
STACK_IDX
+
1
]
33
34
int
Ch
;
35
int
i
;
36
37
char
inc_key
[
sizeof
(
"#include"
)] = {0, };
38
char
inc_path_buff
[
PATHLEN
+ 1] = {0, };
39
40
typedef
unsigned
char
flag_t
;
41
struct
FLAGS_t
{
42
/** is a key of include */
43
flag_t
m_Key
: 1;
44
flag_t
m_path_nclosed
: 1;
45
flag_t
m_ManyCmtOutMaybe
: 1;
46
flag_t
m_Key_Idx
: 4;
47
} FLAGS;
48
49
char
path_closing
;
50
char
*
path_cursor
;
51
52
int
main
(
int
argc,
char
** argv) {
53
assert(argc > 0);
54
if
(argc < 1) {
55
return
-
STATE_ARGC_UNEXPECTED
;
56
}
57
for
(
i
= argc;
i
-- != 1;) {
58
if
(strlen(argv[
i
]) >
DIRLEN
)
59
return
-1;
60
}
61
62
STACK_LAST
.
m_inp
= stdin;
63
STACK_LAST
.
m_dir
[0] = 0;
64
65
KEYGET:
66
67
Ch
= 0;
68
69
memset(
inc_key
, 0,
sizeof
(
inc_key
));
70
inc_key
[0] =
'#'
;
71
72
assert(
STACK_LAST
.
m_inp
);
73
74
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF) {
75
switch
(
Ch
) {
76
case
'\''
:
77
{
78
fputc(
'\''
, stdout);
79
80
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF) {
81
fputc(
Ch
, stdout);
82
if
(
Ch
==
'\''
) {
83
goto
KEYGET;
84
}
85
}
86
87
}
return
-
STATE_EOF
;
88
89
#
if
1
90
case
'"'
:
91
{
92
fputc(
'"'
, stdout);
93
94
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF) {
95
fputc(
Ch
, stdout);
96
if
(
Ch
==
'"'
) {
97
goto
KEYGET;
98
}
99
}
100
101
}
return
-
STATE_EOF
;
102
#
endif
103
104
case
'#'
:
105
{
106
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF
107
&& (
Ch
==
' '
108
||
Ch
==
'\t'
)
109
);
110
if
(
Ch
== EOF)
return
-
STATE_EOF
;
111
inc_key
[1] =
Ch
;
112
113
FLAGS
.
m_Key_Idx
= 2;
114
115
if
(
Ch
==
'i'
)
116
for
(
117
;
118
FLAGS
.
m_Key_Idx
<
sizeof
(
"#include"
) - 1
119
&& (
inc_key
[FLAGS
.
m_Key_Idx
] = fgetc(
STACK_LAST
.
m_inp
))
120
==
"#include"
[FLAGS
.
m_Key_Idx
];
121
FLAGS
.
m_Key_Idx
++
122
);
123
124
if
(FLAGS
.
m_Key_Idx
==
sizeof
(
"#include"
) - 1) {
125
goto
STACK_SUB;
126
}
else
{
127
fputs(
inc_key
, stdout);
128
}
129
}
130
goto
KEYGET;
131
132
case
'/'
:
133
switch
(
Ch
= fgetc(
STACK_LAST
.
m_inp
)) {
134
case
'/'
:
135
{
136
#
if
DEBUG
137
fputs(
"//"
, stdout);
138
#
endif
139
140
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF &&
Ch
!=
'\n'
&&
Ch
!=
'\r'
) {
141
#
if
DEBUG
142
fputc(
Ch
, stdout);
143
#
endif
144
}
145
146
if
(
Ch
== EOF)
return
-
STATE_EOF
;
147
puts(
""
);
148
goto
KEYGET;
149
}
150
default
:
151
fputc(
'/'
, stdout);
152
fputc(
Ch
, stdout);
153
goto
KEYGET;
154
#
if
1
155
case
'*'
:
156
#
if
DEBUG
157
fputs(
"/*"
, stdout);
158
#
endif
159
160
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF) {
161
if
(
Ch
==
'*'
) {
162
if
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) ==
'/'
) {
163
#
if
DEBUG
164
fputs(
"*/"
, stdout);
165
#
endif
166
goto
KEYGET;
167
}
168
#
if
DEBUG
169
fputc(
'*'
, stdout);
170
fputc(
Ch
, stdout);
171
#
endif
172
}
173
#
if
DEBUG
174
fputc(
Ch
, stdout);
175
#
endif
176
}
177
178
goto
KEYGET;
179
#
endif
180
}
return
-
STATE_UNEXPECTED
;
181
182
default
:
183
{
184
fputc(
Ch
, stdout);
185
goto
KEYGET;
186
}
187
}
188
}
189
190
goto
STACK_ADD;
191
STACK_SUB:
192
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF && (
Ch
==
' '
||
Ch
==
'\t'
));
193
switch
(
Ch
) {
194
case
EOF:
195
#
if
DEBUG
196
puts(
"/** Unknown EOF bug has appeared. */ \\"
);
197
#
endif
198
return
-
STATE_EOF
;
199
case
'\"'
:
path_closing
=
'\"'
;
break
;
200
case
'<'
:
path_closing
=
'>'
;
break
;
201
default
:
202
fputc(
Ch
, stdout);
203
goto
KEYGET;
204
}
205
206
#
if
DEBUG
207
puts(
"/** include detected */ \\"
);
208
#
endif
209
210
path_cursor
=
inc_path_buff
;
211
FLAGS
.
m_path_nclosed
= 1;
212
#
if
DEBUG
213
puts(
"/** Path cursor */ \\"
);
214
#
endif
215
if
(
STACK_IDX
==
STACKLEN
) {
216
#
if
DEBUG
217
puts(
"/** Stack has smashed. shame. */ \\"
);
218
fputs(
"/*"
, stdout);
219
220
while
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) != EOF &&
Ch
!=
'\n'
&&
Ch
!=
'\r'
) {
221
#
if
DEBUG
222
fputc(
Ch
, stdout);
223
#
endif
224
}
225
226
puts(
"*/ \\"
);
227
#
endif
228
229
#
if
INC_IGNORE_SMASH
230
231
232
goto
KEYGET;
233
#
endif
234
return
-
STATE_STACK_SMASHED
;
235
}
236
237
while
(
path_cursor
-
inc_path_buff
<
PATHLEN
) {
238
if
((
Ch
= fgetc(
STACK_LAST
.
m_inp
)) == EOF)
239
return
-
STATE_EOF
;
240
if
(
Ch
==
path_closing
) {
241
*
path_cursor
= 0;
242
FLAGS
.
m_path_nclosed
= 0;
243
break
;
244
}
245
*
path_cursor
=
Ch
;
246
path_cursor
++;
247
}
248
if
(FLAGS
.
m_path_nclosed
)
return
-
STATE_OPENING_UNKNOWN
;
249
250
strcpy(
STACK_NXT
.
m_current
,
STACK_LAST
.
m_dir
);
251
strcat(
STACK_NXT
.
m_current
,
inc_path_buff
);
252
253
#
if
DEBUG
254
fputs(
"/* Opening: "
, stdout);
255
fputs(
STACK_NXT
.
m_current
, stdout);
256
fputs(
" */ \\\n"
, stdout);
257
#
endif
258
STACK_NXT
.
m_inp
= fopen(
STACK_NXT
.
m_current
,
"r"
);
259
260
#
if
DEBUG
261
printf(
"/** first fp: %p */ \\\n"
,
STACK_NXT
.
m_inp
);
262
#
endif
263
264
265
for
(
i
= argc; !
STACK_NXT
.
m_inp
&&
i
-- != 1;) {
266
strcpy(
STACK_NXT
.
m_current
, argv[
i
]);
267
strcat(
STACK_NXT
.
m_current
,
inc_path_buff
);
268
269
#
if
DEBUG
270
fputs(
"/* Opening: "
, stdout);
271
fputs(
STACK_NXT
.
m_current
, stdout);
272
fputs(
" */ \\\n"
, stdout);
273
#
endif
274
STACK_NXT
.
m_inp
= fopen(
STACK_NXT
.
m_current
,
"r"
);
275
}
276
if
(!
STACK_NXT
.
m_inp
)
277
#
if
INC_IGNORE_NFOUND
278
{
279
#
if
DEBUG
280
puts(
"/** Failed but ignoring it. */ \\"
);
281
#
endif
282
#
if
INC_LEAVE_NFOUND
283
fputs(
"#include <"
, stdout);
284
fputs(
inc_path_buff
, stdout);
285
puts(
">"
);
286
#
endif
287
288
goto
KEYGET;
289
}
290
#
else
291
{
292
#
if
DEBUG
293
puts(
"/** Failed. */ \\"
);
294
#
endif
295
return
-STATE_FOPEN_BAD;
296
}
297
#
endif
298
299
for
(
i
= strlen(
STACK_NXT
.
m_current
);
i
--;) {
300
if
(
STACK_NXT
.
m_current
[
i
] ==
'/'
) {
301
Ch
=
STACK_NXT
.
m_current
[
i
+ 1];
302
STACK_NXT
.
m_current
[
i
+ 1] = 0;
303
strcpy(
STACK_NXT
.
m_dir
,
STACK_NXT
.
m_current
);
304
STACK_NXT
.
m_current
[
i
+ 1] =
Ch
;
305
306
#
if
DEBUG
307
printf(
"/** Current %s */ \\\n"
,
STACK_NXT
.
m_current
);
308
printf(
"/** Dir %s */ \\\n"
,
STACK_NXT
.
m_dir
);
309
#
endif
310
break
;
311
}
312
}
313
314
315
#
if
INC_REPT_CHECK
316
#
if
DEBUG
317
puts(
"/** Found something. Now let's validate. */ \\"
);
318
#
endif
319
320
for
(
STACK_IDX_BUFF
=
STACK_IDX
;
STACK_IDX_BUFF
--;) {
321
if
(!strcmp(STACK[
STACK_IDX_BUFF
]
.
m_current
,
STACK_NXT
.
m_current
)) {
322
#
if
DEBUG
323
puts(
"/** It's repeating himself for some reason. */ \\"
);
324
goto
KEYGET;
325
#
endif
326
}
327
}
328
329
#
if
DEBUG
330
puts(
"/** It's not repeating */ \\"
);
331
#
endif
332
#
endif
333
334
335
#
if
DEBUG
336
fputs(
337
"/******************** */\\\n"
338
"/* Now the path will be: */ \\\n /*"
339
, stdout
340
);
341
342
fputs(
STACK_NXT
.
m_current
, stdout);
343
344
puts(
" */ \\"
);
345
#
endif
346
++
STACK_IDX
;
347
goto
KEYGET;
348
349
STACK_ADD:
350
if
(
STACK_IDX
) {
351
fclose(
STACK_LAST
.
m_inp
);
352
--
STACK_IDX
;
353
354
#
if
DEBUG
355
puts(
"/** One eof gracefully */ \\"
);
356
#
endif
357
358
goto
KEYGET;
359
}
360
361
#
if
DEBUG
362
puts(
"/** All done. */"
);
363
#
endif
364
365
return
-
STATE_GOOD
;
366
}
INC_IGNORE_NFOUND
#define INC_IGNORE_NFOUND
Definition
main.auto.h:6
PATHLEN
#define PATHLEN
Definition
main.auto.h:4
INC_REPT_CHECK
#define INC_REPT_CHECK
Definition
main.auto.h:8
DIRLEN
#define DIRLEN
Definition
main.auto.h:3
INC_IGNORE_SMASH
#define INC_IGNORE_SMASH
Definition
main.auto.h:7
INC_LEAVE_NFOUND
#define INC_LEAVE_NFOUND
Definition
main.auto.h:9
STACKLEN
#define STACKLEN
Definition
main.auto.h:5
i
size_t i
Definition
MlpTrainXOR-Made-Primal.c:63
main
int main()
Definition
MlpTrainXOR-Made-Primal.c:72
Ch
int Ch
Definition
main.c:6
inc_key
char inc_key[sizeof("#include")]
Definition
main.c:37
flag_t
unsigned char flag_t
Definition
main.c:40
STACK_IDX_BUFF
size_t STACK_IDX_BUFF
Definition
main.c:29
STATE_t
STATE_t
Definition
main.c:13
STATE_UNEXPECTED
@ STATE_UNEXPECTED
Definition
main.c:20
STATE_ARGC_UNEXPECTED
@ STATE_ARGC_UNEXPECTED
Definition
main.c:18
STATE_OPENING_UNKNOWN
@ STATE_OPENING_UNKNOWN
Definition
main.c:16
STATE_GOOD
@ STATE_GOOD
Definition
main.c:14
STATE_EOF
@ STATE_EOF
Definition
main.c:15
STATE_STACK_SMASHED
@ STATE_STACK_SMASHED
Definition
main.c:17
STATE_FOPEN_BAD
@ STATE_FOPEN_BAD
Definition
main.c:19
STACK_IDX
size_t STACK_IDX
Definition
main.c:28
path_closing
char path_closing
Definition
main.c:49
inc_path_buff
char inc_path_buff[PATHLEN+1]
Definition
main.c:38
path_cursor
char * path_cursor
Definition
main.c:50
DEBUG
#define DEBUG
Definition
main.c:6
STACK_NXT
#define STACK_NXT
Definition
main.c:32
STACK_LAST
#define STACK_LAST
Definition
main.c:31
FLAGS_t
Definition
main.c:41
FLAGS_t::m_path_nclosed
flag_t m_path_nclosed
Definition
main.c:44
FLAGS_t::m_Key_Idx
flag_t m_Key_Idx
Definition
main.c:46
FLAGS_t::m_ManyCmtOutMaybe
flag_t m_ManyCmtOutMaybe
Definition
main.c:45
FLAGS_t::m_Key
flag_t m_Key
Definition
main.c:43
STACK_T
Definition
main.c:23
STACK_T::m_dir
char m_dir[DIRLEN+1]
Definition
main.c:25
STACK_T::m_current
char m_current[DIRLEN+PATHLEN+1]
Definition
main.c:26
STACK_T::m_inp
FILE * m_inp
Definition
main.c:24
Generated by
1.14.0