#include #include #include #include #include "bainstlib.h" #include "bainsta.h" // ******************************** // classe TSystemtempfile // ******************************** // classe provvisoria per i system file temporanei, ovvero file temporanei con // caricamento/scaricamento // @doc EXTERNAL // @mfunc Importa un file ascii // // @rdesc Ritorna NOERR se l'operazione di lettura e' riuscita, altrimenti il codice di // di errore generato (vedi ). int TSystemtempfile::load( const char* from, // @parm Nome del file da importare char fs, // @parm Carattere separatore di campo (default ) char fd, // @parm Carattere delimitatore di campi (default '\\0') char rs, // @parm Carattere separatore di record (default '\\n') bool vis, // @parm Indica se visualizzare lo stato dell'operazione (default TRUE) bool extended) // @parm Indica se interpretare alcune stringhe come macro (default FALSE) // @comm Se

e' TRUE e trova alcune stringhe col formato %stringa% (es. %frm%) // ne sostituisce i valori (es. ditta corrente). // @xref { FILE* fl = fopen(from, "r"); int err=NOERR; if (fl == NULL) { error_box("Non riesco ad aprire il file %s",from); return 2; } TRecnotype r = 0, e = 0, nitems = 0, nread = 0; TString16 firm, year, attprev("00000"); if (extended) { TDate d(TODAY); TLocalisamfile ditte(LF_NDITTE); firm.format("%05ld", prefix().get_codditta()); year.format("%04d", d.year()); ditte.zero(); ditte.put("CODDITTA", firm); if (ditte.read() == NOERR) attprev = ditte.get("CODATTPREV"); } if (fl == NULL) { clearerr(fl); setstatus(err); return err; } char w[256]; while ((fgets(w, sizeof(w), fl) != NULL)) { if (strncmp(w, "[Data]", 6) == 0) { nitems = ftell(fl); break; } } fseek(fl, 0L, SEEK_END); nitems = ftell(fl) - nitems; fclose(fl); TScanner f(from); TToken_string s(1024, fs); bool fixedlen = (fs == '\0'); int nflds = curr().items(); TArray fld(nflds); int len[MaxFields]; TString sfd(3); TString s1(64); if (f.paragraph("Header")) { f.equal(); const unsigned int level = atoi(f.line()); const unsigned int stdlev = prefix().get_stdlevel(); if (level > stdlev) 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, stdlev/100, stdlev%100); nflds = 0; while (true) { const TString& riga = f.line(); //quando termina la testata deve uscire dal ciclo! if (riga.blank() || riga[0] == ']') break; //legge il tracciato record if (riga.starts_with("Fields")) { TToken_string s2 = riga.after("="); for (const char * fd = s2.get(); fd != NULL; fd = s2.get()) { TToken_string wfd(fd, ','); fld.add(new TString(wfd.get())); len[nflds] = wfd.get_int(); nflds++; } } } } else { for (int j = 0; j < nflds; j++) { fld.add(TString(curr().fieldname(j)), j); const TString & wfld = (const TString & ) fld[j]; len[j] = (curr().type(wfld) == _datefld) ? 10 : curr().length(wfld); } } if (!f.paragraph("Data")) { error_box("Formato dei dati non valido"); //close(); err = 1; setstatus(err); return err; } if (fd) sfd << fd; int last = NOERR; 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()) { if (extended) { int p, i; while ((p = s.find("%yr%")) >= 0) for (i = 0; i < 4; i++) s[p + i] = year[i]; while ((p = s.find("%frm%")) >= 0) for (i = 0; i < 5; i++) s[p + i] = firm[i]; while ((p = s.find("%att%")) >= 0) for (i = 0; i < 5; i++) s[p + i] = attprev[i]; } if ((r + e) % 50 == 0) { s1.format("Imp. archivio %s\n%6ld records %6ld errori - %3d", filename(), r, e, last); p.set_text(s1); } p.setstatus(nread + 1); nread += s.len() + 1; zero(); if (fixedlen) { int pos = 0; for (int j = 0; j < nflds; j++) { s1 = s.mid(pos,len[j]); s1.rtrim(); put((const TString&) fld[j], s1); pos += len[j]; } } else { s.restart(); for (int j = 0; j < nflds; j++) { char* s2 = (char*) s.get(); if (fd) { s2++; s2[strlen(s2) - 1] = '\0'; } put((const TString&) fld[j], s2); } } if (write() == NOERR) r++; else { #ifdef DBG yesnofatal_box("Numero linea relativa all'errore: %ld",r+e+1); #endif e++; last = status(); } s = f.line(); } s1.format("Imp. archivio %s\n%6ld records %6ld errori - %3d", filename(), r, e, last); p.set_text(s1); //close(); setstatus(err); return err; } // @mfunc Esporta VERSO un file ascii. // // @rdesc Ritorna NOERR se l'operazione di esportazione e' riuscita, altrimenti il codice di // di errore generato (vedi ). int TSystemtempfile::dump( const char* to, // @parm Nome del file verso quale esportare int nkey, // @parm Numero della chiave di ordinamento con cui scaricare i dati (defualt 1) char fs, // @parm Carattere seperatore di campo (defualt ) char fd, // @parm Carattere delimitatore di campo (default '\\0') char rs, // @parm Carattere separatore di record (default '\\n') bool vis, // @parm Indica se visualizzare lo stato dell'operazione (defualt TRUE) bool withdeleted) // @parm Indica se scaricare anche i record cancellati (dafault FALSE) // @xref { FILE* f = fopen(to, "w"); if (f == NULL) { setstatus(2); return 2; } if (withdeleted) nkey = 0; int err = ferror(f); //open(FALSE, nkey ? TRUE : FALSE); TString s(512); bool fixedlen = (fs == '\0'); int nflds = curr().items(); TArray fld(nflds); TBit_array rjust(nflds); int len[MaxFields]; int j; for (j = 0; j < nflds; j++) { fld.add(TString(curr().fieldname(j)), j); const TString & wfld = (const TString&) fld[j]; const TFieldtypes t = curr().type(wfld); rjust.set(j, t == _intfld || t == _longfld || t == _realfld || t == _wordfld || t == _intzerofld || t == _longzerofld); len[j] = (t == _datefld) ? 10 : curr().length(wfld); } TRecnotype i = 0; const TRecnotype nitems = items(); s.format("Esportazione archivio %s", filename()); TProgind p(nitems, s, TRUE, TRUE, 70); TString s1; fprintf(f, "[Header]\nVersion=%ld", (long) prefix().filelevel()); for (int k = 0; k < nflds; k++) { if ((k % 10) == 0) fprintf(f, "\nFields="); else fprintf(f, "|"); fprintf(f, "%s,%d", (const char *) (const TString&) fld[k], len[k]); } fprintf(f, "\n\n[Data]\n"); if (nkey) { setkey(nkey); for ( first(); status() == NOERR && !p.iscancelled(); next(), i++) { p.setstatus(i + 1); s = ""; for (j = 0; j < nflds; j++) { if (fixedlen) { s1 = get((const TString&)fld[j]); if (rjust[j]) s1.right_just(len[j]); else s1.left_just(len[j]); } else { s1 = ""; if (j && fs) s1 << fs; if (fd) s1 << fd; s1 << get((const TString&)fld[j]); if (fd) s1 << fd; } s << s1; } fprintf(f, "%s%c", (const char*) s, rs); } } else { for (i = 0; i < nitems && !p.iscancelled(); i++) { zero(); p.setstatus(i + 1); readat(i + 1); s=""; if (withdeleted || curr().valid()) { for (j = 0; j < nflds; j++) { if (fixedlen) { s1 = get((const TString&)fld[j]); if (rjust[j]) s1.right_just(len[j]); else s1.left_just(len[j]); } else { s1 = ""; if (j && fs) s1 << fs; if (fd) s1 << fd; s1 << get((const TString&)fld[j]); if (fd) s1 << fd; } s << s1; } fprintf(f, "%s%c", (const char*) s, rs); } } } p.setstatus(nitems); //close(); fclose(f); setstatus(err); return err; } // ******************************** // classe TStd_filename // ******************************** bool TStd_filename::check(bool verbose,const char * n) { if (n && *n) set(n); insert("STD\\"); if (!exist()) { *this = name(); if (!exist()) { insert("STD\\"); insert(prefix().get_studio()); if (!exist()) { if (verbose) error_box("Non riesco a trovare il file: %s", (const char *)name()); cut(0); return FALSE; } } } return TRUE; } // ******************************** // classe TInstallmodule_app // ******************************** const char* TInstallmodule_app::module_name() const { return dongle().module_code2desc(module_number()); } const char* TInstallmodule_app::module_code() const { return dongle().module_code2name(module_number()); } bool TInstallmodule_app::create() { if (modal()) { TString16 maskname; maskname.format("bainst%02d",module_number()); _m = new TMask(maskname); } else _m = NULL; TConfig campo_ini(CONFIG_INSTALL, "Main"); const int type = campo_ini.get_int("Type"); if (type == 1 || type == 2) _test_database = true; else _test_database = campo_ini.get_bool("TestDatabase"); return TSkeleton_application::create(); } bool TInstallmodule_app::destroy() { if (_m) delete _m; return TSkeleton_application::destroy(); } bool TInstallmodule_app::something_to_load() const { bool yes = false; //dati comuni TFilename file_to_load = module_code(); file_to_load << "tabcom.txt"; yes = ci_sono_novita(file_to_load, LF_TABCOM); //dati ditta if (!yes) { file_to_load = module_code(); file_to_load << "tab.txt"; yes = ci_sono_novita(file_to_load, LF_TAB); } //dati di modulo if (!yes) { file_to_load = module_code(); file_to_load << "tabmod.txt"; yes = ci_sono_novita(file_to_load, LF_TABMOD); } return yes; } bool TInstallmodule_app::preload_mask() { if (test_database()) { if (something_to_load()) _m->set(F_DEFAULTSDATA,"X"); } else { _m->set(F_DEFAULTSDATA," "); _m->disable(F_DEFAULTSDATA); } return true; } bool TInstallmodule_app::ci_sono_novita(const TFilename& filesource, const int filedest) const { bool ok = (filedest == LF_TAB || filedest == LF_TABCOM || filedest == LF_TABMOD) && filesource.exist(); if (ok) { TScanner scan(filesource); ok = scan.paragraph("Data"); if (ok) { TIsamfile ifile(filedest); ok = ifile.open_ex() != NOERR; //se il file non e' buono va aggiornato... if (!ok) { ok = ifile.read(_isfirst) != NOERR; //se il file e' vuoto ci sono sicuramente novita'!!! TToken_string tok; while (!ok) { tok = scan.line(); if (tok.blank()) break; if (filedest == LF_TABMOD) { ifile.put("MOD", tok.get(0)); ifile.put("CUST", tok.get(1)); ifile.put("COD", tok.get(2)); ifile.put("CODTAB", tok.get(3)); } else { ifile.put("COD", tok.get(0)); ifile.put("CODTAB", tok.get(1)); } ok = ifile.read() != NOERR; } ifile.close(); } } } return ok; } bool TInstallmodule_app::something_checked() const { return _m->get_bool(F_DEFAULTSDATA); } void TInstallmodule_app::main_loop() { bool ok = true; if (modal()) { if (test_database()) //solo se puoi modificare i dati ti e' permesso di entrare, oh utonto! { preload_mask(); if (something_checked() && _m->run() != K_QUIT) //modifica per far apparire il caricamento dei dati standard solo se non ci sono { if (something_checked()) //rifare il test in caso di flaggaggio manuale { install_com(); install_firm(); install_mod(); } } else ok = false; } } if (ok) post_installer(); } bool TInstallmodule_app::install_firm() { // ciclo su ogni ditta TStd_filename txtfile(module_code()); txtfile << "tab.txt"; if (txtfile.check()) { TLocalisamfile ditte(LF_NDITTE); for (ditte.first(); !ditte.eof() ; ditte.next()) { const int newditta=ditte.get_int("CODDITTA"); if (prefix().exist(newditta)) { set_firm(newditta); TSystemisamfile tab(LF_TAB); tab.load(txtfile); } } // carica le tabelle nei file di default TSystemtempfile tabstd(LF_TAB); TFilename stdtabname(prefix().get_studio()); stdtabname << "STD\\lf0005.txt"; tabstd.load(txtfile); tabstd.load(stdtabname); tabstd.set_autodel(); TFilename tempfile; tempfile.temp(); // riconverte le macro delle date tabstd.dump(tempfile); TScanner scan(tempfile); FILE *dest=fopen((const char *)stdtabname,"w"); while (TRUE) { TString &line=scan.line(); if (line.empty()) break; int pos=(line.find("-0000")); while (pos >= 0 ) { line.overwrite("%yr%",pos+1); pos=(line.find("-0000")); } line << '\n' ; fputs((const char * )line,dest); } fclose(dest); } return true; } bool TInstallmodule_app::install_com() { bool ok = true; TStd_filename txtfile(module_code()); txtfile << "tabcom.txt"; if (txtfile.check()) { TSystemisamfile tabcom(LF_TABCOM); tabcom.load(txtfile); } return ok; } bool TInstallmodule_app::install_mod() { // ciclo su ogni ditta TStd_filename txtfile(module_code()); txtfile << "tabmod.txt"; if (txtfile.check()) { TLocalisamfile ditte(LF_NDITTE); for (ditte.first(); !ditte.eof() ; ditte.next()) { const int newditta=ditte.get_int("CODDITTA"); if (prefix().exist(newditta)) { set_firm(newditta); TSystemisamfile tabmod(LF_TABMOD); tabmod.load(txtfile); } } } return true; } void TInstallmodule_app::run(int argc, char* argv[]) { TString title("Installazione "); title << module_name(); TSkeleton_application::run(argc, argv,(const char* )title); } void TInstallmodule_app::set_tab_app(const char * appname) { TConfig ditta(CONFIG_DITTA); ditta.set_paragraph(module_code()); ditta.set("TabApp",appname); }