📄 doc2xml.c
字号:
/*
* doc2xml.c -- program to convert Gnuplot .DOC format to
* eXtensible Markup Language
*
* Created by Russell Lang from doc2html
*
* usage: doc2xml gsviewen.txt gsviewen.xml
*
*/
#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;
int debug = FALSE;
int nolinks = FALSE;
void parse(FILE *a);
int lookup(char *s);
char *title_from_index(int id);
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;
if (argv[argc-1][0]=='-' && argv[argc-1][1]=='d') {
debug = TRUE;
argc--;
}
if (argv[argc-1][0]=='-' && argv[argc-1][1]=='n') {
nolinks = TRUE;
argc--;
}
if ( (argc > 3) || (argc == 1) ) {
fprintf(stderr,"Usage: %s infile outfile\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 (argc == 3) {
if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
fprintf(stderr,"%s: Can't open %s for writing\n",
argv[0], argv[2]);
}
}
else {
outfile = stdout;
}
parse(infile);
convert(infile,outfile);
return(0);
}
/* scan the file and build a list of line numbers where particular levels are */
void parse(FILE *a)
{
static char line[MAX_LINE_LEN];
char *c;
int lineno=0;
int lastline=0;
/* skip title line */
fgets(line,MAX_LINE_LEN,a);
while (fgets(line,MAX_LINE_LEN,a))
{
lineno++;
if (isdigit(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);
list->next = NULL;
}
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);
keylist->next = NULL;
}
}
rewind(a);
}
/* look up an in text reference */
int
lookup(char *s)
{
char *c;
char tokstr[MAX_LINE_LEN];
char *match;
int l;
strcpy(tokstr, s);
/* first try the ? keyword entries */
keylist = keyhead;
while (keylist != NULL)
{
c = keylist->string;
while (isspace(*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(*c)) c++;
if (!strcmp(s, c)) return(list->line);
list = list->next;
}
#endif
return(-1);
}
char *title_from_index(int id)
{
struct LIST *l = NULL;
if (id < 0)
return "";
l = head;
while (l != NULL) {
if (id == l->line)
return l->string;
l = l->next;
}
return NULL;
}
#ifdef NOT_USED
/* search through the list to find any references */
void
refs(int l, FILE *f)
{
int curlevel;
char str[MAX_LINE_LEN];
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;
if (inbody)
fprintf(f,"</body>\n");
inbody = 0;
}
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(*c)) c++;
if (nolinks)
fprintf(f,"<b>%s</b><br></br>\n", c);
else {
char *p = c;
fprintf(f,"<link target=\042");
while (*p) {
if (*p == ' ')
fputc('_', f);
else
fputc(*p, f);
p++;
}
fprintf(f,"\042>%s</link>\n", c);
}
}
list = list->next;
}
if (inlist)
fprintf(f,"<P>\n");
}
#endif
void
convert(FILE *a,FILE *b)
{
static char line[MAX_LINE_LEN];
/* generate html header */
fprintf(b,"<?xml version=\0421.0\042?>\n");
fprintf(b,"<help>\n");
fgets(line,MAX_LINE_LEN,a);
strtok(line, "\n");
fprintf(b,"<helptitle>%s</helptitle>\n", line+1);
/* process each line of the file */
while (fgets(line,MAX_LINE_LEN,a)) {
process_line(line, b);
}
/* close final page and generate trailer */
strcpy(line, "0\n");
process_line(line, b);
fprintf(b,"\n</help>");
}
#define PLATFORM_NONE 0
#define PLATFORM_WIN 1
#define PLATFORM_OS2 2
#define PLATFORM_WINOS2 3
#define PLATFORM_X11 4
void
end_platform(int platform, FILE *b)
{
if (platform != PLATFORM_NONE) {
switch (platform) {
case PLATFORM_WIN:
fputs("</windows>\n", b);
break;
case PLATFORM_OS2:
fputs("</os2>\n", b);
break;
case PLATFORM_WINOS2:
fputs("</winos2>\n", b);
break;
case PLATFORM_X11:
fputs("</x11>\n", b);
break;
}
}
}
void
start_platform(int platform, FILE *b)
{
switch (platform) {
case PLATFORM_WIN:
fputs("<windows>\n", b);
break;
case PLATFORM_OS2:
fputs("<os2>\n", b);
break;
case PLATFORM_WINOS2:
fputs("<winos2>\n", b);
break;
case PLATFORM_X11:
fputs("<x11>\n", b);
break;
}
}
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;
static int startpage = 1;
char str[MAX_LINE_LEN];
char topic[MAX_LINE_LEN];
int k, l;
static int tabl=0;
static int para=0;
static int inquote = FALSE;
static int inref = FALSE;
static int prev_level = 0;
int new_level = 0;
static int inbody = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -