LibOFX
file_preproc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  file_preproc.cpp
3  -------------------
4  copyright : (C) 2004 by Benoit GrĂ©goire
5  email : benoitg@coeus.ca
6 ***************************************************************************/
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 #include <iostream>
21 #include <fstream>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string>
25 #include "libofx.h"
26 #include "messages.hh"
27 #include "ofx_preproc.hh"
28 #include "context.hh"
29 #include "file_preproc.hh"
30 
31 const unsigned int READ_BUFFER_SIZE = 1024;
32 
33 /* get_file_type_description returns a string description of a LibofxFileType
34  * suitable for debugging output or user communication.
35  */
36 const char * libofx_get_file_format_description(const struct LibofxFileFormatInfo format_list[], enum LibofxFileFormat file_format)
37 {
38  const char * retval = "UNKNOWN (File format couldn't be successfully identified)";
39 
40  for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
41  {
42  if (LibofxImportFormatList[i].format == file_format)
43  {
44  retval = LibofxImportFormatList[i].description;
45  }
46  }
47  return retval;
48 }
49 
50 /*
51 libofx_get_file_type returns a proper enum from a file type string.
52 */
53 enum LibofxFileFormat libofx_get_file_format_from_str(const struct LibofxFileFormatInfo format_list[], const char * file_type_string)
54 {
55  enum LibofxFileFormat retval = UNKNOWN;
56  for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
57  {
58  if (strcmp(LibofxImportFormatList[i].format_name, file_type_string) == 0)
59  {
60  retval = LibofxImportFormatList[i].format;
61  }
62  }
63  return retval;
64 }
65 
66 int libofx_proc_file(LibofxContextPtr p_libofx_context, const char * p_filename, LibofxFileFormat p_file_type)
67 {
68  LibofxContext * libofx_context = (LibofxContext *) p_libofx_context;
69 
70  if (p_file_type == AUTODETECT)
71  {
72  message_out(INFO, std::string("libofx_proc_file(): File format not specified, autodetecting..."));
73  libofx_context->setCurrentFileType(libofx_detect_file_type(p_filename));
74  message_out(INFO, std::string("libofx_proc_file(): Detected file format: ") +
75  libofx_get_file_format_description(LibofxImportFormatList,
76  libofx_context->currentFileType() ));
77  }
78  else
79  {
80  libofx_context->setCurrentFileType(p_file_type);
82  std::string("libofx_proc_file(): File format forced to: ") +
83  libofx_get_file_format_description(LibofxImportFormatList,
84  libofx_context->currentFileType() ));
85  }
86 
87  switch (libofx_context->currentFileType())
88  {
89  case OFX:
90  return ofx_proc_file(libofx_context, p_filename);
91  case OFC:
92  return ofx_proc_file(libofx_context, p_filename);
93  default:
94  message_out(ERROR, std::string("libofx_proc_file(): Could not detect file format, or unsupported file format; aborting."));
95  return -1;
96  }
97  return 0; // never reached
98 }
99 
100 enum LibofxFileFormat libofx_detect_file_type(const char * p_filename)
101 {
102  enum LibofxFileFormat retval = UNKNOWN;
103  std::ifstream input_file;
104  char buffer[READ_BUFFER_SIZE];
105  std::string s_buffer;
106  bool type_found = false;
107 
108  if (p_filename != NULL && strcmp(p_filename, "") != 0)
109  {
110  message_out(DEBUG, std::string("libofx_detect_file_type():Opening file: ") + p_filename);
111 
112  input_file.open(p_filename);
113 
114  if (!input_file)
115  {
116  message_out(ERROR, "libofx_detect_file_type():Unable to open the input file " + std::string(p_filename));
117  return retval;
118  }
119  else
120  {
121  do
122  {
123  input_file.getline(buffer, sizeof(buffer), '\n');
124  //cout<<buffer<<"\n";
125  s_buffer.assign(buffer);
126  //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
127  if (input_file.gcount() < int(sizeof(buffer) - 1))
128  {
129  s_buffer.append("\n");//Just in case...
130  }
131  else if ( !input_file.eof() && input_file.fail())
132  {
133  input_file.clear();
134  }
135 
136  if (s_buffer.find("<OFX") != std::string::npos || s_buffer.find("<ofx") != std::string::npos)
137  {
138  message_out(DEBUG, "libofx_detect_file_type():<OFX> tag has been found");
139  retval = OFX;
140  type_found = true;
141  }
142  else if (s_buffer.find("<OFC>") != std::string::npos || s_buffer.find("<ofc>") != std::string::npos)
143  {
144  message_out(DEBUG, "libofx_detect_file_type():<OFC> tag has been found");
145  retval = OFC;
146  type_found = true;
147  }
148 
149  }
150  while (type_found == false && !input_file.eof() && !input_file.bad());
151  }
152  input_file.close();
153  }
154  else
155  {
156  message_out(ERROR, "libofx_detect_file_type(): No input file specified");
157  }
158  if (retval == UNKNOWN)
159  message_out(ERROR, "libofx_detect_file_type(): Failed to identify input file format");
160  return retval;
161 }
ofx_preproc.hh
Preprocessing of the OFX files before parsing.
OFX
@ OFX
Definition: inc/libofx.h:140
LAST
@ LAST
Definition: inc/libofx.h:144
file_preproc.hh
Preprocessing of the OFX files before parsing.
ERROR
@ ERROR
Definition: messages.hh:41
LibofxFileFormat
LibofxFileFormat
Definition: inc/libofx.h:137
LibofxContext
Definition: context.hh:23
OFC
@ OFC
Definition: inc/libofx.h:141
message_out
int message_out(OfxMsgType error_type, const std::string message)
Message output function.
Definition: messages.cpp:67
libofx_get_file_format_from_str
enum LibofxFileFormat libofx_get_file_format_from_str(const struct LibofxFileFormatInfo format_list[], const char *file_type_string)
libofx_get_file_type returns a proper enum from a file type string.
Definition: file_preproc.cpp:53
LibofxFileFormatInfo::format
enum LibofxFileFormat format
Definition: inc/libofx.h:149
INFO
@ INFO
Definition: messages.hh:39
AUTODETECT
@ AUTODETECT
Definition: inc/libofx.h:139
ofx_proc_file
int ofx_proc_file(LibofxContextPtr ctx, const char *p_filename)
File pre-processing of OFX AND for OFC files.
Definition: ofx_preproc.cpp:80
libofx_get_file_format_description
const char * libofx_get_file_format_description(const struct LibofxFileFormatInfo format_list[], enum LibofxFileFormat file_format)
get_file_format_description returns a string description of a LibofxFileType.
Definition: file_preproc.cpp:36
LibofxFileFormatInfo::description
const char * description
Definition: inc/libofx.h:151
libofx_detect_file_type
enum LibofxFileFormat libofx_detect_file_type(const char *p_filename)
libofx_detect_file_type tries to analyze a file to determine it's format.
Definition: file_preproc.cpp:100
LibofxFileFormatInfo
Definition: inc/libofx.h:147
UNKNOWN
@ UNKNOWN
Definition: inc/libofx.h:143
messages.hh
Message IO functionality.
DEBUG
@ DEBUG
Definition: messages.hh:32
libofx_proc_file
int libofx_proc_file(LibofxContextPtr p_libofx_context, const char *p_filename, LibofxFileFormat p_file_type)
libofx_proc_file is the entry point of the library.
Definition: file_preproc.cpp:66