Menu

[0183e4]: / goodies / helpc / ocphhc.c  Maximize  Restore  History

Download this file

548 lines (452 with data), 12.3 kB

  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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* OpenCP hypertext help compiler (OCPHHC) for use with new help system
*
* not copyrighted; written by Fabian Giesen (RYG/Chrome Design)
* it's simple. it's freeware. it's badly coded. it (hardly) works.
*
* revision history: (please note changes here)
* -fg980811 Fabian Giesen <gfabian@jdcs.su.nw.schule.de>
* -first alpha
* -fg980812 Fabian Giesen <gfabian@jdcs.su.nw.schule.de>
* -bugfixing
* -fg980813 Fabian Giesen <gfabian@jdcs.su.nw.schule.de>
* -added some commands
* -fg980814 Fabian Giesen <gfabian@jdcs.su.nw.schule.de>
* -new helpfile format version which is compressed
* -compressed using "deflate" algorithm (via zlib), can be decompressed
* with OpenCP standard INFLATE.DLL
* -fg980815 Fabian Giesen <gfabian@jdcs.su.nw.schule.de>
* -added "statistics" screen
* -ryg_dunno Fabian Giesen <fabian@jdcs.su.nw.schule.de>
* -fixed some bugs (don't remember which ones)
* -changed email address (yeah, i've got a new one)
* -now i use "ryg" instead of "fg"; don't ask me why
* -ryg981205 Fabian Giesen <fabian@jdcs.su.nw.schule.de>
* -now uses some pseudo-zlib with just the deflation code in it (because
* including whole zlib would be just a waste of space)
* -this code is ABSOLUTELY NOT SUPPORTED. if it crashes, it's your own
* problem. also it will be obselete soon because i'll write some
* html->help compiler (which roxxs :)
* -there's no documentation for this. read the help definition file and
* try and understand it. good luck.
* -ryg981211 Fabian Giesen <fabian@jdcs.su.nw.schule.de>
* -fixed some stupid bug in the "parser" which killed almost all escape
* seqeunces (oops..)
* -fd990519 Felix Domke <tmbinc@gmx.net>
* -fixed the stupid "backslashed".
*
* READ THIS SOURCE FILE CAREFULLY. IT'S SURELY ONE OF THE BEST EXAMPLES OF
* BAD C CODE YOU'LL EVER GET.
*
* some other thing: in this file the help pages are called "sections" (don't
* ask me why).
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "zlib.h"
#pragma pack (1) /* this is essential */
#define MAXSECTSIZE (256*1024) /* maximum section size 256k */
/* Help file control characters */
#define CMD_NORMAL 1 /* switch colour to normal */
#define CMD_BRIGHT 2 /* switch colour to bright */
#define CMD_HYPERLINK 3 /* hyperlink */
#define CMD_CENTERED 4 /* centered text */
#define CMD_COLOURCHOOSE 5 /* choose colour */
#define CMD_RAWCHAR 6 /* raw character (use for chars<32) */
typedef struct sect {
char *name;
char *desc;
void *data,
*compdata;
uint32_t datasize,
compdatasize,
textlines;
struct sect *next;
} section;
char line[2048],
sectionbuf[MAXSECTSIZE];
int sectpos=0,
quit=0,
sectlines=0;
section *sections=NULL,
*cursect=NULL;
FILE *in,
*out;
char file_id[5]="OCPH";
uint32_t helpver=0x011000;
int verbose=0,
comment=0;
int totdatasize,
totcompdatasize;
void putfstr(char *what, FILE *to)
{
if (what)
{
putc(strlen(what), to);
if (strlen(what))
if (fwrite(what, strlen(what), 1, to) != 1)
{
perror(__FILE__ ": fwrite failed #1: ");
exit(1);
}
} else putc(0, to);
}
void flush_section(void)
{
if (!cursect || comment)
return;
cursect->data=calloc(1, sectpos);
memcpy(cursect->data, sectionbuf, sectpos);
cursect->datasize=sectpos;
cursect->textlines=sectlines;
sectpos=0;
sectlines=0;
}
void process_section_line(char *line)
{
char *p1,
*p2;
int i;
flush_section();
comment=0;
if (!strncasecmp(line, ".comment", 9)) { comment=1; return; };
if (!strncasecmp(line, ".end", 4)) { quit=1; return; };
if (cursect) {
cursect->next=calloc(sizeof(section), 1); cursect=cursect->next;
} else {
sections=calloc(sizeof(section), 1); cursect=sections;
};
p1=line+1; p2=p1; i=0;
while (*p2 && (*p2!=' '))
p2++;
if (*p2)
i=1;
*p2=0;
cursect->name=strdup(p1);
if (i)
cursect->desc=strdup(p2+1);
else
cursect->desc=NULL;
cursect->data=NULL; cursect->next=NULL;
}
int packhex(char *from)
{
int val;
char buf[3];
strncpy(buf, from, 2);
buf[2]=0;
val=0;
if ((*from>='0') && (*from<='9')) val|=*from-'0';
if ((*from>='A') && (*from<='F')) val|=*from-'A'+10;
if ((*from>='a') && (*from<='f')) val|=*from-'a'+10;
val<<=4; from++;
if ((*from>='0') && (*from<='9')) val|=*from-'0';
if ((*from>='A') && (*from<='F')) val|=*from-'A'+10;
if ((*from>='a') && (*from<='f')) val|=*from-'a'+10;
return val;
}
void process_text_line(char *line)
{
char buffer[2048],
buffer2[2048],
*p;
int len,
brightst,
i,
backslashed;
if (comment)
return;
strcpy(buffer, line);
brightst=0;
backslashed=0;
/* First, process all "bright"-switches */
p=&buffer[0]; i=0;
while (*p) {
backslashed=(*p=='\\')&&(!backslashed); /* better? (fd) */
if (((unsigned)*p)<32) {
strcpy(buffer2, &buffer[i+1]);
strcpy(&buffer[i+2], buffer2);
buffer[i+1]=*p;
buffer[i]=CMD_RAWCHAR;
p++;
i++;
} else if (*p=='~')
{
if (backslashed)
{
strcpy(buffer2, p);
strcpy(p-1, buffer2);
backslashed=0;
} else {
brightst=1-brightst;
if (brightst)
*p=CMD_BRIGHT;
else
*p=CMD_NORMAL;
}
} else if (*p=='^')
{
if (backslashed)
{
strcpy(buffer2, p);
strcpy(p-1, buffer2);
backslashed=0;
} else {
*p=CMD_COLOURCHOOSE; p++; i++;
*p=packhex(p);
strcpy(buffer2, p+2);
strcpy(p+1, buffer2);
}
} else if (*p=='n') {
if (backslashed)
{
*(p-1)=CMD_RAWCHAR;
*p='\n';
backslashed=0;
}
} else if (*p=='r') {
if (backslashed)
{
*(p-1)=CMD_RAWCHAR;
*p='\r';
backslashed=0;
}
} else if (*p=='d') {
if (backslashed)
{
*(p-1)=CMD_RAWCHAR;
*p=26;
backslashed=0;
}
}
p++;
i++;
}
/* Then check for any special marks. If there are, process them, otherwise,
* just copy the text.
*/
if (strchr(buffer, '[')) {
if (*(strchr(buffer, '[')-1)!='\\')
{
char *fptr;;
len=strlen(buffer);
fptr=&buffer[0];
while ((fptr=strchr(fptr, '\\')))
{
strcpy(fptr, fptr+1);
fptr+=1;
}
*strchr(buffer, '[')=CMD_CENTERED;
if (strchr(buffer, ']')) *strchr(buffer, ']')=0;
memcpy(&sectionbuf[sectpos], buffer, len); sectpos+=len+1;
sectionbuf[sectpos-1]='\n';
} else {
char *fptr=&buffer[0];
while ((fptr=strchr(fptr, '\\')))
{
strcpy(fptr, fptr+1);
fptr+=1;
}
len=strlen(buffer); memcpy(&sectionbuf[sectpos], buffer, len);
sectpos+=len+1; sectionbuf[sectpos-1]='\n';
}
} else if (strchr(buffer, '{')) {
if (*(strchr(buffer, '{')-1)!='\\')
{
char *fptr;
len=strlen(buffer);
fptr=&buffer[0];
while ((fptr=strchr(fptr, '\\')))
{
strcpy(fptr, fptr+1);
fptr+=1;
};
*strchr(buffer, '{')=CMD_HYPERLINK;
if (strchr(buffer, '}')) *strchr(buffer, '}')=0;
memcpy(&sectionbuf[sectpos], buffer, len); sectpos+=len+1;
sectionbuf[sectpos-1]='\n';
} else {
char *fptr;
strcpy(strchr(buffer, '{')-1, strchr(buffer, '{'));
fptr=&buffer[0];
while ((fptr=strchr(fptr, '\\')))
{
strcpy(fptr, fptr+1);
fptr+=1;
}
len=strlen(buffer); memcpy(&sectionbuf[sectpos], buffer, len);
sectpos+=len+1; sectionbuf[sectpos-1]='\n';
}
} else {
char *fptr=&buffer[0];
while ((fptr=strchr(fptr, '\\')))
{
strcpy(fptr, fptr+1);
fptr+=1;
}
len=strlen(buffer); memcpy(&sectionbuf[sectpos], buffer, len);
sectpos+=len+1; sectionbuf[sectpos-1]='\n';
}
sectlines++;
}
void read_lines(void)
{
while (!feof(in) && (!quit))
{
if (!fgets(line, 2048, in))
break;
if (line[strlen(line)-1]=='\n')
line[strlen(line)-1]=0;
if (line[strlen(line)-1]=='\r')
line[strlen(line)-1]=0;
if (line[0]=='.')
process_section_line(line);
else
process_text_line(line);
}
}
void compress_sections(void)
{
uLong insize,
outsize;
unsigned char *inbuf,
*outbuf;
section *s;
s=sections;
while (s) {
inbuf=(unsigned char *) s->data;
insize=s->datasize;
outsize=insize+(insize/1000)+2;
outbuf=calloc(outsize, 1);
compress2(outbuf, &outsize, inbuf, insize, 9);
#if 0
s->compdata=(void *) (outbuf+2); s->compdatasize=outsize-2;
/* this is because zlib puts 2 header bytes to the beginning of the data-
* stream we don't need
*/
#else
s->compdata=(void *) (outbuf); s->compdatasize=outsize;
#endif
s=s->next;
};
}
void write_section_directory(void)
{
uint32_t numsects;
section *s;
s=sections;
numsects=0;
while (s)
{
s=s->next;
numsects++;
}
numsects = uint32_little(numsects);
if (fwrite(&numsects, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #2: ");
exit(1);
}
s=sections;
while (s)
{
putfstr(s->name, out);
putfstr(s->desc, out);
s->datasize=uint32_little(s->datasize);
s->textlines=uint32_little(s->textlines);
s->compdatasize=uint32_little(s->compdatasize);
if (fwrite(&s->datasize, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #3: ");
exit(1);
}
if (fwrite(&s->textlines, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #4: ");
exit(1);
}
if (fwrite(&s->compdatasize, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #5: ");
exit(1);
}
s->datasize=uint32_little(s->datasize);
s->textlines=uint32_little(s->textlines);
s->compdatasize=uint32_little(s->compdatasize);
s=s->next;
}
}
void write_sections(void)
{
section *s;
int i;
s=sections;
i=0;
while (s)
{
i++;
if (fwrite(s->compdata, s->compdatasize, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #6: ");
exit(1);
}
if (verbose)
printf("Page: %ld, original size %ld bytes, compressed %ld bytes\n", (long)i,
(long)s->datasize, (long)s->compdatasize);
totdatasize+=s->datasize; totcompdatasize+=s->compdatasize;
s=s->next;
}
}
void write_file_header(void)
{
uint32_t t = uint32_little(helpver);
if (fwrite(file_id, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #7: ");
exit(1);
}
if (fwrite(&t, 4, 1, out) != 1)
{
perror(__FILE__ ": fwrite failed #8: ");
exit(1);
}
}
int main(int argc, char *argv[])
{
printf("OCPHHC - OpenCP hypertext help compiler v1.2a - written by ryg\n\n");
if (argc!=3)
{
printf("Use: OCPHHC <Infile.EXT> <Outfile.HLP>\n\n");
return 0;
}
in=fopen(argv[1], "rb"); out=fopen(argv[2], "wb");
totdatasize=totcompdatasize=0;
printf("Reading input...\n"); read_lines();
printf("Compressing page data...\n"); compress_sections();
printf("Writing output header...\n"); write_file_header();
printf("Writing page directory...\n"); write_section_directory();
printf("Writing pages...\n"); write_sections();
printf("\nDone!\n\n");
printf("Helpfile statistics:\n\n");
printf("Complete data size : %ld bytes (%ld KB)\n", (long)totdatasize,
(long)(totdatasize>>10));
printf("Compressed data size: %ld bytes (%ld KB)\n", (long)totcompdatasize,
(long)(totcompdatasize>>10));
printf("Ratio : %2.1f:1\n",
(float) totdatasize/(float) totcompdatasize);
printf("\n");
fclose(in);
fclose(out);
for(cursect=sections;cursect;cursect=sections)
{
sections=cursect->next;
free(cursect->name);
free(cursect->desc);
free(cursect->data);
free(cursect->compdata);
free(cursect);
}
return 0;
}
Oh no! Some styles failed to load. 😵 Please try reloading this page