Image

Imagelightning_rose wrote in Imagecpp

Converting source to html

The day after Turkey Day was a do-nothing sort of day so I decided to knock out a simple program that I've been wanting to write for a while.

This will convert any (I think) text file to html, preserving the formatting. It's command line driven and writes to stdout, so you need to re-direct or append the output to another file. It takes an optional command line parameter to change the tab width from the default of 8.

I have compiled & tested with gcc under linux and both Dev-C++ and VC++ 6.0 under MS-Win.

There are some online conversion programs, but I like having a command line driven alternative.

Oh, if anybody sees any special chars missing from my conversion table, I'm sure you'll bring it to my attention. :)

(x-posted elsewhere, I'm sorry if it clogs your friends list)





/*****************************************************************************
**                                                                          **
**             Copyright (C) Lorraine Nicole Reed, 1985 thru 2004           **
**               Released Under The GNU General Public License.             **
**                                                                          **
*****************************************************************************/

#define     VERSION     "Convert Text to HTML v1.00"

#include    <stdio.h>
#include    <stdlib.h>
#include    <stdarg.h>

/**********************************************************************/

/* 
** FORTRAN Style Logical Operators 
**
** If you don't like it, *Bite Me*!
*/

#define     EQ          ==
#define     NE          !=
#define     LT          <
#define     GT          >
#define     LE          <=
#define     GE          >=
#define     NOT         !
#define     OR          ||
#define     AND         &&

/**********************************************************************/

typedef     unsigned char   uchar;

struct  conversionRecord
{
     uchar  item;
     char   *substituteText;
};

static const struct conversionRecord conversionTable[]=
{
     { '\"',    "&quot;" },
     { '<',     "&lt;"   }, 
     { '>',     "&gt;"   },
     { '&',     "&amp;"  },
     { 0x00,    NULL     }
};

/**********************************************************************/

static  void    printChar( uchar data )
{
     const struct conversionRecord    *tablePtr;

     tablePtr   = conversionTable;

     while ( tablePtr->item )
     {
          if ( data EQ tablePtr->item )
          {
               printf( tablePtr->substituteText );
               return;
          }

          tablePtr++;
     }

     putchar( data );
}

/**********************************************************************/

static void    bugOut( int errorCode, char *formatString, ... )
{
     va_list args;

     fflush( stdout );

     va_start( args, formatString );

     vfprintf( stderr, formatString, args );

     va_end( args );

     exit( errorCode );
}

/**********************************************************************/

static  void    usage( char *progName )
{
    printf( "\n%s\n", VERSION );
    bugOut( -1, "\n   Usage is: %s [ TAB_WIDTH ] FILENAME \n\n", progName );
}

/**********************************************************************/

static  void    badFname( char *fname )
{
    printf( "\n%s\n", VERSION );
    bugOut( -1, "\n Can't fopen( %s )\n\n", fname );
}

/**********************************************************************/

static  void    printHeader( char *title )
{
     printf( "<html>\n" );
     printf( "<title>%s</title>\n", title );

     printf( "<pre><code>\n" );
}

/**********************************************************************/

static  void    printTail( void )
{
    printf( "</pre></code>\n" );
    printf( "</html>\n" );
}

/**********************************************************************/

static  void    putSpaces( int nSpaces )
{
     int    i;

     for ( i = 0; i LT nSpaces; i++ )
          putchar( 0x20 );
}

/**********************************************************************/

int     main( int argc, char **argv )
{
    FILE    *fp;
    char    ch;
    int     nSpaces, chIndex = 0, tabWidth = 8;

    if (( argc LT 2 )
    OR  ( argc GT 3 ))
        usage( *argv );

    if ( argc EQ 3 )
         tabWidth   = atoi( *++argv );

    if (( fp = fopen( *++argv, "r" )) EQ NULL )
        badFname( *argv );

    printHeader( *argv );

    while(( ch  = fgetc( fp )) NE EOF )
    {
         switch( ch )
         {
              case  0x0A:           // Unix, QNX-4 or MS-DOS Eoline
              case  0x1E:           // QNX-2 Eoline
                   putchar( '\n' );
                   chIndex  = 0;
                   break;

              case  0x0D:           // First MS-DOS Eoline
                   break;

              case  '\t':           // Convert TAB to spaces
                   nSpaces  =  tabWidth - ( chIndex % tabWidth );
                   chIndex  += nSpaces;

                   putSpaces( nSpaces );
                   break;

              default:
                   chIndex++;
                   printChar( (uchar) ch );
         }
    }

    fclose( fp );

    printTail();

    return( 0 );
}

/* EoFile *************************************************************/
/**********************************************************************/