/***************************************************************************
                          xls_read.cpp  -  description
                             -------------------
    begin                : Tue Dec 14 1999
    copyright            : (C) 1999-2006 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#if __BORLANDC__ > 0x500
#include <condefs.h>
#endif

#include <stdio.h>

#include <sptk3/excel/CExcelBook.h>
#include <sptk3/CDateTime.h>
#include <sptk3/CException.h>
#include <sptk3/CFieldList.h>

using namespace std;
using namespace sptk;

// Demo callback function
void cb_readProgress(const char* operation,int percentage) {
   fprintf(stdout,"\r%-20s %i%%         ",operation,percentage);
   fflush(stdout);
}

int main(int argc, char* argv[]) {
#ifdef _WIN32
   string  fileName = "..\\examples\\xls_read.xls";
#else
   string  fileName = "../examples/xls_read.xls";
#endif

   if (argc < 2) {
      puts("Demo Excel decoder.");
      puts("Usage: ");
      puts("  xls_read <filename>.xls");
      puts("  xls_read <filename>.csv");
      puts("Reads an Excel workbook file (Excel 95,97) and writes several");
      puts("text files with the sheet data.\n");
      puts("Since you haven't provided the Excel file name as the command-line argument,");
      puts("the test file 'xls_read.xls' will be used.\n");
   } else {
      fileName = argv[1];
   }

   CExcelBook  excelBook;
   excelBook.callback(cb_readProgress);
   try {
      puts(("Processing file "+fileName+".").c_str());

      if ((unsigned)fileName.find(".csv") == fileName.length()-4)
         excelBook.readCSVFile(fileName);
      else excelBook.readExcelFile(fileName);

      puts("\nPlacing the data into '_results.txt' file.");

      FILE *f = fopen("_results.txt","wb");
      for (int i = 0; i < (int)excelBook.sheets.size(); i++) {
         CExcelSheet *sheet = excelBook.sheets[i];

         CExcelRows&  rows = sheet->rows;
         fprintf( f, "\n------------ %s -------------- \n", sheet->name().c_str() );
         for (int r = 0; r < (int)rows.size(); r++) {
            for (int c = 0; c < (int)rows[r].size(); c++) {
               try {
                  fprintf( f, "%30s", rows[r][c].asString().c_str() );
               }
               catch (exception& e) {
                  puts(e.what());
               }
            }
            fprintf( f, "\n" );
         }
      }
      fclose(f);
   }
   catch (exception& e) {
      puts(e.what());
   }

   return 0;
}
