📄 doc2ipf.c
字号:
/*
* doc2ipf.c -- program to convert Gnuplot .DOC format to OS/2
* ipfc (.inf/.hlp) format.
*
* Modified by Roger Fearick from doc2rtf by M Castro
* Further modified for PM GSview by Russell Lang 1993-10-27
* removed links to self
* added names to panels (for HM_DISPLAY_HELP)
* added index
* examples now in monospaced type
* obtain title from filename
* third command line option for help table header file
* Modified by Russell Lang 1996-10-15
* obtain title from first line of doc file.
* Modified by Russell Lang 2000-03-04
* fixed treatment of '$' characters when not in table.
*
* usage: doc2ipf gnuplot.doc gnuplot.itl [idxfile]
*
*/
/* note that tables must begin in at least the second column to */
/* be formatted correctly and tabs are forbidden */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_LINE_LEN 1024
#define TRUE 1
#define FALSE 0
struct LIST
{
int level;
int line;
char *string;
struct LIST *next;
};
struct LIST *list = NULL;
struct LIST *head = NULL;
struct LIST *keylist = NULL;
struct LIST *keyhead = NULL;
struct TABENTRY { /* may have 3 column tables */
struct TABENTRY *next ;
char col[3][256] ;
} ;
struct TABENTRY table = {NULL} ;
struct TABENTRY *tableins = &table ;
int tablecols = 0 ;
int tablewidth[3] = {0,0,0} ;
int tablelines = 0 ;
int debug = FALSE;
void parse(FILE *a, FILE *b);
int lookup(char *s);
void refs(int l, FILE *f);
void convert(FILE *a, FILE *b);
void process_line(char *line, FILE *b);
int main(int argc, char **argv)
{
FILE * infile;
FILE * outfile;
FILE * idxfile = (FILE *)NULL;
if (argv[argc-1][0]=='-' && argv[argc-1][1]=='d') {
debug = TRUE;
argc--;
}
if ( (argc > 4) || (argc == 1) ) {
fprintf(stderr,"Usage: %s infile outfile [idxfile]\n", argv[0]);
return(1);
}
if ( (infile = fopen(argv[1],"r")) == (FILE *)NULL) {
fprintf(stderr,"%s: Can't open %s for reading\n",
argv[0], argv[1]);
return(1);
}
if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
fprintf(stderr,"%s: Can't open %s for writing\n",
argv[0], argv[2]);
}
if (argc==4) {
if ( (idxfile = fopen(argv[3],"w")) == (FILE *)NULL) {
fprintf(stderr,"%s: Can't open %s for writing\n",
argv[0], argv[3]);
}
}
parse(infile, idxfile);
convert(infile,outfile);
return(0);
}
/* scan the file and build a list of line numbers where particular levels are */
void parse(FILE *a, FILE *b)
{
static char line[MAX_LINE_LEN];
char *c;
int lineno=0;
int lastline=0;
char *lasttopic=NULL;
static char *idxhead ="\
/* generated by doc2ipf.c */\n\
RCDATA IDR_HELP\n\
BEGIN\n";
/* ignore title line */
fgets(line,MAX_LINE_LEN,a);
if (b) {
fprintf(b,idxhead);
}
while (fgets(line,MAX_LINE_LEN,a))
{
lineno++;
if (isdigit((int)(line[0])))
{
if (list == NULL)
head = (list = (struct LIST *) malloc(sizeof(struct LIST)));
else
list = (list->next = (struct LIST *) malloc(sizeof(struct LIST)));
list->line = lastline = lineno;
list->level = line[0] - '0';
list->string = (char *) malloc (strlen(line)+1);
c = strtok(&(line[1]),"\n");
strcpy(list->string, c);
lasttopic = list->string;
list->next = NULL;
if (b)
fprintf(b, " %d, \042%s\042,\n", lineno, c);
}
if (line[0]=='?')
{
if (keylist == NULL)
keyhead = (keylist = (struct LIST *) malloc(sizeof(struct LIST)));
else
keylist = (keylist->next = (struct LIST *) malloc(sizeof(struct LIST)));
keylist->line = lastline;
keylist->level = line[0] - '0';
c = strtok(&(line[1]),"\n");
if( c == NULL || *c == '\0' ) c = list->string ;
keylist->string = (char *) malloc (strlen(c)+1);
strcpy(keylist->string, c);
if (b) {
if (lasttopic && (strcmp(lasttopic, c)!=0))
fprintf(b, " %d, \042%s\042,\n", lastline, c);
}
keylist->next = NULL;
}
}
if (b)
fprintf(b, " 0, \042\042\nEND\n");
rewind(a);
}
/* look up an in text reference */
int
lookup(char *s)
{
char *c;
char tokstr[MAX_LINE_LEN];
strcpy(tokstr, s);
/* first try the ? keyword entries */
keylist = keyhead;
while (keylist != NULL)
{
c = keylist->string;
while (isspace((int)(*c))) c++;
if (!strcmp(s, c)) return(keylist->line);
keylist = keylist->next;
}
/* then try titles */
#ifdef GNUPLOT
match = strtok(tokstr, " \n\t");
l = 0; /* level */
list = head;
while (list != NULL)
{
c = list->string;
while (isspace(*c)) c++;
if (!strcmp(match, c))
{
l = list->level;
match = strtok(NULL, "\n\t ");
if (match == NULL)
{
return(list->line);
}
}
if (l > list->level)
break;
list = list->next;
}
#else
/* we list keys explicitly, rather than building them from multiple levels */
list = head;
while (list != NULL)
{
c = list->string;
while (isspace((int)(*c))) c++;
if (!strcmp(s, c)) return(list->line);
list = list->next;
}
#endif
return(-1);
}
/* search through the list to find any references */
void
refs(int l, FILE *f)
{
int curlevel;
char *c;
int inlist = FALSE;
/* find current line */
list = head;
while (list->line != l)
list = list->next;
curlevel = list->level;
list = list->next; /* look at next element before going on */
if (list != NULL)
{
inlist = TRUE;
fprintf(f,":sl compact.\n");
}
while (list != NULL)
{
/* we are onto the next topic so stop */
if (list->level == curlevel)
break;
/* these are the next topics down the list */
if (list->level == curlevel+1)
{
c = list->string;
while (isspace((int)(*c))) c++;
fprintf(f,":li.:link reftype=hd res=%d.%s:elink.\n", list->line, c);
}
list = list->next;
}
if (inlist)
fprintf(f,":esl.\n");
}
void
convert(FILE *a, FILE *b)
{
static char line[MAX_LINE_LEN];
/* generate ipf header */
fprintf(b,":userdoc.\n:prolog.\n");
fgets(line,MAX_LINE_LEN,a);
fprintf(b,":title.%s", line+1);
fprintf(b,":docprof toc=1234.\n:eprolog.\n");
/* process each line of the file */
while (fgets(line,MAX_LINE_LEN,a)) {
process_line(line, b);
}
/* close final page and generate trailer */
fprintf(b,"\n:euserdoc.\n");
}
void
process_line(char *line, FILE *b)
{
static int line_count = 0;
static char line2[MAX_LINE_LEN];
static int last_line;
char hyplink1[64] ;
char *pt, *tablerow ;
int i;
int j;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -