Mercurial > ~dholland > hg > ag > index.cgi
comparison helpgen/cout.c @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 #include <assert.h> | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 | |
6 #include "topic.h" | |
7 #include "helpgen.h" | |
8 | |
9 static void dumpstring(FILE *f, const char *s, int multiline) { | |
10 int column = 0; | |
11 unsigned char ch; | |
12 | |
13 while (*s) { | |
14 if (column == 0) { | |
15 if (multiline) fprintf(f, " "); | |
16 fprintf(f, "\""); | |
17 } | |
18 ch = *s++; | |
19 | |
20 if (ch >= 32 && ch < 127 && ch != '"' && ch != '\\') { | |
21 fprintf(f, "%c", ch); | |
22 column++; | |
23 } | |
24 else if (ch == '\n') { | |
25 fprintf(f, "\\n"); | |
26 column += 2; | |
27 } | |
28 else { | |
29 assert(ch != '\r'); | |
30 fprintf(f, "\\%03o", ch); | |
31 column += 4; | |
32 } | |
33 | |
34 if (multiline && column > 72) { | |
35 fprintf(f, "\"\n"); | |
36 column = 0; | |
37 } | |
38 } | |
39 | |
40 if (column) { | |
41 fprintf(f, "\""); | |
42 if (multiline) fprintf(f, "\n"); | |
43 } | |
44 } | |
45 | |
46 static void fcout(FILE *f, struct topic **topics, int ntopics) { | |
47 int i, j, n; | |
48 unsigned numentries = 0; | |
49 | |
50 fprintf(f, "/* Automatically generated - do not edit */\n\n"); | |
51 | |
52 fprintf(f, "struct HelpTopic {\n"); | |
53 fprintf(f, " const char *title;\n"); | |
54 fprintf(f, " const char *body;\n"); | |
55 fprintf(f, "};\n\n"); | |
56 | |
57 | |
58 for (i=0; i<ntopics; i++) { | |
59 struct topic *t = topics[i]; | |
60 const char *title = topic_gettitle(t, 0); | |
61 const char *body = topic_getbody(t); | |
62 | |
63 if (!body) body = ""; | |
64 | |
65 fprintf(f, "static const char body_%d[] = \n", i); | |
66 dumpstring(f, body, 1 /* multiline */); | |
67 fprintf(f, ";\n"); | |
68 | |
69 fprintf(f, "static const HelpTopic topic_%d = \n { ", i); | |
70 dumpstring(f, title, 0 /* not multiline */); | |
71 fprintf(f, ", body_%d };\n\n", i); | |
72 } | |
73 | |
74 | |
75 fprintf(f, "static const char *topiclist[] = {\n"); | |
76 for (i=0; i<ntopics; i++) { | |
77 struct topic *t = topics[i]; | |
78 const char *title = topic_gettitle(t, 0); | |
79 | |
80 fprintf(f, " "); | |
81 dumpstring(f, title, 0 /* not multiline */); | |
82 fprintf(f, ",\n"); | |
83 } | |
84 fprintf(f, "};\n"); | |
85 fprintf(f, "static const unsigned numtopiclist = %d;\n\n", ntopics); | |
86 | |
87 | |
88 fprintf(f, "struct helpentry {\n"); | |
89 fprintf(f, " const char *topic;\n"); | |
90 fprintf(f, " const HelpTopic *data;\n"); | |
91 fprintf(f, "};\n\n"); | |
92 | |
93 fprintf(f, "static helpentry helptable[] = {\n"); | |
94 for (i=0; i<ntopics; i++) { | |
95 struct topic *t = topics[i]; | |
96 | |
97 n = topic_getnumtitles(t); | |
98 for (j=0; j<n; j++) { | |
99 fprintf(f, "{ "); | |
100 dumpstring(f, topic_gettitle(t, j), 0 /* not multiline */); | |
101 fprintf(f, ", &topic_%d },\n", i); | |
102 numentries++; | |
103 } | |
104 } | |
105 fprintf(f, "};\n"); | |
106 fprintf(f, "static const unsigned helptablenum = %u;\n\n", numentries); | |
107 } | |
108 | |
109 void cout(const char *file, struct topic **topics, int ntopics) { | |
110 FILE *f; | |
111 | |
112 f = fopen(file, "w"); | |
113 if (!f) { | |
114 fprintf(stderr, "%s: fopen failed", file); | |
115 exit(1); | |
116 } | |
117 | |
118 fcout(f, topics, ntopics); | |
119 | |
120 if (ferror(f)) { | |
121 fprintf(stderr, "%s: write error", file); | |
122 exit(1); | |
123 } | |
124 if (fclose(f)) { | |
125 fprintf(stderr, "%s: fclose failed", file); | |
126 exit(1); | |
127 } | |
128 } |