00001
00002
00003
00004
00005
00006
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include <fstream>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <string>
00025 #include "ParserEventGeneratorKit.h"
00026 #include "libofx.h"
00027 #include "messages.hh"
00028 #include "ofx_sgml.hh"
00029 #include "ofx_preproc.hh"
00030
00031 using namespace std;
00032 const unsigned int READ_BUFFER_SIZE = 1024;
00033
00034 int ofx_proc_file(int argc, char *argv[])
00035 {
00036 bool ofx_start=false;
00037 bool ofx_end=false;
00038
00039 ifstream input_file;
00040 ofstream tmp_file;
00041 char buffer[READ_BUFFER_SIZE];
00042 char tmp;
00043 string s_buffer;
00044 char *filenames[3];
00045 char tmp_filename[50];
00046 char filename_dtd[255];
00047 char filename_ofx[255];
00048
00049 if(argc >= 2){
00050 message_out(DEBUG, string("ofx_proc_file():Opening file: ")+argv[1]);
00051
00052 input_file.open(argv[1]);
00053 strncpy(tmp_filename,"/tmp/libofxtmpXXXXXX",50);
00054 mkstemp(tmp_filename);
00055 tmp_file.open(tmp_filename);
00056
00057 message_out(DEBUG,"ofx_proc_file(): Creating temp file: "+string(tmp_filename));
00058 if(!input_file){
00059 message_out(ERROR,"ofx_proc_file():Unable to open the input file "+string(argv[1]));
00060 }
00061 else if(!tmp_file){
00062 message_out(ERROR,"ofx_proc_file():Unable to open the output file "+string(tmp_filename));
00063 }
00064 else
00065 {
00066 do{
00067 input_file.getline(buffer, sizeof(buffer),'\n');
00068
00069 s_buffer.assign(buffer);
00070
00071 if(input_file.gcount()<(sizeof(buffer)-1))
00072 {
00073 s_buffer.append("\n");
00074 }
00075 else if( !input_file.eof()&&input_file.fail())
00076 {
00077 input_file.clear();
00078 }
00079 int ofx_start_idx;
00080 if(ofx_start==false&&((ofx_start_idx=s_buffer.find("<OFX>"))!=string::npos||(ofx_start_idx=s_buffer.find("<ofx>"))!=string::npos)){
00081 ofx_start=true;
00082 s_buffer.erase(0,ofx_start_idx);
00083 message_out(DEBUG,"ofx_proc_file():<OFX> has been found");
00084 }
00085
00086 if(ofx_start==true&&ofx_end==false){
00087 s_buffer=sanitize_proprietary_tags(s_buffer);
00088
00089 tmp_file.write(s_buffer.c_str(), s_buffer.length());
00090 }
00091
00092 if(ofx_start==true&&(s_buffer.find("</OFX>")!=string::npos||s_buffer.find("</ofx>")!=string::npos)){
00093 ofx_end=true;
00094 message_out(DEBUG,"ofx_proc_file():</OFX> has been found");
00095 }
00096
00097 }while(!input_file.eof()&&!input_file.bad());
00098 }
00099 input_file.close();
00100 tmp_file.close();
00101 strncpy(filename_dtd,find_dtd().c_str(),255);
00102 if(filename_dtd!=NULL)
00103 {
00104 strncpy(filename_ofx,tmp_filename,255);
00105 filenames[0]=filename_dtd;
00106 filenames[1]=filename_ofx;
00107 ofx_proc_sgml(2,filenames);
00108 if(remove(tmp_filename)!=0)
00109 {
00110 message_out(ERROR,"ofx_proc_file(): Error deleting temporary file "+string(tmp_filename));
00111 }
00112 }
00113 else
00114 {
00115 message_out(ERROR,"ofx_proc_file(): FATAL: Missing DTD, aborting");
00116 }
00117 }
00118 else{
00119 message_out(ERROR,"ofx_proc_file():No input file specified");
00120 }
00121 return 0;
00122 }
00123
00128 string sanitize_proprietary_tags(string input_string)
00129 {
00130 unsigned int i;
00131 size_t input_string_size;
00132 bool strip=false;
00133 bool tag_open=false;
00134 int tag_open_idx=0;
00135 bool closing_tag_open=false;
00136 int orig_tag_open_idx=0;
00137 bool proprietary_tag=false;
00138 bool proprietary_closing_tag=false;
00139 int crop_end_idx=0;
00140 char buffer[READ_BUFFER_SIZE]="";
00141 char tagname[READ_BUFFER_SIZE]="";
00142 int tagname_idx=0;
00143 char close_tagname[READ_BUFFER_SIZE]="";
00144
00145 for(i=0;i<READ_BUFFER_SIZE;i++){
00146 buffer[i]=0;
00147 tagname[i]=0;
00148 close_tagname[i]=0;
00149 }
00150
00151 input_string_size=input_string.size();
00152
00153 for(i=0;i<=input_string_size;i++){
00154 if(input_string.c_str()[i]=='<'){
00155 tag_open=true;
00156 tag_open_idx=i;
00157 if(proprietary_tag==true&&input_string.c_str()[i+1]=='/'){
00158
00159 closing_tag_open=true;
00160
00161 if(strncmp(tagname,&(input_string.c_str()[i+2]),strlen(tagname))!=0){
00162
00163
00164 crop_end_idx=i-1;
00165 strip=true;
00166 }
00167 else{
00168
00169 proprietary_closing_tag=true;
00170 }
00171 }
00172 else if(proprietary_tag==true){
00173
00174 crop_end_idx=i-1;
00175 strip=true;
00176 }
00177 }
00178 else if(input_string.c_str()[i]=='>'){
00179 tag_open=false;
00180 closing_tag_open=false;
00181 tagname[tagname_idx]=0;
00182 tagname_idx=0;
00183 if(proprietary_closing_tag==true){
00184 crop_end_idx=i;
00185 strip=true;
00186 }
00187 }
00188 else if(tag_open==true&&closing_tag_open==false){
00189 if(input_string.c_str()[i]=='.'){
00190 if(proprietary_tag!=true){
00191 orig_tag_open_idx = tag_open_idx;
00192 proprietary_tag=true;
00193 }
00194 }
00195 tagname[tagname_idx]=input_string.c_str()[i];
00196 tagname_idx++;
00197 }
00198
00199 if(strip==true)
00200 {
00201 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00202 message_out(INFO,"sanitize_proprietary_tags() (end tag or new tag) removed: "+string(buffer));
00203 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00204 i=orig_tag_open_idx-1;
00205 proprietary_tag=false;
00206 proprietary_closing_tag=false;
00207 closing_tag_open=false;
00208 tag_open=false;
00209 strip=false;
00210 }
00211
00212 }
00213 if(proprietary_tag==true){
00214 if(crop_end_idx==0){
00215 crop_end_idx=input_string.size()-1;
00216 }
00217 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00218 message_out(INFO,"sanitize_proprietary_tags() (end of line) removed: "+string(buffer));
00219 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00220 }
00221 return input_string;
00222 }
00223
00224
00225
00231 string find_dtd(const int requested_version)
00232 {
00233 int i;
00234 ifstream dtd_file;
00235 string dtd_filename;
00236 string dtd_path_filename;
00237 bool dtd_found=false;
00238
00239 dtd_filename="ofx160.dtd";
00240
00241 for(i=0;i<DTD_SEARCH_PATH_NUM&&dtd_found==false;i++){
00242 dtd_path_filename=DTD_SEARCH_PATH[i];
00243 dtd_path_filename.append(dtd_filename);
00244 dtd_file.clear();
00245 dtd_file.open(dtd_path_filename.c_str());
00246 if(!dtd_file){
00247 message_out(DEBUG,"find_dtd():Unable to open the file "+dtd_path_filename);
00248 }
00249 else{
00250 message_out(STATUS,"find_dtd():DTD found: "+dtd_path_filename);
00251 dtd_file.close();
00252 dtd_found=true;
00253 }
00254 }
00255 if(dtd_found==false){
00256 message_out(ERROR,"find_dtd():Unable to find the DTD for the requested version");
00257 dtd_path_filename="";
00258 }
00259 return dtd_path_filename;
00260 }
00261
00262