hlp2sgml.c 5.91 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*
 * Copyright 1996 Ulrich Schmid
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <fcntl.h>
#include "windows.h"
#include "hlpfile.h"

typedef struct
{
  const char *header1;
  const char *header2;
  const char *section;
  const char *first_paragraph;
  const char *newline;
  const char *next_paragraph;
  const char *special_char;
  const char *begin_italic;
  const char *end_italic;
  const char *begin_boldface;
  const char *end_boldface;
  const char *begin_typewriter;
  const char *end_typewriter;
  const char *tail;
} FORMAT;

typedef struct
{
  const char ch;
  const char *subst;
} CHARMAP[];


FORMAT format =
{
  "<!doctype linuxdoc system>\n"
  "<article>\n"
  "<title>\n",

  "\n<author>\n%s\n"
  "<date>\n%s\n",

  "\n<sect>\n",
  "\n<p>\n",
  "\n<newline>\n",
  "\n\n",

  "&%s;",

  "<em>",
  "</em>",
  "<bf>",
  "</bf>",
  "<tt>",
  "</tt>",

  "\n</article>\n"
};

CHARMAP charmap =
  {{'', "AElig"},
   {'', "Aacute"},
   {'', "Acirc"},
   {'', "Agrave"},
   {'', "Atilde"},
   {'', "Ccedil"},
   {'', "Eacute"},
   {'', "Egrave"},
   {'', "Euml"},
   {'', "Iacute"},
   {'', "Icirc"},
   {'', "Igrave"},
   {'', "Iuml"},
   {'', "Ntilde"},
   {'', "Oacute"},
   {'', "Ocirc"},
   {'', "Ograve"},
   {'', "Oslash"},
   {'', "Uacute"},
   {'', "Ugrave"},
   {'', "Yacute"},
   {'', "aacute"},
   {'', "acirc"},
   {'', "aelig"},
   {'', "agrave"},
   {'', "aring"},
   {'', "atilde"},
   {'', "ccedil"},
   {'', "eacute"},
   {'', "ecirc"},
   {'', "egrave"},
   {'', "euml"},
   {'', "iacute"},
   {'', "icirc"},
   {'', "igrave"},
   {'', "iuml"},
   {'', "ntilde"},
   {'', "oacute"},
   {'', "yuml"},
   {'', "ocirc"},
   {'', "ograve"},
   {'', "oslash"},
   {'', "otilde"},
   {'', "uacute"},
   {'', "ucirc"},
   {'', "ugrave"},
   {'', "yacute"},
   {'<', "lt"},
   {'&', "amp"},
   {'"', "dquot"},
   {'#', "num"},
   {'%', "percnt"},
   {'\'', "quot"},
#if 0
   {'(', "lpar"},
   {')', "rpar"},
   {'*', "ast"},
   {'+', "plus"},
   {',', "comma"},
   {'-', "hyphen"},
   {':', "colon"},
   {';', "semi"},
   {'=', "equals"},
   {'@', "commat"},
   {'[', "lsqb"},
   {']', "rsqb"},
   {'^', "circ"},
   {'_', "lowbar"},
   {'{', "lcub"},
   {'|', "verbar"},
   {'}', "rcub"},
   {'~', "tilde"},
#endif
   {'\\', "bsol"},
   {'$', "dollar"},
   {'', "Auml"},
   {'', "auml"},
   {'', "Ouml"},
   {'', "ouml"},
   {'', "Uuml"},
   {'', "uuml"},
   {'', "szlig"},
   {'>', "gt"},
   {'', "sect"},
   {'', "para"},
   {'', "copy"},
   {'', "iexcl"},
   {'', "iquest"},
   {'', "cent"},
   {'', "pound"},
   {'', "times"},
   {'', "plusmn"},
   {'', "divide"},
   {'', "not"},
   {'', "mu"},
   {0,0}};

/***********************************************************************
 *
 *           print_text
 */

static void print_text(const char *p)
{
  int i;

  for (; *p; p++)
    {
      for (i = 0; charmap[i].ch; i++)
	if (*p == charmap[i].ch)
	  {
	    printf(format.special_char, charmap[i].subst);
	    break;
	  }
      if (!charmap[i].ch)
	printf("%c", *p);
    }
}

/***********************************************************************
 *
 *           main
 */

int main(int argc, char **argv)
{
  HLPFILE   *hlpfile;
  HLPFILE_PAGE *page;
  HLPFILE_PARAGRAPH *paragraph;
  time_t t;
  char date[50];
  char *filename;

  hlpfile = HLPFILE_ReadHlpFile(argc > 1 ? argv[1] : "");

  if (!hlpfile) return(2);

  time(&t);
  strftime(date, sizeof(date), "%x", localtime(&t));
  filename = strrchr(hlpfile->lpszPath, '/');
  if (filename) filename++;
  else filename = hlpfile->lpszPath;

  /* Header */
  printf(format.header1);
  print_text(hlpfile->lpszTitle);
  printf(format.header2, filename, date);

  for (page = hlpfile->first_page; page; page = page->next)
    {
      paragraph = page->first_paragraph;
      if (!paragraph) continue;

      /* Section */
      printf(format.section);
      for (; paragraph && !paragraph->wVSpace; paragraph = paragraph->next)
	print_text(paragraph->lpszText);
      printf(format.first_paragraph);

      for (; paragraph; paragraph = paragraph->next)
	{
	  /* New line; new paragraph */
	  if (paragraph->wVSpace == 1)
	    printf(format.newline);
	  else if (paragraph->wVSpace > 1)
	    printf(format.next_paragraph);

	  if (paragraph->wFont)
	    printf(format.begin_boldface);

	  print_text(paragraph->lpszText);

	  if (paragraph->wFont)
	    printf(format.end_boldface);
	}
    }

  printf(format.tail);

  return(0);
}

/***********************************************************************
 *
 *           Substitutions for some WINELIB functions
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
253 254
static FILE *file = 0;

255
HFILE WINAPI OpenFile( LPCSTR path, OFSTRUCT *ofs, UINT mode )
Alexandre Julliard's avatar
Alexandre Julliard committed
256
{
Alexandre Julliard's avatar
Alexandre Julliard committed
257
  file = *path ? fopen(path, "r") : stdin;
258
  return file ? (HFILE)1 : HFILE_ERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
259 260
}

261
HFILE WINAPI _lclose( HFILE hFile )
Alexandre Julliard's avatar
Alexandre Julliard committed
262
{
Alexandre Julliard's avatar
Alexandre Julliard committed
263
  fclose(file);
Alexandre Julliard's avatar
Alexandre Julliard committed
264 265 266
  return 0;
}

267
LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count )
Alexandre Julliard's avatar
Alexandre Julliard committed
268
{
Alexandre Julliard's avatar
Alexandre Julliard committed
269
  return fread(buffer, 1, count, file);
Alexandre Julliard's avatar
Alexandre Julliard committed
270 271
}

272
HGLOBAL WINAPI GlobalAlloc( UINT flags, DWORD size )
Alexandre Julliard's avatar
Alexandre Julliard committed
273 274 275 276
{
  return (HGLOBAL) malloc(size);
}

277
LPVOID WINAPI GlobalLock( HGLOBAL handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
278 279 280 281
{
  return (LPVOID) handle;
}

282
HGLOBAL WINAPI GlobalFree( HGLOBAL handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
283 284 285 286 287 288 289 290 291 292 293
{
  free((VOID*) handle);
  return(0);
}

/*
 * String functions
 *
 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
 */

294
INT WINAPI lstrcmp(LPCSTR str1,LPCSTR str2)
Alexandre Julliard's avatar
Alexandre Julliard committed
295 296 297 298
{
  return strcmp( str1, str2 );
}

299
INT WINAPI lstrcmpi( LPCSTR str1, LPCSTR str2 )
Alexandre Julliard's avatar
Alexandre Julliard committed
300 301 302 303 304 305 306 307 308 309 310 311
{
  INT res;

  while (*str1)
    {
      if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
      str1++;
      str2++;
    }
  return toupper(*str1) - toupper(*str2);
}

312
INT WINAPI lstrlen(LPCSTR str)
Alexandre Julliard's avatar
Alexandre Julliard committed
313 314 315 316
{
  return strlen(str);
}

317
LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
318
{
Alexandre Julliard's avatar
Alexandre Julliard committed
319 320 321
    if (!src || !dst) return NULL;
    strcpy( dst, src );
    return dst;
Alexandre Julliard's avatar
Alexandre Julliard committed
322 323
}

324
void WINAPI hmemcpy16(LPVOID hpvDest, LPCVOID hpvSource, LONG cbCopy)
Alexandre Julliard's avatar
Alexandre Julliard committed
325 326 327 328 329 330 331
{
  memcpy(hpvDest, hpvSource, cbCopy);
}

/* Local Variables:    */
/* c-file-style: "GNU" */
/* End:                */