Debian Codegraph @

support/encyclopedia2c/encyclopedia2c.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // encyclopedia2c.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <fstream>
•7 #include <string.h>
8 #include <stdlib.h>
9
10 using namespace std;
11
12 void
13 writeQuotedString(ostream &os, const char *s)
14 {
15 os << "\"";
16
17 while (*s)
18 {
19 if (*s == '"')
20 {
21 os << "\\\"";
22 }
23 else if (*s == '\\')
24 {
25 os << "\\";
26 }
27 else
28 os << *s;
29
30 s++;
31 }
32
33 os << "\"";
34 }
35
36 class LINE
37 {
38 public:
39 explicit LINE(const char *line);
40 ~LINE();
41
42 LINE *getNext() const { return myNext; }
43 void setNext(LINE *line) { myNext = line; }
44
45 const char *getLine() const { return myLine; }
46
47 private:
48 LINE *myNext;
49 char *myLine;
50 };
51
52 LINE::LINE(const char *line)
53 {
54 myNext = 0;
55 myLine = strdup(line);
56 }
57
58 LINE::~LINE()
59 {
60 free(myLine);
61 delete myNext;
62 }
63
64 class ENTRY
65 {
66 public:
67 explicit ENTRY(const char *key, const char *name);
68 ~ENTRY();
69
70 ENTRY *getNext() const { return myNext; }
71 void setNext(ENTRY *next) { myNext = next; }
72
73 const char *getKey() const { return myKey; }
74
75 // This appends the given text, taking into account
76 // merging adjacent lines & implicit paragraphs/
77 // returns.
78 void appendText(const char *text);
79
80 void save(ostream &os, ostream &hs) const;
81
82 private:
83 // This appends the given line as a new LINE structure.
84 void appendLine(const char *line);
85
86 LINE *myLines;
87 ENTRY *myNext;
88 char *myKey, *myName;
89
90 char *myLastLine;
91 };
92
93 ENTRY::ENTRY(const char *key, const char *name)
94 {
95 myKey = strdup(key);
96 myName = strdup(name);
97 myNext = 0;
98 myLines = 0;
99 myLastLine = 0;
100 }
101
102 ENTRY::~ENTRY()
103 {
104 free(myKey);
105 free(myName);
106 delete myLines;
107 delete myNext;
108 }
109
110 void
111 ENTRY::appendLine(const char *txtline)
112 {
113 LINE *line, *last;
114
115 line = new LINE(txtline);
116
117 for (last = myLines; last && last->getNext(); last = last->getNext());
118
119 if (last)
120 last->setNext(line);
121 else
122 myLines = line;
123 }
124
125 void
126 ENTRY::appendText(const char *text)
127 {
128 size_t linelen;
129
130 linelen = strlen(text);
131
132 if (!linelen)
133 {
134 // We want to do a new paragraph.
135 // We thus finish any current line,
136 // and append a blank line for
137 // good measure.
138 if (myLastLine)
139 appendLine(myLastLine);
140 appendLine("");
141 delete [] myLastLine;
142 myLastLine = 0;
143
144 return;
145 }
146
147 // Append into our last line structure.
148 if (myLastLine)
149 {
150 char *newlast;
151 char lastchar;
152
153 newlast = new char[linelen + strlen(myLastLine) + 5];
154 strcpy(newlast, myLastLine);
155 if (strlen(newlast))
156 {
157 lastchar = newlast[strlen(newlast)-1];
158 // If we didn't have a trailing space, make sure
159 // we add one. If we had a punctuation, we must
160 // make sure we do two spaces.
161 if (strchr(".!?", lastchar))
162 {
163 strcat(newlast, " ");
164 }
165 else if (lastchar != ' ')
166 strcat(newlast, " ");
167 }
168 strcat(newlast, text);
169 delete [] myLastLine;
170 myLastLine = newlast;
171 }
172 else
173 {
174 myLastLine = new char[linelen+3];
175 strcpy(myLastLine, text);
176 }
177
178 if (linelen <= 30)
179 {
180 // This is an implied return.
181 appendLine(myLastLine);
182 delete [] myLastLine;
183 myLastLine = 0;
184 }
185 }
186
187 void
188 ENTRY::save(ostream &os, ostream &hs) const
189 {
190 int numline = 0;
191 LINE *line;
192
193 // Save our line list.
194 os << "const char *glb_encyclopedia_entry_" << myKey
195 << "_lines[] =" << endl;
196 os << "{" << endl;
197
198 // Trim blank lines.
199 bool hasblank = true;
200 LINE *last;
201 while (hasblank)
202 {
203 hasblank = false;
204 last = 0;
205 for (line = myLines; line && line->getNext(); line = line->getNext())
206 {
207 last = line;
208 }
209
210 // See if last line was blank...
211 // Can't delete only line.
212 if (line && last)
213 {
214 if (!strcmp(line->getLine(), ""))
215 {
216 hasblank = true;
217 last->setNext(0);
218 delete line;
219 }
220 }
221 }
222
223 for (line = myLines; line; line = line->getNext())
224 {
225 os << " ";
226 writeQuotedString(os, line->getLine());
227 if (line->getNext())
228 os << ",";
229 os << endl;
230
231 numline++;
232 }
233
234 os << "};" << endl;
235 os << endl;
236
237 // Now, save the actual entry.
238 os << "const encyclopedia_entry glb_encyclopedia_entry_" << myKey
239 << " =" << endl;
240 os << "{" << endl;
241 os << " " << myKey << "," << endl;
242 os << " ";
243 writeQuotedString(os, myName);
244 os << "," << endl;
245 os << " " << numline << "," << endl;
246 os << " glb_encyclopedia_entry_" << myKey << "_lines" << endl;
247 os << "};" << endl;
248 os << endl;
249 }
250
251 class BOOK
252 {
253 public:
254 explicit BOOK(const char *bookname);
255 ~BOOK();
256
257 static BOOK *getBook(const char *name);
258
259 BOOK *getNext() const { return myNext; }
260 void setNext(BOOK *book) { myNext = book; }
261
262 const char *getName() const { return myName; }
263
264 void appendEntry(ENTRY *entry);
265
266 void save(ostream &os, ostream &hs) const;
267
268 private:
269 ENTRY *myEntries;
270 char *myName;
271 BOOK *myNext;
272 };
273
274 BOOK *glb_booklist = 0;
275
276 BOOK::BOOK(const char *bookname)
277 {
278 myName = strdup(bookname);
279 myEntries = 0;
280 myNext = 0;
281 }
282
283 BOOK::~BOOK()
284 {
285 delete myEntries;
286 free(myName);
287 }
288
289 BOOK *
290 BOOK::getBook(const char *name)
291 {
292 BOOK *book, *last;
293
294 last = 0;
295 for (book = glb_booklist; book; book = book->getNext())
296 {
297 if (!strcmp(book->myName, name))
298 return book;
299 last = book;
300 }
301
302 // We need a new book.
303 book = new BOOK(name);
304 if (last)
305 last->setNext(book);
306 else
307 glb_booklist = book;
308 return book;
309 }
310
311 void
312 BOOK::appendEntry(ENTRY *entry)
313 {
314 ENTRY *last;
315
316 for (last = myEntries; last && last->getNext(); last = last->getNext());
317
318 if (last)
319 last->setNext(entry);
320 else
321 myEntries = entry;
322 }
323
324 void
325 BOOK::save(ostream &os, ostream &hs) const
326 {
327 int numentry;
328 ENTRY *entry;
329
330 numentry = 0;
331 for (entry = myEntries; entry; entry = entry->getNext())
332 {
333 entry->save(os, hs);
334 numentry++;
335 }
336
337 // Save our list of entry list.
338 os << "const encyclopedia_entry *glb_encyclopedia_book_" << myName
339 << "_entrylist[] =" << endl;
340 os << "{" << endl;
341 for (entry = myEntries; entry; entry = entry->getNext())
342 {
343 os << " &glb_encyclopedia_entry_" << entry->getKey();
344 if (entry->getNext())
345 os << ",";
346 os << endl;
347 }
348 os << "};" << endl;
349 os << endl;
350
351 // Save the book proper.
352 os << "const encyclopedia_book glb_encyclopedia_book_" << myName
353 << " =" << endl;
354 os << "{" << endl;
355 os << " ";
356 writeQuotedString(os, myName);
357 os << "," << endl;
358 os << " " << numentry << "," << endl;
359 os << " glb_encyclopedia_book_" << myName << "_entrylist" << endl;
360 os << "};" << endl;
361 os << endl;
362 }
363
364 //
365 // Stream controls.
366 // These allow one to undo the fetching of a line.
367 //
368 bool glb_putbackline = false;
369 char raw_input[1024];
370 char tabless_input[8192]; // Enough for 1k of tabs.
371
372 // Removes tabs treating them as mod 8 events.
373 void
374 killtabs(char *raw, char *tabless)
375 {
376 int off;
377
378 for (off = 0; *raw; raw++)
379 {
380 if (*raw == '\t')
381 {
382 tabless[off++] = ' ';
383 while (off & 7)
384 tabless[off++] = ' ';
385 }
386 else
387 {
388 tabless[off++] = *raw;
389 }
390 }
391 tabless[off++] = '\0';
392 }
393
394 bool
395 input_waiting(ifstream &is)
396 {
397 if (glb_putbackline) return true;
398
399 if (is)
400 return true;
401
402 return false;
403 }
404
405 char *
406 input_getline(ifstream &is)
407 {
408 if (glb_putbackline)
409 {
410 glb_putbackline = false;
411 // Ensure we don't have corrupted input, retab.
412 killtabs(raw_input, tabless_input);
413 return tabless_input;
414 }
415
416 // read a line.
417 is.getline(raw_input, 1020);
418 killtabs(raw_input, tabless_input);
419 return tabless_input;
420 }
421
422 void
423 input_putbackline()
424 {
425 glb_putbackline = true;
426 }
427
428 int
429 main(int argc, char* argv[])
430 {
431 if (argc < 2)
432 {
433 cerr << "Usage:" << endl;
434 cerr << "\tencyclopedia2c encyclopedia.txt" << endl;
435 cerr << "This will create encyclopedia.cpp and encyclopedia.h" << endl;
436 return -1;
437 }
438
439 // Construct our output file names.
440 char *headername, *cppname;
441
442 headername = new char[strlen(argv[1]) + 10];
443 cppname = new char[strlen(argv[1]) + 10];
444 strcpy(headername, argv[1]);
445 strcpy(cppname, argv[1]);
446 if (strrchr(headername, '.'))
447 *strrchr(headername, '.') = '\0';
448 if (strrchr(cppname, '.'))
449 *strrchr(cppname, '.') = '\0';
450
451 strcat(headername, ".h");
452 strcat(cppname, ".cpp");
453
454 // See if we can open the incoming stream.
455 ifstream is(argv[1]);
456
457 if (!is)
458 {
459 cerr << "Unable to open " << argv[1] << " for reading." << endl;
460 return -1;
461 }
462
463 // Open the output streams.
464 ofstream os(cppname), hs(headername);
465
466 if (!os)
467 {
468 cerr << "Unable to open " << cppname << " for writing." << endl;
469 return -2;
470 }
471 if (!hs)
472 {
473 cerr << "Unable to open " << headername << " for writing." << endl;
474 return -3;
475 }
476
477 // Standard header information.
478 hs << "//" << endl;
479 hs << "// " << headername << endl;
480 hs << "// Automagically Generated by " << argv[0] << endl;
481 hs << "// DO NOT HAND EDIT." << endl;
482 hs << "// (Yes, this does mean you!)" << endl;
483 hs << "//" << endl;
484 hs << endl;
485
486 os << "//" << endl;
487 os << "// " << cppname << endl;
488 os << "// Automagically Generated by " << argv[0] << endl;
489 os << "// DO NOT HAND EDIT." << endl;
490 os << "// (Yes, this does mean you!)" << endl;
491 os << "//" << endl;
492 os << endl;
493 os << "#include \"encyclopedia.h\"" << endl;
494 os << "#include \"glbdef.h\"" << endl;
495 os << endl;
496
497 // Create our data structures.
498 hs << "struct encyclopedia_entry" << endl;
499 hs << "{" << endl;
500 hs << " int key;" << endl;
501 hs << " const char *name;" << endl;
502 hs << " int numlines;" << endl;
503 hs << " const char **lines;" << endl;
504 hs << "};" << endl;
505 hs << endl;
506 hs << "struct encyclopedia_book" << endl;
507 hs << "{" << endl;
508 hs << " const char *bookname;" << endl;
509 hs << " int numentries;" << endl;
510 hs << " const encyclopedia_entry**entries;" << endl;
511 hs << "};" << endl;
512 hs << endl;
513
514 // Start reading in lines...
515 while (input_waiting(is))
516 {
517 char *line;
518
519 line = input_getline(is);
520 if (*line == '#')
521 {
522 // Ignore comments.
523 continue;
524 }
525
526
527 // Parse out the book/key/entry name.
528 char bookname[1024], keyname[1024], entryname[1024];
529 char *readhead;
530
531 // If line is blank, ignore it.
532 for (readhead = line; *readhead == ' '; readhead++);
533 if (!*readhead)
534 continue;
535
536 readhead = line;
537 strcpy(bookname, line);
538 if (strchr(bookname, ':'))
539 *strchr(bookname, ':') = '\0';
540
541 for (readhead = line; *readhead; readhead++)
542 {
543 if (*readhead == ':' && readhead[1] == ':')
544 break;
545 }
546 if (!*readhead)
547 {
548 cerr << "Improper formatted line:" << endl;
549 cerr << line;
550 continue;
551 }
552 readhead += 2;
553
554 if (*readhead == '-')
555 {
556 cerr << "Improper formatted line:" << endl;
557 cerr << line;
558 continue;
559 }
560 strcpy(keyname, bookname);
561 strcat(keyname, "_");
562 strcat(keyname, readhead);
563 if (*keyname != '-' && strchr(keyname, '-'))
564 strchr(keyname, '-')[-1] = '\0';
565
566 for (; *readhead; readhead++)
567 {
568 if (*readhead == '-' && readhead[-1] == ' ' && readhead[1] == ' ')
569 break;
570 }
571 if (!readhead)
572 {
573 cerr << "Improper formatted line:" << endl;
574 cerr << line;
575 continue;
576 }
577 strcpy(entryname, readhead+2);
578
579 // Create a new entry matching this setup.
580 ENTRY *entry;
581 entry = new ENTRY(keyname, entryname);
582 BOOK *book;
583
584 book = BOOK::getBook(bookname);
585 book->appendEntry(entry);
586
587 // Now add every line that occurs...
588 while (input_waiting(is))
589 {
590 line = input_getline(is);
591
592 // Add blank lines.
593 for (readhead = line; *readhead == ' '; readhead++);
594 if (!*readhead)
595 {
596 // Append a truly blank line.
597 entry->appendText("");
598 continue;
599 }
600
601 // Verify we have 4 spaces.
602 if (line[0] != ' ' &&
603 line[1] != ' ' &&
604 line[2] != ' ' &&
605 line[3] != ' ')
606 {
607 // Likely start of another entry.
608 input_putbackline();
609 break;
610 }
611
612 // Add the line.
613 entry->appendText(&line[4]);
614 }
615 }
616
617 // We have now read in all the books: output.
618 if (glb_booklist)
619 {
620 BOOK *book;
621 int numbook = 0;
622
623 for (book = glb_booklist; book; book = book->getNext())
624 {
625 book->save(os, hs);
626 numbook++;
627 }
628
629 // Now, build global list.
630 hs << "#define NUM_ENCYCLOPEDIA_BOOKS " << numbook << endl;
631 hs << "extern const encyclopedia_book *glb_encyclopedia[NUM_ENCYCLOPEDIA_BOOKS];" << endl;
632
633 os << "const encyclopedia_book *glb_encyclopedia[NUM_ENCYCLOPEDIA_BOOKS] =" << endl;
634 os << "{" << endl;
635
636 for (book = glb_booklist; book; book = book->getNext())
637 {
638 os << " &glb_encyclopedia_book_" << book->getName();
639 if (book->getNext())
640 os << ",";
641 os << endl;
642 }
643 os << "};" << endl;
644 }
645
646 return 0;
647 }
648