MOdifiche di Matteo all classe Config per gestire un file di configurazione

qualsiasi (non codificato con un intero)


git-svn-id: svn://10.65.10.50/trunk@279 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
guy 1994-09-22 16:47:50 +00:00
parent 5f655ece6d
commit 1accdb22cf
8 changed files with 176 additions and 171 deletions

View File

@ -18,35 +18,35 @@ extern "C"
};
bool TConfig::_read_paragraph()
// ritorna TRUE se il paragrafo c'era, FALSE altrimenti
// ritorna TRUE se il paragrafo c'era, FALSE altrimenti
{
bool itwas = FALSE;
_data.destroy();
TScanner scan(_file);
if (scan.paragraph(_paragraph))
{
itwas = TRUE;
// populate array
TString l, key, val;
for (;;)
{
itwas = TRUE;
// populate array
TString l, key, val;
for (;;)
{
l = scan.line();
if (l[0] == '#') continue;
if (l == "" || l[0] == '[') break;
int ind = l.find('=');
if (ind == -1)
{
warning_box("Errore configurazione: file %s, vicino a riga %ud",
(const char*)_file, scan.linenum());
continue;
}
key = l.left(ind); key.trim();
val = l.mid(ind+1); val.trim();
// sostituzione abilitata
_data.add(key,val,TRUE);
}
l = scan.line();
if (l[0] == '#') continue;
if (l == "" || l[0] == '[') break;
int ind = l.find('=');
if (ind == -1)
{
warning_box("Errore configurazione: file %s, vicino a riga %ud",
(const char*)_file, scan.linenum());
continue;
}
key = l.left(ind); key.trim();
val = l.mid(ind+1); val.trim();
// sostituzione abilitata
_data.add(key,val,TRUE);
}
}
return itwas;
}
@ -57,10 +57,10 @@ void TConfig::_write_paragraph(ofstream& out)
cnf << '[' << _paragraph << ']';
out << cnf << '\n';
for (int i = 0; i < _data.items(); i++)
{
THash_object* o = _data.get_hashobj();
out << o->key() << "\t= " << (TString&)(o->obj()) << '\n';
}
{
THash_object* o = _data.get_hashobj();
out << o->key() << "\t= " << (TString&)(o->obj()) << '\n';
}
out << '\n';
}
@ -77,22 +77,22 @@ void TConfig::_write_file()
bool skip = FALSE, done = FALSE;
while (!in.eof())
{
in.getline(__tmp_string,sizeof(__tmp_string)-1);
l.trim();
{
in.getline(__tmp_string,sizeof(__tmp_string)-1);
l.trim();
if (cnf == l)
{
// write paragraph and all variables
_write_paragraph(out);
skip = TRUE; done = TRUE;
}
else
{
if (skip) skip = l[0] != '[';
if (!skip) out << l << '\n';
}
if (cnf == l)
{
// write paragraph and all variables
_write_paragraph(out);
skip = TRUE; done = TRUE;
}
else
{
if (skip) skip = l[0] != '[';
if (!skip) out << l << '\n';
}
}
// new paragraph
if (!done) _write_paragraph(out);
@ -106,13 +106,13 @@ void TConfig::_write_file()
void TConfig::_check_paragraph(const char* section)
{
if (section != NULL && section != _paragraph)
{
if (_dirty) _write_file();
_paragraph = section;
_dirty = FALSE;
_read_paragraph();
}
if (section != NULL && section != _paragraph)
{
if (_dirty) _write_file();
_paragraph = section;
_dirty = FALSE;
_read_paragraph();
}
}
TString& TConfig::get(const char* var, const char* section, int index)
@ -121,8 +121,8 @@ TString& TConfig::get(const char* var, const char* section, int index)
// quella specificata
static TFixed_string s(__tmp_string, 256);
TString vvar(var); if (index != -1) vvar << '(' << index << ')';
_check_paragraph(section);
_check_paragraph(section);
if (_data.is_key(vvar))
s = (TString&)_data[vvar];
@ -151,29 +151,29 @@ bool TConfig::get_bool(const char* var, const char* section, int index)
bool TConfig::set(const char* var, const char* value, const char* section,
bool force, int index)
bool force, int index)
{
// setta variabile nella sezione corrente o specificata
// se force == TRUE crea la variabile se non esiste; altrimenti
// da' errore; ritorna TRUE se la variabile c'era, FALSE diversamente
_check_paragraph(section);
TString vvar(var); if (index != -1) vvar << '(' << index << ')';
_check_paragraph(section);
TString vvar(var); if (index != -1) vvar << '(' << index << ')';
bool itwas = _data.is_key(vvar);
if (itwas && !force)
warning_box("Tentativo di ridefinizione simbolo: %s", (const char*)vvar);
else
{
_dirty = TRUE;
_data.add(vvar, new TString(value), force);
}
{
_dirty = TRUE;
_data.add(vvar, new TString(value), force);
}
return itwas;
}
bool TConfig::set(const char* var, long value, const char* section,
bool force, int index)
bool force, int index)
{
TString t; t << value;
return set(var,t,section,force,index);
@ -184,41 +184,57 @@ word TConfig::items(const char* var, const char* section)
_check_paragraph(section);
TString vvar(16);
for (int cnt = 0; /* uncazzo */ ;cnt++)
{
vvar = var; vvar << '(' << cnt << ')';
if (!_data.is_key(var))
break;
}
{
vvar = var; vvar << '(' << cnt << ')';
if (!_data.is_key(var))
break;
}
return cnt;
}
TConfig::TConfig(int which_config, const char* paragraph) :
_paragraph(paragraph), _dirty(FALSE), _ispresent(FALSE)
void TConfig::init(const char *fn, const char* pa)
{
_file = fn;
_paragraph = pa;
_dirty = FALSE;
if (!fexist(_file))
fatal_box("Impossibile aprire il file di configurazione %s", fn );
if (_paragraph.empty())
{
_paragraph = main_app().name();
_paragraph.cut(2);
}
_ispresent = _read_paragraph();
}
TConfig::TConfig(int which_config, const char* paragraph)
{
if (which_config < CONFIG_STUDIO) _file = CONFIG_FILE;
else
if (which_config == CONFIG_STUDIO) _file = CONFIG_FILE_STUDIO;
else
{
_file.format("%s/%s", MainApp()->get_firm_dir(), CONFIG_FILE_DITTA);
if (!fexist(_file))
{
fcopy(CONFIG_FILE_DITTA, _file);
if (!fexist(_file))
fatal_box("Impossibile aprire la configurazione %s",
which_config < CONFIG_STUDIO ? "generale" :
which_config == CONFIG_STUDIO ? "di studio" : "della ditta");
}
}
if (_paragraph.empty())
if (which_config == CONFIG_STUDIO) _file = CONFIG_FILE_STUDIO;
else
{
_paragraph = MainApp()->name();
_paragraph.cut(2);
}
_ispresent = _read_paragraph();
_file.format("%s/%s", main_app().get_firm_dir(), CONFIG_FILE_DITTA);
if (!fexist(_file))
fcopy(CONFIG_FILE_DITTA, _file);
}
if (!fexist(_file))
fatal_box("Impossibile aprire la configurazione %s",
which_config < CONFIG_STUDIO ? "generale" :
which_config == CONFIG_STUDIO ? "di studio" : "della ditta");
init( _file, paragraph );
}
TConfig::TConfig(const char *fn, const char* pa)
{ init(fn, pa); }
TConfig::~TConfig()
{
// il distruttore riscrive il file con le modifiche se necessario

View File

@ -12,7 +12,7 @@
class ofstream;
// questo sara' il principale, per ora non c'e'
#define CONFIG_GENERAL -1
#define CONFIG_GENERAL -1
// file parametri studio (uno per studio, per ora e' il principale)
#define CONFIG_STUDIO 0
// file parametri ditta (uno per ditta)
@ -30,8 +30,9 @@ class TConfig : public TObject
void _write_paragraph(ofstream&);
void _write_file();
void _check_paragraph(const char*);
void init(const char *fn, const char* pa);
public:
public:
// ritorna valore di variabile nella sezione corrente o in
// quella specificata; se non c'e' ritorna ""
@ -50,9 +51,9 @@ class TConfig : public TObject
// da' errore; ritorna TRUE se la sezione/var c'era, FALSE diversamente
// index come per get()
bool set(const char* var, const char* value, const char* section = NULL,
bool force = TRUE, int index = -1);
bool force = TRUE, int index = -1);
bool set(const char* var, long value, const char* section = NULL,
bool force = TRUE, int index = -1);
bool force = TRUE, int index = -1);
// TRUE se il paragrafo corrente e' nuovo
bool new_paragraph() { return !_ispresent; }
@ -64,6 +65,8 @@ class TConfig : public TObject
// il paragrafo iniziale e' il modulo corrente salvo diversa indicazione
TConfig(int which_config = CONFIG_GENERAL, const char* paragraph = NULL);
TConfig(const char* file, const char* paragraph = NULL);
// il distruttore riscrive il file con le modifiche se necessrio,
virtual ~TConfig();
};

View File

@ -10,35 +10,35 @@
extern "C" {
/* @DPUB */
extern SecDef fdir[2], rdir[2];
extern int dirfl[2], recfl[2];
extern short formflag;
extern int SerNo;
extern word ModAd;
extern char __ptprf[80];
/* @DPUB */
extern SecDef fdir[2], rdir[2];
extern int dirfl[2], recfl[2];
extern short formflag;
extern int SerNo;
extern word ModAd;
extern char __ptprf[80];
// Guy moved these outside extern
extern isfdptr* openf;
extern Str80 cprefix;
// Guy moved these outside extern
extern isfdptr* openf;
extern Str80 cprefix;
#ifdef __STDTYPES_CPP
#define extern
#endif
// extern TDitta cditta;
// extern short flprassi;
// extern short flaltmodins;
// extern TDitta cditta;
// extern short flprassi;
// extern short flaltmodins;
extern short isjournal;
extern short isjournal;
#ifdef __STDTYPES_CPP
#undef extern
#endif
/* @END */
/* @END */
/* @FPUB */
/* @FPUB */
short cverdata(char *);
TrDate cpackdata(char *);
void ceditdata(TrDate ,char *);
@ -60,10 +60,11 @@ extern short isjournal;
void CChsize(SecDef *,char *,unsigned ,unsigned ,RecNoType);
void CClose(SecDef *);
void CDelete(SecDef *,char *);
void CRead(SecDef *,RecType,RecNoType ,unsigned );
void CRead(SecDef *, RecType, RecNoType, unsigned );
void CLockRec(SecDef *, RecNoType, unsigned);
void CWrite(SecDef *,RecType,RecNoType ,unsigned );
int excllock(char *, short);
int exclunlock(char *, short);
int excllock(char *, short);
int exclunlock(char *, short);
void COpenDir(int, int);
void CCloseDir(int);
void COpenFile(int, FileDes *, int, int);
@ -94,9 +95,9 @@ extern short isjournal;
int CZeroField(char *, RecDes *, RecType);
void CZeroRec(RecDes *, RecType);
int CBuildKey(RecDes *,int ,RecType, char *);
void CBOpenFile(FilCtrl *,char *,unsigned int, int *);
void CBCloseFile(FilCtrl *,int *);
void GetHead(FilCtrl *, int, int *);
void CBOpenFile(FilCtrl *,char *,unsigned int, int *);
void CBCloseFile(FilCtrl *,int *);
void GetHead(FilCtrl *, int, int *);
int GetAPage(FilCtrl *, Page *,RecNoType ,int *);
void GetAKey( Page ,int ,int ,char *,RecNoType *);
void BTrRead(FilCtrl *,char *,char *,RecNoType *,int *);
@ -141,7 +142,7 @@ extern short isjournal;
char *sort_op(void);
void sort_stats(void);
/* @END */
/* @END */
};
#endif // __EXTCDECL_H

View File

@ -1,4 +1,4 @@
// $Id: maskfld.cpp,v 1.22 1994-09-22 07:47:53 guy Exp $
// $Id: maskfld.cpp,v 1.23 1994-09-22 16:47:41 guy Exp $
#include <xvt.h>
#include <applicat.h>
@ -2773,23 +2773,31 @@ word TRadio_field::class_id() const
void TRadio_field::create(WINDOW parent)
{
const short id = dlg(); // Salva il control id
const int items = _codes.items();
if (_prompt.not_empty())
{
const int dy = _codes.items()+2;
const int dy = _flags.persistent ? 3 : items+2;
create_prompt(parent, _width, dy);
}
_x++; _y++;
_values.restart();
const char* s;
for(_nitems = 0; (s = _values.get()) != NULL; _nitems++, _y++)
const int width = _flags.persistent ? (_width-2)/items-1 : _width-2;
for(_nitems = 0; (s = _values.get()) != NULL; _nitems++)
{
CHECKD(_nitems < MAX_RADIO, "Too many items in radio button ", id);
wincreate(WC_RADIOBUTTON, _width-2, 1, s, parent,0);
wincreate(WC_RADIOBUTTON, width, 1, s, parent,0);
_radio_ctl_win[_nitems] = _win;
_dlg += 1000;
if (_flags.persistent)
_x += width+1;
else
_y++;
}
_radio_ctl_win[_nitems] = NULL_WIN; // Comodo per debug

View File

@ -1,4 +1,4 @@
// $Id: relation.cpp,v 1.10 1994-09-22 07:48:05 guy Exp $
// $Id: relation.cpp,v 1.11 1994-09-22 16:47:45 guy Exp $
// relation.cpp
// fv 12/8/93
// relation class for isam files
@ -1074,64 +1074,34 @@ TRecnotype TCursor::readrec()
return nrec;
}
int TCursor::lock(TReclock l)
{
CLockRec(&file().filehnd()->f, _pos, l);
return file().filehnd()->f.IOR;
}
TRecnotype TCursor::operator =(const TRecnotype pos)
{
if (changed())
_totrec = update();
CHECKD(pos >= 0 && pos <= _totrec, "Bad cursor position : ", pos);
_pos = pos;
if (_pos > _totrec) _pos = _totrec;
readrec();
return _pos;
}
TCursor& TCursor::operator +=(const TRecnotype npos)
TRecnotype TCursor::operator +=(const TRecnotype npos)
{
if (changed())
_totrec = update();
_pos += npos;
if (_pos > _totrec) _pos = _totrec;
if (_pos > _totrec) _pos = _totrec;
else
if (_pos < 0) _pos = 0;
readrec();
return *this;
}
TCursor& TCursor::operator -=(const TRecnotype npos)
{
if (changed())
_totrec = update();
_pos -= npos;
if (_pos < 0) _pos = 0;
readrec();
return *this;
}
TCursor& TCursor::operator ++()
{
if (changed())
_totrec = update();
_pos++;
if (_pos > _totrec) _pos = _totrec;
readrec();
return *this;
}
TCursor& TCursor::operator --()
{
if (changed())
_totrec = update();
if (_pos) _pos--;
readrec();
return *this;
return _pos;
}

View File

@ -1,4 +1,4 @@
/* $Id: relation.h,v 1.5 1994-09-22 07:48:07 guy Exp $ */
/* $Id: relation.h,v 1.6 1994-09-22 16:47:47 guy Exp $ */
// join.h
// fv 12/8/93
// join class for isam files
@ -182,21 +182,24 @@ protected:
public:
// @FPUB
TRecnotype operator =(const TRecnotype nr); // Assegnazione
TCursor& operator +=(const TRecnotype nr); // Scorri avanti
TCursor& operator -=(const TRecnotype nr); // Scorri indietro
TCursor& operator ++(); // Avanti di un record
TCursor& operator --(); // Indietro di un record
TRecnotype operator =(const TRecnotype nr); // Va alla posizione nr
TRecnotype operator +=(const TRecnotype nr);
TRecnotype operator -=(const TRecnotype npos) { return operator +=(-npos); }
TRecnotype operator ++() { return operator +=(1); }
TRecnotype operator --() { return operator -=(1); }
TRecnotype pos() const { return _pos; }
TRecnotype items();
TRecnotype size() const { return file().eod(); }
const TString& from() const { return _keyfrom; }
const TString& to() const { return _keyto; }
TRectype& curr(int log = 0) const { return _if->curr(log); }
TRectype& curr(const char * tab) const
{ return _if->lfile(tab).curr(); }
TRectype& curr(const char * tab) const { return _if->lfile(tab).curr(); }
TRecnotype read(TIsamop op = _isgteq, TReclock lockop = _nolock, TDate& atdate = (TDate&)botime);
int lock(TReclock = _lock);
int unlock() { return lock(_unlock); }
virtual bool ok() const;
const char* filter() const { return _filter; }

View File

@ -56,7 +56,11 @@ public:
// @FPUB
operator const char*() const { return (const char*)_str; } // *(TString) -> _str
char& operator[](int i) const { return _str[i]; } // TString[i] -> _str[i]
char& operator[](int i) // TString[i] -> _str[i]
{
CHECKD(i >= 0 && i < _size, "Bad string subscript: ", i);
return _str[i];
}
int size() const { return _size; }
int len() const { return strlen(_str); }
@ -216,7 +220,7 @@ class TToken_string : public TString
protected:
// @FPROT
TObject* dup() const; // Crea un duplicato della token string
virtual TObject* dup() const; // Crea un duplicato della token string
bool set_item(const char* v, int n);
public:
@ -241,7 +245,7 @@ public:
char get_char(int n = -1); // Ritorna un carattere
int get_int(int n = -1); // Ritorna un intero
long get_long(int n = -1); // Ritorna un intero esteso
int get_pos(const char* s); // Ritorna la posizione dell'item s
int get_pos(const char* s); // Ritorna la posizione dell'item s
int items() const; // Ritorna il numero di token presenti
bool empty_items() const; // Controlla se tutti i token sono nulli
};

View File

@ -484,7 +484,7 @@ HIDDEN bool _autoexit_val(TEdit_field& f, KEY key)
HIDDEN bool _numcalc_val(TEdit_field& f, KEY k)
{
if (k != K_TAB) return TRUE;
TExpression e(get_val_param(0), _numexpr);
TExpression e(get_val_param(0), _numexpr);
for (int i = 0 ; i < e.numvar(); i++)
{
@ -498,7 +498,7 @@ HIDDEN bool _numcalc_val(TEdit_field& f, KEY k)
const int fldid = atoi(s);
e.setvar(i, fldid == 0 ? f.get() : f.mask().get(fldid));
}
TFixed_string s((const char*) e);
const TFixed_string s((const char*)e);
f.set(s);
return TRUE;
}