~mterry/ubuntu/natty/libofx/libofx.new-upstream-benoit-sru

« back to all changes in this revision

Viewing changes to lib/ofx_preproc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Saïvann Carignan
  • Date: 2008-02-06 17:25:16 UTC
  • mto: (3.1.2 lenny)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20080206172516-bnzxb29igye8um9h
Tags: upstream-0.9.0
Import upstream version 0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *   (at your option) any later version.                                   *
18
18
 *                                                                         *
19
19
 ***************************************************************************/
 
20
#include "../config.h"
20
21
#include <iostream>
21
22
#include <fstream>
22
23
#include <stdlib.h>
28
29
#include "ofx_sgml.hh"
29
30
#include "ofc_sgml.hh"
30
31
#include "ofx_preproc.hh"
 
32
#include "ofx_utilities.hh"
 
33
#ifdef HAVE_ICONV
 
34
#include <iconv.h>
 
35
#endif
 
36
 
 
37
#ifdef OS_WIN32
 
38
# include "win32.hh"
 
39
#endif
 
40
 
 
41
#define LIBOFX_DEFAULT_INPUT_ENCODING "CP1252"
 
42
#define LIBOFX_DEFAULT_OUTPUT_ENCODING "UTF-8"
31
43
 
32
44
using namespace std;
33
45
/**
64
76
  ifstream input_file;
65
77
  ofstream tmp_file;
66
78
  char buffer[READ_BUFFER_SIZE];
 
79
  char iconv_buffer[READ_BUFFER_SIZE];
67
80
  string s_buffer;
68
81
  char *filenames[3];
69
 
  char tmp_filename[50];
70
 
 
 
82
  char tmp_filename[256];
 
83
#ifdef HAVE_ICONV
 
84
        iconv_t conversion_descriptor;
 
85
#endif
71
86
  libofx_context=(LibofxContext*)ctx;
72
87
 
73
88
  if(p_filename!=NULL&&strcmp(p_filename,"")!=0)
75
90
    message_out(DEBUG, string("ofx_proc_file():Opening file: ")+ p_filename);
76
91
    
77
92
    input_file.open(p_filename);
78
 
    strncpy(tmp_filename,"/tmp/libofxtmpXXXXXX",50);
 
93
    mkTempFileName("libofxtmpXXXXXX", tmp_filename, sizeof(tmp_filename));
79
94
    mkstemp(tmp_filename);
80
95
    tmp_file.open(tmp_filename);
81
96
 
88
103
    }
89
104
    else
90
105
      {
91
 
 
 
106
        int header_separator_idx;
 
107
        string header_name;
 
108
        string header_value;
 
109
        string ofx_encoding;
 
110
        string ofx_charset;
92
111
        do {
93
112
          input_file.getline(buffer, sizeof(buffer),'\n');
94
113
          //cout<<buffer<<"\n";
117
136
              ofx_start=true;
118
137
              s_buffer.erase(0,ofx_start_idx);//Fix for really broken files that don't have a newline after the header.
119
138
              message_out(DEBUG,"ofx_proc_file():<OFX> or <OFC> has been found");
 
139
#ifdef HAVE_ICONV
 
140
              string fromcode;
 
141
              string tocode; 
 
142
              if(ofx_encoding.compare("USASCII")==0){
 
143
                if(ofx_charset.compare("ISO-8859-1")==0){
 
144
                  fromcode="ISO-8859-1";
 
145
                }
 
146
                else if(ofx_charset.compare("1252")==0){
 
147
                  fromcode="CP1252";
 
148
                }
 
149
                else if(ofx_charset.compare("NONE")==0){
 
150
                  fromcode=LIBOFX_DEFAULT_INPUT_ENCODING;
 
151
                }
 
152
              }
 
153
              else if(ofx_encoding.compare("USASCII")==0) {
 
154
                fromcode="UTF-8";
 
155
              }
 
156
              else
 
157
                {
 
158
                  fromcode=LIBOFX_DEFAULT_INPUT_ENCODING;
 
159
                }
 
160
              tocode = LIBOFX_DEFAULT_OUTPUT_ENCODING;
 
161
              message_out(DEBUG,"ofx_proc_file(): Setting up iconv for fromcode: "+fromcode+", tocode: "+tocode);
 
162
              conversion_descriptor = iconv_open (tocode.c_str(), fromcode.c_str());
 
163
#endif
120
164
            }
 
165
          else {
 
166
            //We are still in the headers
 
167
            if ((header_separator_idx=s_buffer.find(':')) != string::npos) {
 
168
              //Header processing
 
169
              header_name.assign(s_buffer.substr(0,header_separator_idx));
 
170
              header_value.assign(s_buffer.substr(header_separator_idx+1));
 
171
              message_out(DEBUG,"ofx_proc_file():Header: "+header_name+" with value: "+header_value+" has been found");
 
172
              if(header_name.compare("ENCODING")==0) {
 
173
                ofx_encoding.assign(header_value);
 
174
              }
 
175
              if(header_name.compare("CHARSET")==0) {
 
176
                ofx_charset.assign(header_value);
 
177
              }
 
178
            }
 
179
          }
121
180
 
122
181
          if(ofx_start==true && ofx_end==false){
123
182
            s_buffer=sanitize_proprietary_tags(s_buffer);
124
183
            //cout<< s_buffer<<"\n";
125
 
            tmp_file.write(s_buffer.c_str(), s_buffer.length());
 
184
#ifdef HAVE_ICONV
 
185
            memset(iconv_buffer,0,READ_BUFFER_SIZE);
 
186
            size_t inbytesleft = strlen(s_buffer.c_str());
 
187
            size_t outbytesleft = READ_BUFFER_SIZE;
 
188
#ifdef OS_WIN32
 
189
            const char * inchar = (const char *)s_buffer.c_str();
 
190
#else
 
191
            char * inchar = (char *)s_buffer.c_str();
 
192
#endif
 
193
            char * outchar = iconv_buffer;
 
194
            int iconv_retval = iconv (conversion_descriptor,
 
195
                    &inchar, &inbytesleft,
 
196
                   &outchar, &outbytesleft);
 
197
            if(iconv_retval==-1){
 
198
              message_out(ERROR,"ofx_proc_file(): Conversion error");
 
199
            }
 
200
            s_buffer = iconv_buffer;
 
201
#endif
 
202
              tmp_file.write(s_buffer.c_str(), s_buffer.length());
126
203
          }
127
204
          
128
205
          if (ofx_start==true &&
144
221
      }
145
222
    input_file.close();
146
223
    tmp_file.close();
147
 
 
 
224
#ifdef HAVE_ICONV
 
225
              iconv_close(conversion_descriptor);
 
226
#endif
148
227
    char filename_openspdtd[255];
149
228
    char filename_dtd[255];
150
229
    char filename_ofx[255];
151
 
    strncpy(filename_openspdtd,find_dtd(OPENSPDCL_FILENAME).c_str(),255);//The opensp sgml dtd file
 
230
    strncpy(filename_openspdtd,find_dtd(ctx, OPENSPDCL_FILENAME).c_str(),255);//The opensp sgml dtd file
152
231
    if(libofx_context->currentFileType()==OFX)
153
232
      {
154
 
        strncpy(filename_dtd,find_dtd(OFX160DTD_FILENAME).c_str(),255);//The ofx dtd file
 
233
        strncpy(filename_dtd,find_dtd(ctx, OFX160DTD_FILENAME).c_str(),255);//The ofx dtd file
155
234
      }
156
235
    else if(libofx_context->currentFileType()==OFC)
157
236
      {
158
 
        strncpy(filename_dtd,find_dtd(OFCDTD_FILENAME).c_str(),255);//The ofc dtd file
 
237
        strncpy(filename_dtd,find_dtd(ctx, OFCDTD_FILENAME).c_str(),255);//The ofc dtd file
159
238
      }
160
239
    else
161
240
      {
203
282
  ofstream tmp_file;
204
283
  string s_buffer;
205
284
  char *filenames[3];
206
 
  char tmp_filename[50];
207
 
  int pos;
 
285
  char tmp_filename[256];
 
286
  ssize_t pos;
208
287
  LibofxContext *libofx_context;
209
288
 
210
289
  libofx_context=(LibofxContext*)ctx;
216
295
  }
217
296
  s_buffer=string(s, size);
218
297
 
219
 
  strncpy(tmp_filename,"/tmp/libofxtmpXXXXXX",50);
 
298
  mkTempFileName("libofxtmpXXXXXX", tmp_filename, sizeof(tmp_filename));
220
299
  mkstemp(tmp_filename);
221
300
  tmp_file.open(tmp_filename);
222
301
 
240
319
    message_out(ERROR,"ofx_proc(): unknown file type");
241
320
    return -1;
242
321
  }
243
 
  if (pos==string::npos) {
 
322
  if (pos==string::npos || pos > s_buffer.size()) {
244
323
    message_out(ERROR,"ofx_proc():<OFX> has not been found");
245
324
    return -1;
246
325
  }
265
344
    return -1;
266
345
  }
267
346
 
268
 
  if (pos==string::npos) {
 
347
  if (pos==string::npos || pos > s_buffer.size()) {
269
348
    message_out(ERROR,"ofx_proc():</OF?> has not been found");
270
349
    return -1;
271
350
  }
272
351
  else {
273
352
    // erase everything after the /OFX tag
274
 
    s_buffer.erase(pos+6);
 
353
    if (s_buffer.size() > pos+6)
 
354
      s_buffer.erase(pos+6);
275
355
    message_out(DEBUG,"ofx_proc_file():<OFX> has been found");
276
356
  }
277
357
 
283
363
  char filename_openspdtd[255];
284
364
  char filename_dtd[255];
285
365
  char filename_ofx[255];
286
 
  strncpy(filename_openspdtd,find_dtd(OPENSPDCL_FILENAME).c_str(),255);//The opensp sgml dtd file
 
366
  strncpy(filename_openspdtd,find_dtd(ctx, OPENSPDCL_FILENAME).c_str(),255);//The opensp sgml dtd file
287
367
  if(libofx_context->currentFileType()==OFX){
288
 
    strncpy(filename_dtd,find_dtd(OFX160DTD_FILENAME).c_str(),255);//The ofx dtd file
 
368
    strncpy(filename_dtd,find_dtd(ctx, OFX160DTD_FILENAME).c_str(),255);//The ofx dtd file
289
369
  }
290
370
  else if(libofx_context->currentFileType()==OFC){
291
 
    strncpy(filename_dtd,find_dtd(OFCDTD_FILENAME).c_str(),255);//The ofc dtd file
 
371
    strncpy(filename_dtd,find_dtd(ctx, OFCDTD_FILENAME).c_str(),255);//The ofc dtd file
292
372
  }
293
373
  else {
294
374
    message_out(ERROR,string("ofx_proc_file(): Error unknown file format for the OFX parser"));
431
511
   *
432
512
   Please note that currently the function will ALWAYS look for version 160, since OpenSP can't parse the 201 DTD correctly
433
513
*/
434
 
string find_dtd(string dtd_filename)
 
514
string find_dtd(LibofxContextPtr ctx, string dtd_filename)
435
515
{
436
516
  int i;
437
517
  ifstream dtd_file;
438
518
  string dtd_path_filename;
439
519
  bool dtd_found=false;
440
520
 
441
 
  for(i=0;i<DTD_SEARCH_PATH_NUM&&dtd_found==false;i++){
442
 
    dtd_path_filename=DTD_SEARCH_PATH[i];
 
521
  dtd_path_filename=((LibofxContext*)ctx)->dtdDir();
 
522
  if (!dtd_path_filename.empty()) {
443
523
    dtd_path_filename.append(dtd_filename);
444
524
    dtd_file.clear();
445
525
    dtd_file.open(dtd_path_filename.c_str());
446
 
    if(!dtd_file){
447
 
      message_out(DEBUG,"find_dtd():Unable to open the file "+dtd_path_filename);
448
 
    }
449
 
    else{
 
526
    if(dtd_file){
450
527
      message_out(STATUS,"find_dtd():DTD found: "+dtd_path_filename);
451
528
      dtd_file.close();
452
529
      dtd_found=true;
453
530
    }
454
531
  }
 
532
 
 
533
  if (!dtd_found) {
 
534
    for(i=0;i<DTD_SEARCH_PATH_NUM&&dtd_found==false;i++){
 
535
      dtd_path_filename=DTD_SEARCH_PATH[i];
 
536
      dtd_path_filename.append(dtd_filename);
 
537
      dtd_file.clear();
 
538
      dtd_file.open(dtd_path_filename.c_str());
 
539
      if(!dtd_file){
 
540
        message_out(DEBUG,"find_dtd():Unable to open the file "+dtd_path_filename);
 
541
      }
 
542
      else{
 
543
        message_out(STATUS,"find_dtd():DTD found: "+dtd_path_filename);
 
544
        dtd_file.close();
 
545
        dtd_found=true;
 
546
      }
 
547
    }
 
548
  }
 
549
 
455
550
  if(dtd_found==false){
456
551
    message_out(ERROR,"find_dtd():Unable to find the DTD named " + dtd_filename);
457
552
    dtd_path_filename="";