john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

text to html txt2htm

/* 
john pfeiffer 08oct07 txt2htm  
input is any file with name less than 64 chars, 
output is inputfilename.htm 
replaces < or > with & #60 or & #62 (see http://www.w3.org/MarkUp/html-spec/html-spec_13.html)
TODO: make all file pointers pass by reference, add a <noads> tag, add a <title> tag
*/

#include <stdio.h>

void open_infile(FILE** ifpByRef, char* argv[])
{
    if( strlen( argv[1] ) > 64 )
    {   printf("filename too large!");
        exit(1);
    }

    *ifpByRef = fopen(argv[1],"r");

    if( !(*ifpByRef) )
    {   printf("infile open error!");
        exit(1);
    }else{    printf("opening %s\n",argv[1]);   }

}/* end function tricky pass by reference of a FILE struct (_iobuf) */



FILE* open_outfile(char* argv[], FILE* ofp)
{
    char ofname[40];

    strcpy(ofname,argv[1]);
    strcat(ofname,".htm");

    ofp = fopen( ofname,"w" );
    if( ofp == NULL )
    {   printf("outfile open error!");
        exit(1);
    }else{    printf("creating %s\n",ofname);   }

    return ofp;
}/* end open_outfile func */



int main(int argc, char* argv[])
{
    FILE* ifp = NULL;
    FILE* ofp = NULL;
    char c;

    if( (argc < 2) || (argc > 3))       /* argc=1, argv[0] holds program name */
    {   printf("wrong number of arguments");
        exit(1);
    }else{      printf("%s is still in progress...\n",argv[0]);   }

    open_infile(&ifp, argv);
    ofp = open_outfile(argv, ofp);

    c = fgetc(ifp);
    fputs("<pre>", ofp);        /* forget the case of an empty file */

    while( c != EOF )
    {
        printf("%c",c);

        if( (c != '<') && (c != '>') )
        {
            fputc(c,ofp);
        }
        else if( c == '<' )
        {   fputs("<",ofp);   }
        else if( c == '>' )
        {   fputs(">",ofp);   }

        c = fgetc(ifp);
    }
    fputs("</pre>", ofp);

    fclose(ifp);
    fclose(ofp);

    return 0;
}/* end of main */

  • « file size ftell
  • tar compress extract gpg encrypt a file decompress bzip2 zip unzip »

Published

Oct 7, 2008

Category

c

~190 words

Tags

  • c 95
  • txt2htm 1