Some more warnings and bugs fixed.
[wine] / programs / winhelp / hlp2sgml.c
1 /*
2  * Copyright 1996 Ulrich Schmid
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <time.h>
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include "windows.h"
12 #include "hlpfile.h"
13
14 typedef struct
15 {
16   const char *header1;
17   const char *header2;
18   const char *section;
19   const char *first_paragraph;
20   const char *newline;
21   const char *next_paragraph;
22   const char *special_char;
23   const char *begin_italic;
24   const char *end_italic;
25   const char *begin_boldface;
26   const char *end_boldface;
27   const char *begin_typewriter;
28   const char *end_typewriter;
29   const char *tail;
30 } FORMAT;
31
32 typedef struct
33 {
34   const char ch;
35   const char *subst;
36 } CHARMAP[];
37
38
39 FORMAT format =
40 {
41   "<!doctype linuxdoc system>\n"
42   "<article>\n"
43   "<title>\n",
44
45   "\n<author>\n%s\n"
46   "<date>\n%s\n",
47
48   "\n<sect>\n",
49   "\n<p>\n",
50   "\n<newline>\n",
51   "\n\n",
52
53   "&%s;",
54
55   "<em>",
56   "</em>",
57   "<bf>",
58   "</bf>",
59   "<tt>",
60   "</tt>",
61
62   "\n</article>\n"
63 };
64
65 CHARMAP charmap =
66   {{'Æ', "AElig"},
67    {'Á', "Aacute"},
68    {'Â', "Acirc"},
69    {'À', "Agrave"},
70    {'Ã', "Atilde"},
71    {'Ç', "Ccedil"},
72    {'É', "Eacute"},
73    {'È', "Egrave"},
74    {'Ë', "Euml"},
75    {'Í', "Iacute"},
76    {'Î', "Icirc"},
77    {'Ì', "Igrave"},
78    {'Ï', "Iuml"},
79    {'Ñ', "Ntilde"},
80    {'Ó', "Oacute"},
81    {'Ô', "Ocirc"},
82    {'Ò', "Ograve"},
83    {'Ø', "Oslash"},
84    {'Ú', "Uacute"},
85    {'Ù', "Ugrave"},
86    {'Ý', "Yacute"},
87    {'á', "aacute"},
88    {'â', "acirc"},
89    {'æ', "aelig"},
90    {'à', "agrave"},
91    {'å', "aring"},
92    {'ã', "atilde"},
93    {'ç', "ccedil"},
94    {'é', "eacute"},
95    {'ê', "ecirc"},
96    {'è', "egrave"},
97    {'ë', "euml"},
98    {'í', "iacute"},
99    {'î', "icirc"},
100    {'ì', "igrave"},
101    {'ï', "iuml"},
102    {'ñ', "ntilde"},
103    {'ó', "oacute"},
104    {'ÿ', "yuml"},
105    {'ô', "ocirc"},
106    {'ò', "ograve"},
107    {'ø', "oslash"},
108    {'õ', "otilde"},
109    {'ú', "uacute"},
110    {'û', "ucirc"},
111    {'ù', "ugrave"},
112    {'ý', "yacute"},
113    {'<', "lt"},
114    {'&', "amp"},
115    {'"', "dquot"},
116    {'#', "num"},
117    {'%', "percnt"},
118    {'\'', "quot"},
119 #if 0
120    {'(', "lpar"},
121    {')', "rpar"},
122    {'*', "ast"},
123    {'+', "plus"},
124    {',', "comma"},
125    {'-', "hyphen"},
126    {':', "colon"},
127    {';', "semi"},
128    {'=', "equals"},
129    {'@', "commat"},
130    {'[', "lsqb"},
131    {']', "rsqb"},
132    {'^', "circ"},
133    {'_', "lowbar"},
134    {'{', "lcub"},
135    {'|', "verbar"},
136    {'}', "rcub"},
137    {'~', "tilde"},
138 #endif
139    {'\\', "bsol"},
140    {'$', "dollar"},
141    {'Ä', "Auml"},
142    {'ä', "auml"},
143    {'Ö', "Ouml"},
144    {'ö', "ouml"},
145    {'Ü', "Uuml"},
146    {'ü', "uuml"},
147    {'ß', "szlig"},
148    {'>', "gt"},
149    {'§', "sect"},
150    {'¶', "para"},
151    {'©', "copy"},
152    {'¡', "iexcl"},
153    {'¿', "iquest"},
154    {'¢', "cent"},
155    {'£', "pound"},
156    {'×', "times"},
157    {'±', "plusmn"},
158    {'÷', "divide"},
159    {'¬', "not"},
160    {'µ', "mu"},
161    {0,0}};
162
163 /***********************************************************************
164  *
165  *           print_text
166  */
167
168 static void print_text(const char *p)
169 {
170   int i;
171
172   for (; *p; p++)
173     {
174       for (i = 0; charmap[i].ch; i++)
175         if (*p == charmap[i].ch)
176           {
177             printf(format.special_char, charmap[i].subst);
178             break;
179           }
180       if (!charmap[i].ch)
181         printf("%c", *p);
182     }
183 }
184
185 /***********************************************************************
186  *
187  *           main
188  */
189
190 int main(int argc, char **argv)
191 {
192   HLPFILE   *hlpfile;
193   HLPFILE_PAGE *page;
194   HLPFILE_PARAGRAPH *paragraph;
195   time_t t;
196   char date[50];
197   char *filename;
198
199   hlpfile = HLPFILE_ReadHlpFile(argc > 1 ? argv[1] : "");
200
201   if (!hlpfile) return(2);
202
203   time(&t);
204   strftime(date, sizeof(date), "%x", localtime(&t));
205   filename = strrchr(hlpfile->lpszPath, '/');
206   if (filename) filename++;
207   else filename = hlpfile->lpszPath;
208
209   /* Header */
210   printf(format.header1);
211   print_text(hlpfile->lpszTitle);
212   printf(format.header2, filename, date);
213
214   for (page = hlpfile->first_page; page; page = page->next)
215     {
216       paragraph = page->first_paragraph;
217       if (!paragraph) continue;
218
219       /* Section */
220       printf(format.section);
221       for (; paragraph && !paragraph->wVSpace; paragraph = paragraph->next)
222         print_text(paragraph->lpszText);
223       printf(format.first_paragraph);
224
225       for (; paragraph; paragraph = paragraph->next)
226         {
227           /* New line; new paragraph */
228           if (paragraph->wVSpace == 1)
229             printf(format.newline);
230           else if (paragraph->wVSpace > 1)
231             printf(format.next_paragraph);
232
233           if (paragraph->wFont)
234             printf(format.begin_boldface);
235
236           print_text(paragraph->lpszText);
237
238           if (paragraph->wFont)
239             printf(format.end_boldface);
240         }
241     }
242
243   printf(format.tail);
244
245   return(0);
246 }
247
248 /***********************************************************************
249  *
250  *           Substitutions for some WINELIB functions
251  */
252
253 static FILE *file = 0;
254
255 HFILE WINAPI OpenFile32( LPCSTR path, OFSTRUCT *ofs, UINT mode )
256 {
257   file = *path ? fopen(path, "r") : stdin;
258   return file ? 1 : HFILE_ERROR;
259 }
260
261 HFILE WINAPI _lclose32( HFILE hFile )
262 {
263   fclose(file);
264   return 0;
265 }
266
267 LONG WINAPI _hread32( HFILE hFile, LPVOID buffer, LONG count )
268 {
269   return fread(buffer, 1, count, file);
270 }
271
272 HGLOBAL WINAPI GlobalAlloc( UINT flags, DWORD size )
273 {
274   return (HGLOBAL) malloc(size);
275 }
276
277 LPVOID WINAPI GlobalLock( HGLOBAL handle )
278 {
279   return (LPVOID) handle;
280 }
281
282 HGLOBAL WINAPI GlobalFree( HGLOBAL handle )
283 {
284   free((VOID*) handle);
285   return(0);
286 }
287
288 /*
289  * String functions
290  *
291  * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
292  */
293
294 INT WINAPI lstrcmp(LPCSTR str1,LPCSTR str2)
295 {
296   return strcmp( str1, str2 );
297 }
298
299 INT WINAPI lstrcmpi( LPCSTR str1, LPCSTR str2 )
300 {
301   INT res;
302
303   while (*str1)
304     {
305       if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
306       str1++;
307       str2++;
308     }
309   return toupper(*str1) - toupper(*str2);
310 }
311
312 INT WINAPI lstrlen(LPCSTR str)
313 {
314   return strlen(str);
315 }
316
317 LPSTR WINAPI lstrcpy32A( LPSTR dst, LPCSTR src )
318 {
319     if (!src || !dst) return NULL;
320     strcpy( dst, src );
321     return dst;
322 }
323
324 void WINAPI hmemcpy(LPVOID hpvDest, LPCVOID hpvSource, LONG cbCopy)
325 {
326   memcpy(hpvDest, hpvSource, cbCopy);
327 }
328
329 /* Local Variables:    */
330 /* c-file-style: "GNU" */
331 /* End:                */