84 bool ofx_start =
false;
86 bool file_is_xml =
false;
87 bool used_iconv =
false;
91 char tmp_filename[256];
94 iconv_t conversion_descriptor;
98 if (p_filename != NULL && strcmp(p_filename,
"") != 0)
102 input_file.open(p_filename);
105 message_out(
ERROR,
"ofx_proc_file():Unable to open the input file " +
string(p_filename));
108 mkTempFileName(
"libofxtmpXXXXXX", tmp_filename,
sizeof(tmp_filename));
110 message_out(
DEBUG,
"ofx_proc_file(): Creating temp file: " +
string(tmp_filename));
112 tmp_file_fd = mkstemp_win32(tmp_filename);
114 tmp_file_fd = mkstemp(tmp_filename);
118 tmp_file.open(tmp_filename);
121 message_out(
ERROR,
"ofx_proc_file():Unable to open the created temp file " +
string(tmp_filename));
127 message_out(
ERROR,
"ofx_proc_file():Unable to create a temp file at " +
string(tmp_filename));
131 if (input_file && tmp_file)
133 std::size_t header_separator_idx;
142 input_file.get(buffer,
'\n');
144 s_buffer = buffer.str();
148 if (!input_file.eof())
151 if (input_file.fail())
161 if (input_file.peek() ==
'\n')
168 if (ofx_start ==
false && (s_buffer.find(
"<?xml") != string::npos))
170 message_out(
DEBUG,
"ofx_proc_file(): File is an actual XML file, iconv conversion will be skipped.");
174 std::size_t ofx_start_idx;
175 if (ofx_start ==
false)
178 (libofx_context->currentFileType() ==
OFX &&
179 ((ofx_start_idx = s_buffer.find(
"<OFX>")) != string::npos ||
180 (ofx_start_idx = s_buffer.find(
"<ofx>")) != string::npos))
182 (libofx_context->currentFileType() ==
OFC &&
183 ((ofx_start_idx = s_buffer.find(
"<OFC>")) != string::npos ||
184 (ofx_start_idx = s_buffer.find(
"<ofc>")) != string::npos))
188 if (file_is_xml ==
false)
190 s_buffer.erase(0, ofx_start_idx);
194 if (file_is_xml ==
true)
196 static char sp_charset_fixed[] =
"SP_CHARSET_FIXED=1";
197 if (putenv(sp_charset_fixed) != 0)
208 static char sp_encoding[] =
"SP_ENCODING=ms-dos";
209 if (putenv(sp_encoding) != 0)
216 static char sp_charset_fixed[] =
"SP_CHARSET_FIXED=1";
217 if (putenv(sp_charset_fixed) != 0)
221 static char sp_encoding[] =
"SP_ENCODING=ms-dos";
222 if (putenv(sp_encoding) != 0)
229 if (ofx_encoding.compare(
"USASCII") == 0)
231 if (ofx_charset.compare(
"ISO-8859-1") == 0 || ofx_charset.compare(
"8859-1") == 0)
234 fromcode =
"ISO-8859-1";
236 else if (ofx_charset.compare(
"1252") == 0 || ofx_charset.compare(
"CP1252") == 0)
241 else if (ofx_charset.compare(
"NONE") == 0)
243 fromcode = LIBOFX_DEFAULT_INPUT_ENCODING;
247 fromcode = LIBOFX_DEFAULT_INPUT_ENCODING;
250 else if (ofx_encoding.compare(
"UTF-8") == 0 || ofx_encoding.compare(
"UNICODE") == 0)
257 fromcode = LIBOFX_DEFAULT_INPUT_ENCODING;
259 tocode = LIBOFX_DEFAULT_OUTPUT_ENCODING;
260 message_out(
DEBUG,
"ofx_proc_file(): Setting up iconv for fromcode: " + fromcode +
", tocode: " + tocode);
261 conversion_descriptor = iconv_open (tocode.c_str(), fromcode.c_str());
269 if ((header_separator_idx = s_buffer.find(
':')) != string::npos)
272 header_name.assign(s_buffer.substr(0, header_separator_idx));
273 header_value.assign(s_buffer.substr(header_separator_idx + 1));
274 while ( header_value[header_value.length() - 1 ] ==
'\n' ||
275 header_value[header_value.length() - 1 ] ==
'\r' )
276 header_value.erase(header_value.length() - 1);
277 message_out(
DEBUG,
"ofx_proc_file():Header: " + header_name +
" with value: " + header_value +
" has been found");
278 if (header_name.compare(
"ENCODING") == 0)
280 ofx_encoding.assign(header_value);
282 if (header_name.compare(
"CHARSET") == 0)
284 ofx_charset.assign(header_value);
290 if (file_is_xml ==
true || (ofx_start ==
true && ofx_end ==
false))
292 if (ofx_start ==
true)
299 if (s_buffer.empty())
303 if (file_is_xml ==
false)
306 size_t inbytesleft = s_buffer.size();
307 size_t outbytesleft = inbytesleft * 2 - 1;
308 char * iconv_buffer = (
char*) malloc (inbytesleft * 2);
309 memset(iconv_buffer, 0, inbytesleft * 2);
310 const char* inchar = s_buffer.c_str();
311 char * outchar = iconv_buffer;
312 int iconv_retval = iconv (conversion_descriptor,
313 const_cast<char**
>(&inchar), &inbytesleft,
314 &outchar, &outbytesleft);
315 if (iconv_retval == -1)
321 s_buffer = std::string(iconv_buffer, outchar - iconv_buffer);
326 tmp_file << s_buffer << endl;
329 if (ofx_start ==
true &&
331 (libofx_context->currentFileType() ==
OFX &&
332 ((ofx_start_idx = s_buffer.find(
"</OFX>")) != string::npos ||
333 (ofx_start_idx = s_buffer.find(
"</ofx>")) != string::npos))
334 || (libofx_context->currentFileType() ==
OFC &&
335 ((ofx_start_idx = s_buffer.find(
"</OFC>")) != string::npos ||
336 (ofx_start_idx = s_buffer.find(
"</ofc>")) != string::npos))
345 while (!input_file.eof() && !input_file.bad());
350 if (used_iconv ==
true)
352 iconv_close(conversion_descriptor);
355 char filename_openspdtd[255];
356 char filename_dtd[255];
357 char filename_ofx[255];
359 if (libofx_context->currentFileType() ==
OFX)
363 else if (libofx_context->currentFileType() ==
OFC)
369 message_out(
ERROR,
string(
"ofx_proc_file(): Error unknown file format for the OFX parser"));
372 if ((
string)filename_dtd !=
"" && (
string)filename_openspdtd !=
"")
374 strncpy(filename_ofx, tmp_filename, 255);
375 filenames[0] = filename_openspdtd;
376 filenames[1] = filename_dtd;
377 filenames[2] = filename_ofx;
379 if (libofx_context->currentFileType() ==
OFX)
383 else if (libofx_context->currentFileType() ==
OFC)
389 message_out(
ERROR,
string(
"ofx_proc_file(): Error unknown file format for the OFX parser"));
392 if (remove(tmp_filename) != 0)
394 message_out(
ERROR,
"ofx_proc_file(): Error deleting temporary file " +
string(tmp_filename));
482 size_t last_known_good_pos = 0;
483 size_t open_tag_start_pos = last_known_good_pos;
484 size_t open_tag_end_pos;
485 size_t close_tag_end_pos;
487 string tag_name = find_tag_open(input_string, open_tag_start_pos, open_tag_end_pos);
488 while (!tag_name.empty())
491 if ((tag_name.find(
'.') != string::npos) ||
492 (tag_name ==
"CATEGORY"))
494 close_tag_end_pos = open_tag_end_pos;
495 find_tag_close (input_string, tag_name, close_tag_end_pos);
496 size_t tag_size = close_tag_end_pos - open_tag_start_pos;
497 string prop_tag = input_string.substr(open_tag_start_pos, tag_size);
499 input_string.erase(open_tag_start_pos, tag_size);
500 last_known_good_pos = open_tag_start_pos;
504 last_known_good_pos = open_tag_end_pos;
507 open_tag_start_pos = last_known_good_pos;
508 if (last_known_good_pos != string::npos)
509 tag_name = find_tag_open(input_string, open_tag_start_pos, open_tag_end_pos);
553std::string
find_dtd(LibofxContextPtr ctx,
const std::string& dtd_filename)
555 string dtd_path_filename;
558 dtd_path_filename =
reinterpret_cast<const LibofxContext*
>(ctx)->dtdDir();
559 if (!dtd_path_filename.empty())
561 dtd_path_filename.append(dtd_filename);
562 ifstream dtd_file(dtd_path_filename.c_str());
566 return dtd_path_filename;
571 dtd_path_filename = get_dtd_installation_directory();
572 if (!dtd_path_filename.empty())
574 dtd_path_filename.append(DIRSEP);
575 dtd_path_filename.append(dtd_filename);
576 ifstream dtd_file(dtd_path_filename.c_str());
580 return dtd_path_filename;
585 env_dtd_path = getenv(
"OFX_DTD_PATH");
588 dtd_path_filename.append(env_dtd_path);
589 dtd_path_filename.append(DIRSEP);
590 dtd_path_filename.append(dtd_filename);
591 ifstream dtd_file(dtd_path_filename.c_str());
594 message_out(
STATUS,
"find_dtd():OFX_DTD_PATH env variable was was present, but unable to open the file " + dtd_path_filename);
599 return dtd_path_filename;
606 dtd_path_filename.append(DIRSEP);
607 dtd_path_filename.append(dtd_filename);
608 ifstream dtd_file(dtd_path_filename.c_str());
611 message_out(
DEBUG,
"find_dtd():Unable to open the file " + dtd_path_filename);
616 return dtd_path_filename;
621 dtd_path_filename =
"";
622 dtd_path_filename.append(
"..");
623 dtd_path_filename.append(DIRSEP);
624 dtd_path_filename.append(
"dtd");
625 dtd_path_filename.append(DIRSEP);
626 dtd_path_filename.append(dtd_filename);
627 ifstream dtd_file(dtd_path_filename.c_str());
630 message_out(
DEBUG,
"find_dtd(): Unable to open the file " + dtd_path_filename +
", most likely we are not in the source tree.");
635 return dtd_path_filename;
639 message_out(
ERROR,
"find_dtd():Unable to find the DTD named " + dtd_filename);