From af43d798cdd15d58c1dd797b3edfd1402d66bd60 Mon Sep 17 00:00:00 2001 From: guy Date: Wed, 28 Jan 1998 08:29:13 +0000 Subject: [PATCH] filetext.* Aggiunto supporto per saltare le prime _skiplines di un file form.cpp Rimodernato uso della clessidra: niente piu' main_app().begin_wait(); e main_app().end_wait(); ma solo e semplicemente TWait_cursor hourglass; isam.* Aggiunto metodo TBaseisamfile::open_ex che non da' errore fatale se non puo' aprire un file, bensi' ritorna un codice d'errore appropriato. Aggiunto parametro zap alla TSystemisamfile::pack_file che permette di azzerare completamente i record di un file lffiles.h Aggiunta #define LF_EXTERNAL 1000 per indicare la base dei numeri logici dei file dbf esterni git-svn-id: svn://10.65.10.50/trunk@6041 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- include/filetext.cpp | 57 +++++++++--- include/filetext.h | 3 +- include/form.cpp | 12 +-- include/isam.cpp | 216 ++++++++++++++++++++++++++++++------------- include/isam.h | 10 +- include/lffiles.h | 4 +- 6 files changed, 210 insertions(+), 92 deletions(-) diff --git a/include/filetext.cpp b/include/filetext.cpp index 93c86a41c..26c131f17 100755 --- a/include/filetext.cpp +++ b/include/filetext.cpp @@ -73,8 +73,10 @@ const TString& TFile_text::fpicture(const TTracciato_campo &tc ) const //////////////////////////////////////// TTracciato_record //////////////////////////////////////// -void TTracciato_record::set_relation(TRelation * rel) + +void TTracciato_record::set_relation(TRelation* rel) { + if (_rel) delete _rel; _rel = rel; } @@ -82,7 +84,7 @@ TObject* TTracciato_record::dup() const { TTracciato_record* t = new TTracciato_record(*this); return t; -} +} TTracciato_record::TTracciato_record(const TTracciato_record& tr) { @@ -167,8 +169,10 @@ void TFile_text::set_gen_parm(TConfig& config, const TString& section) _recordsize = config.get_int("RECORDSIZE",section); // solo se a lung. fissa _fieldsep = config.get_char("FIELDSEP",section); // solo se a lung. variabile _recordsep = config.get("RECORDSEP",section); // solo se a lung. variabile - if (_recordsize <= 0 && (_recordsep.empty())) // separatore di record standard - _recordsep = "\r\n"; + if (_recordsize <= 0 && _recordsep.empty()) // separatore di record standard + _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) _fixedlen = _fieldsep <= ' '; // && _recordsep.blank(); _typepos = -1; @@ -276,7 +280,7 @@ void TFile_text::set_rec_parm(TConfig& config, const char* section) if (exp.empty()) yesnofatal_box("JOIN senza espressioni INTO"); #endif - if (isdigit(j[0])) + if (j[0] >= '0' && j[0] <= '9') { tmprel->add(atoi(j), exp, nkey, to, alias); // join file tmprel->write_enable(atoi(j),key.left(6)!="JOINRO"); @@ -378,7 +382,7 @@ void TFile_text::set_rec_parm(TConfig& config, const char* section) } TFile_text::TFile_text(const char* file_name, const char* config_name) - : _name(file_name) + : _name(file_name), _skiplines(0) { _read_file = NULL; _write_file = NULL; @@ -428,10 +432,9 @@ int TFile_text::read(TRecord_text& rec) CHECK(_read_file, "Impossibile leggere da un file chiuso."); TToken_string buffer(_recordsize), lavoro; - if (_recordsize>0) + if (_recordsize > 0) { - buffer.cut(0); - _read_file->read(buffer.get_buffer(),buffer.size()); + _read_file->read(buffer.get_buffer(), _recordsize); if (!ok_r()) return 1; //non ritorna errore se fine file ed il record e' completo! } else @@ -491,7 +494,6 @@ int TFile_text::read(TRecord_text& rec) } } return 0; - } //Scrive su file il record_text (valido anche per header e footer) @@ -830,11 +832,36 @@ int TFile_text::open(char mode) delete _read_file; _read_file = new ifstream(_name, ios::binary | ios::nocreate); - if (!ok_r()) - { - error_box("Impossibile aprire il file %s in lettura", (const char *)_name); - return _read_file->rdstate() != ios::goodbit; - } + if (!ok_r()) + { + error_box("Impossibile aprire il file %s in lettura", (const char *)_name); + return _read_file->rdstate() != ios::goodbit; + } + + if (_skiplines > 0) + { + if (_recordsize > 0) + { + streamoff bytestoskip = streamoff(_recordsize) * _skiplines; + _read_file->seekg(bytestoskip, ios::beg); + } else + if (_recordsep.not_empty()) + { + int s = _skiplines; + for (int c = _read_file->get(); c != EOF; c = _read_file->get()) + { + if (c == _recordsep[0]) + { + for (int k = _recordsep.len()-1; k > 0; k--) + c = _read_file->get(); + if (--s == 0) + break; + } + } + if (c == EOF) + return 1; + } + } } if (mode == 'w')//apertura in scrittura { diff --git a/include/filetext.h b/include/filetext.h index 12347aadb..4f54640d2 100755 --- a/include/filetext.h +++ b/include/filetext.h @@ -140,6 +140,7 @@ class TFile_text : public TObject TRecord_text* _current;//puntatore al record_text corrente TAssoc_array _tipi;//tracciati campo per i vari tipi predefiniti TAssoc_array _tracciati_record;//tracciati record per i vari tipi di record + int _skiplines;//Righe iniziali da ignorare char _decsep;//separatore decimale int _recordsize;//dimensione dei record TString _recordsep;//separatore di record a lunghezza variabile (blank() se lung.fissa) @@ -205,6 +206,7 @@ public: const int typepos() const {return _typepos;} const int typelen() const {return _typelen;} const int typefield() const {return _typefield;} + const int skiplines() const {return _skiplines;} const TRecord_text& curr() const {return *_current;}//ritorna il record corrente void set_curr(TRecord_text& rec) {_current = &rec;}//setta il record corrente a rec void set_curr(TRecord_text* rec) {_current = rec;}//setta il record corrente a rec @@ -241,7 +243,6 @@ public: 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); - }; #endif //__FILETEXT_H \ No newline at end of file diff --git a/include/form.cpp b/include/form.cpp index 1acaa28a5..682f8a635 100755 --- a/include/form.cpp +++ b/include/form.cpp @@ -2187,7 +2187,7 @@ HIDDEN bool font_handler(TMask_field& f, KEY key) { if (key == K_SPACE) { - main_app().begin_wait(); + TWait_cursor hourglass; const char* family = f.get(); const int MAXSIZES = 16; @@ -2218,8 +2218,6 @@ HIDDEN bool font_handler(TMask_field& f, KEY key) TList_field& lst = (TList_field&)f.mask().field(F_SIZE); lst.replace_items(pn1, pn2); lst.set(format("%d",printer().get_char_size())); - - main_app().end_wait(); } return TRUE; } @@ -5003,7 +5001,7 @@ bool TForm::validate(TForm_item &cf, TToken_string &s) void TForm::print_on(ostream& out) const { - main_app().begin_wait(); + TWait_cursor hourglass; if (relation()) { @@ -5019,8 +5017,6 @@ void TForm::print_on(ostream& out) const print_section(out, 'F'); out << "END" << endl; - - main_app().end_wait(); } @@ -5221,7 +5217,7 @@ void TForm::read( _editlevel= lev; _desc= desc; - main_app().begin_wait(); + TWait_cursor hourglass; if (_code.not_empty()) { @@ -5314,8 +5310,6 @@ void TForm::read( _fontsize = pr.get_char_size(); } set_fink_mode(TRUE); - - main_app().end_wait(); } void TForm::set_compulsory_specials() diff --git a/include/isam.cpp b/include/isam.cpp index 6db53cbaf..32d94089f 100755 --- a/include/isam.cpp +++ b/include/isam.cpp @@ -33,7 +33,6 @@ #define RECLOCKTYPES 0xFF00 #define READTYPES 0x00FF #define INVFLD 255 -#define EXTERNAL_FILE 1000 // Files with id >= are considered to be externals isfdptr* ext_files; isfdptr* openf; @@ -396,7 +395,7 @@ HIDDEN const char * translate_key(const char* key) // Traduce l'espressione chia HIDDEN void getisfd(isfdptr & isfd, int logicnum) { - CHECK(logicnum < EXTERNAL_FILE, "Incorrect use of getisfd() with external file definition"); + CHECK(logicnum < LF_EXTERNAL, "Incorrect use of getisfd() with external file definition"); isfd = new isdef ; isfd->r = new RecDes ; isfd->d = new FileDes ; @@ -814,7 +813,7 @@ TBaseisamfile::TBaseisamfile( for (int i=0; iln = i+EXTERNAL_FILE; // Primo slot libero + _logicnum = _isamfile->ln = i+LF_EXTERNAL; // Primo slot libero ext_files[i] = _isamfile; break; } @@ -853,7 +852,7 @@ const char* TBaseisamfile::name() const const char* TBaseisamfile::filename() const { - if (_isamfile == NULL && num() < EXTERNAL_FILE) + if (_isamfile == NULL && num() < LF_EXTERNAL) { TDir d; d.get(num()); @@ -869,7 +868,7 @@ const char* TBaseisamfile::filename() const const char* TBaseisamfile::description() { - if (_isamfile == NULL && num() < EXTERNAL_FILE) + if (_isamfile == NULL && num() < LF_EXTERNAL) { TDir d; d.get(num()); @@ -1377,6 +1376,69 @@ int TBaseisamfile::_open( return (_lasterr); } +int TBaseisamfile::_open_ex( + unsigned int mode, // @parm Indica il modo di apertura del file (default _manulock) + bool index) // @parm Indica se aprire con indici o meno (default TRUE) + +// @comm Il parametro

puo' assumere i valori: +// +// @flag _manulock | Il lock dei record viene fatto manualmente +// @flag _exclock | Il file viene aperte in modo esclusivo +// @flag _autolock | Il lock dei record viene fatto in modo automatico +// @comm Il parametro

puo' assumere i valori: +// +// @flag TRUE | Il file viene aperto con indici +// @flag FALSE | Il file viene aperto senza indici + +{ + if (openf[num() - 1] != NULL) + return _isalropen; // Gia' aperto + + getisfd(_isamfile,num()); + + if (filehnd()->r->NKeys <= 0) + { + relisfd(_isamfile); + return _lasterr = _ispatherr; // Nessun indice + } + + if ((filehnd()->fhnd = DB_open(filehnd()->d->SysName,mode==_excllock,index)) >= 0) + { + TRecnotype n=DB_reccount(filehnd()->fhnd); + TDir d; + d.get(num(),_nolock,_nordir,_sysdirop); + if (d.is_com()) d.get(num(),_nolock,_comdir,_sysdirop); + if ((filehnd()->d->EOD != n && n > 0) || (n > d.eox())) + { + filehnd()->d->EOD = d.eod() = n; + filehnd()->d->EOX = d.eox() = n; + if (d.is_com()) + d.put(num(),_comdir,_sysdirop); + else + d.put(num(),_nordir,_sysdirop); + } + filehnd()->ln = num(); + openf[num() - 1] = filehnd(); + _recno = RECORD_NON_FISICO; + + setkey(1); + _lasterr = NOERR; + } + else + { + _lasterr = get_error(filehnd()->fhnd); + if (_lasterr == -60) + { + if (fexist(filename())) + _lasterr = _islocked; + else + _lasterr = 2; + } + relisfd(_isamfile); + } + return _lasterr; +} + int TBaseisamfile::_close() @@ -1404,7 +1466,7 @@ int TBaseisamfile::_close() err=DB_close(filehnd()->fhnd); if (err != NOERR) err = get_error(err); if ((err == NOERR) && (num() > 0)) - openf[num() - 1] = NULL ; + openf[num() - 1] = NULL; } else if (filehnd() == NULL) @@ -1463,7 +1525,8 @@ TLocalisamfile::TLocalisamfile( { if (linkrecinst <= TRUE) { - open(); + if (open() != NOERR) + fatal_box("Impossibile aprire il file %d", logicnum); if (_was_open) _oldkey = getkey(); setkey(1); @@ -1541,7 +1604,7 @@ int TLocalisamfile::open(unsigned int mode) } else { - err = _open(mode, TRUE); + err = _open_ex(mode, TRUE); _was_open = FALSE; } @@ -1855,7 +1918,7 @@ int TExternisamfile::close() if (err != NOERR) err = get_error(err); if (err == NOERR) { - ext_files[_isamfile->ln - EXTERNAL_FILE] = NULL; + ext_files[_isamfile->ln - LF_EXTERNAL] = NULL; relisfd(_isamfile); clearfilehnd(); } @@ -2252,7 +2315,8 @@ int TSystemisamfile::update( // @rdesc Ritorna NOERR se l'operazione di compattamento e' riuscita, altrimenti il codice di // di errore generato (vedi ). int TSystemisamfile::packfile( - bool vis) // @parm Indica se visualizzare lo stato dell'operazione + bool vis, // @parm Indica se visualizzare lo stato dell'operazione + bool zap) // @parm Indica se distruggere tutti i records // @xref @@ -2260,14 +2324,22 @@ int TSystemisamfile::packfile( int err=NOERR; TDir d; - d.get(num(),_nolock, _nordir,_sysdirop); +// d.get(num(),_nolock, _nordir,_sysdirop); d.get(num(),_nolock, (d.is_com()) ? _comdir : _nordir); CHECKS(filehnd() == NULL, "Can't pack open file", (const char*)filename()); - err=DB_packfile(vis,d.name(),d.eod()); + err=DB_packfile(vis, d.name(), zap ? 0L : d.eod()); + + if (zap && err == NOERR) + err = packindex(vis, FALSE); if (err == NOERR && curr().has_memo()) err = DB_packmemo(vis,d.name()); - if (err != NOERR) err = get_error(err); - if (err != NOERR) error_box("Errore in compattamento dati.\nFile %d : %d", num(),err); + + if (err != NOERR) + { + err = get_error(err); + if (err != NOERR) + error_box("Errore in compattamento dati.\nFile %d : %d", num(),err); + } setstatus(err); return err; } @@ -2339,12 +2411,12 @@ int TSystemisamfile::load( // @xref { - FILE* fl = fopen(from, "r"); int err=NOERR; + FILE* fl = fopen(from, "r"); if (fl == NULL) { - error_box("Non riesco ad aprire il file %s",from); - return 2; + error_box("Impossibile aprire il file %s",from); + return err = 2; } TRecnotype r = 0, e = 0, nitems = 0, nread = 0; TString16 firm, year, attprev("00000"); @@ -2362,10 +2434,10 @@ int TSystemisamfile::load( attprev = ditte.get("CODATTPREV"); } if (fl == NULL) - { + { // Come fa' ad arrivare qui, se fl non puo' essere NULL???? clearerr(fl); setstatus(err); - return err; + return err; } char w[80]; while ((fgets(w, 80, fl) != NULL)) @@ -2379,53 +2451,61 @@ int TSystemisamfile::load( fseek(fl, 0L, SEEK_END); nitems = ftell(fl) - nitems; fclose(fl); - TScanner f(from); - - open(); + const bool open_now = filehnd() == NULL; + if (open_now) + { + err = open_ex(_excllock); + setstatus(err); + return err; + } + + TScanner f(from); TToken_string s(1024, fs); bool fixedlen = (fs == '\0'); int nflds = curr().items(); - TArray fld(nflds); + TString_array fld(nflds); int len[MaxFields]; TString sfd(3); TString s1(64); bool lcf = FALSE; if (f.paragraph("Header")) - { - f.equal(); - const long level = atol(f.line()); - if (level > get_std_level()) - error_box("L'archivio %s e' stato generato con gli archivi di livello %ld%/%ld.\n Il livello attuale e' %ld/%ld.\n Convertire gli archivi e ripetere l' operazione.", - from, level/100, level%100, get_std_level()/100, get_std_level()%100); - lcf = getlcf(level); + { + int equal; + TString key; nflds = 0; - TToken_string s2(f.line()); - int p = s2.find('='); - if (p > 0) + while ((equal = f.line().find('=')) > 0) { - s1 = s2.left(p); - s2.ltrim(p+1); - } - else s1.cut(0); - while (s1 == "Fields") - { - for (const char * fd = s2.get(); fd != NULL; fd = s2.get()) + key = f.token().left(equal); + key.trim(); + if (key == "Version") { - TToken_string wfd(fd, ','); - fld.add(new TString(wfd.get())); - len[nflds] = wfd.get_int(); - nflds++; - } - s2 = f.line(); - p = s2.find('='); - if (p > 0) + const long level = atol(f.token().mid(equal+1)); + if (level > prefix().filelevel()) + error_box("L'archivio %s e' stato generato con gli archivi di livello %ld%/%ld.\n Il livello attuale e' %ld/%ld.\n Convertire gli archivi e ripetere l' operazione.", + from, level/100, level%100, get_std_level()/100, get_std_level()%100); + lcf = getlcf(level); + } else + if (key == "File") { - s1 = s2.left(p); - s2.ltrim(p+1); - } - else s1.cut(0); + const int logic = atoi(f.token().mid(equal+1)); + if (logic != num()) + error_box("L'archivio %s e' stato generato dal file %d", + from, logic); + } else + if (key == "Fields") + { + TToken_string riga = f.token().mid(equal+1); + TToken_string wfd(32, ','); + FOR_EACH_TOKEN(riga, fd) + { + wfd = fd; wfd.strip_spaces(); + fld.add(wfd.get(0)); + len[nflds] = wfd.get_int(); + nflds++; + } + } } } else @@ -2437,6 +2517,7 @@ int TSystemisamfile::load( len[j] = (curr().type(wfld) == _datefld) ? 10 : curr().length(wfld); } } + if (!f.paragraph("Data")) { error_box("Formato dei dati non valido"); @@ -2451,8 +2532,7 @@ int TSystemisamfile::load( s1.format("Imp. archivio %s\n%6ld records %6ld errori - %3d", filename(), r, e, last); TProgind p(nitems, s1, TRUE, TRUE, 70); - s = f.line(); - while (s.not_empty() && !p.iscancelled()) + for (s = f.line(); s.not_empty() && !p.iscancelled(); s = f.line()) { if (extended) { @@ -2498,20 +2578,27 @@ int TSystemisamfile::load( put((const TString&) fld[j], s2); } } - if (write() == NOERR) r++; + + int err = write(); + if (err == _isreinsert) + err = rewrite(); + + if (err == NOERR) + r++; else { -#ifdef DBG - yesnofatal_box("Numero linea relativa all'errore: %ld",r+e+1); -#endif + error_box("Errore di scrittura alla riga %ld", r+e+1); e++; last = status(); + break; } - s = f.line(); } s1.format("Imp. archivio %s\n%6ld records %6ld errori - %3d", filename(), r, e, last); p.set_text(s1); - close(); + + if (!open_now) + close(); + setstatus(err); return err; } @@ -2568,7 +2655,8 @@ int TSystemisamfile::dump( TProgind p(nitems, s, TRUE, TRUE, 70); TString s1; - fprintf(f, "[Header]\nVersion=%ld", prefix().filelevel()); + fprintf(f, "[Header]\nVersion=%ld\nFile=%d", + prefix().filelevel(), num()); for (int k = 0; k < nflds; k++) { if ((k % 10) == 0) fprintf(f, "\nFields="); @@ -2693,13 +2781,13 @@ TRectype::TRectype(const TBaseisamfile* i) bool has_memo_fld = FALSE; _logicnum = i->num(); - if (i->filehnd() != NULL && _logicnum < EXTERNAL_FILE) + if (i->filehnd() != NULL && _logicnum < LF_EXTERNAL) { _length = DB_reclen(i->filehnd()->fhnd); has_memo_fld = rec_has_memo(rec_des()); } else - if (_logicnum >= EXTERNAL_FILE) + if (_logicnum >= LF_EXTERNAL) { _length = i->filehnd()->d->LenR; has_memo_fld = rec_has_memo(i->filehnd()->r); @@ -2889,7 +2977,7 @@ HIDDEN int fld_cmp(const char* a, const char* b, int len, bool number) RecDes* TRectype::rec_des() const { - const isdef* i = _logicnum < EXTERNAL_FILE ? openf[_logicnum-1] : ext_files[_logicnum - EXTERNAL_FILE]; + const isdef* i = _logicnum < LF_EXTERNAL ? openf[_logicnum-1] : ext_files[_logicnum - LF_EXTERNAL]; CHECKD(i, "Can't use a record of closed file ", _logicnum); RecDes* r = i->r; CHECKD(r, "Missing record description of file", _logicnum); diff --git a/include/isam.h b/include/isam.h index 3e8542f6f..e5b128b95 100755 --- a/include/isam.h +++ b/include/isam.h @@ -332,6 +332,8 @@ protected: void recover(); // @cmember Apre il file isam di base con lock, permettendo di specificare se usare gli indici o no int _open(unsigned int mode = _manulock, bool index = TRUE); + int _open_ex(unsigned int mode = _manulock, bool index = TRUE); + // @cmember Chiude il file isam di base int _close(); @@ -556,7 +558,11 @@ public: int flags(bool updateeod = FALSE); // @cmember Apre un file di isam con lock (vedi ) int open(unsigned int mode = _manulock, bool index = TRUE) - { return _open(mode, index);} + { return _open(mode, index); } + + int open_ex(unsigned int mode = _manulock, bool index = TRUE) + { return _open_ex(mode, index); } + // @cmember Chiude il file di isam int close() { return _close();} @@ -608,7 +614,7 @@ public: // @cmember Esegue sia e int pack(bool vis = TRUE, bool ask = TRUE); // @cmember Rimuove fisicamente i record cancellati - int packfile(bool vis = TRUE); + int packfile(bool vis = TRUE, bool zap = FALSE); // @cmember Rimuove fisicamente gli indici cancellati int packindex(bool vis = TRUE, bool ask = TRUE); diff --git a/include/lffiles.h b/include/lffiles.h index b58caf11e..ada7e91d6 100755 --- a/include/lffiles.h +++ b/include/lffiles.h @@ -100,7 +100,7 @@ #define LF_BENEM 94 #define LF_FAMIGLIE 95 #define LF_SEZIONI 96 -#define LF_MEDICI 97 +#define LF_MEDICI 97 #define LF_RIGHEF 98 #define LF_RIGHEF1 99 @@ -137,6 +137,8 @@ #define LF_ATOPERA 130 #define LF_ATROPERA 131 +#define LF_EXTERNAL 1000 // Files with id >= are considered to be externals + #define CNF_GENERAL 10000 #define CNF_STUDIO CNF_GENERAL + 1 #define CNF_DITTA CNF_GENERAL + 2