- Main Page
- Namespaces
- Data Structures
- Files

# 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           return retval;
    00117         }
    00118       else
    00119         {
    00120           do
    00121             {
    00122               input_file.getline(buffer, sizeof(buffer),'\n');
    00123               //cout<<buffer<<"\n";
    00124               s_buffer.assign(buffer);
    00125               //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
    00126               if(input_file.gcount()<(sizeof(buffer)-1))
    00127                 {
    00128                   s_buffer.append("\n");//Just in case...
    00129                 }
    00130               else if( !input_file.eof()&&input_file.fail())
    00131                 {
    00132                   input_file.clear();
    00133                 }
    00134
    00135               if(s_buffer.find("<OFX>")!=string::npos||s_buffer.find("<ofx>")!=string::npos)
    00136                 {
    00137                   message_out(DEBUG,"libofx_detect_file_type():<OFX> tag has been found");
    00138                   retval=OFX;
    00139                   type_found=true;
    00140                 }
    00141               else if(s_buffer.find("<OFC>")!=string::npos||s_buffer.find("<ofc>")!=string::npos)
    00142                 {
    00143                   message_out(DEBUG,"libofx_detect_file_type():<OFC> tag has been found");
    00144                   retval=OFC;
    00145                   type_found=true;
    00146                 }
    00147
    00148             }while(type_found==false&&!input_file.eof()&&!input_file.bad());
    00149         }
    00150       input_file.close();
    00151     }
    00152   else{
    00153     message_out(ERROR,"libofx_detect_file_type(): No input file specified");
    00154   }
    00155   if(retval==UNKNOWN)
    00156     message_out(ERROR,"libofx_detect_file_type(): Failed to identify input file format");
    00157   return retval;
    00158 }
    00159
    00160
    00161
    00162
    00163