LibOFX

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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(libofx_detect_file_type(p_filename));
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 ofx_proc_file(libofx_context, p_filename);
92 break;
93 case OFC:
94 ofx_proc_file(libofx_context, p_filename);
95 break;
96 default:
97 message_out(ERROR, string("libofx_proc_file(): Detected file format not yet supported ou couldn't detect file format; aborting."));
98 }
99 return 0;
100 }
101
102 enum LibofxFileFormat libofx_detect_file_type(const char * p_filename)
103 {
104 enum LibofxFileFormat retval = UNKNOWN;
105 ifstream input_file;
106 char buffer[READ_BUFFER_SIZE];
107 string s_buffer;
108 bool type_found = false;
109
110 if (p_filename != NULL && strcmp(p_filename, "") != 0)
111 {
112 message_out(DEBUG, string("libofx_detect_file_type():Opening file: ") + p_filename);
113
114 input_file.open(p_filename);
115
116 if (!input_file)
117 {
118 message_out(ERROR, "libofx_detect_file_type():Unable to open the input file " + string(p_filename));
119 return retval;
120 }
121 else
122 {
123 do
124 {
125 input_file.getline(buffer, sizeof(buffer), '\n');
126 //cout<<buffer<<"\n";
127 s_buffer.assign(buffer);
128 //cout<<"input_file.gcount(): "<<input_file.gcount()<<" sizeof(buffer): "<<sizeof(buffer)<<endl;
129 if (input_file.gcount() < (sizeof(buffer) - 1))
130 {
131 s_buffer.append("\n");//Just in case...
132 }
133 else if ( !input_file.eof() && input_file.fail())
134 {
135 input_file.clear();
136 }
137
138 if (s_buffer.find("<OFX>") != string::npos || s_buffer.find("<ofx>") != string::npos)
139 {
140 message_out(DEBUG, "libofx_detect_file_type():<OFX> tag has been found");
141 retval = OFX;
142 type_found = true;
143 }
144 else if (s_buffer.find("<OFC>") != string::npos || s_buffer.find("<ofc>") != string::npos)
145 {
146 message_out(DEBUG, "libofx_detect_file_type():<OFC> tag has been found");
147 retval = OFC;
148 type_found = true;
149 }
150
151 }
152 while (type_found == false && !input_file.eof() && !input_file.bad());
153 }
154 input_file.close();
155 }
156 else
157 {
158 message_out(ERROR, "libofx_detect_file_type(): No input file specified");
159 }
160 if (retval == UNKNOWN)
161 message_out(ERROR, "libofx_detect_file_type(): Failed to identify input file format");
162 return retval;
163 }
164
165
166
167
168
AUTODETECT
Definition: inc/libofx.h:125
LibofxFileFormatInfo
Definition: inc/libofx.h:133
LibofxFileFormatInfo::format
enum LibofxFileFormat format
Definition: inc/libofx.h:135
INFO
Definition: messages.hh:32
LibofxFileFormatInfo::description
const char * description
Definition: inc/libofx.h:137
file_preproc.hh
Preprocessing of the OFX files before parsing.
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:81
OFX
Definition: inc/libofx.h:126
LibofxContext
Definition: context.hh:25
LAST
Definition: inc/libofx.h:130
UNKNOWN
Definition: inc/libofx.h:129
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:37
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:67
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: messages.cpp:60
DEBUG
Definition: messages.hh:25
ERROR
Definition: messages.hh:34
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:102
OFC
Definition: inc/libofx.h:127
LibofxFileFormat
LibofxFileFormat
Definition: inc/libofx.h:123
messages.hh
Message IO functionality.
ofx_preproc.hh
Preprocessing of the OFX files before parsing.
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:54