diff --git a/include/filetext.cpp b/include/filetext.cpp index a1ff86215..a3b7edd51 100755 --- a/include/filetext.cpp +++ b/include/filetext.cpp @@ -181,10 +181,11 @@ void TFile_text::set_gen_parm(TConfig& config, const TString& section) _recordsep = "\r\n"; _skiplines = config.get_int("SKIPLINES",section); // righe iniziali da ignorare - _typefield = config.get_int("TYPEFIELD",section); // Numero del campo tipo (puo' essere -1) + _typefield = config.get_int("TYPEFIELD",section); // Numero del campo tipo (puo' essere -1) _fixedlen = _fieldsep <= ' '; // && _recordsep.blank(); - _typepos = -1; - _typelen = -1; + _typepos = config.get_int("TYPEPOS", section, -1, -1); + _typelen = config.get_int("TYPELEN", section, -1, -1); + CHECKD(_typelen <= 16, "Tipo record di lunghezza spropositata: ", _typelen); } void TFile_text::set_type_parm(TConfig& config, TString& section) @@ -332,17 +333,24 @@ void TFile_text::set_rec_parm(TConfig& config, const char* section) } if (lavoro == "POS") { - int pos = atoi(obj); - tc.set_position(pos); - if (_fixedlen && _typepos < 0 && n == _typefield) - _typepos = pos; + const int pos = atoi(obj); + if (pos >= 0) + { + tc.set_position(pos); + if (_typepos < 0 && n == _typefield) + _typepos = pos; + } continue; } if (lavoro == "LEN") { - int len = atoi(obj); - if (tc.length() <= 0 && len >= 0 && tc.length() != len) - tc.set_length(len); + const int len = atoi(obj); + if (tc.length() <= 0 && len > 0) + { + tc.set_length(len); + if (_typelen < 0 && n == _typefield) + _typelen = len; + } continue; } if (lavoro == "DEC") @@ -445,11 +453,16 @@ TFile_text::~TFile_text() //Legge da file il record text int TFile_text::read(TRecord_text& rec) { - CHECK(_read_file, "Impossibile leggere da un file chiuso."); + if (_read_file == NULL) + open('r'); - TToken_string buffer(_recordsize), lavoro; + TToken_string buffer; + if (_recordsize > 0) { + if (buffer.size() < _recordsize) + buffer.spaces(_recordsize); + _read_file->read(buffer.get_buffer(), _recordsize); if (!ok_r()) return 1; //non ritorna errore se fine file ed il record e' completo! } @@ -461,12 +474,13 @@ int TFile_text::read(TRecord_text& rec) return EOF; while (c != _recordsep[0]) - { + { buffer << c; c = _read_file->get(); if (!ok_r()) return EOF; //non ritorna errore se fine file ed il record e' completo! } + // prendo il resto del separatore int l = _recordsep.len()-1; for (int j = 0; j < l;c = _read_file-> get (), j++); @@ -481,32 +495,35 @@ int TFile_text::read(TRecord_text& rec) rec.set_type(tipo);//istanzio il tipo del record text TArray& a_tc = tr.tracciati_campo(); - int items = a_tc.items(); + const int items = a_tc.items(); buffer.restart(); for (int i = 0; i < items; i++) { -// TTracciato_campo& tc = tr.get(i); - lavoro = buffer.get(); + const char* lavoro = buffer.get(); + if (lavoro == NULL) + lavoro = ""; rec.add(lavoro, i); } } else { - TString tipo = buffer.mid(_typepos, _typelen); + TString16 tipo = buffer.mid(_typepos, _typelen); tipo.trim(); rec.set_type(tipo);//istanzio il tipo del record text TTracciato_record* tr = t_rec(tipo); - CHECK(tr,"Tipo di tracciato record non riconosciuto"); - //ora che ho il tracciato record devo scandire i tracciati campo e caricare il record text - TArray& a_tc = tr->tracciati_campo(); - int items = a_tc.items(); - for (int i = 0; i < items; i++) + if (tr != NULL) { - TTracciato_campo& tc = tr->get(i); - int pos = tc.position(); - int len = tc.length(); - lavoro = buffer.mid(pos, len); - rec.add(lavoro, i); + //ora che ho il tracciato record devo scandire i tracciati campo e caricare il record text + const TArray& a_tc = tr->tracciati_campo(); + const int items = a_tc.items(); + for (int i = 0; i < items; i++) + { + TTracciato_campo& tc = tr->get(i); + const int pos = tc.position(); + const int len = tc.length(); + const char* lavoro = buffer.mid(pos, len); + rec.add(lavoro, i); + } } } return 0; @@ -515,12 +532,13 @@ int TFile_text::read(TRecord_text& rec) //Scrive su file il record_text (valido anche per header e footer) int TFile_text::write(TRecord_text& rec) { - TString buffer; TString campo; + TToken_string buffer; + const TString& type = rec.type(); TTracciato_record& tr = *t_rec(type); TArray& a_tc = tr.tracciati_campo(); - int items = rec.items(); + const int items = rec.items(); if (_typepos>=0) rec.add(type,_typepos); if (_fixedlen) // campi a lunghezza fissa @@ -528,8 +546,7 @@ int TFile_text::write(TRecord_text& rec) for (int i = 0; i < items; i++) { TTracciato_campo& tc = tr.get(i); - campo = rec.row(i); - campo = format_textfield(tc, campo); + campo = format_textfield(tc, rec.row(i)); buffer.insert(campo, tc.position()); campo.cut(0); } @@ -539,31 +556,29 @@ int TFile_text::write(TRecord_text& rec) // Record a lunghezza var *_write_file << buffer; *_write_file << _recordsep; - } else { + } + else + { // Record a lunghezza fissa buffer.rpad(_recordsize); buffer.cut(_recordsize); *_write_file << buffer; } - if (!ok_w()) return 1; - buffer.cut(0); + if (!ok_w()) return 1; } else { - TToken_string ts(buffer, _fieldsep); + buffer.separator(_fieldsep); for (int i = 0; i < items; i++) { TTracciato_campo& tc = tr.get(i); - campo = rec.row(i); - campo = format_textfield(tc, campo); - ts.add(campo, i); - campo.cut(0); + campo = format_textfield(tc, rec.row(i)); + buffer.add(campo); } CHECK(_write_file, "Impossibile scrivere su un file chiuso."); - *_write_file << ts; + *_write_file << buffer; *_write_file << _recordsep; if (!ok_w()) return 1; - ts.cut(0); } return 0; } @@ -728,8 +743,9 @@ int TFile_text::_autosave(TRelation& rel, const TRecord_text& rec, TTracciato_re if (err == _isdupkey || err ==_isreinsert) err = rel.rewrite(); return err; - } else - return NOERR; + } + + return NOERR; } //Scarica dal record_text il campo alla posizione @@ -755,11 +771,18 @@ const TString& TFile_text::get_field(const TRecord_text& rec, const char* name) //Carica nel record_text il campo alla posizione con il valore già formattato void TFile_text::add_field(TRecord_text& rec, const int ncampo, const char* val) { - TTracciato_record& tr = *t_rec(rec.type()); - TTracciato_campo& tc = tr.get(ncampo); - TString valore = val; - //valore =format_textfield(tc, valore); - rec.add(valore, ncampo); + TTracciato_record& tr = *t_rec(rec.type()); + TTracciato_campo& tc = tr.get(ncampo); + rec.add(val, ncampo); +} + +//Carica nel record_text il campo con il valore già formattato +void TFile_text::add_field(TRecord_text& rec, const char* name, const char* val) +{ + TTracciato_record& tr = *t_rec(rec.type()); + int ncampo = tr.get_pos(name); + CHECKS(ncampo >= 0, "Campo inesistente ", name); + rec.add(val, ncampo); } //Formatta la data in base al tracciato diff --git a/include/filetext.h b/include/filetext.h index 74709d811..c60bb779f 100755 --- a/include/filetext.h +++ b/include/filetext.h @@ -116,20 +116,24 @@ public: //////////////////////////////////////////////////////////////////////////// class TRecord_text : public TObject { - TString _type;//tipo del record - TString_array _array;//array che contiene i valori dei campi + TString16 _type; //tipo del record + TString_array _array; //array che contiene i valori dei campi + public: - TRecord_text() {} - TRecord_text(const TString& type): _type(type) {} - virtual ~TRecord_text(){} const TString& type() const {return _type;} void set_type(const TString& type) {_type = type;} const TString& row(int pos) const;//usare la get!!! TString& row(int pos); const TString& get(int pos) const {return row(pos);}//ritorna il campo dell'array della posizione void add(const TString& c, int pos = -1);//scrive il campo nell'array alla posizione + const int items() const {return _array.items();}//ritorna il numero di elementi dell'array - virtual bool destroy(int index = -1, bool pack = FALSE) {return _array.destroy(index, pack);} + virtual bool destroy(int index = -1, bool pack = FALSE) + { return _array.destroy(index, pack); } + + TRecord_text() { } + TRecord_text(const TString& type) : _type(type) { } + virtual ~TRecord_text(){} }; ///////////////////////////////// TFile_text ////////////////////////////////////// // Classe per la definizione di un file di testo capace di leggersi e scriversi,// @@ -186,8 +190,6 @@ protected: virtual bool pre_writerel(TRelation& rel,const TRecord_text& rec) {return TRUE;} virtual bool can_write(TRecord_text&, TRelation&) {return TRUE;} public: - TFile_text(const char* file_name, const char* config_name); - virtual ~TFile_text(); ifstream* read_file() {return _read_file;} ofstream* write_file() {return _write_file;} void set_gen_parm(TConfig& config, const TString& section);//scarica i parametri generali dal file di configurazione @@ -241,14 +243,19 @@ public: int autosave(const TRecord_text& rec, int mainfile=0); //caricamento automatico della relazione definita nel tracciato dal record_text int autosave(int mainfile); - int read(TRecord_text & rec);//legge da file di testo il record_text + int read(TRecord_text& rec);//legge da file di testo il record_text int read() {return read(*_current);}//legge da file di testo il record_text corrente //carica nel record_text il campo alla posizione con il valore già formattato void add_field(TRecord_text& rec, const int ncampo, const char* val); + //carica nel record_text il campo con il valore già formattato + void add_field(TRecord_text& rec, const char* name, const char* val); //scarica dal record_text il campo alla posizione const TString& get_field(const TRecord_text& rec, int ncampo); //scarica dal record_text il campo di nome const TString& get_field(const TRecord_text& rec, const char* name); + + TFile_text(const char* file_name, const char* config_name); + virtual ~TFile_text(); }; #endif //__FILETEXT_H \ No newline at end of file