LibOFX

build/libofx-0.10.0/lib/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 using namespace std;
32 const unsigned int READ_BUFFER_SIZE = 1024;
33
34 /* get_file_type_description returns a string description of a LibofxFileType
35 * suitable for debugging output or user communication.
36 */
37 const char * libofx_get_file_format_description(const struct LibofxFileFormatInfo format_list[], enum LibofxFileFormat file_format)
38 {
39 const char * retval = "UNKNOWN (File format couldn't be successfully identified)";
40
41 for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
42 {
43 if (LibofxImportFormatList[i].format == file_format)
44 {
45 retval = LibofxImportFormatList[i].description;
46 }
47 }
48 return retval;
49 }
50
51 /*
52 libofx_get_file_type returns a proper enum from a file type string.
53 */
54 enum LibofxFileFormat libofx_get_file_format_from_str(const struct LibofxFileFormatInfo format_list[], const char * file_type_string)
55 {
56 enum LibofxFileFormat retval = UNKNOWN;
57 for (int i = 0; LibofxImportFormatList[i].format != LAST; i++)
58 {
59 if (strcmp(LibofxImportFormatList[i].format_name, file_type_string) == 0)
60 {
61 retval = LibofxImportFormatList[i].format;
62 }
63 }
64 return retval;
65 }
66
67 int libofx_proc_file(LibofxContextPtr p_libofx_context, const char * p_filename, LibofxFileFormat p_file_type)
68 {
69 LibofxContext * libofx_context = (LibofxContext *) p_libofx_context;
70
71 if (p_file_type == AUTODETECT)
72 {
73 message_out(INFO, string("libofx_proc_file(): File format not specified, autodetecting..."));
74 libofx_context->setCurrentFileType(libofx_detect_file_type(p_filename));
75 message_out(INFO, string("libofx_proc_file(): Detected file format: ") +
76 libofx_get_file_format_description(LibofxImportFormatList,
77 libofx_context->currentFileType() ));
78 }
79 else
80 {
81 libofx_context->setCurrentFileType(p_file_type);
82 message_out(INFO,
83 string("libofx_proc_file(): File format forced to: ") +
84 libofx_get_file_format_description(LibofxImportFormatList,
85 libofx_context->currentFileType() ));
86 }
87
88 switch (libofx_context->currentFileType())
89 {
90 case OFX:
91 return ofx_proc_file(libofx_context, p_filename);
92 case OFC:
93 return ofx_proc_file(libofx_context, p_filename);
94 default:
95 message_out(ERROR, string("libofx_proc_file(): Could not detect file format, or unsupported file format; aborting."));
96 return -1;
97 }
98 return 0; // never reached
99 }
100
101 enum LibofxFileFormat libofx_detect_file_type(const char * p_filename)
102 {
103 enum LibofxFileFormat retval = UNKNOWN;
104 ifstream input_file;
105 char buffer[READ_BUFFER_SIZE];
106 string s_buffer;
107 bool type_found = false;
108
109 if (p_filename != NULL && strcmp(p_filename, "") != 0)
110 {
111 message_out(DEBUG, string("libofx_detect_file_type():Opening file: ") + p_filename);
112
113 input_file.open(p_filename);
114
115 if (!input_file)
116 {
117 message_out(ERROR, "libofx_detect_file_type():Unable to open the input file " + string(p_filename));
118 return retval;
119 }
120 else
121 {
122 do
123 {
124 input_file.getline(buffer, sizeof(buffer), '\n');
125 //cout<<buffer<<"\n";
126 s_buffer.assign(buffer);
127 //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
128 if (input_file.gcount() < int(sizeof(buffer) - 1))
129 {
130 s_buffer.append("\n");//Just in case...
131 }
132 else if ( !input_file.eof() && input_file.fail())
133 {
134 input_file.clear();
135 }
136
137 if (s_buffer.find("<OFX") != string::npos || s_buffer.find("<ofx") != string::npos)
138 {
139 message_out(DEBUG, "libofx_detect_file_type():<OFX> tag has been found");
140 retval = OFX;
141 type_found = true;
142 }
143 else if (s_buffer.find("<OFC>") != string::npos || s_buffer.find("<ofc>") != string::npos)
144 {
145 message_out(DEBUG, "libofx_detect_file_type():<OFC> tag has been found");
146 retval = OFC;
147 type_found = true;
148 }
149
150 }
151 while (type_found == false && !input_file.eof() && !input_file.bad());
152 }
153 input_file.close();
154 }
155 else
156 {
157 message_out(ERROR, "libofx_detect_file_type(): No input file specified");
158 }
159 if (retval == UNKNOWN)
160 message_out(ERROR, "libofx_detect_file_type(): Failed to identify input file format");
161 return retval;
162 }
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: build/libofx-0.10.0/lib/file_preproc.cpp:67
AUTODETECT
Definition: inc/libofx.h:128
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: build/libofx-0.10.0/lib/file_preproc.cpp:37
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: build/libofx-0.10.0/lib/file_preproc.cpp:54
LibofxFileFormatInfo
Definition: inc/libofx.h:136
LibofxFileFormatInfo::format
enum LibofxFileFormat format
Definition: inc/libofx.h:138
INFO
Definition: build/libofx-0.10.0/lib/messages.hh:32
LibofxFileFormatInfo::description
const char * description
Definition: inc/libofx.h:140
std
OFX
Definition: inc/libofx.h:129
LibofxContext
Definition: build/libofx-0.10.0/lib/context.hh:25
LAST
Definition: inc/libofx.h:133
UNKNOWN
Definition: inc/libofx.h:132
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: build/libofx-0.10.0/lib/messages.cpp:61
ofx_preproc.hh
Preprocessing of the OFX files before parsing.
file_preproc.hh
Preprocessing of the OFX files before parsing.
messages.hh
Message IO functionality.
OFC
Definition: inc/libofx.h:130
DEBUG
Definition: build/libofx-0.10.0/lib/messages.hh:25
LibofxFileFormat
LibofxFileFormat
Definition: inc/libofx.h:126
ERROR
Definition: build/libofx-0.10.0/lib/messages.hh:34
ofx_proc_file
int ofx_proc_file(LibofxContextPtr ctx, const char *p_filename)
File pre-processing of OFX AND for OFC files.
Definition: build/libofx-0.10.0/lib/ofx_preproc.cpp:81
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&#39;s format.
Definition: build/libofx-0.10.0/lib/file_preproc.cpp:101