home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
alib
/
d9xx
/
d941
/
guide2doc.lha
/
Guide2Doc
/
Guide2Doc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-20
|
11KB
|
324 lines
///___________"Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi"___________START
/* Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi
based on "ConvertGuide2Doc" by Jorrit Tyberghein
Convert AmigaGuide® file to a normal document
without any "@{xxx}"'s, but full ANSI-support!
CLI-ONLY-Usage: INPUTFILE/A,PAGELENGTH/N,FIRSTPAGE/N
Optional number PAGELENGTH will force chapters
to start on next page if less than ten lines are
left on the actual page and a table of contents
will be generated.
Additional number FIRSTPAGE will start counting
pages from this and put pagenumbers centered in
an added footline (2 lines of pagelength used).
Output goes to StandardOut, so you may redirect
it, eg. ">prt:", or read the guide in CLI.
*/
///_________________________________"1.0"__________________________________ENDE
///_________________________"#include <stdlib.h>"_________________________START
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char version[] = "$VER: Guide2Doc 1.0 (02-Nov-1993) FreeWare - © Koessi";
char pagestr[] = "\n\t\t\t\t--- %ld ---\n";
char *nullstr = "";
char helpstr[] = "\n\t\t\x9B0;32m%s\x9B0;33m\n"
"\t\tbased on \"ConvertGuide2Doc\" by Jorrit Tyberghein\n\n"
"\t\tConvert AmigaGuide® file to a normal document -\n"
"\t\twithout any \"@{xxx}\"'s, but full ANSI-support!\x9B0m\n\n"
"CLI-ONLY-Usage:\t\x9B1;32m%s INPUTFILE/A,PAGELENGTH/N,FIRSTPAGE/N\x9B0;33m\n\n"
"\t\tOptional number \x9B0;32mPAGELENGTH\x9B0;33m will force chapters\n"
"\t\tto start on next page if less than ten lines are\n"
"\t\tleft on the actual page and a table of contents\n"
"\t\twill be generated.\n\n"
"\t\tAdditional number \x9B0;32mFIRSTPAGE\x9B0;33m will start counting\n"
"\t\tpages from this and put pagenumbers centered in\n"
"\t\tan added footline (2 lines of pagelength used).\n\n"
"\t\tOutput goes to \x9B0;32mStandardOut\x9B0;33m, so you may redirect\n"
"\t\tit, eg. \">prt:\", or read the guide in CLI.\x9B0m\n";
#define STAT_NO (0)
#define STAT_BD (1<<1)
#define STAT_IT (1<<3)
#define STAT_UL (1<<4)
#define ERRORRR -1
#define BUFSIZE 4096
#define ANSI_NO(s) { *((int *)s) = 0x9B306D00; s += 3; }
#define ANSI_BD(s) { *((int *)s) = 0x9B316D00; s += 3; }
#define ANSI_IT(s) { *((int *)s) = 0x9B336D00; s += 3; }
#define ANSI_UL(s) { *((int *)s) = 0x9B346D00; s += 3; }
///_________________________"#include <stdlib.h>"__________________________ENDE
int
main(int argc, char *argv[])
{
if (argc < 2 || *argv[1] == '?')
{
printf(helpstr, &version[6], argv[0]); return;
}
FILE *fp;
char *buf = NULL, *buf2 = NULL, *buf3 = NULL, *p, *p2;
int state = ERRORRR, ansi = STAT_NO, line = 1, maxline = 100000, page = 1, nodes = 0, t;
if ((buf = malloc(BUFSIZE)) && (buf2 = malloc(BUFSIZE)) && (buf3 = malloc(32)))
{
if (fp = fopen(argv[1], "r"))
{
if (fgets(buf, BUFSIZE - 1, fp) && !strnicmp(buf, "@Database", 9))
{
///______________________"________@Database"______________________________START
buf[strlen(buf) - 1] = '\0';
*buf3 = '\0';
p = &buf[9];
p2 = buf2;
ANSI_BD(p2);
state = 1;
while (*p)
{
switch (state)
{
case 1: /* Waiting for first/third double quote */
if (*p == '"')
{
state = 2;
p2 = &(buf2[3]);
t = (72 - strlen(p)) / 2;
while (t-- > 0) *p2++ = ' ';
}
else
*p2++ = *p;
break;
case 2: /* Waiting for second/fourth double quote */
if (*p == '"')
state = 1;
else
*p2++ = *p;
default:
break;
}
p++;
}
ANSI_NO(p2); *p2 = '\0';
puts(buf2);
++line;
///______________________________"@Database"_______________________________ENDE
if (argc > 2)
{
maxline = atoi(argv[2]);
if (argc > 3)
{
page = atoi(argv[3]);
maxline -= 2;
}
}
while (fgets(buf, BUFSIZE - 1, fp)) /* LOOP STARTS HERE */
{
buf[strlen(buf) - 1] = 0;
p = buf;
if (*((short *)p) == '##') continue; /* skip commentline */
if (*p == '@') /* '@' - found that special token at start of line */
{
///__________________"_____________@Author"_______________________________START
if (!strnicmp(p + 1, "Author", 6))
{
if (line < maxline)
{
*++p = 'A';
puts(p); ++line;
}
continue;
}
///_______________________________"@Author"________________________________ENDE
///___________________"_____________@Node"________________________________START
if (!strnicmp(p + 1, "Node", 4))
{
if (line >= (maxline - 10))
{
while (line++ <= maxline) puts(nullstr);
if (argc > 3) printf(pagestr, page);
line = 1;
++page;
}
++nodes;
p2 = buf2;
ANSI_BD(p2);
state = 1;
p += 5;
while (*p)
{
switch (state)
{
case 1: /* Waiting for first/third double quote */
if (*p == '"')
{
state = 2;
p2 = &(buf2[3]);
}
else
*p2++ = *p;
break;
case 2: /* Waiting for second/fourth double quote */
if (*p == '"')
state = 1;
else
*p2++ = *p;
break;
}
p++;
}
ANSI_NO(p2);
puts(buf2);
++line;
if ((argc > 2) && (buf3 = realloc(buf3, nodes * 80))) /* make contents entry */
{
p = *buf3 ? &buf3[strlen(buf3)] : buf3;
p2 = &buf2[3];
*p++ = ' ';
*p++ = ' ';
t = 0;
while (*p2 != (char)0x9B && t++ < 60) *p++ = *p2++;
while (t++ < 65) *p++ = '.';
sprintf(p, "%3ld\n", page);
}
continue;
}
///________________________________"Node"__________________________________ENDE
if (p[1] != '{') continue; /* ignore everything but links and ansicodes */
}
///_____"______________'@' - found that special token"____________________START
if (p2 = strstr(p, "@{")) /* find that special token */
{
state = 0;
p2 = buf2;
while (*p)
{
switch (state)
{
case 0: /* Initial state */
if (*((short *)p) == '@{') /* found that special token */
{
switch(p[2])
{
case 'b': ansi |= STAT_BD; break; /* start bold */
case 'i': ansi |= STAT_IT; break; /* start italic */
case 'u':
switch(p[3])
{
case '}': ansi |= STAT_UL; break; /* start underlined */
case 'b': ansi &= ~STAT_BD; ++p; break; /* stop bold */
case 'i': ansi &= ~STAT_IT; ++p; break; /* stop italic */
case 'u': ansi &= ~STAT_UL; ++p; break; /* stop underline */
default: state = ERRORRR; break; /* unknown token - error */
}
break;
default: ++p; state = 1; break; /* this must be a link ! */
}
if (0 == state) /* (re)set ansi state */
{
p += 3; ANSI_NO(p2);
if (ansi & STAT_BD) ANSI_BD(p2);
if (ansi & STAT_IT) ANSI_IT(p2);
if (ansi & STAT_UL) ANSI_UL(p2);
}
}
else *p2++ = *p;
break;
case 1: /* Waiting for first double quote */
if (*p == '"') { ANSI_IT(p2); state = 2; }
break;
case 2: /* Waiting for second double quote */
if (*p == '"') { ANSI_NO(p2); state = 3; }
else
*p2++ = *p;
break;
case 3: /* Waiting for '}' */
if (*p == '}') state = 0;
break;
}
p++; /* get next char from inputlinebuffer */
} /* this inputline is done */
*p2 = 0; /* mark end of outputbuffer */
p = buf2;
}
///___________________"'@' - found that special token"_____________________ENDE
if (ERRORRR == state) break; /* after-each-line-error-check !!! */
puts(p); /* printout line */
if (++line > maxline) /* inc counters */
{
if (argc > 3) printf(pagestr, page);
line = 1;
++page;
}
}
if (ERRORRR == state) /* final error check !!! */
printf("Error in Database - Line %ld\n", line);
else
{
if ((argc > 2) && *buf3)
{
puts(nullstr); ++line;
t = (nodes > maxline) ? (nodes % maxline) : (maxline - nodes);
if (t > (maxline - line - 2))
{
while (line++ < maxline) puts(nullstr);
if (argc > 3) printf(pagestr, page);
}
printf("\x9B1mCONTENTS\x9B0m\n\n%s", buf3);
}
printf("\n %s converted by\n %s\n", argv[1], &version[6]);
}
}
fclose(fp);
}
else puts("Error opening file!");
}
if (buf) free(buf);
if (buf2) free(buf2);
if (buf3) free(buf3);
}