Added Finnish resources.
[wine] / programs / winhelp / hlp2sgml.c
1 /*
2  * Copyright 1996 Ulrich Schmid
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <time.h>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <assert.h>
25 #include "windows.h"
26 #include "hlpfile.h"
27
28 typedef struct
29 {
30     const char *header1;
31     const char *header2;
32     const char *section;
33     const char *first_paragraph;
34     const char *newline;
35     const char *next_paragraph;
36     const char *special_char;
37     const char *begin_italic;
38     const char *end_italic;
39     const char *begin_boldface;
40     const char *end_boldface;
41     const char *begin_typewriter;
42     const char *end_typewriter;
43     const char *tail;
44 } FORMAT;
45
46 typedef struct
47 {
48     const char ch;
49     const char *subst;
50 } CHARMAP_ENTRY;
51
52
53 FORMAT format =
54 {
55     "<!doctype linuxdoc system>\n"
56     "<article>\n"
57     "<title>\n",
58
59     "\n<author>\n%s\n"
60     "<date>\n%s\n",
61
62     "\n<sect>\n",
63     "\n<p>\n",
64     "\n<newline>\n",
65     "\n\n",
66
67     "&%s;",
68
69     "<em>",
70     "</em>",
71     "<bf>",
72     "</bf>",
73     "<tt>",
74     "</tt>",
75
76     "\n</article>\n"
77 };
78
79 CHARMAP_ENTRY charmap[] =
80 {{'Æ', "AElig"},
81  {'Á', "Aacute"},
82  {'Â', "Acirc"},
83  {'À', "Agrave"},
84  {'Ã', "Atilde"},
85  {'Ç', "Ccedil"},
86  {'É', "Eacute"},
87  {'È', "Egrave"},
88  {'Ë', "Euml"},
89  {'Í', "Iacute"},
90  {'Î', "Icirc"},
91  {'Ì', "Igrave"},
92  {'Ï', "Iuml"},
93  {'Ñ', "Ntilde"},
94  {'Ó', "Oacute"},
95  {'Ô', "Ocirc"},
96  {'Ò', "Ograve"},
97  {'Ø', "Oslash"},
98  {'Ú', "Uacute"},
99  {'Ù', "Ugrave"},
100  {'Ý', "Yacute"},
101  {'á', "aacute"},
102  {'â', "acirc"},
103  {'æ', "aelig"},
104  {'à', "agrave"},
105  {'å', "aring"},
106  {'ã', "atilde"},
107  {'ç', "ccedil"},
108  {'é', "eacute"},
109  {'ê', "ecirc"},
110  {'è', "egrave"},
111  {'ë', "euml"},
112  {'í', "iacute"},
113  {'î', "icirc"},
114  {'ì', "igrave"},
115  {'ï', "iuml"},
116  {'ñ', "ntilde"},
117  {'ó', "oacute"},
118  {'ÿ', "yuml"},
119  {'ô', "ocirc"},
120  {'ò', "ograve"},
121  {'ø', "oslash"},
122  {'õ', "otilde"},
123  {'ú', "uacute"},
124  {'û', "ucirc"},
125  {'ù', "ugrave"},
126  {'ý', "yacute"},
127  {'<', "lt"},
128  {'&', "amp"},
129  {'"', "dquot"},
130  {'#', "num"},
131  {'%', "percnt"},
132  {'\'', "quot"},
133 #if 0
134  {'(', "lpar"},
135  {')', "rpar"},
136  {'*', "ast"},
137  {'+', "plus"},
138  {',', "comma"},
139  {'-', "hyphen"},
140  {':', "colon"},
141  {';', "semi"},
142  {'=', "equals"},
143  {'@', "commat"},
144  {'[', "lsqb"},
145  {']', "rsqb"},
146  {'^', "circ"},
147  {'_', "lowbar"},
148  {'{', "lcub"},
149  {'|', "verbar"},
150  {'}', "rcub"},
151  {'~', "tilde"},
152 #endif
153  {'\\', "bsol"},
154  {'$', "dollar"},
155  {'Ä', "Auml"},
156  {'ä', "auml"},
157  {'Ö', "Ouml"},
158  {'ö', "ouml"},
159  {'Ü', "Uuml"},
160  {'ü', "uuml"},
161  {'ß', "szlig"},
162  {'>', "gt"},
163  {'§', "sect"},
164  {'¶', "para"},
165  {'©', "copy"},
166  {'¡', "iexcl"},
167  {'¿', "iquest"},
168  {'¢', "cent"},
169  {'£', "pound"},
170  {'×', "times"},
171  {'±', "plusmn"},
172  {'÷', "divide"},
173  {'¬', "not"},
174  {'µ', "mu"},
175  {0,0}};
176
177 /***********************************************************************
178  *
179  *           print_text
180  */
181
182 static void print_text(const char *p)
183 {
184     int i;
185
186     for (; *p; p++)
187     {
188         for (i = 0; charmap[i].ch; i++)
189             if (*p == charmap[i].ch)
190             {
191                 printf(format.special_char, charmap[i].subst);
192                 break;
193             }
194         if (!charmap[i].ch)
195             printf("%c", *p);
196     }
197 }
198
199 /***********************************************************************
200  *
201  *           main
202  */
203
204 int main(int argc, char **argv)
205 {
206     HLPFILE   *hlpfile;
207     HLPFILE_PAGE *page;
208     HLPFILE_PARAGRAPH *paragraph;
209     time_t t;
210     char date[50];
211     char *filename;
212
213     hlpfile = HLPFILE_ReadHlpFile(argc > 1 ? argv[1] : "");
214
215     if (!hlpfile) return 2;
216
217     time(&t);
218     strftime(date, sizeof(date), "%x", localtime(&t));
219     filename = strrchr(hlpfile->lpszPath, '/');
220     if (filename) filename++;
221     else filename = hlpfile->lpszPath;
222
223     /* Header */
224     printf(format.header1);
225     print_text(hlpfile->lpszTitle);
226     printf(format.header2, filename, date);
227
228     for (page = hlpfile->first_page; page; page = page->next)
229     {
230         paragraph = page->first_paragraph;
231         if (!paragraph) continue;
232
233         /* Section */
234         printf(format.section);
235         for (; paragraph && !paragraph->u.text.wVSpace; paragraph = paragraph->next)
236             print_text(paragraph->u.text.lpszText);
237         printf(format.first_paragraph);
238
239         for (; paragraph; paragraph = paragraph->next)
240         {
241             switch (paragraph->cookie)
242             {
243             case para_normal_text:
244             case para_debug_text:
245                 /* New line; new paragraph */
246                 if (paragraph->u.text.wVSpace == 1)
247                     printf(format.newline);
248                 else if (paragraph->u.text.wVSpace > 1)
249                     printf(format.next_paragraph);
250
251                 if (paragraph->u.text.wFont)
252                     printf(format.begin_boldface);
253
254                 print_text(paragraph->u.text.lpszText);
255
256                 if (paragraph->u.text.wFont)
257                     printf(format.end_boldface);
258                 break;
259             case para_bitmap:
260             case para_metafile:
261                 break;
262             }
263         }
264     }
265
266     printf(format.tail);
267
268     return 0;
269 }
270
271 /***********************************************************************
272  *
273  *           Substitutions for some WINELIB functions
274  */
275
276 static FILE *file = 0;
277
278 HFILE WINAPI OpenFile( LPCSTR path, OFSTRUCT *ofs, UINT mode )
279 {
280     file = *path ? fopen(path, "r") : stdin;
281     return file ? (HFILE)1 : HFILE_ERROR;
282 }
283
284 HFILE WINAPI _lclose( HFILE hFile )
285 {
286     fclose(file);
287     return 0;
288 }
289
290 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count )
291 {
292     return fread(buffer, 1, count, file);
293 }
294
295 HANDLE WINAPI GetProcessHeap(void)
296 {
297     return 0;
298 }
299
300 void* WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
301 {
302     assert(flags == 0);
303     return malloc(size);
304 }
305
306 void* WINAPI HeapReAlloc( HANDLE heap, DWORD flags, void* ptr, SIZE_T size)
307 {
308     assert(flags == 0);
309     return realloc(ptr, size);
310 }
311
312 BOOL WINAPI HeapFree( HGLOBAL handle, DWORD flags, void* ptr )
313 {
314     free(ptr);
315     return TRUE;
316 }
317
318 char __wine_dbch_winhelp[] = "\003winhelp";
319
320 int wine_dbg_log( int cls, const char *channel, const char *func, const char *format, ... )
321 {
322     return 1;
323 }
324
325 const char *wine_dbgstr_a( const char *s )
326 {
327     return NULL;
328 }
329
330 HBITMAP WINAPI CreateDIBitmap(HDC hdc, CONST BITMAPINFOHEADER* bih, DWORD a, CONST void* ptr, CONST BITMAPINFO* bi, UINT c)
331 {
332     return 0;
333 }
334
335 HMETAFILE WINAPI SetMetaFileBitsEx(UINT cbBuffer, CONST BYTE *lpbBuffer)
336 {
337     return 0;
338 }
339
340 BOOL WINAPI DeleteMetaFile(HMETAFILE h)
341 {
342     return 0;
343 }
344
345 HDC WINAPI GetDC(HWND h)
346 {
347     return 0;
348 }
349
350 int WINAPI ReleaseDC(HWND h, HDC hdc)
351 {
352     return 0;
353 }
354
355 BOOL WINAPI DeleteObject(HGDIOBJ h)
356 {
357     return TRUE;
358 }
359 /*
360  * String functions
361  *
362  * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
363  */
364
365 INT WINAPI lstrcmp( LPCSTR str1, LPCSTR str2 )
366 {
367     return strcmp( str1, str2 );
368 }
369
370 INT WINAPI lstrcmpi( LPCSTR str1, LPCSTR str2 )
371 {
372     INT res;
373
374     while (*str1)
375     {
376         if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
377         str1++;
378         str2++;
379     }
380     return toupper(*str1) - toupper(*str2);
381 }
382
383 INT WINAPI lstrlen( LPCSTR str )
384 {
385     return strlen(str);
386 }
387
388 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
389 {
390     if (!src || !dst) return NULL;
391     strcpy( dst, src );
392     return dst;
393 }
394
395 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
396 {
397     LPSTR d = dst;
398     LPCSTR s = src;
399     UINT count = n;
400
401     while ((count > 1) && *s)
402     {
403         count--;
404         *d++ = *s++;
405     }
406     if (count) *d = 0;
407     return dst;
408 }