/*-Header---------------------------------------------------------------------
CSV2HTML - Converts CSV files to HTML tables.
Copyright (C) 1999 Jonathan Combe
Ported to AmigaOS by Chris De Maeyer

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the Licence, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Publi License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

The license file is in the file LICENSE supplied in the archive.

See the file INSTALL for more information on any issues of compilation
and installation into a UNIX system.

This is the source code for version 1.0

Jonathan Combe 26/09/1999
email : joncombe@yahoo.com


Chris De Maeyer 18/10/1999
email : ceppe@pandora.be
*/

/* Uncomment this if you are using a compiler such as CC under IRIX, which
   doesn't support BOOL's.
*/

enum BOOL {FALSE=0,TRUE=1};

/* Include files */

#include <iostream.h>
#include <string.h>
#include <fstream.h>

/* Global Variables */

char version[100]; /* Version string */

/*-Process Files function----------------------------------------------------- 

   Version 1.0

   Processes the files and writes html file

   Returns: - 

   0 for OK. 
   1 for invalid csv file
   2 for invalid HTML output file
   3 for process error
*/

int ProcessFiles(char* csvfile, char* htmlfile)
{

  /* Declare file streams */

  ifstream csv;  // Input CSV File
  ofstream html; // Output HTML file
  char read; // For reading in the file charachter by charachter

  BOOL trflag=FALSE; // Flag for weather to write TR or not
  BOOL tdflag=FALSE; // Says whether to create new TD or not
  BOOL newlineflag=FALSE; // Set to TRUE when new line
  BOOL quoteflag=FALSE; // Stroes wheteher we are in quotes or not

  /* Open CSV File */

  csv.open(csvfile,ios::in);

  if (!csv.good()) /* File failed to open */
  {
    return 1; /* Return failure */
  }

  /* CSV File opened OK so open output file */ 

  html.open(htmlfile,ios::out);

  if (!html.good()) /* File open failed */
  {
    return 2; /* Failed */
  }

  /* Both files opened OK so we can proceed! */

  /* Initialise HTML File */

  html << "<HTML>\n";
  html << "<TABLE BORDER=1>\n";
  
  /* Start main loop */

  html << "<TR>\n<TD>";
  trflag=FALSE;

  while (!csv.eof())
  {
    csv.get(read); /* Read in charachter */
    
    /* If we are starting a quote this sets to ignore any commas until
       the quote is closed. This is beacuse if a .CSV file is generated
       from data containg a comma the data will be put in quotes. This
       flag is also to stop the quote being written into the HTML file.
    */

    if (read=='"')
    {
      if (quoteflag==FALSE)
      {
        quoteflag=TRUE;
      }
      else
      {
        quoteflag=FALSE;
      }
    }

    if (read=='\n') /* Special Case */
    {
      if (tdflag==TRUE)
      {
        html << "<TD>";
        tdflag=FALSE;
      }
      trflag=TRUE;
      newlineflag=TRUE;
    }
    else if ((read==',') && (quoteflag==FALSE))
    {
      if (newlineflag==TRUE)
      {
        html << "</TD>\n</TR>";
        newlineflag=FALSE;
      }
      if ((tdflag==TRUE) && (trflag==FALSE))
      {
        html << "<TD>";
        tdflag=FALSE;
      }
      if (trflag==TRUE)
      {
        html << "\n<TR>\n<TD>";
        trflag=FALSE;
      }
      html << "</TD>";
      tdflag=TRUE;
    }
    else /* Normal Charachter */
    {
      if (newlineflag==TRUE)
      {
        html << "</TD>\n</TR>";
        newlineflag=FALSE;
      }
      if ((trflag==FALSE) && (tdflag==TRUE))
      {
        html << "<TD>";
        tdflag=FALSE;
      }
      if (trflag==TRUE)
      {
        html << "\n<TR>\n<TD>";
        trflag=FALSE;
      }
      if (read!='"')
      {
        html << read;
      }
    }
  }
  html << "</TD>\n</TR>\n</TABLE>\n</HTML>";
  csv.close();
  html.close();
  return 0; // Conversion was succesful
}


/*-checkhelp function---------------------------------------------------------

   Checks if help was called (--help parameter)

   Returns TRUE for yes, FALSE for no
*/

BOOL checkhelp(char* csvfile)
{
  if (strcmp(csvfile,"--help")==0)
  {
    /* Help asked for */
    cout << "csv2html <csvfile> <htmloutputfile>\n\nVersion " << version <<"\n\nConverts a .CSV file to an HTML table. <csvfile> is the .csv file and <htmloutputfile> is the HTML file to write.\n";
    return TRUE;
  }
  else
  {
    return FALSE;
  }
}

/*-checkversion function------------------------------------------------------
*/

/* Function as above but checks for --version instead */

BOOL checkversion(char* csvfile)
{
        if (strcmp(csvfile,"--version")==0)
        {
                /* Version number requested */
                cout << "Version " << version <<".\n";
                return TRUE;
        }
        else
        {
                return FALSE;
        }
}

/*-main function--------------------------------------------------------------
*/

/* main. Includes parameters of csv filename and html filename (outfile).
*/

void main(int argv,char * argc[])
{

  /* Set up version string */

  strcpy(version,"1.0");

  /* argv = number of parameters 
     argc = parameters
  */
 
  int returned=0; /* Takes return value from process */

  if (argv>3) /* More than two parameters so display warning! */
  {
    cerr << "Warning : additional parameters ignored!\n";
  }

  if (argv<3) /* Less than 3 parameters so error! */
  {
    if (argv==2) /* Check for help */
    {
      if (checkhelp(argc[1])==TRUE)
      {
        exit(0);
      }
      else if (checkversion(argc[1])==TRUE)
      {
        exit(0);
      }
      else
      {
        cerr << "csv2html: Misssing file name\nUse csv2html <csvfile> <htmloutputfile> or csv2html --help for help\n";
        exit(0);
      }
    }
    else
    {
      cerr << "csv2html: Misssing file name\nUse csv2html <csvfile> <htmloutputfile> or csv2html --help for help\n";
      exit(0);
    }  
  }
 
  /* Parameters are correct and we have got file names so start processing! */
 
  returned=ProcessFiles(argc[1],argc[2]);
  /* Check for status before quitting */
  if (returned==0) /* OK */
  {
  }
  if (returned==1) /* Invalid CSV File */
  {
    cerr << "Invalid csv file - check the file name exists!\n";
  }
  if (returned==2) /* Invalid HTML File */
  {
    cerr << "Cannot create output file - check if the name exists and that\nyou have write permission to the directory.\n";
  }
}
