-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparsefil.cpp.html
More file actions
executable file
·467 lines (368 loc) · 20.5 KB
/
parsefil.cpp.html
File metadata and controls
executable file
·467 lines (368 loc) · 20.5 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<html>
<head>
<title>PARSEFIL.CPP.html</title>
<style type="text/css"> PRE { font-family: Verdana; font-size: 8pt } </style>
</head>
<body><pre>
<font color="green">// CodeToHTML version </font>
<font color="blue">#include</font> <iostream>
<font color="blue">#include</font> <string>
<font color="blue">#include</font> <cstring>
<font color="blue">#include</font> <algorithm>
<font color="blue">#include</font> <cstdlib>
<font color="blue">#include</font> <fstream>
<font color="blue">#ifdef</font> _WIN32
<font color="blue">#include</font> <windows.h>
<font color="blue">#endif</font>
<font color="blue">#include</font> <stdio.h>
<font color="blue">#include</font> <errno.h>
<font color="green">// Added wildcard support for Win32</font>
<font color="green">// Added proper file extension support</font>
<font color="green">// Added <pre> tag and formatting directive</font>
<font color="green">// Added C++ pointer/reference/address-of semantics</font>
<font color="green">// Added string literal colorization </font>
<font color="green">// Fixed tab character problem</font>
<font color="green">// Fixed escaped characters in string literal bug</font>
<font color="green">// Fixed <> chars in comments not being handled properly</font>
<font color="blue">using</font> namespace std;
<font color="blue">const</font> <font color="blue">int</font> MAX_KEYWORD = 64;
<font color="blue">const</font> <font color="blue">int</font> KEYWORDS = 42;
<font color="blue">const</font> <font color="blue">char</font>* keyWords[KEYWORDS] = { <font color="gray">"void"</font>, <font color="gray">"short"</font>, <font color="gray">"char"</font>, <font color="gray">"int"</font>, <font color="gray">"float"</font>, <font color="gray">"double"</font>, <font color="gray">"long"</font>,
<font color="gray">"while"</font>, <font color="gray">"enum"</font> , <font color="gray">"if"</font>, <font color="gray">"else"</font>, <font color="gray">"switch"</font>, <font color="gray">"case"</font>, <font color="gray">"break"</font>, <font color="gray">"default"</font>,<font color="gray">"union"</font>, <font color="gray">"struct"</font>,
<font color="gray">"class"</font> ,<font color="gray">"private"</font>, <font color="gray">"public"</font>, <font color="gray">"for"</font>, <font color="gray">"do"</font>, <font color="gray">"#define"</font>, <font color="gray">"#include"</font>, <font color="gray">"#import"</font>, <font color="gray">"const"</font>,
<font color="gray">"new"</font>, <font color="gray">"delete"</font>, <font color="gray">"this"</font>, <font color="gray">"friend"</font>, <font color="gray">"using"</font>, <font color="gray">"unsigned"</font>, <font color="gray">"signed"</font>, <font color="gray">"sizeof"</font>,
<font color="gray">"return"</font>, <font color="gray">"true"</font>, <font color="gray">"false"</font>, <font color="gray">"bool"</font>, <font color="gray">"#pragma"</font>, <font color="gray">"#ifndef"</font>, <font color="gray">"#endif"</font>, <font color="gray">"#ifdef"</font> };
<font color="blue">const</font> <font color="blue">int</font> MAX_DELIMS = 13;
<font color="blue">const</font> <font color="blue">char</font> delims[MAX_DELIMS] = { <font color="gray">'='</font>,<font color="gray">'<'</font>, <font color="gray">'>'</font>, <font color="gray">';'</font>, <font color="gray">'\n'</font>, <font color="gray">'\t'</font>,<font color="gray">'*'</font>, <font color="gray">'&'</font>, <font color="gray">':'</font>, <font color="gray">'('</font>, <font color="gray">')'</font>,<font color="gray">','</font>, <font color="gray">' '</font> };
<font color="blue">char</font> keyWord[MAX_KEYWORD] = <font color="gray">""</font>;
<font color="blue">int</font> keyIndex = 0;
<font color="blue">int</font> outputChar(<font color="blue">char</font> ch, ofstream& outFile) {
<font color="blue">if</font>(ch == <font color="gray">'>'</font>) {
outFile << <font color="gray">"&gt;"</font>;
<font color="blue">return</font> 0;
}
<font color="blue">else</font> <font color="blue">if</font> (ch == <font color="gray">'<'</font>) {
outFile << <font color="gray">"&lt;"</font>;
<font color="blue">return</font> 0;
}
<font color="blue">else</font> <font color="blue">if</font>(ch == <font color="gray">'\n'</font>) {
outFile << <font color="gray">"\n"</font>;
<font color="blue">return</font> 0;
}
<font color="blue">else</font> <font color="blue">if</font>(ch == <font color="gray">'&'</font>) {
outFile << <font color="gray">"&amp;"</font>;
<font color="blue">return</font> 0;
}
<font color="blue">else</font> {
outFile.put(ch);
<font color="blue">return</font> 0;
}
}
<font color="blue">bool</font> isDelim(<font color="blue">char</font> ch) {
<font color="blue">for</font>(<font color="blue">int</font> i = 0; i < MAX_DELIMS; ++i)
<font color="blue">if</font>(ch == delims[i])
<font color="blue">return</font> <font color="blue">true</font>;
<font color="blue">return</font> <font color="blue">false</font>;
}
<font color="blue">int</font> keyMatch(<font color="blue">char</font> ch, ofstream& outFile) {
<font color="green">// no existing keyword, and character cannot be part of a keyword,</font>
<font color="green">// (i.e. a delimiter), output it</font>
<font color="blue">if</font>(strcmp(keyWord,<font color="gray">""</font>) == 0) {
<font color="blue">if</font>(isDelim(ch)) {
outputChar(ch, outFile);
<font color="blue">return</font> 0;
}
}
<font color="green">// delimiter marks the end of a possible existing keyword </font>
<font color="blue">if</font>(isDelim(ch)) {
<font color="green">// Search for a match</font>
<font color="blue">for</font>(<font color="blue">int</font> i = 0; i < KEYWORDS; ++i) {
<font color="blue">if</font>((strcmp((<font color="blue">const</font> <font color="blue">char</font>*)keyWord, keyWords[i])) == 0) {
outFile << <font color="gray">"<font color=\"blue\">"</font>;
outFile << keyWord;
outFile << <font color="gray">"</font>"</font>;
outputChar(ch, outFile);
keyIndex = 0;
strncpy(keyWord,<font color="gray">"\0"</font>, 64);
<font color="blue">return</font> 1;
}
}
<font color="green">// No match, keyword could be a var name, etc. </font>
outFile << keyWord;
outFile.flush();
outputChar(ch, outFile);
strncpy(keyWord, <font color="gray">"\0"</font>, 64);
keyIndex = 0;
<font color="blue">return</font> 0;
}
<font color="blue">else</font>
{
<font color="green">// tack the character onto the keyword</font>
keyWord[keyIndex++] = ch;
}
}
<font color="green">// File states</font>
<font color="blue">enum</font> Context { string_literal, char_literal, cpp_comment, c_comment, file_end, newline, keyword };
<font color="green">// C-comment state</font>
<font color="blue">void</font> handle_c_comment(ifstream& inFile, ofstream& outFile) {
<font color="blue">char</font> ch;
outFile << <font color="gray">"<font color=\"green\">"</font>;
outFile << <font color="gray">"/*"</font>;
<font color="blue">while</font>(inFile.get(ch)) {
outFile << ch; <font color="green">// normal char</font>
<font color="blue">if</font>(ch == <font color="gray">'*'</font>) {
<font color="blue">while</font>(inFile.get(ch) && ch == <font color="gray">'*'</font>)
outFile << ch;
<font color="blue">if</font>(ch == <font color="gray">'/'</font>) {
outFile << ch;
outFile << <font color="gray">"</font>"</font>;
<font color="blue">break</font>;
}
}
}
}
<font color="green">// C++ comment state</font>
<font color="blue">void</font> handle_cpp_comment(ifstream& inFile, ofstream& outFile) {
<font color="blue">char</font> ch;
outFile << <font color="gray">"<font color=\"green\">"</font>;
outFile << <font color="gray">"//"</font>;
<font color="blue">while</font>(inFile.get(ch) && ch != <font color="gray">'\n'</font>)
outputChar(ch, outFile);
outFile << <font color="gray">"</font>\n"</font>;
}
<font color="green">// Literals (char, string)</font>
<font color="green">// Make sure that <,>, etc are escaped inside literals!</font>
<font color="blue">void</font> handle_literal(<font color="blue">char</font> delimiter, ifstream& inFile, ofstream& outFile) {
outFile << <font color="gray">"<font color=\"gray\">"</font>;
outFile.put(delimiter);
<font color="blue">char</font> ch;
<font color="blue">while</font>(inFile.get(ch)) {
<font color="blue">if</font>(ch == delimiter) {
outFile.put(ch);
<font color="blue">break</font>;
}
<font color="blue">else</font> <font color="blue">if</font>(ch == <font color="gray">'\\'</font>){
outFile.put(ch); <font color="green">// TEST</font>
inFile.get(ch) && outFile.put(ch);
}
<font color="blue">else</font>
outputChar(ch, outFile);
}
outFile << <font color="gray">"</font>"</font>;
}
Context handle_code(ifstream& inFile, ofstream& outFile) {
<font color="blue">char</font> ch;
<font color="blue">while</font>(inFile.get(ch)) {
<font color="blue">switch</font>(ch) {
<font color="blue">case</font> <font color="gray">'/'</font>:
<font color="blue">if</font>(!inFile.get(ch)) {
outFile.put(<font color="gray">'/'</font>);
<font color="blue">return</font> file_end;
}
<font color="blue">else</font>
{
<font color="blue">if</font>(ch == <font color="gray">'*'</font>)
<font color="blue">return</font> c_comment;
<font color="blue">else</font> <font color="blue">if</font>(ch == <font color="gray">'/'</font>)
<font color="blue">return</font> cpp_comment;
<font color="blue">else</font> {
outFile.put(<font color="gray">'/'</font>);
inFile.putback(ch);
<font color="blue">break</font>;
}
}
<font color="blue">case</font> <font color="gray">'\"'</font> : <font color="blue">return</font> string_literal;
<font color="blue">case</font> <font color="gray">'\''</font> : <font color="blue">return</font> char_literal;
<font color="blue">case</font> <font color="gray">'\n'</font> : <font color="blue">return</font> newline;
<font color="blue">default</font>:
{
keyMatch(ch, outFile);
}
}
}
<font color="blue">return</font> file_end;
}
<font color="blue">#ifdef</font> _WIN32
<font color="blue">int</font> error(<font color="blue">int</font> errCode, <font color="blue">char</font>* message, <font color="blue">char</font>* param)
{
cerr << <font color="gray">"ERROR:"</font> << errCode << <font color="gray">' '</font> << message << <font color="gray">' '</font> << param << endl;
<font color="blue">return</font> 0;
}
<font color="blue">void</font> expand_wild (<font color="blue">int</font> argc, <font color="blue">char</font>** argv, <font color="blue">int</font>* pargc, <font color="blue">char</font>*** pargv)
{
<font color="blue">int</font> i;
<font color="blue">int</font> new_argc;
<font color="blue">char</font> **new_argv;
<font color="green">/* Allocated size of new_argv. We arrange it so there is always room for
one more element. */</font>
<font color="blue">int</font> max_new_argc;
new_argc = 0;
<font color="green">/* Add one so this is never zero. */</font>
max_new_argc = argc + 1;
new_argv = (<font color="blue">char</font> **) malloc (max_new_argc * <font color="blue">sizeof</font> (<font color="blue">char</font> *));
<font color="blue">for</font> (i = 0; i < argc; ++i)
{
HANDLE h;
WIN32_FIND_DATA fdata;
<font color="green">/* These variables help us extract the directory name from the
given pathname. */</font>
<font color="blue">char</font> *last_forw_slash, *last_back_slash, *end_of_dirname;
<font color="blue">int</font> dirname_length = 0;
<font color="green">/* FIXME: If argv[i] is ".", this code will expand it to the
name of the current directory in its parent directory which
will cause start_recursion to do all manner of strange things
with it (culminating in an error). This breaks "cvs co .".
As nearly as I can guess, this bug has existed since
expand_wild was first created. At least, it is in CVS 1.9 (I
just tried it). */</font>
<font color="green">/* FindFirstFile doesn't return pathnames, so we have to do
this ourselves. Luckily, it's no big deal, since globbing
characters under Win32s can only occur in the last segment
of the path. For example,
/a/path/q*h valid
/w32/q*dir/cant/do/this/q*h invalid */</font>
<font color="green">/* Win32 can handle both forward and backward slashes as
filenames -- check for both. */</font>
last_forw_slash = strrchr (argv[i], <font color="gray">'/'</font>);
last_back_slash = strrchr (argv[i], <font color="gray">'\\'</font>);
<font color="blue">#define</font> cvs_max(x,y) ((x >= y) ? (x) : (y))
<font color="green">/* FIXME: this comparing a NULL pointer to a non-NULL one is
extremely ugly, and I strongly suspect *OT*sanctioned by
ANSI C. The code should just use last_component instead. */</font>
end_of_dirname = cvs_max (last_forw_slash, last_back_slash);
<font color="blue">if</font> (end_of_dirname == NULL)
dirname_length = 0; <font color="green">/* no directory name */</font>
<font color="blue">else</font>
dirname_length = end_of_dirname - argv[i] + 1; <font color="green">/* include slash */</font>
h = FindFirstFile (argv[i], &fdata);
<font color="blue">if</font> (h == INVALID_HANDLE_VALUE)
{
<font color="blue">if</font> (GetLastError() == ENOENT)
{
<font color="green">/* No match. The file specified didn't contain a wildcard (in which case
we clearly should return it unchanged), or it contained a wildcard which
didn't match (in which case it might be better for it to be an error,
but we don't try to do that). */</font>
new_argv [new_argc++] = strdup(argv[i]);
<font color="blue">#ifdef</font> _DEBUG
cout << <font color="gray">"new_argv["</font> << new_argc << <font color="gray">"]="</font> << argv[i] << endl;
<font color="blue">#endif</font>
<font color="blue">if</font> (new_argc == max_new_argc)
{
max_new_argc *= 2;
new_argv = (<font color="blue">char</font>**)realloc(new_argv, max_new_argc * <font color="blue">sizeof</font> (<font color="blue">char</font>*));
}
}
<font color="blue">else</font>
{
error (errno, <font color="gray">"cannot find %s"</font>, argv[i]);
}
}
<font color="blue">else</font>
{
<font color="blue">while</font> (1)
{
new_argv[new_argc] = (<font color="blue">char</font> *) malloc (strlen (fdata.cFileName) + 1 + dirname_length);
<font color="blue">#ifdef</font> _DEBUG
cout << <font color="gray">"new_argv["</font> << new_argc << <font color="gray">"]="</font> << argv[i] << endl;
<font color="blue">#endif</font>
<font color="green">/* Copy the directory name, if there is one. */</font>
<font color="blue">if</font> (dirname_length)
{
strncpy (new_argv[new_argc], argv[i], dirname_length);
new_argv[new_argc][dirname_length] = <font color="gray">'\0'</font>;
}
<font color="blue">else</font>
new_argv[new_argc][0] = <font color="gray">'\0'</font>;
<font color="green">/* Copy the file name. */</font>
<font color="blue">if</font> (strcmp(argv[i] + dirname_length, fdata.cFileName) == 0)
<font color="green">/* We didn't expand a wildcard; we just matched a filename.
Use the file name as specified rather than the filename
which exists in the directory (they may differ in case).
This is needed to make cvs add on a directory consistently
use the name specified on the command line, but it is
probably a good idea in other contexts too. */</font>
strcpy (new_argv[new_argc], argv[i]);
<font color="blue">else</font>
strcat (new_argv[new_argc], fdata.cFileName);
new_argc++;
<font color="blue">if</font> (new_argc == max_new_argc)
{
max_new_argc *= 2;
new_argv = (<font color="blue">char</font>**)realloc (new_argv, max_new_argc * <font color="blue">sizeof</font> (<font color="blue">char</font> *));
}
<font color="blue">if</font> (!FindNextFile (h, &fdata))
{
<font color="blue">if</font> (GetLastError () == ERROR_NO_MORE_FILES)
<font color="blue">break</font>;
<font color="blue">else</font>
error (errno, <font color="gray">"cannot find %s"</font>, argv[i]);
}
}
<font color="blue">if</font> (!FindClose (h))
error (GetLastError (), <font color="gray">"cannot close %s"</font>, argv[i]);
}
}
*pargc = new_argc;
*pargv = new_argv;
}
<font color="blue">#endif</font>
<font color="blue">void</font> printHeader(<font color="blue">char</font>* fileName, ofstream& outFile) {
outFile << <font color="gray">"<html>\n<head>\n<title>"</font> << fileName << <font color="gray">"</title>\n"</font>;
outFile << <font color="gray">"<style type=\"text/css\"> PRE { font-family: Verdana; font-size: 8pt } </style>\n"</font>;
outFile << <font color="gray">"\n</head>\n<body><pre>\n"</font>;
}
<font color="blue">void</font> printFooter(ofstream& outFile) {
outFile << <font color="gray">"</pre></body>\n</html>"</font>;
}
<font color="green">// Main entry point</font>
<font color="blue">int</font> main(<font color="blue">int</font> argc, <font color="blue">char</font>** argv) {
Context context;
<font color="blue">if</font>(argc < 2) {
cerr << <font color="gray">"Usage: CodeToHTML <infile>"</font> << endl;
cerr << <font color="gray">"Wildcards may be used, e.g. CodeToHTML *.cpp"</font> << endl;
exit(1);
}
<font color="blue">#ifdef</font> _DEBUG
cout << <font color="gray">"argc="</font> << argc << <font color="gray">'\t'</font> << argv[1] << endl;
<font color="blue">#endif</font>
<font color="blue">#ifdef</font> _WIN32
expand_wild(argc, argv, &argc, &argv);
<font color="blue">#endif</font>
<font color="blue">#ifdef</font> _DEBUG
cout << <font color="gray">"argc="</font> << argc << <font color="gray">'\t'</font> << argv[1] << endl;
<font color="blue">#endif</font>
<font color="blue">for</font>(<font color="blue">int</font> i = 1; i < argc; ++i) {
<font color="blue">char</font> ofName[64];
cout << <font color="gray">"processing "</font> << argv[i] << <font color="gray">"..."</font>;
ifstream inFile(argv[i]);
strcpy(ofName, argv[i]);
strcat(ofName, <font color="gray">".html"</font>);
ofstream outFile(ofName);
printHeader(ofName, outFile);
<font color="blue">while</font>((context = handle_code(inFile, outFile)) != file_end)
<font color="blue">switch</font>(context) {
<font color="blue">case</font> c_comment:
handle_c_comment(inFile, outFile);
<font color="blue">break</font>;
<font color="blue">case</font> cpp_comment:
handle_cpp_comment(inFile, outFile);
<font color="blue">break</font>;
<font color="blue">case</font> string_literal:
handle_literal(<font color="gray">'\"'</font>, inFile, outFile);
<font color="blue">break</font>;
<font color="blue">case</font> char_literal:
handle_literal(<font color="gray">'\''</font>, inFile, outFile);
<font color="blue">break</font>;
<font color="blue">case</font> newline:
keyMatch(<font color="gray">'\n'</font>, outFile);
<font color="blue">break</font>;
}
printFooter(outFile);
outFile.close();
cout << <font color="gray">"done\n"</font>;
}
<font color="blue">return</font> 0;
}
</pre></body>
</html>