Debian Codegraph @

support/encyclopedia2c/encyclopedia2c.cpp

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