isam.cpp Corretta cancellazione dei memo nei file temporanei maskfld.* Aggiunta keyword FSELECT agli edit field multirec.* Aggiunto metodo compare prefix.cpp Corretta gestione autochiusura e nome dei file temporanei strins.cpp Aggiunti delete [] qua e la' git-svn-id: svn://10.65.10.50/trunk@6775 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			371 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			371 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include <multirec.h>
 | 
						|
 | 
						|
void TMultiple_rectype::set_body_key(TRectype & rowrec)
 | 
						|
{
 | 
						|
  const RecDes* recd = rowrec.rec_des();    // Descrizione del record della testata
 | 
						|
  const KeyDes& kd = recd->Ky[0];       // Elenco dei campi della chiave 1
 | 
						|
  
 | 
						|
    // Copia tutti i campi chiave, tranne l'ultimo, in tutti i records
 | 
						|
  for (int i = kd.NkFields-2; i >= 0; i--)
 | 
						|
  {                        
 | 
						|
    const int nf = kd.FieldSeq[i] % MaxFields;
 | 
						|
    const RecFieldDes& rf = recd->Fd[nf];  
 | 
						|
    const TString& val = get(rf.Name);
 | 
						|
    rowrec.renum_key(rf.Name, val);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
void TMultiple_rectype::load_rows_file(int logicnum)
 | 
						|
{
 | 
						|
  const int index = log2ind(logicnum);
 | 
						|
  TRectype * rec = new_body_record(logicnum);
 | 
						|
  set_body_key(*rec);
 | 
						|
  if (_files.objptr(index) == NULL)
 | 
						|
  {
 | 
						|
    // crea
 | 
						|
    TRecord_array * r = new TRecord_array(logicnum, (TString &) _numfields[index]);
 | 
						|
    _files.add( r, index);
 | 
						|
  }
 | 
						|
  ((TRecord_array &)_files[index]).read(rec);  // rilegge
 | 
						|
  _changed.reset(index);
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::find(int logicnum, const char * fieldname, const char * s, int from, bool reverse) const
 | 
						|
{
 | 
						|
  const TRecord_array & recarray = body(logicnum);
 | 
						|
  const int last = recarray.last_row();
 | 
						|
  const int len = s ? strlen(s) : 0;
 | 
						|
 | 
						|
  if (reverse)
 | 
						|
  {
 | 
						|
    if (from > 0)
 | 
						|
    {
 | 
						|
      if (len == 0)
 | 
						|
        return from - 1;
 | 
						|
      for (int i = recarray.pred_row(from); i > 0; i = recarray.pred_row(i))
 | 
						|
        if (recarray[i].get(fieldname) == s)
 | 
						|
          return i;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  else
 | 
						|
  {
 | 
						|
    if (last > from)
 | 
						|
    {
 | 
						|
      if (len == 0)
 | 
						|
        return from + 1;
 | 
						|
      for (int i = recarray.succ_row(from); i <= last; i = recarray.succ_row(i))
 | 
						|
        if (recarray[i].get(fieldname) == s)
 | 
						|
          return i;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return -1;
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::write_rewrite(TBaseisamfile & f, bool re) const
 | 
						|
{
 | 
						|
  int err = NOERR;
 | 
						|
 | 
						|
  if (_nuovo && re)           // E' nuovo di zecca! quindi ...
 | 
						|
    re = FALSE;               // ... non fare la rewrite
 | 
						|
 | 
						|
  if (re)
 | 
						|
  {
 | 
						|
    for (int i = _files.last(); err == NOERR && i >= 0 ; i = _files.pred(i))
 | 
						|
    {
 | 
						|
      TRecord_array * r = (TRecord_array *) _files.objptr(i);
 | 
						|
      if (r && !_changed[i])
 | 
						|
        err = r->write(re);
 | 
						|
    }
 | 
						|
    // rewrite:
 | 
						|
    if (err == NOERR)
 | 
						|
    {
 | 
						|
      err = TRectype::rewrite(f);
 | 
						|
      if (err != NOERR)
 | 
						|
        err = TRectype::write(f);
 | 
						|
    }
 | 
						|
  }
 | 
						|
  else
 | 
						|
  {
 | 
						|
    // write:
 | 
						|
    TMultiple_rectype & myself = *(TMultiple_rectype *)this;
 | 
						|
    const bool to_complete = !myself.key_complete();
 | 
						|
    if (to_complete)
 | 
						|
      myself.renum();
 | 
						|
    myself.renum_key(); 
 | 
						|
    myself._nuovo |= to_complete;
 | 
						|
    if (_nuovo)
 | 
						|
    {
 | 
						|
      do
 | 
						|
      {
 | 
						|
        err = TRectype::write(f);
 | 
						|
        if (err == _isreinsert && myself.renum())
 | 
						|
           myself.renum_key();
 | 
						|
      } while (err == _isreinsert);
 | 
						|
      myself._nuovo = (err != NOERR);
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      err = TRectype::write(f);
 | 
						|
      if (err != NOERR)
 | 
						|
        err = TRectype::rewrite(f);
 | 
						|
    }
 | 
						|
    for (int i = _files.last(); err == NOERR && i >= 0 ; i = _files.pred(i))
 | 
						|
    {
 | 
						|
      TRecord_array * r = (TRecord_array *)_files.objptr(i);
 | 
						|
      if (r && !_changed[i])
 | 
						|
        err = r->write(re);
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
// @mfunc confronta due record multipli
 | 
						|
int TMultiple_rectype::compare(const TSortable& s) const
 | 
						|
{                                  
 | 
						|
  int res = TRectype::compare(s);
 | 
						|
  TMultiple_rectype & m = (TMultiple_rectype &) s;
 | 
						|
  for (int i =  0 ; res == 0 && i < _files.items(); i++)
 | 
						|
  {                                                 
 | 
						|
    const int logicnum = _logicnums.get_int(i);
 | 
						|
    TRecord_array & r = body(logicnum);
 | 
						|
    TRecord_array & r1 = m.body(logicnum);
 | 
						|
    res =  r.rows() - r1.rows();
 | 
						|
    if (res == 0 && r.rows() > 0 && r1.rows() > 0)
 | 
						|
    {
 | 
						|
      for (int j = r.first_row(), k = r1.first_row(); res == 0 && j >= 0 && k >= 0; j = r.succ_row(j), k = r1.succ_row(k))
 | 
						|
      {
 | 
						|
        res = j - k;
 | 
						|
        if (res == 0)
 | 
						|
          res = r[j] != r1[j];
 | 
						|
      }
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return res;
 | 
						|
}
 | 
						|
 | 
						|
void TMultiple_rectype::remove_body(int logicnum)
 | 
						|
{
 | 
						|
  const int index = log2ind(logicnum);
 | 
						|
 | 
						|
  if (_files.objptr(index) != NULL)
 | 
						|
    _files.remove(index);
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::log2ind(int logicnum) const
 | 
						|
{
 | 
						|
  if (logicnum <= 0)
 | 
						|
    return 0;
 | 
						|
  for (int i = _logicnums.last(); i >= 0 ; i--)
 | 
						|
    if (lognum(i) == logicnum)
 | 
						|
      return i;
 | 
						|
  NFCHECK("Can't find file %d in multiple record", logicnum);
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
TRecord_array& TMultiple_rectype::body(int logicnum) const
 | 
						|
{
 | 
						|
  const int index = log2ind(logicnum);
 | 
						|
 | 
						|
  if (_files.objptr(index) == NULL || _changed[index])
 | 
						|
    ((TMultiple_rectype*) this)->load_rows_file(lognum(index));
 | 
						|
  return (TRecord_array&) _files[index];
 | 
						|
}
 | 
						|
 | 
						|
void TMultiple_rectype::renum_key()
 | 
						|
{
 | 
						|
  for (int i = _files.last(); i >= 0 ; i = _files.pred(i)) 
 | 
						|
  {
 | 
						|
    TRecord_array& b = (TRecord_array&)_files[i];
 | 
						|
    TRectype* rec = new TRectype(b.key());
 | 
						|
    set_body_key(*rec);
 | 
						|
    b.set_key(rec); // Aggiorna righe
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
TRectype & TMultiple_rectype::operator =(const TRectype & r)
 | 
						|
{
 | 
						|
  TRectype::operator=(r);
 | 
						|
  reset_fields(*this);
 | 
						|
  set_fields(*this);
 | 
						|
  return *this;
 | 
						|
}
 | 
						|
 | 
						|
TRectype & TMultiple_rectype::operator =(const char * r)
 | 
						|
{
 | 
						|
  TRectype::operator=(r);
 | 
						|
  reset_fields(*this);
 | 
						|
  set_fields(*this);
 | 
						|
  return *this;
 | 
						|
}
 | 
						|
 | 
						|
void TMultiple_rectype::zero(char c)
 | 
						|
{
 | 
						|
  reset_fields(*this);
 | 
						|
  TAuto_variable_rectype::zero(c);
 | 
						|
  for (int i = _files.last(); i >= 0 ; i = _files.pred(i))
 | 
						|
    ((TRecord_array &)_files[i]).destroy_rows();
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::readat(TBaseisamfile& f, TRecnotype nrec, word lockop)
 | 
						|
{
 | 
						|
  _nuovo = FALSE;
 | 
						|
  int err = TRectype::readat(f, nrec, lockop);
 | 
						|
  synchronize_bodies();
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::read(TBaseisamfile & f, word op, word lockop)
 | 
						|
{
 | 
						|
  int err = NOERR;
 | 
						|
 | 
						|
  _nuovo = FALSE;
 | 
						|
  if (op == _isequal)
 | 
						|
  {
 | 
						|
    TRectype r(*this);
 | 
						|
  
 | 
						|
    err = TRectype::read(f, op, lockop);
 | 
						|
    if (err != NOERR)
 | 
						|
    {
 | 
						|
      *this = r;
 | 
						|
      _nuovo = TRUE;
 | 
						|
    }
 | 
						|
  }                
 | 
						|
  else
 | 
						|
    err = TRectype::read(f, op, lockop);
 | 
						|
  synchronize_bodies();
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void TMultiple_rectype::synchronize_bodies()
 | 
						|
{
 | 
						|
  for (int i = _logicnums.last(); i >= 0 ; i--)
 | 
						|
  {
 | 
						|
    if (_nuovo)          
 | 
						|
    {   
 | 
						|
      TRecord_array * b = (TRecord_array *) _files.objptr(i);
 | 
						|
      if (b != NULL)
 | 
						|
      {
 | 
						|
        _changed.reset(i);
 | 
						|
        b->destroy_rows();  
 | 
						|
        TRectype * r = new_body_record(lognum(i));
 | 
						|
        set_body_key(*r);
 | 
						|
        b->set_key(r);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      if (_autoload[i])
 | 
						|
        load_rows_file(lognum(i));
 | 
						|
      else
 | 
						|
        if (_files.objptr(i) != NULL)
 | 
						|
          _changed.set(i);
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
// @mfunc Copia il multiple rectype r su quello corrente
 | 
						|
TMultiple_rectype & TMultiple_rectype::copy(const TMultiple_rectype& r)
 | 
						|
{
 | 
						|
  // copia..  
 | 
						|
  TAuto_variable_rectype::operator=((TAuto_variable_rectype&)r);
 | 
						|
 | 
						|
  _nuovo     = r._nuovo;
 | 
						|
  _files     = r._files;
 | 
						|
  _logicnums = r._logicnums;
 | 
						|
  _changed   = r._changed;
 | 
						|
  _autoload  = r._autoload;
 | 
						|
  _numfields = r._numfields;   
 | 
						|
  
 | 
						|
  return *this;
 | 
						|
}
 | 
						|
 | 
						|
// @mfunc Copia il multiple rectype r su quello corrente
 | 
						|
int TMultiple_rectype::loaded_rows(int logicnum) const
 | 
						|
{          
 | 
						|
  const int index = log2ind(logicnum);       
 | 
						|
  TRecord_array * r = (TRecord_array *) ((TMultiple_rectype *) this)->_files.objptr(index);
 | 
						|
  
 | 
						|
  if (_changed[index] ||  r == NULL) 
 | 
						|
    return 0;
 | 
						|
  return r->rows();
 | 
						|
}
 | 
						|
 | 
						|
int TMultiple_rectype::remove(TBaseisamfile & f) const
 | 
						|
{
 | 
						|
  int err = NOERR;
 | 
						|
  for (int i = _files.last(); err == NOERR && i >= 0 ; i = _files.pred(i))
 | 
						|
  {
 | 
						|
    TRecord_array & r = body(lognum(i));
 | 
						|
    err = r.remove();
 | 
						|
  }
 | 
						|
  if (err == NOERR)
 | 
						|
    err = TRectype::remove(f);
 | 
						|
  return err;
 | 
						|
}
 | 
						|
 | 
						|
// @doc INTERNAL
 | 
						|
TMultiple_rectype::TMultiple_rectype(int hfn)
 | 
						|
                  : TAuto_variable_rectype(hfn), _nuovo(TRUE)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
void TMultiple_rectype::enable_autoload(int logicnum,bool on)
 | 
						|
{
 | 
						|
  const int index = log2ind(logicnum);
 | 
						|
  _autoload.set(index, on);
 | 
						|
}
 | 
						|
              
 | 
						|
TRectype & TMultiple_rectype::insert_row(int row, int logicnum)
 | 
						|
{       
 | 
						|
  TRectype * r = new_body_record(logicnum);
 | 
						|
  TRecord_array & b = body(logicnum);
 | 
						|
  const int index = log2ind(logicnum);
 | 
						|
  
 | 
						|
  set_body_key(*r);
 | 
						|
  r->put(_numfields.row(index), row);     
 | 
						|
  b.insert_row(r);
 | 
						|
  return *r;
 | 
						|
}
 | 
						|
 | 
						|
TRectype & TMultiple_rectype::new_row(int logicnum)
 | 
						|
{
 | 
						|
  TRecord_array & b = body(logicnum);
 | 
						|
  TRectype & r = b.row(-1, TRUE);
 | 
						|
  
 | 
						|
  return r;
 | 
						|
}
 | 
						|
 | 
						|
bool TMultiple_rectype::autoload_enabled(int logicnum)
 | 
						|
{
 | 
						|
  return _autoload[log2ind(logicnum)];
 | 
						|
}
 | 
						|
 | 
						|
// @cmember Aggiunge il corpo lgicnum
 | 
						|
void TMultiple_rectype::add_file(int logicnum, const char* numfield)
 | 
						|
{
 | 
						|
  _logicnums.add_long(logicnum);
 | 
						|
  _numfields.add(numfield);
 | 
						|
}
 | 
						|
 | 
						|
TMultiple_rectype::TMultiple_rectype(const TBaseisamfile* file)
 | 
						|
                  :TAuto_variable_rectype(file), _nuovo(TRUE)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
  // @ cmember costruttore dal record
 | 
						|
TMultiple_rectype::TMultiple_rectype(const TRectype & rec)
 | 
						|
                  :TAuto_variable_rectype(rec), _nuovo(TRUE)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
// @mfunc costruttore di copia
 | 
						|
TMultiple_rectype::TMultiple_rectype(const TMultiple_rectype& r)
 | 
						|
                  :TAuto_variable_rectype(r)
 | 
						|
{
 | 
						|
  // copia..
 | 
						|
  copy(r);
 | 
						|
}
 | 
						|
 |