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_openspdtd[255];
00047 char filename_ofxdtd[255];
00048 char filename_ofx[255];
00049
00050 if(argc >= 2){
00051 message_out(DEBUG, string("ofx_proc_file():Opening file: ")+argv[1]);
00052
00053 input_file.open(argv[1]);
00054 strncpy(tmp_filename,"/tmp/libofxtmpXXXXXX",50);
00055 mkstemp(tmp_filename);
00056 tmp_file.open(tmp_filename);
00057
00058 message_out(DEBUG,"ofx_proc_file(): Creating temp file: "+string(tmp_filename));
00059 if(!input_file){
00060 message_out(ERROR,"ofx_proc_file():Unable to open the input file "+string(argv[1]));
00061 }
00062 else if(!tmp_file){
00063 message_out(ERROR,"ofx_proc_file():Unable to open the output file "+string(tmp_filename));
00064 }
00065 else
00066 {
00067 do{
00068 input_file.getline(buffer, sizeof(buffer),'\n');
00069
00070 s_buffer.assign(buffer);
00071
00072 if(input_file.gcount()<(sizeof(buffer)-1))
00073 {
00074 s_buffer.append("\n");
00075 }
00076 else if( !input_file.eof()&&input_file.fail())
00077 {
00078 input_file.clear();
00079 }
00080 int ofx_start_idx;
00081 if(ofx_start==false&&((ofx_start_idx=s_buffer.find("<OFX>"))!=string::npos||(ofx_start_idx=s_buffer.find("<ofx>"))!=string::npos)){
00082 ofx_start=true;
00083 s_buffer.erase(0,ofx_start_idx);
00084 message_out(DEBUG,"ofx_proc_file():<OFX> has been found");
00085 }
00086
00087 if(ofx_start==true&&ofx_end==false){
00088 s_buffer=sanitize_proprietary_tags(s_buffer);
00089
00090 tmp_file.write(s_buffer.c_str(), s_buffer.length());
00091 }
00092
00093 if(ofx_start==true&&(s_buffer.find("</OFX>")!=string::npos||s_buffer.find("</ofx>")!=string::npos)){
00094 ofx_end=true;
00095 message_out(DEBUG,"ofx_proc_file():</OFX> has been found");
00096 }
00097
00098 }while(!input_file.eof()&&!input_file.bad());
00099 }
00100 input_file.close();
00101 tmp_file.close();
00102
00103 strncpy(filename_openspdtd,find_dtd(OPENSPDCL_FILENAME).c_str(),255);
00104 strncpy(filename_ofxdtd,find_dtd(OFX160DTD_FILENAME).c_str(),255);
00105 if((string)filename_ofxdtd!="" && (string)filename_openspdtd!="")
00106 {
00107 strncpy(filename_ofx,tmp_filename,255);
00108 filenames[0]=filename_openspdtd;
00109 filenames[1]=filename_ofxdtd;
00110 filenames[2]=filename_ofx;
00111 ofx_proc_sgml(3,filenames);
00112 if(remove(tmp_filename)!=0)
00113 {
00114 message_out(ERROR,"ofx_proc_file(): Error deleting temporary file "+string(tmp_filename));
00115 }
00116 }
00117 else
00118 {
00119 message_out(ERROR,"ofx_proc_file(): FATAL: Missing DTD, aborting");
00120 }
00121 }
00122 else{
00123 message_out(ERROR,"ofx_proc_file():No input file specified");
00124 }
00125 return 0;
00126 }
00127
00132 string sanitize_proprietary_tags(string input_string)
00133 {
00134 unsigned int i;
00135 size_t input_string_size;
00136 bool strip=false;
00137 bool tag_open=false;
00138 int tag_open_idx=0;
00139 bool closing_tag_open=false;
00140 int orig_tag_open_idx=0;
00141 bool proprietary_tag=false;
00142 bool proprietary_closing_tag=false;
00143 int crop_end_idx=0;
00144 char buffer[READ_BUFFER_SIZE]="";
00145 char tagname[READ_BUFFER_SIZE]="";
00146 int tagname_idx=0;
00147 char close_tagname[READ_BUFFER_SIZE]="";
00148
00149 for(i=0;i<READ_BUFFER_SIZE;i++){
00150 buffer[i]=0;
00151 tagname[i]=0;
00152 close_tagname[i]=0;
00153 }
00154
00155 input_string_size=input_string.size();
00156
00157 for(i=0;i<=input_string_size;i++){
00158 if(input_string.c_str()[i]=='<'){
00159 tag_open=true;
00160 tag_open_idx=i;
00161 if(proprietary_tag==true&&input_string.c_str()[i+1]=='/'){
00162
00163 closing_tag_open=true;
00164
00165 if(strncmp(tagname,&(input_string.c_str()[i+2]),strlen(tagname))!=0){
00166
00167
00168 crop_end_idx=i-1;
00169 strip=true;
00170 }
00171 else{
00172
00173 proprietary_closing_tag=true;
00174 }
00175 }
00176 else if(proprietary_tag==true){
00177
00178 crop_end_idx=i-1;
00179 strip=true;
00180 }
00181 }
00182 else if(input_string.c_str()[i]=='>'){
00183 tag_open=false;
00184 closing_tag_open=false;
00185 tagname[tagname_idx]=0;
00186 tagname_idx=0;
00187 if(proprietary_closing_tag==true){
00188 crop_end_idx=i;
00189 strip=true;
00190 }
00191 }
00192 else if(tag_open==true&&closing_tag_open==false){
00193 if(input_string.c_str()[i]=='.'){
00194 if(proprietary_tag!=true){
00195 orig_tag_open_idx = tag_open_idx;
00196 proprietary_tag=true;
00197 }
00198 }
00199 tagname[tagname_idx]=input_string.c_str()[i];
00200 tagname_idx++;
00201 }
00202
00203 if(strip==true)
00204 {
00205 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00206 message_out(INFO,"sanitize_proprietary_tags() (end tag or new tag) removed: "+string(buffer));
00207 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00208 i=orig_tag_open_idx-1;
00209 proprietary_tag=false;
00210 proprietary_closing_tag=false;
00211 closing_tag_open=false;
00212 tag_open=false;
00213 strip=false;
00214 }
00215
00216 }
00217 if(proprietary_tag==true){
00218 if(crop_end_idx==0){
00219 crop_end_idx=input_string.size()-1;
00220 }
00221 input_string.copy(buffer,(crop_end_idx-orig_tag_open_idx)+1,orig_tag_open_idx);
00222 message_out(INFO,"sanitize_proprietary_tags() (end of line) removed: "+string(buffer));
00223 input_string.erase(orig_tag_open_idx,(crop_end_idx-orig_tag_open_idx)+1);
00224 }
00225 return input_string;
00226 }
00227
00228
00229
00235 string find_dtd(string dtd_filename)
00236 {
00237 int i;
00238 ifstream dtd_file;
00239 string dtd_path_filename;
00240 bool dtd_found=false;
00241
00242 for(i=0;i<DTD_SEARCH_PATH_NUM&&dtd_found==false;i++){
00243 dtd_path_filename=DTD_SEARCH_PATH[i];
00244 dtd_path_filename.append(dtd_filename);
00245 dtd_file.clear();
00246 dtd_file.open(dtd_path_filename.c_str());
00247 if(!dtd_file){
00248 message_out(DEBUG,"find_dtd():Unable to open the file "+dtd_path_filename);
00249 }
00250 else{
00251 message_out(STATUS,"find_dtd():DTD found: "+dtd_path_filename);
00252 dtd_file.close();
00253 dtd_found=true;
00254 }
00255 }
00256 if(dtd_found==false){
00257 message_out(ERROR,"find_dtd():Unable to find the DTD named " + dtd_filename);
00258 dtd_path_filename="";
00259 }
00260 return dtd_path_filename;
00261 }
00262
00263