ftp.nice.ch/users/felix/rtf2asc.c

This is rtf2asc.c in view mode; [Download] [Up]

/*
	rtf2ascii.c - convert rtf to ascii
	coded in 1994 by Felix Rauch
	felix@nice.ch
*/

#include <stdio.h>

FILE *in;

void scanblock(void)	/* scan a block in `{}' */
{
    int c;
    while(!feof(in) && ((c = fgetc(in)) != '}')) {
	if(c == '{')
	    scanblock();
    }
}

int scanpart(void)	/* return 1 if text follows, otherwise 0 */
{
    int c;
    while(!feof(in)) {
	c = fgetc(in);
	if(c == ' ')	/* the rest until '\n' is test */
	    return 1;
	else if(c == '\n')	/* end of command-part */
	    return 0;
	else if(c == '\{')	/* skip whole block */
	    scanblock();
    }
    return 0;	/* not reached if there are no errors */
}

void scantext(void)	/* scan text until '\n' */
{
    int c;
    while(!feof(in) && ((c = fgetc(in)) != '\n')) {
	if(c == '\\')	/* escape char */
	    c = fgetc(in);
	fputc(c, stdout);
    }
}

void main(int argc, char *argv[])
{
    int i, c, ret;
    char rtfc[7] = "{\\rtf0";
    
    if(argc == 1)
	in = stdin;
    else if(argc == 2)
	in = fopen(argv[1], "r");
    for(i = 0; i < 6; i++) {		/* rtf-magic-cookie test */
	if(fgetc(in) != rtfc[i]) {
	    puts("not in rtf-format");
	    exit(1);
	}
    }
    while(!feof(in)) {
    	ret = 2;	/* no command part or block found yet */
	c = fgetc(in);
	if(c == '\\') {
	    ret = scanpart();	/* command part */
	}
	else if(c == '\{') {	/* block found */
	    scanblock();
	    ret = 0;
	    if((c = fgetc(in)) != '\n') /* a '\n' after a block must be ignored */
		ungetc(c, in);
	}
	else if(c == '}') {	/* end of rtf-stream */
	    break;
	}
	if(ret > 0) {	/* if there's text after a command part or just text */
	    if(ret == 2)	/* if just text, the print first char */
		fputc(c, stdout);  /* (otherwise it's a '//' */
	    scantext();		/* text block */
	}
    }
    exit(0);		/* that's all, dude */
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.