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 if(ofx_start==false&&(s_buffer.find("<OFX>")!=string::npos||s_buffer.find("<ofx>")!=string::npos)){
00080 ofx_start=true;
00081 message_out(DEBUG,"ofx_proc_file():<OFX> has been found");
00082 }
00083
00084 if(ofx_start==true&&ofx_end==false){
00085 s_buffer=sanitize_proprietary_tags(s_buffer);
00086
00087 tmp_file.write(s_buffer.c_str(), s_buffer.length());
00088 }
00089
00090 if(ofx_start==true&&(s_buffer.find("</OFX>")!=string::npos||s_buffer.find("</ofx>")!=string::npos)){
00091 ofx_end=true;
00092 message_out(DEBUG,"ofx_proc_file():</OFX> has been found");
00093 }
00094
00095 }while(!input_file.eof()&&!input_file.bad());
00096 }
00097 input_file.close();
00098 tmp_file.close();
00099 strncpy(filename_dtd,find_dtd().c_str(),255);
00100 if(filename_dtd!=NULL)
00101 {
00102 strncpy(filename_ofx,tmp_filename,255);
00103 filenames[0]=filename_dtd;
00104 filenames[1]=filename_ofx;
00105 ofx_proc_sgml(2,filenames);
00106 if(remove(tmp_filename)!=0)
00107 {
00108 message_out(ERROR,"ofx_proc_file(): Error deleting temporary file "+string(tmp_filename));
00109 }
00110 }
00111 else
00112 {
00113 message_out(ERROR,"ofx_proc_file(): FATAL: Missing DTD, aborting");
00114 }
00115 }
00116 else{
00117 message_out(ERROR,"ofx_proc_file():No input file specified");
00118 }
00119 return 0;
00120 }
00121
00126 string sanitize_proprietary_tags(string input_string)
00127 {
00128 unsigned int i;
00129 size_t input_string_size;
00130 bool strip=false;
00131 bool tag_open=false;
00132 int tag_open_idx=0;
00133 bool closing_tag_open=false;
00134 int orig_tag_open_idx=0;
00135 bool proprietary_tag=false;
00136 bool proprietary_closing_tag=false;
00137 int crop_end_idx=0;
00138 char buffer[READ_BUFFER_SIZE]="";
00139 char tagname[READ_BUFFER_SIZE]="";
00140 int tagname_idx=0;
00141 char close_tagname[READ_BUFFER_SIZE]="";
00142
00143 for(i=0;i<READ_BUFFER_SIZE;i++){
00144 buffer[i]=0;
00145 tagname[i]=0;
00146 close_tagname[i]=0;
00147 }
00148
00149 input_string_size=input_string.size();
00150
00151 for(i=0;i<=input_string_size;i++){
00152 if(input_string.c_str()[i]=='<'){
00153 tag_open=true;
00154 tag_open_idx=i;
00155 if(proprietary_tag==true&&input_string.c_str()[i+1]=='/'){
00156
00157 closing_tag_open=true;
00158
00159 if(strncmp(tagname,&(input_string.c_str()[i+2]),strlen(tagname))!=0){
00160
00161
00162 crop_end_idx=i-1;
00163 strip=true;
00164 }
00165 else{
00166
00167 proprietary_closing_tag=true;
00168 }
00169 }
00170 else if(proprietary_tag==true){
00171
00172 crop_end_idx=i-1;
00173 strip=true;
00174 }
00175 }
00176 else if(input_string.c_str()[i]=='>'){
00177 tag_open=false;
00178 closing_tag_open=false;
00179 tagname[tagname_idx]=0;
00180 tagname_idx=0;
00181 if(proprietary_closing_tag==true){
00182 crop_end_idx=i;
00183 strip=true;
00184 }
00185 }
00186 else if(tag_open==true&&closing_tag_open==false){
00187 if(input_string.c_str()[i]=='.'){
00188 if(proprietary_tag!=true){
00189 orig_tag_open_idx = tag_open_idx;
00190 proprietary_tag=true;
00191 }
00192 }
00193 tagname[tagname_idx]=input_string.c_str()[i];
00194 tagname_idx++;
00195 }
00196
00197 if(strip==true)
00198 {
00199 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00200 message_out(INFO,"sanitize_proprietary_tags() (end tag or new tag) removed: "+string(buffer));
00201 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00202 i=orig_tag_open_idx-1;
00203 proprietary_tag=false;
00204 proprietary_closing_tag=false;
00205 closing_tag_open=false;
00206 tag_open=false;
00207 strip=false;
00208 }
00209
00210 }
00211 if(proprietary_tag==true){
00212 if(crop_end_idx==0){
00213 crop_end_idx=input_string.size()-1;
00214 }
00215 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00216 message_out(INFO,"sanitize_proprietary_tags() (end of line) removed: "+string(buffer));
00217 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00218 }
00219 return input_string;
00220 }
00221
00222
00223
00229 string find_dtd(const int requested_version)
00230 {
00231 int i;
00232 ifstream dtd_file;
00233 string dtd_filename;
00234 string dtd_path_filename;
00235 bool dtd_found=false;
00236
00237 dtd_filename="ofx160.dtd";
00238
00239 for(i=0;i<DTD_SEARCH_PATH_NUM&&dtd_found==false;i++){
00240 dtd_path_filename=DTD_SEARCH_PATH[i];
00241 dtd_path_filename.append(dtd_filename);
00242 dtd_file.clear();
00243 dtd_file.open(dtd_path_filename.c_str());
00244 if(!dtd_file){
00245 message_out(DEBUG,"find_dtd():Unable to open the file "+dtd_path_filename);
00246 }
00247 else{
00248 message_out(STATUS,"find_dtd():DTD found: "+dtd_path_filename);
00249 dtd_file.close();
00250 dtd_found=true;
00251 }
00252 }
00253 if(dtd_found==false){
00254 message_out(ERROR,"find_dtd():Unable to find the DTD for the requested version");
00255 dtd_path_filename="";
00256 }
00257 return dtd_path_filename;
00258 }
00259
00260