Patch level : 4.0 no patch
Files correlati : Ricompilazione Demo : [ ] Commento : Aggiunte funzioni dump con lista di campi e overwrite ai Systemisamfile git-svn-id: svn://10.65.10.50/trunk@14575 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
bf955ed08e
commit
4428975ba2
407
include/isam.cpp
407
include/isam.cpp
@ -2608,6 +2608,227 @@ int TSystemisamfile::load(
|
||||
|
||||
// @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 <t TIsamerr>).
|
||||
int TSystemisamfile::overwrite(
|
||||
const char* from, // @parm Nome del file da importare
|
||||
char fs, // @parm Carattere separatore di campo (default <pipe>)
|
||||
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)
|
||||
|
||||
// @comm Se <p extended> e' TRUE e trova alcune stringhe col formato %stringa% (es. %frm%)
|
||||
// ne sostituisce i valori (es. ditta corrente).
|
||||
|
||||
// @xref <mf TSystemisamfile::dump>
|
||||
|
||||
{
|
||||
int err=NOERR;
|
||||
FILE* fl = fopen(from, "r");
|
||||
if (fl == NULL)
|
||||
{
|
||||
error_box("Impossibile aprire il file %s",from);
|
||||
clearerr(fl);
|
||||
setstatus(2);
|
||||
return 2;
|
||||
}
|
||||
TRecnotype r = 0, e = 0, nitems = 0;
|
||||
TString16 firm, year, attprev("00000");
|
||||
|
||||
fseek(fl, 0L, SEEK_END);
|
||||
nitems = ftell(fl);
|
||||
fclose(fl);
|
||||
|
||||
err = _open_ex();
|
||||
if (err != NOERR)
|
||||
{
|
||||
error_box("Impossibile aprire il file %d", _logicnum);
|
||||
return err;
|
||||
}
|
||||
|
||||
TScanner f(from);
|
||||
bool fixedlen = (fs == '\0');
|
||||
TToken_string s(1024, fixedlen ? char(255) : fs);
|
||||
int nflds = curr().items();
|
||||
TString_array fld(nflds);
|
||||
TString_array keyfld(nflds);
|
||||
TAssoc_array vals;
|
||||
int len[MaxFields];
|
||||
TString sfd(3);
|
||||
TString s1(256);
|
||||
bool lcf = FALSE;
|
||||
|
||||
if (f.paragraph("Header"))
|
||||
{
|
||||
int equal;
|
||||
TString key;
|
||||
nflds = 0;
|
||||
while ((equal = f.line().find('=')) > 0)
|
||||
{
|
||||
key = f.token().left(equal);
|
||||
key.trim();
|
||||
if (key == "Version")
|
||||
{
|
||||
const unsigned int level = atoi(f.token().mid(equal+1));
|
||||
if (level > prefix().filelevel())
|
||||
{
|
||||
const unsigned int stdlevel = prefix().get_stdlevel();
|
||||
error_box(FR("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, stdlevel/100, stdlevel%100);
|
||||
}
|
||||
lcf = getlcf(level);
|
||||
} else
|
||||
if (key == "File")
|
||||
{
|
||||
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++;
|
||||
}
|
||||
const RecDes& rd = * curr().rec_des();
|
||||
const KeyDes& kd = rd.Ky[0];
|
||||
|
||||
for (int i = 0 ; i < kd.NkFields; i++)
|
||||
{
|
||||
const int nf = kd.FieldSeq[i] % MaxFields;
|
||||
const RecFieldDes& rf = rd.Fd[nf];
|
||||
keyfld.add(rf.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nflds == 0 || fld.items() == 0)
|
||||
{
|
||||
nflds = curr().items();
|
||||
for (int j = 0; j < nflds; j++)
|
||||
{
|
||||
fld.add(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 file non valido: manca il paragrafo [Data]");
|
||||
close();
|
||||
err = 1;
|
||||
setstatus(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (fd) sfd << fd;
|
||||
int last = NOERR;
|
||||
|
||||
s1.format("Importazione archivio %d da %s\n%6ld records %6ld errori - %3d",
|
||||
_logicnum, from, r, e, last);
|
||||
TProgind p(nitems, s1, true, true, 78);
|
||||
long pos = 16*nflds;
|
||||
for (s = f.line(); s.not_empty() && !p.iscancelled(); s = f.line())
|
||||
{
|
||||
if ((r + e) % 50 == 0)
|
||||
{
|
||||
s1.format("Importazione archivio %d da %s\n%6ld records %6ld errori - %3d",
|
||||
_logicnum, from, r, e, last);
|
||||
p.set_text(s1);
|
||||
}
|
||||
|
||||
pos += s.len()+2;
|
||||
p.setstatus(pos);
|
||||
zero();
|
||||
if (fixedlen)
|
||||
{
|
||||
int pos = 0;
|
||||
for (int j = 0; j < nflds; j++)
|
||||
{
|
||||
const TString & fldname = fld.row(j);
|
||||
|
||||
s1 = s.mid(pos,len[j]);
|
||||
s1.rtrim();
|
||||
vals.add(fldname, s1);
|
||||
pos += len[j];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
s.restart();
|
||||
for (int j = 0; j < nflds; j++)
|
||||
{
|
||||
const TString & fldname = fld.row(j);
|
||||
|
||||
s1 = s.get();
|
||||
if (fd)
|
||||
{
|
||||
s1.rtrim(1);
|
||||
s1.ltrim(1);
|
||||
}
|
||||
if (curr().type(fldname) == _memofld)
|
||||
{
|
||||
TString s2 = s1;
|
||||
s1 = esc(s2);
|
||||
}
|
||||
vals.add(fldname, s1);
|
||||
}
|
||||
}
|
||||
|
||||
FOR_EACH_ARRAY_ROW(keyfld, r0, kfldname)
|
||||
{
|
||||
const TString * value = (const TString *) vals.objptr(*kfldname);
|
||||
|
||||
if (value != NULL)
|
||||
put(*kfldname, *value);
|
||||
}
|
||||
|
||||
const bool nuovo = read() != NOERR;
|
||||
|
||||
if (nuovo)
|
||||
zero();
|
||||
|
||||
|
||||
FOR_EACH_ASSOC_STRING(vals, obj, fldname, value)
|
||||
put(fldname, value);
|
||||
|
||||
const int err = nuovo ? write() : rewrite();
|
||||
|
||||
if (err == NOERR)
|
||||
r++;
|
||||
else
|
||||
{
|
||||
e++;
|
||||
last = status();
|
||||
}
|
||||
}
|
||||
s1.format("Importazione archivio %d da %s\n%6ld records %6ld errori - %3d",
|
||||
_logicnum, from, r, e, last);
|
||||
p.set_text(s1);
|
||||
|
||||
close();
|
||||
|
||||
setstatus(err);
|
||||
|
||||
//aggiorna lo sheet con i nuovi valori di EOX EOD caricati
|
||||
if (err == NOERR)
|
||||
update_file();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// @doc EXTERNAL
|
||||
|
||||
// @mfunc Esporta VERSO un file ascii.
|
||||
//
|
||||
// @rdesc Ritorna NOERR se l'operazione di esportazione e' riuscita, altrimenti il codice di
|
||||
@ -2789,6 +3010,192 @@ int TSystemisamfile::dump(
|
||||
return err;
|
||||
}
|
||||
|
||||
int TSystemisamfile::dump(
|
||||
const char* to, // @parm Nome del file verso quale esportare
|
||||
TToken_string & field_list, // @parm Lista dei campi
|
||||
int nkey, // @parm Numero della chiave di ordinamento con cui scaricare i dati (defualt 1)
|
||||
char fs, // @parm Carattere seperatore di campo (defualt <pipe>)
|
||||
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)
|
||||
const char * filter) // @parm Indica l'espressione filtro
|
||||
|
||||
// @xref <mf TSystemisamfile::load>
|
||||
|
||||
{
|
||||
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();
|
||||
TString_array fld(nflds);
|
||||
TBit_array rjust(nflds);
|
||||
int len[MaxFields];
|
||||
int j = 0;
|
||||
|
||||
FOR_EACH_TOKEN(field_list, str)
|
||||
{
|
||||
const TString16 fldname(str);
|
||||
|
||||
fld.add(fldname);
|
||||
const TFieldtypes t = curr().type(fldname);
|
||||
rjust.set(j, t == _intfld || t == _longfld || t == _realfld ||
|
||||
t == _wordfld || t == _intzerofld || t == _longzerofld);
|
||||
len[j++] = (t == _datefld) ? 10 : curr().length(fldname);
|
||||
if (fixedlen && t == _memofld)
|
||||
return error_box("Non e' possibile scaricare a lunghezza fissa un file con campi memo");
|
||||
}
|
||||
TRecnotype i = 0;
|
||||
const TRecnotype nitems = items();
|
||||
|
||||
s.format("Esportazione archivio %s", filename());
|
||||
TProgind p(nitems, s, TRUE, TRUE, 70);
|
||||
TString s1, sfld;
|
||||
|
||||
fprintf(f, "[Header]\nVersion=%ld\nFile=%d",
|
||||
(long) prefix().filelevel(), num());
|
||||
FOR_EACH_ARRAY_ROW(fld, k, el)
|
||||
{
|
||||
if ((k % 10) == 0) fprintf(f, "\nFields=");
|
||||
else fprintf(f, "|");
|
||||
|
||||
const TString & fldname =fld.row(k);
|
||||
|
||||
fprintf(f, "%s,%d", (const char *) fldname, len[k]);
|
||||
}
|
||||
fprintf(f, "\n\n[Data]\n");
|
||||
if (nkey)
|
||||
{
|
||||
setkey(nkey);
|
||||
for ( first(); status() == NOERR && !p.iscancelled(); next(), i++)
|
||||
{
|
||||
p.setstatus(i + 1);
|
||||
if (filter && *filter)
|
||||
{
|
||||
TToken_string filter_str(filter);
|
||||
TString16 fname;
|
||||
bool skip = false;
|
||||
while (!skip && !(fname=filter_str.get()).empty())
|
||||
{
|
||||
const char* fval = filter_str.get();
|
||||
const TString& cmp = get(fname);
|
||||
skip = cmp != fval;
|
||||
}
|
||||
if (skip)
|
||||
continue;
|
||||
}
|
||||
s = "";
|
||||
FOR_EACH_ARRAY_ROW(fld, j, el)
|
||||
{
|
||||
const TString & fldname =fld.row(j);
|
||||
|
||||
if (fixedlen)
|
||||
{
|
||||
s1 = get(fldname);
|
||||
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;
|
||||
sfld = get(fldname);
|
||||
if (curr().type(fldname) == _memofld)
|
||||
{
|
||||
int p = 0;
|
||||
while ((p = sfld.find('\n', 0)) >= 0)
|
||||
{
|
||||
sfld.overwrite("\\", p);
|
||||
sfld.insert("n", p+1);
|
||||
}
|
||||
}
|
||||
s1 << sfld;
|
||||
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);
|
||||
|
||||
if (filter && *filter)
|
||||
{
|
||||
TToken_string filter_str(filter);
|
||||
TString16 fname;
|
||||
bool skip = FALSE;
|
||||
while (!skip && !(fname=filter_str.get()).empty())
|
||||
{
|
||||
const char* fval = filter_str.get();
|
||||
const TString& cmp = get(fname);
|
||||
skip = cmp != fval;
|
||||
}
|
||||
if (skip)
|
||||
continue;
|
||||
}
|
||||
|
||||
s="";
|
||||
if (withdeleted || curr().valid())
|
||||
{
|
||||
FOR_EACH_ARRAY_ROW(fld, j, el)
|
||||
{
|
||||
const TString & fldname = fld.row(j);
|
||||
|
||||
if (fixedlen)
|
||||
{
|
||||
s1 = get(fldname);
|
||||
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;
|
||||
sfld = get(fldname);
|
||||
if (curr().type(fldname) == _memofld)
|
||||
{
|
||||
int p = 0;
|
||||
while ((p = sfld.find('\n', 0)) >= 0)
|
||||
{
|
||||
sfld.overwrite("\\", p);
|
||||
sfld.insert("n", p+1);
|
||||
}
|
||||
}
|
||||
s1 << sfld;
|
||||
if (fd) s1 << fd;
|
||||
}
|
||||
s << s1;
|
||||
}
|
||||
fprintf(f, "%s%c", (const char*) s, rs);
|
||||
}
|
||||
}
|
||||
}
|
||||
p.setstatus(nitems);
|
||||
close();
|
||||
fclose(f);
|
||||
setstatus(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif // FOXPRO
|
||||
|
||||
|
||||
|
@ -610,10 +610,12 @@ public:
|
||||
int zap();
|
||||
|
||||
// @cmember Importa un file ascii
|
||||
int load(const char* from, char fs = '|', char fd = '\0', char rs = '\n', bool vis = TRUE, bool extended = FALSE) ;
|
||||
int load(const char* from, char fs = '|', char fd = '\0', char rs = '\n', bool vis = true, bool extended = false) ;
|
||||
int overwrite(const char* from, char fs = '|', char fd = '\0', char rs = '\n', bool vis = true) ;
|
||||
|
||||
// @cmember Esporta VERSO un file ascii.
|
||||
int dump(const char* to, int nkey = 1, char fs = '|', char fd = '\0', char rs = '\n', bool vis = TRUE, bool withdeleted = FALSE, const char * filter =NULL);
|
||||
int dump(const char* to, int nkey = 1, char fs = '|', char fd = '\0', char rs = '\n', bool vis = true, bool withdeleted = false, const char * filter = NULL);
|
||||
int dump(const char* to, TToken_string & field_list, int nkey = 1, char fs = '|', char fd = '\0', char rs = '\n', bool vis = true, bool withdeleted = false, const char * filter = NULL);
|
||||
|
||||
virtual word class_id() const { return CLASS_SYSTEMISAMFILE; }
|
||||
virtual bool is_kind_of(word id) const { return id == class_id() || TIsamfile::is_kind_of(id); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user