LibOFX: file_preproc.cpp Source File

Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

# 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                : bock@step.polymtl.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 sucessfully 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 CFCT 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, autodecting..."));
    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: ofx_proc_file(libofx_context, p_filename);
    00091       break;
    00092     case OFC: ofx_proc_file(libofx_context, p_filename);
    00093       break;
    00094     default: message_out(ERROR, string("libofx_proc_file(): Detected file format not yet supported ou couldn't detect file format; aborting."));
    00095     }
    00096   return 0;
    00097 }
    00098
    00099 enum LibofxFileFormat libofx_detect_file_type(const char * p_filename)
    00100 {
    00101   enum LibofxFileFormat retval = UNKNOWN;
    00102   ifstream input_file;
    00103   char buffer[READ_BUFFER_SIZE];
    00104   string s_buffer;
    00105   bool type_found=false;
    00106
    00107   if(p_filename!=NULL&&strcmp(p_filename,"")!=0)
    00108     {
    00109       message_out(DEBUG, string("libofx_detect_file_type():Opening file: ")+p_filename);
    00110
    00111       input_file.open(p_filename);
    00112
    00113       if(!input_file)
    00114         {
    00115           message_out(ERROR,"libofx_detect_file_type():Unable to open the input file "+string(p_filename));
    00116         }
    00117       else
    00118         {
    00119           do
    00120             {
    00121               input_file.getline(buffer, sizeof(buffer),'\n');
    00122               //cout<<buffer<<"\n";
    00123               s_buffer.assign(buffer);
    00124               //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
    00125               if(input_file.gcount()<(sizeof(buffer)-1))
    00126                 {
    00127                   s_buffer.append("\n");//Just in case...
    00128                 }
    00129               else if( !input_file.eof()&&input_file.fail())
    00130                 {
    00131                   input_file.clear();
    00132                 }
    00133
    00134               if(s_buffer.find("<OFX>")!=string::npos||s_buffer.find("<ofx>")!=string::npos)
    00135                 {
    00136                   message_out(DEBUG,"libofx_detect_file_type():<OFX> tag has been found");
    00137                   retval=OFX;
    00138                   type_found=true;
    00139                 }
    00140               else if(s_buffer.find("<OFC>")!=string::npos||s_buffer.find("<ofc>")!=string::npos)
    00141                 {
    00142                   message_out(DEBUG,"libofx_detect_file_type():<OFC> tag has been found");
    00143                   retval=OFC;
    00144                   type_found=true;
    00145                 }
    00146
    00147             }while(type_found==false&&!input_file.eof()&&!input_file.bad());
    00148         }
    00149       input_file.close();
    00150     }
    00151   else{
    00152     message_out(ERROR,"libofx_detect_file_type(): No input file specified");
    00153   }
    00154   if(retval==UNKNOWN)
    00155     message_out(ERROR,"libofx_detect_file_type(): Failed to identify input file format");
    00156   return retval;
    00157 }
    00158
    00159
    00160
    00161
    00162