LibOFX
file_preproc.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002           file_preproc.cpp
00003                              -------------------
00004     copyright            : (C) 2004 by Benoit Grégoire
00005     email                : benoitg@coeus.ca
00006 ***************************************************************************/
00012 /***************************************************************************
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  ***************************************************************************/
00020 #include <iostream>
00021 #include <fstream>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <string>
00025 #include "libofx.h"
00026 #include "messages.hh"
00027 #include "ofx_preproc.hh"
00028 #include "context.hh"
00029 #include "file_preproc.hh"
00030 
00031 using namespace std;
00032 const unsigned int READ_BUFFER_SIZE = 1024;
00033 
00034 /* get_file_type_description returns a string description of a LibofxFileType
00035  * suitable for debugging output or user communication.
00036  */
00037 const char * libofx_get_file_format_description(const struct LibofxFileFormatInfo format_list[], enum LibofxFileFormat file_format)
00038 {
00039   const char * retval = "UNKNOWN (File format couldn't be successfully identified)";
00040 
00041   for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
00042   {
00043     if (LibofxImportFormatList[i].format == file_format)
00044     {
00045       retval = LibofxImportFormatList[i].description;
00046     }
00047   }
00048   return retval;
00049 };
00050 
00051 /*
00052 libofx_get_file_type returns a proper enum from a file type string.
00053 */
00054 enum LibofxFileFormat libofx_get_file_format_from_str(const struct LibofxFileFormatInfo format_list[], const char * file_type_string)
00055 {
00056   enum LibofxFileFormat retval = UNKNOWN;
00057   for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
00058   {
00059     if (strcmp(LibofxImportFormatList[i].format_name, file_type_string) == 0)
00060     {
00061       retval = LibofxImportFormatList[i].format;
00062     }
00063   }
00064   return retval;
00065 }
00066 
00067 int libofx_proc_file(LibofxContextPtr p_libofx_context, const char * p_filename, LibofxFileFormat p_file_type)
00068 {
00069   LibofxContext * libofx_context = (LibofxContext *) p_libofx_context;
00070 
00071   if (p_file_type == AUTODETECT)
00072   {
00073     message_out(INFO, string("libofx_proc_file(): File format not specified, autodetecting..."));
00074     libofx_context->setCurrentFileType(libofx_detect_file_type(p_filename));
00075     message_out(INFO, string("libofx_proc_file(): Detected file format: ") +
00076                 libofx_get_file_format_description(LibofxImportFormatList,
00077                     libofx_context->currentFileType() ));
00078   }
00079   else
00080   {
00081     libofx_context->setCurrentFileType(libofx_detect_file_type(p_filename));
00082     message_out(INFO,
00083                 string("libofx_proc_file(): File format forced to: ") +
00084                 libofx_get_file_format_description(LibofxImportFormatList,
00085                     libofx_context->currentFileType() ));
00086   }
00087 
00088   switch (libofx_context->currentFileType())
00089   {
00090   case OFX:
00091     ofx_proc_file(libofx_context, p_filename);
00092     break;
00093   case OFC:
00094     ofx_proc_file(libofx_context, p_filename);
00095     break;
00096   default:
00097     message_out(ERROR, string("libofx_proc_file(): Detected file format not yet supported ou couldn't detect file format; aborting."));
00098   }
00099   return 0;
00100 }
00101 
00102 enum LibofxFileFormat libofx_detect_file_type(const char * p_filename)
00103 {
00104   enum LibofxFileFormat retval = UNKNOWN;
00105   ifstream input_file;
00106   char buffer[READ_BUFFER_SIZE];
00107   string s_buffer;
00108   bool type_found = false;
00109 
00110   if (p_filename != NULL && strcmp(p_filename, "") != 0)
00111   {
00112     message_out(DEBUG, string("libofx_detect_file_type():Opening file: ") + p_filename);
00113 
00114     input_file.open(p_filename);
00115 
00116     if (!input_file)
00117     {
00118       message_out(ERROR, "libofx_detect_file_type():Unable to open the input file " + string(p_filename));
00119       return retval;
00120     }
00121     else
00122     {
00123       do
00124       {
00125         input_file.getline(buffer, sizeof(buffer), '\n');
00126         //cout<<buffer<<"\n";
00127         s_buffer.assign(buffer);
00128         //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
00129         if (input_file.gcount() < (sizeof(buffer) - 1))
00130         {
00131           s_buffer.append("\n");//Just in case...
00132         }
00133         else if ( !input_file.eof() && input_file.fail())
00134         {
00135           input_file.clear();
00136         }
00137 
00138         if (s_buffer.find("<OFX>") != string::npos || s_buffer.find("<ofx>") != string::npos)
00139         {
00140           message_out(DEBUG, "libofx_detect_file_type():<OFX> tag has been found");
00141           retval = OFX;
00142           type_found = true;
00143         }
00144         else if (s_buffer.find("<OFC>") != string::npos || s_buffer.find("<ofc>") != string::npos)
00145         {
00146           message_out(DEBUG, "libofx_detect_file_type():<OFC> tag has been found");
00147           retval = OFC;
00148           type_found = true;
00149         }
00150 
00151       }
00152       while (type_found == false && !input_file.eof() && !input_file.bad());
00153     }
00154     input_file.close();
00155   }
00156   else
00157   {
00158     message_out(ERROR, "libofx_detect_file_type(): No input file specified");
00159   }
00160   if (retval == UNKNOWN)
00161     message_out(ERROR, "libofx_detect_file_type(): Failed to identify input file format");
00162   return retval;
00163 }
00164 
00165 
00166 
00167 
00168