1994-08-12 10:52:49 +00:00
# include <stdio.h>
# include <stdlib.h>
# define __ISAM_CPP
1994-09-27 10:19:36 +00:00
# ifndef FOXPRO
1994-11-24 14:01:20 +00:00
# include <applicat.h>
1994-10-04 15:38:57 +00:00
# include <expr.h>
1994-11-24 14:01:20 +00:00
# include <execp.h>
1994-09-27 10:19:36 +00:00
# include <progind.h>
# endif
1994-11-24 14:01:20 +00:00
1994-09-27 10:19:36 +00:00
# include <config.h>
1994-08-12 10:52:49 +00:00
# include <extcdecl.h>
1994-11-24 14:01:20 +00:00
# include <mailbox.h>
# include <prefix.h>
# include <relation.h>
1994-09-27 10:19:36 +00:00
# include <scanner.h>
1994-08-12 10:52:49 +00:00
# include <utility.h>
1995-04-10 15:28:03 +00:00
# if XVT_OS==XVT_OS_WIN
# include <process.h>
# else
1994-10-14 17:51:32 +00:00
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# endif
1994-08-12 10:52:49 +00:00
1995-04-10 15:28:03 +00:00
1994-08-12 10:52:49 +00:00
# define NOT_LINKED(i,f) CHECKS(i != NULL, "Record senza tracciato: impossibile eseguire ", f)
1995-04-10 15:28:03 +00:00
# define NOT_OPEN() CHECKS(_isamfile != NULL, "File chiuso: ", (const char*)name())
1995-04-20 14:35:14 +00:00
HIDDEN char _isam_string [ 257 ] ;
1994-09-12 09:34:05 +00:00
HIDDEN void UNKNOWN_FIELD ( int num , const char * name )
{ yesnofatal_box ( " Il campo '%s' non appartiene al file %d " , name , num ) ; }
1994-08-12 10:52:49 +00:00
# define NOALLOC (char **) -1
1994-10-31 16:24:26 +00:00
HIDDEN bool __autoload = TRUE ;
void set_autoload_new_files ( bool on )
{
__autoload = on ;
}
1995-02-10 17:42:33 +00:00
///////////////////////////////////////////////////////////
// TExtrectype
///////////////////////////////////////////////////////////
1994-08-12 10:52:49 +00:00
class TExtrectype : public TRectype
1995-04-10 15:28:03 +00:00
{
RecDes * _rd ;
protected : // TRectype
virtual RecDes * rec_des ( ) const { return _rd ; }
1994-08-22 11:10:49 +00:00
public :
// FPUB
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
TExtrectype ( const TTrec & r ) ; // Costruisce il record a partire da r
virtual ~ TExtrectype ( ) ;
1994-08-12 10:52:49 +00:00
} ;
1994-09-12 09:34:05 +00:00
TExtrectype : : TExtrectype ( const TTrec & r ) : TRectype ( 6 )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
delete _rec ;
_length = r . len ( ) ;
_rec = new char [ _length ] ;
1995-04-10 15:28:03 +00:00
// _i = new isdef;
// _i->r = r.rec();
_rd = r . rec ( ) ;
1994-08-22 11:10:49 +00:00
zero ( ) ;
1994-08-12 10:52:49 +00:00
}
TExtrectype : : ~ TExtrectype ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
delete _rec ;
1995-04-10 15:28:03 +00:00
// delete _i;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
///////////////////////////////////////////////////////////
// TBaseisamfile
///////////////////////////////////////////////////////////
1994-08-12 10:52:49 +00:00
TBaseisamfile : : TBaseisamfile ( int logicnum , bool linkrecinst )
{
1994-08-22 11:10:49 +00:00
TTrec r ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
_isamfile = NULL ;
_logicnum = logicnum ;
_lasterr = NOERR ;
_delopenrec = FALSE ;
if ( ( openrec [ _logicnum - 1 ] = = NULL ) | | ( ! linkrecinst ) )
{
_current = new TRectype ( this ) ;
1994-08-12 10:52:49 +00:00
if ( openrec [ _logicnum - 1 ] = = NULL )
1994-08-22 11:10:49 +00:00
{
openrec [ _logicnum - 1 ] = _current ;
_delopenrec = TRUE ;
}
1994-08-12 10:52:49 +00:00
_delrec = TRUE ;
}
1994-08-22 11:10:49 +00:00
else
{
1994-08-12 10:52:49 +00:00
_current = openrec [ _logicnum - 1 ] ;
_delrec = FALSE ;
}
1994-08-22 11:10:49 +00:00
r . get ( logicnum ) ;
1994-12-28 15:01:04 +00:00
/*
_historicfile = ( ( r . field ( RFLD_SYS_DATE ) ! = FIELDERR ) & &
( r . field ( RFLD_SYS_FIRST ) ! = FIELDERR ) & &
( r . field ( RFLD_SYS_LAST ) ! = FIELDERR ) ) ;
*/
_historicfile = FALSE ;
1994-08-12 10:52:49 +00:00
}
TBaseisamfile : : ~ TBaseisamfile ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
if ( _delrec )
{
if ( _delopenrec )
1994-08-12 10:52:49 +00:00
openrec [ _logicnum - 1 ] = NULL ;
delete _current ;
}
1994-08-22 11:10:49 +00:00
if ( _isamfile ! = NULL )
delete _isamfile ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : gethr ( TRectype & rec , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
TRecfield fd0 ( rec , RFLD_SYS_DATE ) ;
TDate d0 ( ( const TDate & ) fd0 ) ;
if ( d0 < atdate )
{
1994-08-12 10:52:49 +00:00
atdate = eotime ;
return NOERR ;
}
1994-12-28 15:01:04 +00:00
TRectype wr ( rec ) ;
1994-08-22 11:10:49 +00:00
TRecfield flf ( wr , RFLD_SYS_FIRST ) ,
fll ( wr , RFLD_SYS_LAST ) ,
fd1 ( wr , RFLD_SYS_DATE ) ;
1994-12-28 15:01:04 +00:00
1994-08-22 11:10:49 +00:00
TDate d1 ;
TRecnotype wr0 = - 1L , wr1 = - 1L ;
if ( ( wr0 = = flf . ptr ( ) ) = = - 1L ) return _iskeynotfound ;
_hf . read ( wr . string ( ) , wr0 ) ;
if ( ( d1 = ( const TDate & ) fd1 ) > atdate )
{
1994-08-12 10:52:49 +00:00
rec = wr ;
1994-08-22 11:10:49 +00:00
atdate = d0 - 1L ;
1994-08-12 10:52:49 +00:00
return _iskeynotfound ;
}
1994-08-22 11:10:49 +00:00
while ( ( d1 < atdate ) & & ( wr0 > 0 ) )
{
1994-08-12 10:52:49 +00:00
rec = wr ;
1994-08-22 11:10:49 +00:00
wr1 = wr0 ;
1994-08-12 10:52:49 +00:00
if ( ( wr0 = fll . ptr ( ) ) > 0 )
1994-08-22 11:10:49 +00:00
{
_hf . read ( wr . string ( ) , wr0 ) ;
d1 = ( const TDate & ) fd1 ;
}
1994-08-12 10:52:49 +00:00
}
1994-08-22 11:10:49 +00:00
if ( wr0 < = 0 )
atdate = d0 - 1L ;
else
atdate = d1 - 1L ;
return NOERR ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : addhr ( const TRectype & rec , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
return NOERR ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : rewhr ( const TRectype & rec , TDate & atdate )
{
1994-08-22 11:10:49 +00:00
return NOERR ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : delhr ( const TRectype & rec , TDate & atdate )
{
1994-08-22 11:10:49 +00:00
return NOERR ;
1994-08-12 10:52:49 +00:00
}
long TBaseisamfile : : items ( ) const
{
1994-08-22 11:10:49 +00:00
return filehnd ( ) - > i . Base [ filehnd ( ) - > i . PN ] . PEOD ;
1994-08-12 10:52:49 +00:00
}
const char * TBaseisamfile : : name ( ) const
1995-04-10 15:28:03 +00:00
{
1995-04-20 14:35:14 +00:00
sprintf ( _isam_string , " %d " , num ( ) ) ;
1995-04-10 15:28:03 +00:00
return _isam_string ;
1994-08-12 10:52:49 +00:00
}
const char * TBaseisamfile : : filename ( ) const
{
1994-12-27 14:59:25 +00:00
if ( _isamfile = = NULL )
{
TDir d ;
d . get ( num ( ) ) ;
1995-04-20 14:35:14 +00:00
strcpy ( _isam_string , d . name ( ) ) ;
1995-04-10 15:28:03 +00:00
return _isam_string ;
1994-12-27 14:59:25 +00:00
}
1994-08-22 11:10:49 +00:00
return _isamfile - > d - > SysName ;
1994-08-12 10:52:49 +00:00
}
1994-10-03 08:47:32 +00:00
const char * TBaseisamfile : : description ( ) const
{
1994-12-27 14:59:25 +00:00
if ( _isamfile = = NULL )
{
TDir d ;
d . get ( num ( ) ) ;
1995-04-20 14:35:14 +00:00
strcpy ( _isam_string , d . des ( ) ) ;
1995-04-10 15:28:03 +00:00
return _isam_string ;
1994-12-27 14:59:25 +00:00
}
1994-10-03 08:47:32 +00:00
return _isamfile - > d - > Des ;
}
1994-08-12 10:52:49 +00:00
void TBaseisamfile : : setkey ( int nkey )
{
1995-04-10 15:28:03 +00:00
CHECKD ( nkey > 0 & & nkey - 1 < = _isamfile - > r - > NKeys , " Chiave non valida " , nkey ) ;
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
_isamfile - > i . PN = nkey - 1 ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : getkey ( ) const
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
return _isamfile - > i . PN + 1 ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : first ( word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
cisread ( _isamfile , curr ( ) . string ( ) , _isfirst + lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : last ( word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
cisread ( _isamfile , curr ( ) . string ( ) , _islast + lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : next ( word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
cisread ( _isamfile , curr ( ) . string ( ) , _isnext + lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : next ( TDate & atdate )
1994-08-22 11:10:49 +00:00
{
TRectype wr ( curr ( ) ) ;
TRecfield fll ( curr ( ) , RFLD_SYS_LAST ) ,
fd ( wr , RFLD_SYS_DATE ) ;
TRecnotype wrn ;
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ! _historicfile )
error_box ( " %s not historic file " , filename ( ) ) ;
if ( _lasthf = = - 1L ) return _iseof ;
curr ( ) . setdirty ( ) ;
_hf . read ( curr ( ) . string ( ) , _lasthf ) ;
wrn = fll . ptr ( ) ;
if ( wrn < 0L )
{
1994-08-12 10:52:49 +00:00
readat ( - wrn ) ;
atdate = eotime ;
_lasthf = - 1L ;
}
1994-08-22 11:10:49 +00:00
else
{
1994-08-12 10:52:49 +00:00
_hf . read ( curr ( ) . string ( ) , wrn ) ;
_lasthf = wrn ;
wrn = fll . ptr ( ) ;
1994-08-22 11:10:49 +00:00
if ( wrn < 0 ) readat ( wr , - wrn ) ;
1994-08-12 10:52:49 +00:00
else _hf . read ( wr . string ( ) , wrn ) ;
atdate = ( const TDate & ) fd - 1L ;
}
1994-08-22 11:10:49 +00:00
return NOERR ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : prev ( word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
cisread ( _isamfile , curr ( ) . string ( ) , _isprev + lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : prev ( TDate & atdate )
1994-08-22 11:10:49 +00:00
{
TRecfield flf ( curr ( ) , RFLD_SYS_LAST ) ,
fd ( curr ( ) , RFLD_SYS_DATE ) ;
TRecnotype wrn ;
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ! _historicfile )
error_box ( " %s not historic file " , filename ( ) ) ;
if ( _lasthf = = - 1L ) return _isbof ;
curr ( ) . setdirty ( ) ;
_hf . read ( curr ( ) . string ( ) , _lasthf ) ;
wrn = flf . ptr ( ) ;
atdate = ( const TDate & ) fd - 1L ;
if ( wrn < 0L ) _lasthf = - 1L ;
else
{
1994-08-12 10:52:49 +00:00
_hf . read ( curr ( ) . string ( ) , wrn ) ;
_lasthf = wrn ;
}
1994-08-22 11:10:49 +00:00
return NOERR ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : reread ( word lockop , TDate & atdate )
1994-08-22 11:10:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
return cisread ( _isamfile , curr ( ) . string ( ) , _iscurr + lockop , & _lasterr ) ;
else
{
1994-08-12 10:52:49 +00:00
if ( cisread ( _isamfile , curr ( ) . string ( ) , _iscurr + lockop , & _lasterr ) = = NOERR )
_lasterr = gethr ( curr ( ) , atdate ) ;
}
1994-08-22 11:10:49 +00:00
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : reread ( TRectype & rec , word lockop , TDate & atdate )
1994-08-22 11:10:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
rec . setdirty ( ) ;
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisread ( _isamfile , rec . string ( ) , _iscurr + lockop , & _lasterr ) ;
else
{
1994-08-12 10:52:49 +00:00
if ( cisread ( _isamfile , rec . string ( ) , _iscurr + lockop , & _lasterr ) = = NOERR )
_lasterr = gethr ( rec , atdate ) ;
}
1994-08-22 11:10:49 +00:00
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : skip ( TRecnotype nrec , word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ! nrec ) return NOERR ;
curr ( ) . setdirty ( ) ;
if ( nrec > 0 )
cisread ( _isamfile , curr ( ) . string ( ) , _isnextn + lockop + ( UINT16 ) nrec , & _lasterr ) ;
else
cisread ( _isamfile , curr ( ) . string ( ) , _isprevn + lockop - ( UINT16 ) nrec , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : read ( word op , word lockop , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
CHECKD ( op > = _iscurr & & op < = _isgteq , " Invalid read operation : " , op ) ;
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisread ( _isamfile , curr ( ) . string ( ) , op + lockop , & _lasterr ) ;
else
{
if ( cisread ( _isamfile , curr ( ) . string ( ) , op + lockop , & _lasterr ) = = NOERR )
_lasterr = gethr ( curr ( ) , atdate ) ;
}
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : read ( TRectype & rec , word op , word lockop , TDate & atdate )
1994-08-22 11:10:49 +00:00
{
CHECKD ( op > = _iscurr & & op < = _isgteq , " Invalid read operation : " , op ) ;
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
rec . setdirty ( ) ;
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisread ( _isamfile , rec . string ( ) , op + lockop , & _lasterr ) ;
else
{
1994-08-12 10:52:49 +00:00
if ( cisread ( _isamfile , rec . string ( ) , op + lockop , & _lasterr ) = = NOERR )
_lasterr = gethr ( rec , atdate ) ;
1994-08-22 11:10:49 +00:00
}
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : readat ( TRecnotype nrec , word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
curr ( ) . setdirty ( ) ;
cisreadrec ( _isamfile , nrec , curr ( ) . string ( ) , lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : readat ( TRectype & rec , TRecnotype nrec , word lockop )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
rec . setdirty ( ) ;
cisreadrec ( _isamfile , nrec , rec . string ( ) , lockop , & _lasterr ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : write ( TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
ciswrite ( _isamfile , curr ( ) . string ( ) , & _lasterr ) ;
else
_lasterr = addhr ( curr ( ) , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : write ( const TRectype & rec , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
ciswrite ( _isamfile , rec . string ( ) , & _lasterr ) ;
else
_lasterr = addhr ( rec , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : rewrite ( TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisrewrite ( _isamfile , curr ( ) . string ( ) , & _lasterr ) ;
else
_lasterr = rewhr ( curr ( ) , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : rewrite ( const TRectype & rec , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisrewrite ( _isamfile , rec . string ( ) , & _lasterr ) ;
else
_lasterr = rewhr ( rec , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
}
int TBaseisamfile : : rewriteat ( TRecnotype nrec )
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
return cisrewrec ( _isamfile , nrec , curr ( ) . string ( ) , & _lasterr ) ;
}
int TBaseisamfile : : rewriteat ( const TRectype & rec , TRecnotype nrec )
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
return cisrewrec ( _isamfile , nrec , curr ( ) . string ( ) , & _lasterr ) ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : remove ( TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisdelete ( _isamfile , curr ( ) . string ( ) , & _lasterr ) ;
else
_lasterr = delhr ( curr ( ) , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : remove ( const TRectype & rec , TDate & atdate )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
if ( ( ! _historicfile ) | | ( atdate = = botime ) )
cisdelete ( _isamfile , rec . string ( ) , & _lasterr ) ;
else
_lasterr = delhr ( rec , atdate ) ;
_recno = _isamfile - > RecNo ;
return _lasterr ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : lock ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
return cislock ( _isamfile , & _lasterr ) ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : unlock ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
return cisunlock ( _isamfile , & _lasterr ) ;
1994-08-12 10:52:49 +00:00
}
void TBaseisamfile : : indexon ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
IndexOn ( ) ;
1994-08-12 10:52:49 +00:00
}
bool TBaseisamfile : : empty ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
return _isamfile - > i . Base [ _isamfile - > i . PN ] . PEOD = = 0 ;
}
void TBaseisamfile : : indexoff ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
IndexOff ( ) ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : _open ( unsigned int mode )
1994-08-22 11:10:49 +00:00
{
1995-04-10 15:28:03 +00:00
CHECKS ( filehnd ( ) = = NULL , " File already open " , ( const char * ) filename ( ) ) ;
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
int err ;
1994-08-22 11:10:49 +00:00
if ( ( cisopen ( & _isamfile , _logicnum , NOALLOC , mode , & err ) = = NOERR ) & &
( _historicfile ) )
{
1994-08-12 10:52:49 +00:00
TFilename s ( filename ( ) ) ;
s . ext ( " hst " ) ;
_hf . len ( ) = filehnd ( ) - > d - > LenR ;
_hf . base ( ) = 1 ;
_hf . open ( s ) ;
if ( _hf . status ( ) = = 2 )
1994-08-22 11:10:49 +00:00
{
_hf . create ( s ) ;
_hf . open ( s ) ;
}
1994-08-12 10:52:49 +00:00
if ( _hf . error ( ) ) return err = _hf . status ( ) ;
_lasthf = - 1L ;
_hfhd = _hf ;
_hfhd . len ( ) = sizeof ( TRecnotype ) ;
_hfhd . base ( ) = 0 ;
}
1994-11-10 14:15:41 +00:00
if ( err = = NOERR )
{
isdef * fh = filehnd ( ) ;
const TRecnotype nitems = fh - > i . Base [ 0 ] . PEOX ;
if ( fh - > d - > EOX ! = nitems )
1994-11-24 14:01:20 +00:00
recover ( ) ;
1994-11-10 14:15:41 +00:00
}
1994-08-22 11:10:49 +00:00
_recno = - 1L ;
1995-04-10 15:28:03 +00:00
// _current->_i = filehnd();
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
int TBaseisamfile : : _close ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
int err = NOERR ;
if ( filehnd ( ) ! = NULL )
{
1994-08-12 10:52:49 +00:00
cisclose ( ptrfilehnd ( ) , NULL , & err ) ;
clearfilehnd ( ) ;
}
1995-04-10 15:28:03 +00:00
// _current->_i = NULL;
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
///////////////////////////////////////////////////////////
// TLocalisamfile
///////////////////////////////////////////////////////////
1995-02-22 14:21:34 +00:00
// Attenzione linkrecinst puo' valere 0, 1, 2.
// Se vale 2 significa che si sta costruendo un file temporaneo
// per cui linkrecinst va' considerato FALSE
1994-08-12 10:52:49 +00:00
TLocalisamfile : : TLocalisamfile ( int logicnum , bool linkrecinst )
1995-02-22 14:21:34 +00:00
: TBaseisamfile ( logicnum , linkrecinst = = TRUE )
1995-02-10 17:42:33 +00:00
{
if ( linkrecinst < = TRUE )
{
open ( ) ;
if ( _was_open )
_oldkey = getkey ( ) ;
setkey ( 1 ) ;
}
else _was_open = FALSE ;
1994-08-12 10:52:49 +00:00
}
TLocalisamfile : : ~ TLocalisamfile ( )
1995-01-04 15:39:38 +00:00
{
1995-01-05 17:50:40 +00:00
if ( _was_open )
1995-01-04 15:39:38 +00:00
setkey ( _oldkey ) ;
1994-08-12 10:52:49 +00:00
close ( ) ;
}
int TLocalisamfile : : close ( )
{
int err = NOERR ;
if ( ! _was_open )
{
clearfilehnd ( ) ;
}
else
{
if ( _isamfile )
{
cisclose ( & _isamfile , NULL , & err ) ;
_isamfile = NULL ;
}
}
setstatus ( err ) ;
return err ;
}
int TLocalisamfile : : open ( unsigned int mode )
{
int err = NOERR ;
const int logicnum = num ( ) ;
if ( openf [ logicnum - 1 ] ! = NULL )
{
_was_open = FALSE ;
_isamfile = openf [ logicnum - 1 ] ;
1994-08-22 11:10:49 +00:00
}
else
{
err = _open ( ) ;
_was_open = TRUE ;
}
1995-04-10 15:28:03 +00:00
// _current->_i = filehnd();
1995-03-22 09:07:04 +00:00
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
int TLocalisamfile : : operator + = ( const TRecnotype npos )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
skip ( npos ) ;
return status ( ) ;
}
int TLocalisamfile : : operator - = ( const TRecnotype npos )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
skip ( - npos ) ;
return status ( ) ;
1994-08-12 10:52:49 +00:00
}
int TLocalisamfile : : operator + + ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
next ( ) ;
return status ( ) ;
1994-08-12 10:52:49 +00:00
}
int TLocalisamfile : : operator - - ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
prev ( ) ;
return status ( ) ;
1994-08-12 10:52:49 +00:00
}
TIsamfile : : TIsamfile ( int logicnum , bool linkrecinst ) : TBaseisamfile ( logicnum , linkrecinst ) { }
TIsamfile : : ~ TIsamfile ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
close ( ) ;
1994-08-12 10:52:49 +00:00
}
int TIsamfile : : flags ( bool updateeod )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
int err ;
1995-04-10 15:28:03 +00:00
NOT_OPEN ( ) ;
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return cisupdflags ( filehnd ( ) , & err , updateeod ) ;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
///////////////////////////////////////////////////////////
// TIsamtempfile
///////////////////////////////////////////////////////////
1994-08-12 10:52:49 +00:00
1995-02-10 17:42:33 +00:00
TIsamtempfile : : TIsamtempfile ( int logicnum , const char * radix , bool create )
: TLocalisamfile ( logicnum , 2 )
{
TRecnotype eod = 0 ;
TRecnotype eox = 100 ;
1995-03-24 11:16:17 +00:00
TFilename n ;
if ( radix & & * radix )
{
if ( * radix = = ' % ' )
n = radix ;
else
{
n . tempdir ( ) ;
n < < ' / ' < < radix ;
}
}
1995-02-10 17:42:33 +00:00
1995-03-24 11:16:17 +00:00
n . ext ( " dta " ) ;
1995-02-10 17:42:33 +00:00
if ( ! create )
{
TDir dir ; dir . get ( logicnum ) ;
1995-03-24 11:16:17 +00:00
const word & len = dir . len ( ) ;
// Tolgo il % senno' che fa la fopen ?
if ( n [ 0 ] = = ' % ' )
n = n . sub ( 1 ) ;
1995-02-10 17:42:33 +00:00
FILE * f = fopen ( n , " r " ) ;
1995-03-24 11:16:17 +00:00
1995-04-10 15:28:03 +00:00
# if XVT_OS == XVT_OS_WIN
1995-03-24 11:16:17 +00:00
CHECKS ( f , " Can't open temporary file " , ( const char * ) strerror ( NULL ) ) ;
# else
1995-02-10 17:42:33 +00:00
CHECKS ( f , " Can't open temporary file " , ( const char * ) n ) ;
1995-03-24 11:16:17 +00:00
# endif
1995-02-10 17:42:33 +00:00
fseek ( f , 0 , SEEK_END ) ;
eod = eox = ftell ( f ) / len ;
fclose ( f ) ;
}
_autodel = create > TRUE ;
1995-02-15 17:02:44 +00:00
1995-03-24 11:16:17 +00:00
n . ext ( " " ) ;
// Ci rimetto il % se prima l'avevo tolto, senno' che fa la open ?
// if (*radix == '%')
if ( n [ 0 ] ! = ' % ' )
n . insert ( " % " , 0 ) ;
1995-02-10 17:42:33 +00:00
open ( n , create , eod , eox ) ;
}
1994-08-12 10:52:49 +00:00
TIsamtempfile : : ~ TIsamtempfile ( )
{
1994-08-22 11:10:49 +00:00
close ( ) ;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
int TIsamtempfile : : open ( const char * radix , bool create , TRecnotype eod , TRecnotype eox )
1994-08-22 11:10:49 +00:00
{
1995-04-10 15:28:03 +00:00
CHECKS ( filehnd ( ) = = NULL , " File already open " , ( const char * ) filename ( ) ) ;
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
int err ;
1995-02-10 17:42:33 +00:00
if ( ( cisopentemp ( ptrfilehnd ( ) , _logicnum , ( char * ) radix , NOALLOC , create , eod , eox , & err ) = = NOERR ) & &
1994-08-22 11:10:49 +00:00
( _historicfile ) )
{
1995-02-10 17:42:33 +00:00
TFilename s ( filename ( ) ) ; s . ext ( " hst " ) ;
1994-08-12 10:52:49 +00:00
_hf . len ( ) = filehnd ( ) - > d - > LenR ;
_hf . base ( ) = 1 ;
_hf . open ( s ) ;
if ( _hf . status ( ) = = 2 )
1994-08-22 11:10:49 +00:00
{
_hf . create ( s ) ;
_hf . open ( s ) ;
}
1994-08-12 10:52:49 +00:00
if ( _hf . error ( ) ) return err = _hf . status ( ) ;
_lasthf = - 1L ;
_hfhd = _hf ;
_hfhd . len ( ) = sizeof ( TRecnotype ) ;
_hfhd . base ( ) = 0 ;
}
1995-04-10 15:28:03 +00:00
// _current->_i = filehnd();
1994-08-22 11:10:49 +00:00
_recno = - 1L ;
setstatus ( err ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
int TIsamtempfile : : close ( )
1994-08-12 10:52:49 +00:00
{
1995-02-10 17:42:33 +00:00
int err = NOERR ;
1994-08-22 11:10:49 +00:00
if ( filehnd ( ) ! = NULL )
{
1995-02-10 17:42:33 +00:00
cisclosetemp ( ptrfilehnd ( ) , NULL , _autodel , & err ) ;
1994-08-12 10:52:49 +00:00
clearfilehnd ( ) ;
}
1995-04-10 15:28:03 +00:00
// _current->_i = NULL;
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
1995-02-10 17:42:33 +00:00
///////////////////////////////////////////////////////////
// TSystemsamfile
///////////////////////////////////////////////////////////
1994-08-12 10:52:49 +00:00
int TSystemisamfile : : build ( TRecnotype eox )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
CHECKS ( filehnd ( ) = = NULL , " Can't build open file " , ( const char * ) filename ( ) ) ;
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
int err ;
1994-10-14 17:51:32 +00:00
TDir d ;
d . get ( num ( ) ) ;
TFilename f ( d . name ( ) ) ;
1995-04-28 13:14:15 +00:00
f = f . path ( ) ; if ( ! is_not_slash ( f . right ( 1 ) [ 0 ] ) ) f . rtrim ( 1 ) ;
1994-10-14 17:51:32 +00:00
if ( ! fexist ( f ) )
1995-04-10 15:28:03 +00:00
make_dir ( f ) ;
1994-08-22 11:10:49 +00:00
cisbuild ( filehnd ( ) , num ( ) , eox , & err ) ;
setstatus ( err ) ;
clearfilehnd ( ) ;
1994-11-24 14:01:20 +00:00
# ifndef FOXPRO
1994-10-31 16:24:26 +00:00
if ( err = = NOERR & & __autoload )
{
TFilename lf ;
1994-11-22 15:03:51 +00:00
lf . format ( " %sstd/lf%04d.txt " , __ptprf , num ( ) ) ;
1994-10-31 16:24:26 +00:00
if ( fexist ( lf ) )
1994-12-02 13:06:39 +00:00
load ( lf , ' | ' , ' \0 ' , ' \n ' , TRUE , TRUE ) ;
1994-10-31 16:24:26 +00:00
}
1994-11-24 14:01:20 +00:00
# endif
1994-08-22 11:10:49 +00:00
return err ;
1994-08-12 10:52:49 +00:00
}
int TSystemisamfile : : extend ( TRecnotype eox )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
CHECKS ( filehnd ( ) = = NULL , " Can't extend open file " , ( const char * ) filename ( ) ) ;
int err ;
1994-08-22 11:10:49 +00:00
cisextend ( filehnd ( ) , num ( ) , eox , & err ) ;
setstatus ( err ) ;
clearfilehnd ( ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
long TSystemisamfile : : size ( TRecnotype eox )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
int err ;
const long size = cisextension ( num ( ) , eox , & err ) ;
if ( err ! = NOERR ) setstatus ( err ) ;
return size ;
1994-08-12 10:52:49 +00:00
}
# ifndef FOXPRO
1994-11-18 17:39:45 +00:00
bool TSystemisamfile : : exec_convapp ( long flev , const bool before )
{
const char * const v = before ? " BCNV " : " ACNV " ;
if ( flev = = 0 ) flev = 199401 ;
else flev + + ;
1995-03-22 09:07:04 +00:00
for ( long l = flev ; l < = get_std_level ( ) ; l + + )
1994-11-18 17:39:45 +00:00
{
TString16 paragraph ( format ( " %06ld " , l ) ) ;
TConfig conv ( CONFIG_FCONV , paragraph ) ;
if ( ! conv . new_paragraph ( ) & & conv . exist ( v , num ( ) ) )
{
int err = 0 ;
TString80 s ( conv . get ( v , NULL , num ( ) ) ) ; s < < " " < < main_app ( ) . get_firm ( ) ;
1994-11-24 14:01:20 +00:00
TExternal_app app ( s ) ;
1994-11-18 17:39:45 +00:00
if ( app . can_run ( ) )
{
app . run ( ) ;
err = app . error ( ) ;
TMailbox mail ;
TMessage * msg = mail . next ( TRUE ) ;
if ( err = = 0 & & msg ! = NULL )
err = atoi ( msg - > body ( ) ) ;
}
else err = 16 ;
if ( err )
1995-04-10 15:28:03 +00:00
return error_box ( " Impossibile eseguire il programma di %sconversione \n del livello %ld/%ld \n Errore n.ro %d " , before ? " pre " : " post " , l / 100 , l % 100 , err ) ;
1994-11-18 17:39:45 +00:00
}
}
return TRUE ;
}
1994-08-12 10:52:49 +00:00
1994-09-28 10:36:08 +00:00
bool TSystemisamfile : : getlcf ( long flev )
1994-09-27 10:19:36 +00:00
{
_flds . destroy ( ) ;
_exps . destroy ( ) ;
if ( flev = = 0 ) flev = 199401 ;
else flev + + ;
1995-03-22 09:07:04 +00:00
for ( long l = flev ; l < = get_std_level ( ) ; l + + )
1994-09-27 10:19:36 +00:00
{
TString16 paragraph ( format ( " %06ld " , l ) ) ;
TConfig conv ( CONFIG_FCONV , paragraph ) ;
1994-11-18 17:39:45 +00:00
if ( ! conv . new_paragraph ( ) & & conv . exist ( " F " , num ( ) ) )
1994-09-27 10:19:36 +00:00
{
TToken_string exprline ( conv . get ( " F " , NULL , num ( ) ) ) ;
if ( exprline . empty ( ) ) return FALSE ;
TToken_string w ( " " , ' = ' ) ;
const char * wexprs = exprline . get ( ) ;
while ( wexprs ! = NULL )
{
w = wexprs ;
TFixed_string fld ( w . get ( ) ) ;
_flds . add ( new TFieldref ( fld , 0 ) ) ;
_exps . add ( new TExpression ( w . get ( ) , _strexpr ) ) ;
wexprs = exprline . get ( ) ;
}
}
}
return _flds . items ( ) > 0 ;
}
void TSystemisamfile : : makelc ( TRectype & rec )
{
for ( int i = 0 ; i < _flds . items ( ) ; i + + )
{
TFieldref & f = ( TFieldref & ) _flds [ i ] ;
TExpression & e = ( TExpression & ) _exps [ i ] ;
for ( int k = 0 ; k < e . numvar ( ) ; k + + )
e . setvar ( k , get ( e . varname ( k ) ) ) ;
f . write ( e , rec ) ;
}
}
1994-08-12 10:52:49 +00:00
int TSystemisamfile : : update ( TTrec & newrec , bool vis )
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
{
CHECKS ( filehnd ( ) = = NULL , " Can't update open file " , ( const char * ) filename ( ) ) ;
1994-11-10 14:15:41 +00:00
1994-08-22 11:10:49 +00:00
TDir dir ;
1994-10-31 16:24:26 +00:00
1995-04-28 13:14:15 +00:00
dir . get ( num ( ) , _unlock , _nordir , _sysdirop ) ;
1994-10-31 16:24:26 +00:00
const char p = * dir . name ( ) ;
1995-03-22 09:07:04 +00:00
const bool is_com = prefix ( ) . is_com ( ) ;
1995-02-02 18:08:50 +00:00
const bool toconvert = is_com ? dir . is_com ( ) : dir . is_firm ( ) ;
1994-10-31 16:24:26 +00:00
int err = NOERR ;
1994-08-22 11:10:49 +00:00
TTrec oldrec ;
oldrec . get ( num ( ) ) ;
1995-03-22 09:07:04 +00:00
const long lev = prefix ( ) . filelevel ( ) ;
1994-11-18 17:39:45 +00:00
const bool lcf = getlcf ( lev ) ;
exec_convapp ( lev , TRUE ) ;
1994-09-27 10:19:36 +00:00
1994-11-18 17:39:45 +00:00
if ( ! lcf & & newrec = = oldrec )
{
exec_convapp ( lev , FALSE ) ;
return NOERR ;
}
1994-11-10 14:15:41 +00:00
1994-08-22 11:10:49 +00:00
const TRecnotype nitems = dir . eod ( ) ;
const unsigned int lenr = newrec . len ( ) ;
1994-11-10 14:15:41 +00:00
if ( ! toconvert & & dir . eox ( ) > 0L )
{
dir . eod ( ) = 0L ;
dir . eox ( ) = 0L ;
}
1994-10-31 16:24:26 +00:00
if ( toconvert & & dir . eox ( ) > 0L )
1994-08-22 11:10:49 +00:00
{
TRecnotype ni = 0L ;
TFilename tmpfname ;
tmpfname . temp ( NULL ) ;
FILE * f = fopen ( tmpfname , " w " ) ;
err = ferror ( f ) ;
if ( f = = NULL )
{
clearerr ( f ) ;
setstatus ( err ) ;
return err ;
}
1995-04-28 13:14:15 +00:00
open ( ) ;
1994-08-22 11:10:49 +00:00
TFilename fname ( filename ( ) ) ;
TString s ( 80 ) ;
s . format ( " Aggiornamento archivio %s " , ( const char * ) fname ) ;
1994-10-31 16:24:26 +00:00
TProgind p ( nitems ? nitems : 1 , s , TRUE , TRUE , 70 ) ;
1994-08-22 11:10:49 +00:00
int nflds = curr ( ) . items ( ) ;
TArray fld ( nflds ) ;
TExtrectype nrec ( newrec ) ;
TRecnotype i ;
1994-09-27 10:19:36 +00:00
1994-08-22 11:10:49 +00:00
for ( int j = 0 ; j < nflds ; j + + )
fld . add ( TString ( curr ( ) . fieldname ( j ) ) , j ) ;
for ( i = 1 ; i < = nitems & & ! p . iscancelled ( ) ; i + + )
{
if ( ( i % 50 ) = = 0 ) p . setstatus ( i + 1 ) ;
readat ( i ) ;
if ( curr ( ) . valid ( ) )
{
nrec . zero ( ) ;
ni + + ;
for ( j = 0 ; j < nflds ; j + + )
if ( nrec . exist ( ( const TString & ) fld [ j ] ) )
nrec . put ( ( const TString & ) fld [ j ] , get ( ( const TString & ) fld [ j ] ) ) ;
1994-09-27 10:19:36 +00:00
if ( lcf )
makelc ( ( TRectype & ) nrec ) ;
1994-08-22 11:10:49 +00:00
fwrite ( nrec . string ( ) , lenr , 1 , f ) ;
}
}
1994-09-27 10:19:36 +00:00
p . setstatus ( nitems ) ;
1994-08-22 11:10:49 +00:00
close ( ) ;
fclose ( f ) ;
fcopy ( tmpfname , fname ) ;
: : remove ( tmpfname ) ;
dir . eod ( ) = ni ;
}
// aggiornare il log file
dir . set_len ( lenr ) ;
dir . put ( num ( ) , _nordir , _sysdirop ) ;
newrec . put ( num ( ) ) ;
1994-10-31 16:24:26 +00:00
if ( toconvert & & dir . eox ( ) > 0L ) packindex ( ) ;
1994-11-18 17:39:45 +00:00
exec_convapp ( lev , FALSE ) ;
1994-08-22 11:10:49 +00:00
return err ;
1994-08-12 10:52:49 +00:00
}
int TSystemisamfile : : packfile ( bool vis )
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
{
CHECKS ( filehnd ( ) = = NULL , " Can't pack open file " , ( const char * ) filename ( ) ) ;
1994-08-22 11:10:49 +00:00
int err ;
setstatus ( creorgfile ( num ( ) , vis , & err ) ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
int TSystemisamfile : : packindex ( bool vis )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
CHECKS ( filehnd ( ) = = NULL , " Can't pack index of open file " , ( const char * ) filename ( ) ) ;
1994-08-22 11:10:49 +00:00
int err ;
setstatus ( creorgindex ( num ( ) , vis , & err ) ) ;
return err ;
1994-08-12 10:52:49 +00:00
}
1994-12-02 13:06:39 +00:00
int TSystemisamfile : : load ( const char * from , char fs , char fd , char rs , bool vis , bool extended )
1994-08-22 11:10:49 +00:00
{
1994-09-27 10:19:36 +00:00
FILE * fl = fopen ( from , " r+ " ) ;
int err = ferror ( fl ) ;
TRecnotype r = 0 , e = 0 , nitems = 0 , nread = 0 ;
1994-12-02 13:06:39 +00:00
TString16 firm , year , attprev ( " 00000 " ) ;
if ( extended )
{
TDate d ( TODAY ) ;
TLocalisamfile ditte ( LF_NDITTE ) ;
firm . format ( " %05ld " , main_app ( ) . get_firm ( ) ) ;
year . format ( " %04d " , d . year ( ) ) ;
ditte . zero ( ) ;
ditte . put ( " CODDITTA " , firm ) ;
if ( ditte . read ( ) = = NOERR )
attprev = ditte . get ( " CODATTPREV " ) ;
}
1994-09-27 10:19:36 +00:00
if ( fl = = NULL )
1994-08-22 11:10:49 +00:00
{
1994-09-27 10:19:36 +00:00
clearerr ( fl ) ;
1994-08-22 11:10:49 +00:00
setstatus ( err ) ;
return err ;
}
1994-09-27 10:19:36 +00:00
char w [ 80 ] ;
while ( ( fgets ( w , 80 , 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 ) ;
1994-08-22 11:10:49 +00:00
open ( ) ;
1994-09-27 10:19:36 +00:00
1994-08-22 11:10:49 +00:00
TToken_string s ( 1024 , fs ) ;
bool fixedlen = ( fs = = ' \0 ' ) ;
int nflds = curr ( ) . items ( ) ;
TArray fld ( nflds ) ;
1994-09-27 10:19:36 +00:00
int len [ MaxFields ] ;
1994-08-22 11:10:49 +00:00
TString sfd ( 3 ) ;
TString s1 ( 64 ) ;
1994-09-27 10:19:36 +00:00
bool lcf = FALSE ;
if ( f . paragraph ( " Header " ) )
{
f . equal ( ) ;
const long level = atol ( f . line ( ) ) ;
1995-03-22 09:07:04 +00:00
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 ) ;
1994-09-27 10:19:36 +00:00
lcf = getlcf ( level ) ;
nflds = 0 ;
TToken_string s2 ( f . line ( ) ) ;
int p = s2 . find ( ' = ' ) ;
if ( p > 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 ( ) )
{
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 )
{
s1 = s2 . left ( p ) ;
s2 . ltrim ( p + 1 ) ;
}
else s1 . cut ( 0 ) ;
}
}
else
1994-08-22 11:10:49 +00:00
{
1994-09-27 10:19:36 +00:00
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 ) ;
}
1994-08-22 11:10:49 +00:00
}
1994-09-27 10:19:36 +00:00
if ( ! f . paragraph ( " Data " ) )
{
error_box ( " Formato dei dati non valido " ) ;
close ( ) ;
1994-11-10 14:15:41 +00:00
err = 1 ;
setstatus ( err ) ;
return err ;
1994-09-27 10:19:36 +00:00
}
1994-08-22 11:10:49 +00:00
if ( fd ) sfd < < fd ;
int last = NOERR ;
1994-11-11 18:04:29 +00:00
s1 . format ( " Imp. archivio %s \n %6ld records %6ld errori - %3d " , filename ( ) , r , e , last ) ;
1994-09-27 10:19:36 +00:00
TProgind p ( nitems , s1 , TRUE , TRUE , 70 ) ;
s = f . line ( ) ;
while ( s . not_empty ( ) & & ! p . iscancelled ( ) )
1994-08-22 11:10:49 +00:00
{
1994-12-02 13:06:39 +00:00
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 ] ;
}
1994-08-22 11:10:49 +00:00
if ( ( r + e ) % 50 = = 0 )
{
1994-11-11 18:04:29 +00:00
s1 . format ( " Imp. archivio %s \n %6ld records %6ld errori - %3d " , filename ( ) , r , e , last ) ;
1994-08-22 11:10:49 +00:00
p . set_text ( s1 ) ;
}
p . setstatus ( nread + 1 ) ;
nread + = s . len ( ) + 1 ;
zero ( ) ;
if ( fixedlen )
{
int pos = 0 ;
1994-09-27 10:19:36 +00:00
for ( int j = 0 ; j < nflds ; j + + )
1994-08-22 11:10:49 +00:00
{
1994-09-27 10:19:36 +00:00
s1 = s . mid ( pos , len [ j ] ) ;
s1 . rtrim ( ) ;
put ( ( const TString & ) fld [ j ] , s1 ) ;
1994-08-22 11:10:49 +00:00
pos + = len [ j ] ;
}
}
else
{
s . restart ( ) ;
1994-09-27 10:19:36 +00:00
for ( int j = 0 ; j < nflds ; j + + )
1994-08-22 11:10:49 +00:00
{
char * s2 = ( char * ) s . get ( ) ;
if ( fd )
{
s2 + + ;
s2 [ strlen ( s2 ) - 1 ] = ' \0 ' ;
}
put ( ( const TString & ) fld [ j ] , s2 ) ;
}
}
if ( write ( ) = = NOERR ) r + + ;
else
{
e + + ;
last = status ( ) ;
}
1994-09-27 10:19:36 +00:00
s = f . line ( ) ;
1994-08-22 11:10:49 +00:00
}
1994-11-11 18:04:29 +00:00
s1 . format ( " Imp. archivio %s \n %6ld records %6ld errori - %3d " , filename ( ) , r , e , last ) ;
1994-08-22 11:10:49 +00:00
p . set_text ( s1 ) ;
close ( ) ;
1994-11-10 14:15:41 +00:00
setstatus ( err ) ;
1994-08-22 11:10:49 +00:00
return err ;
1994-08-12 10:52:49 +00:00
}
int TSystemisamfile : : dump ( const char * to , int nkey , char fs , char fd , char rs , bool vis , bool withdeleted )
{
1994-08-22 11:10:49 +00:00
FILE * f = fopen ( to , " w " ) ;
int err = ferror ( f ) ;
if ( withdeleted ) nkey = 0 ;
if ( f = = NULL )
{
clearerr ( f ) ;
setstatus ( err ) ;
return err ;
}
open ( ) ;
TString s ( 512 ) ;
bool fixedlen = ( fs = = ' \0 ' ) ;
int nflds = curr ( ) . items ( ) ;
TArray fld ( nflds ) ;
TBit_array rjust ( nflds ) ;
1994-09-27 10:19:36 +00:00
int len [ MaxFields ] ;
1994-08-22 11:10:49 +00:00
for ( int j = 0 ; j < nflds ; j + + )
{
fld . add ( TString ( curr ( ) . fieldname ( j ) ) , j ) ;
1994-09-27 10:19:36 +00:00
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 ) ;
1994-08-22 11:10:49 +00:00
}
TRecnotype i = 0 ;
1994-09-01 13:28:55 +00:00
const TRecnotype nitems = nkey ? items ( ) : filehnd ( ) - > d - > EOD ;
1994-08-22 11:10:49 +00:00
s . format ( " Esportazione archivio %s " , filename ( ) ) ;
1994-10-31 16:24:26 +00:00
TProgind p ( nitems , s , TRUE , TRUE , 70 ) ;
1994-08-22 11:10:49 +00:00
TString s1 ;
1994-09-27 10:19:36 +00:00
1995-03-22 09:07:04 +00:00
fprintf ( f , " [Header] \n Version=%ld " , prefix ( ) . filelevel ( ) ) ;
1994-09-27 10:19:36 +00:00
for ( int k = 0 ; k < nflds ; k + + )
{
if ( ( k % 10 ) = = 0 ) fprintf ( f , " \n Fields= " ) ;
else fprintf ( f , " | " ) ;
fprintf ( f , " %s,%d " , ( const char * ) ( const TString & ) fld [ k ] , len [ k ] ) ;
}
fprintf ( f , " \n \n [Data] \n " ) ;
1994-08-22 11:10:49 +00:00
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 + + )
{
1994-11-10 14:15:41 +00:00
zero ( ) ;
1994-08-22 11:10:49 +00:00
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 ) ;
}
}
}
1994-09-27 10:19:36 +00:00
p . setstatus ( nitems ) ;
1994-08-22 11:10:49 +00:00
close ( ) ;
fclose ( f ) ;
1994-09-27 10:19:36 +00:00
setstatus ( err ) ;
1994-08-22 11:10:49 +00:00
return err ;
1994-11-24 14:01:20 +00:00
}
1994-08-12 10:52:49 +00:00
# endif // FOXPRO
1994-11-24 14:01:20 +00:00
void TBaseisamfile : : recover ( )
{
isdef * fh = filehnd ( ) ;
const TRecnotype nitems = fh - > i . Base [ 0 ] . PEOX ;
TFilename fn = filename ( ) ;
fn = fn . name ( ) ;
1995-04-20 14:35:14 +00:00
if ( getkey ( ) ! = 1 )
fatal_box ( " La chiave per il recover dell'archivio %s deve essere 1 non %d " ,
( const char * ) fn , getkey ( ) ) ;
1994-11-24 14:01:20 +00:00
# ifndef FOXPRO
1994-11-30 09:04:03 +00:00
if ( yesno_box ( " La dimensione dell'archivio %s e' errata. Cerco di recuperarlo? " , ( const char * ) fn ) )
1994-11-24 14:01:20 +00:00
{
TDir d ;
TString mess ( 80 ) ;
mess . format ( " Ricostruzione archivio %s : I Fase " , ( const char * ) fn ) ;
TProgind p ( nitems ? nitems : 1 , mess , TRUE , TRUE , 70 ) ;
1994-11-30 09:04:03 +00:00
const TDirtype dir = ( fh - > ft = = 0 ) ? _nordir : _comdir ;
d . get ( num ( ) , _lock , dir , _sysdirop ) ;
1994-11-24 14:01:20 +00:00
d . eod ( ) = fh - > d - > EOD = nitems ;
d . eox ( ) = fh - > d - > EOX = nitems ;
1994-11-30 09:04:03 +00:00
d . put ( num ( ) , dir , _sysdirop ) ;
1994-11-24 14:01:20 +00:00
for ( TRecnotype r = 1 ; r < = nitems ; r + + )
{
p . addstatus ( 1 ) ;
CRead ( & fh - > f , curr ( ) . string ( ) , r , _nolock ) ;
curr ( ) . discard ( ) ;
CWrite ( & fh - > f , curr ( ) . string ( ) , r , _nolock ) ;
}
p . close_modal ( ) ;
mess . format ( " Ricostruzione archivio %s : II Fase " , ( const char * ) fn ) ;
TProgind pi ( items ( ) ? items ( ) : 1 , mess , TRUE , TRUE , 70 ) ;
for ( first ( ) ; good ( ) ; next ( ) )
{
pi . addstatus ( 1 ) ;
curr ( ) . recall ( ) ;
rewrite ( ) ;
}
message_box ( " L'archivio %s deve essere compattato " , ( const char * ) fn ) ;
}
else
# endif
fatal_box ( " L'archivio %s e' incosistente e deve essere corretto prima di utilizzarlo " , ( const char * ) fn ) ;
}
1994-08-12 10:52:49 +00:00
////////////////////////////////////////////////////////////
// TRectype
////////////////////////////////////////////////////////////
1995-04-10 15:28:03 +00:00
TRectype : : TRectype ( int logicnum ) : _cod ( NULL )
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
TDir wdir ;
_logicnum = logicnum ;
wdir . get ( _logicnum , _nolock , _nordir , _sysdirop ) ;
if ( wdir . name ( ) [ 0 ] = = ' % ' )
wdir . get ( _logicnum , _nolock , _comdir , _sysdirop ) ;
_length = wdir . len ( ) ;
_rec = new char [ _length ] ;
1995-04-10 15:28:03 +00:00
// _i = openf[_logicnum - 1];
1994-09-27 10:19:36 +00:00
* _tab = ' \0 ' ;
1994-09-28 15:52:37 +00:00
setempty ( TRUE ) ;
1994-08-12 10:52:49 +00:00
}
1995-04-10 15:28:03 +00:00
TRectype : : TRectype ( const TBaseisamfile * i ) : _cod ( NULL )
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
_logicnum = i - > num ( ) ;
if ( i - > filehnd ( ) ! = NULL )
_length = i - > filehnd ( ) - > d - > LenR ;
else
{
TDir wdir ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
wdir . get ( _logicnum , _nolock , _nordir , _sysdirop ) ;
if ( wdir . name ( ) [ 0 ] = = ' % ' )
wdir . get ( _logicnum , _nolock , _comdir , _sysdirop ) ;
_length = wdir . len ( ) ;
}
1994-09-27 10:19:36 +00:00
* _tab = ' \0 ' ;
1994-08-22 11:10:49 +00:00
_rec = new char [ _length ] ;
1995-04-10 15:28:03 +00:00
// _i = i->filehnd();
1994-09-28 15:52:37 +00:00
setempty ( TRUE ) ;
1994-08-12 10:52:49 +00:00
}
TRectype : : TRectype ( const TRectype & r )
1994-09-27 10:19:36 +00:00
: _cod ( NULL )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
_logicnum = r . _logicnum ;
_length = r . len ( ) ;
_rec = new char [ _length ] ;
_rec [ 0 ] = r . _rec [ 0 ] ;
memcpy ( _rec + 1 , r . _rec + 1 , _length - 1 ) ;
1995-04-10 15:28:03 +00:00
// _i = r._i;
1994-09-27 10:19:36 +00:00
strcpy ( _tab , r . _tab ) ;
if ( r . _cod ! = NULL )
_cod = new TRecfield ( * this , " COD " ) ;
1994-08-22 11:10:49 +00:00
setempty ( r . empty ( ) ) ;
1994-08-12 10:52:49 +00:00
}
TRectype : : ~ TRectype ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-09-27 10:19:36 +00:00
if ( _cod ! = NULL ) delete _cod ;
1994-08-22 11:10:49 +00:00
delete _rec ;
1994-08-12 10:52:49 +00:00
}
1994-09-27 10:19:36 +00:00
void TRectype : : settab ( const char * tab )
{
if ( _cod ! = NULL )
{
delete _cod ;
_cod = NULL ;
}
strcpy ( _tab , tab ) ;
if ( * _tab ! = ' \0 ' )
1994-09-28 15:52:37 +00:00
_cod = new TRecfield ( * this , " COD " ) ;
zero ( ) ;
1994-09-27 10:19:36 +00:00
}
1994-08-12 10:52:49 +00:00
TObject * TRectype : : dup ( ) const
{
1994-08-22 11:10:49 +00:00
TRectype * o = new TRectype ( * this ) ;
return o ;
1994-08-12 10:52:49 +00:00
}
HIDDEN bool fld_empty ( const char * s , int len , bool number )
{
1994-08-22 11:10:49 +00:00
/*if (number)
{
for ( ; len ; s + + , len - - )
if ( strchr ( " 0. " , * s ) = = NULL ) return FALSE ;
}
else */
if ( * s )
{
for ( ; len ; s + + , len - - )
if ( * s ! = ' ' ) return FALSE ;
}
return TRUE ;
1994-08-12 10:52:49 +00:00
}
HIDDEN int fld_cmp ( const char * a , const char * b , int len , bool number )
{
1994-08-22 11:10:49 +00:00
for ( int i = 0 ; i < len & & * a = = * b ; b + + , a + + , i + + ) ;
if ( i = = len ) return 0 ;
int res = * a - * b ;
if ( number )
{
b - = i ;
i = 0 ;
}
return fld_empty ( b , len - i , number ) ? 0 : res ;
1994-08-12 10:52:49 +00:00
}
///////////////////////////////////////////////////////////
// TRectype (record di un file)
///////////////////////////////////////////////////////////
1995-04-10 15:28:03 +00:00
RecDes * TRectype : : rec_des ( ) const
{
const isdef * i = openf [ _logicnum - 1 ] ;
NOT_LINKED ( i , " Record description " ) ;
RecDes * r = i - > r ;
CHECK ( r , " Missing record description " ) ;
return r ;
}
1994-08-12 10:52:49 +00:00
int TRectype : : items ( ) const
{
1995-04-10 15:28:03 +00:00
return rec_des ( ) - > NFields ;
1994-08-12 10:52:49 +00:00
}
const char * TRectype : : start ( int nf ) const
{
1995-04-10 15:28:03 +00:00
return string ( ) + rec_des ( ) - > Fd [ nf ] . RecOff ;
1994-08-12 10:52:49 +00:00
}
int TRectype : : compare ( const TSortable & s ) const
{
const TRectype & br = ( const TRectype & ) s ;
1994-08-22 11:10:49 +00:00
int res = 0 ;
1994-08-12 10:52:49 +00:00
if ( br . empty ( ) ) return UNDEFINED ;
for ( int i = 0 ; i < items ( ) ; i + + )
1994-08-22 11:10:49 +00:00
{
const char * b = br . start ( i ) ;
const char * a = start ( i ) ;
1995-04-10 15:28:03 +00:00
const byte typ = rec_des ( ) - > Fd [ i ] . TypeF ;
1994-08-22 11:10:49 +00:00
/* if (typ == _boolfld) res = *a - *b;
else
{ */
1995-04-10 15:28:03 +00:00
const int sz = rec_des ( ) - > Fd [ i ] . Len ;
1994-08-22 11:10:49 +00:00
const bool number = ( typ = = _intfld ) | | ( typ = = _realfld ) | |
( typ = = _longfld ) | | ( typ = = _wordfld ) | |
( typ = = _intzerofld ) | | ( typ = = _longzerofld )
| | ( typ = = _datefld ) ;
if ( fld_empty ( b , sz , number ) ) continue ;
res = : : fld_cmp ( a , b , sz , number ) ;
/* } */
if ( res ) return res ;
}
1994-08-12 10:52:49 +00:00
return 0 ;
}
TFieldtypes TRectype : : type ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "type");
return ( TFieldtypes ) CFieldType ( ( char * ) fieldname , rec_des ( ) ) ;
1994-08-12 10:52:49 +00:00
}
int TRectype : : length ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "length");
return CFieldSize ( ( char * ) fieldname , rec_des ( ) ) ;
1994-08-12 10:52:49 +00:00
}
int TRectype : : ndec ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "dec");
return CFieldDec ( ( char * ) fieldname , rec_des ( ) ) ;
1994-08-12 10:52:49 +00:00
}
bool TRectype : : exist ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "exist");
return findfld ( rec_des ( ) , ( char * ) fieldname ) ! = - 1 ;
1994-08-12 10:52:49 +00:00
}
const char * TRectype : : fieldname ( int i ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "fieldname");
RecDes * rd = rec_des ( ) ;
return i > = 0 & & i < rd - > NFields ? rd - > Fd [ i ] . Name : NULL ;
1994-08-12 10:52:49 +00:00
}
1995-04-20 14:35:14 +00:00
const char * TRectype : : get_str ( const char * fieldname ) const
{
RecDes * rd = rec_des ( ) ;
if ( CFieldType ( ( char * ) fieldname , rd ) = = _datefld )
{
const TRecfield f ( ( TRectype & ) * this , fieldname ) ;
strcpy ( _isam_string , ( const char * ) f ) ;
}
else
if ( CGetFieldBuff ( ( char * ) fieldname , rd , _rec , _isam_string ) = = - 1 )
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
return _isam_string ;
}
1994-08-12 10:52:49 +00:00
# ifndef FOXPRO
1995-04-20 14:35:14 +00:00
const TString & TRectype : : get ( const char * fieldname ) const
{
static TFixed_string tmp ( _isam_string , sizeof ( _isam_string ) ) ;
get_str ( fieldname ) ;
return tmp ;
}
1994-08-12 10:52:49 +00:00
int TRectype : : get_int ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_int");
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
return atoi ( _isam_string ) ;
1994-08-12 10:52:49 +00:00
}
long TRectype : : get_long ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_long");
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
return atol ( _isam_string ) ;
1994-08-12 10:52:49 +00:00
}
word TRectype : : get_word ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_word");
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
return ( word ) atoi ( _isam_string ) ;
1994-08-12 10:52:49 +00:00
}
real TRectype : : get_real ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_real");
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
real r ( _isam_string ) ;
1994-08-22 11:10:49 +00:00
return r ;
1994-08-12 10:52:49 +00:00
}
char TRectype : : get_char ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_char");
1994-08-22 11:10:49 +00:00
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
return * _isam_string ;
1994-08-12 10:52:49 +00:00
}
bool TRectype : : get_bool ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_bool");
1995-04-20 14:35:14 +00:00
if ( CGetFieldBuff ( ( char * ) fieldname , rec_des ( ) , _rec , _isam_string ) = = - 1 )
1994-08-22 11:10:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1995-04-10 15:28:03 +00:00
return * _isam_string = = ' X ' ;
1994-08-12 10:52:49 +00:00
}
# endif // FOXPRO
TDate TRectype : : get_date ( const char * fieldname ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "get_date");
1994-08-22 11:10:49 +00:00
const TRecfield f ( ( TRectype & ) * this , fieldname ) ;
return ( TDate ) f ;
1994-08-12 10:52:49 +00:00
}
# ifndef FOXPRO
void TRectype : : put ( const char * fieldname , int val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
if ( CPutField ( ( char * ) fieldname , rec_des ( ) , & val , _rec ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
}
void TRectype : : put ( const char * fieldname , long val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
if ( CPutField ( ( char * ) fieldname , rec_des ( ) , & val , _rec ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
}
void TRectype : : put ( const char * fieldname , word val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
if ( CPutField ( ( char * ) fieldname , rec_des ( ) , & val , _rec ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1994-08-22 11:10:49 +00:00
setempty ( FALSE ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : put ( const char * fieldname , const real & val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
if ( CPutField ( ( char * ) fieldname , rec_des ( ) , val . ptr ( ) , _rec ) = = - 1 )
1994-08-22 11:10:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : put ( const char * fieldname , const TDate & val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
1994-08-22 11:10:49 +00:00
TRecfield f ( * this , fieldname ) ;
1995-05-09 09:12:26 +00:00
f = val . string ( full ) ;
1994-08-22 11:10:49 +00:00
setempty ( FALSE ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : put ( const char * fieldname , char val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
1994-08-12 10:52:49 +00:00
char w [ 2 ] = { val , ' \0 ' } ;
1995-04-10 15:28:03 +00:00
if ( CPutFieldBuff ( ( char * ) fieldname , rec_des ( ) , w , _rec ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
}
void TRectype : : put ( const char * fieldname , bool val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
1994-08-12 10:52:49 +00:00
char * s = val ? " X " : " " ;
1995-04-10 15:28:03 +00:00
if ( CPutFieldBuff ( ( char * ) fieldname , rec_des ( ) , s , _rec ) = = - 1 )
1994-08-12 10:52:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
}
# endif // FOXPRO
void TRectype : : put ( const char * fieldname , const char * val )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "put");
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
if ( val = = NULL | | * val = = ' \0 ' ) // Da provare
{
TRecfield f ( * this , fieldname ) ;
if ( * f . pos ( ) = = ' \0 ' ) return ;
}
1994-08-12 10:52:49 +00:00
1995-04-10 15:28:03 +00:00
if ( CFieldType ( ( char * ) fieldname , rec_des ( ) ) = = _datefld )
1994-08-22 11:10:49 +00:00
{
TRecfield f ( * this , fieldname ) ;
f = val ;
}
else
1995-04-10 15:28:03 +00:00
if ( CPutFieldBuff ( ( char * ) fieldname , rec_des ( ) , ( char * ) val , _rec ) = = - 1 )
1994-08-22 11:10:49 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
setempty ( FALSE ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : zero ( const char * fieldname )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "zero");
1994-09-27 10:19:36 +00:00
if ( _cod ! = NULL & & strcmp ( fieldname , " COD " ) = = 0 )
* _cod = _tab ;
else
1995-04-10 15:28:03 +00:00
if ( CZeroField ( ( char * ) fieldname , rec_des ( ) , _rec ) = = - 1 )
1994-09-27 10:19:36 +00:00
UNKNOWN_FIELD ( num ( ) , fieldname ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : zero ( )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-08-22 11:10:49 +00:00
zero ( ' \0 ' ) ;
1994-08-12 10:52:49 +00:00
}
void TRectype : : zero ( char c )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "zero");
1994-08-22 11:10:49 +00:00
recall ( ) ;
memset ( _rec + 1 , c , len ( ) - 1 ) ;
1994-12-28 15:01:04 +00:00
1994-09-27 10:19:36 +00:00
if ( _cod ! = NULL )
1994-12-28 15:01:04 +00:00
* _cod = _tab ;
1994-08-22 11:10:49 +00:00
setempty ( TRUE ) ;
1994-08-12 10:52:49 +00:00
}
// Certified 99%
TRectype & TRectype : : operator = ( const TRectype & rec )
1994-08-22 11:10:49 +00:00
1994-08-12 10:52:49 +00:00
{
1994-09-27 10:19:36 +00:00
CHECK ( num ( ) = = rec . num ( ) , " Can't assign records of different file " ) ;
1994-08-12 10:52:49 +00:00
1995-04-10 15:28:03 +00:00
// _i = rec._i; // Copy filehndl
1994-08-12 10:52:49 +00:00
memcpy ( _rec , rec . _rec , _length ) ; // Copy contents
setempty ( rec . empty ( ) ) ; // Copy emptiness status
return * this ;
}
// Certified 100%
TRectype & TRectype : : operator = ( const TBaseisamfile & f )
{
1994-08-22 11:10:49 +00:00
return * this = f . curr ( ) ;
1994-08-12 10:52:49 +00:00
}
// Certified 99%
TRectype & TRectype : : operator = ( const char * rec )
{
memcpy ( _rec , rec , _length ) ;
setempty ( FALSE ) ;
return * this ;
}
const char * TRectype : : key ( int numkey ) const
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(_i, "key");
1995-04-20 14:35:14 +00:00
CBuildKey ( rec_des ( ) , numkey , _rec , _isam_string ) ;
1995-04-10 15:28:03 +00:00
return _isam_string ;
1994-08-12 10:52:49 +00:00
}
///////////////////////////////////////////////////////////
// TRecfield (campo/sottocampo di un record)
///////////////////////////////////////////////////////////
void TRecfield : : set ( int from , int to )
{
1994-08-22 11:10:49 +00:00
int nf ;
1995-04-10 15:28:03 +00:00
RecDes * rd = _rec - > rec_des ( ) ;
1994-08-22 11:10:49 +00:00
1995-04-10 15:28:03 +00:00
if ( ( nf = findfld ( rd , _name ) ) = = - 1 )
1994-08-22 11:10:49 +00:00
{
_p = NULL ;
_len = 0 ;
_dec = 0 ;
_type = _nullfld ;
yesnofatal_box ( " File n. %d unknown field %s " , _rec - > num ( ) , _name ) ;
}
else
{
CHECK ( from > = 0 , " Invalid Start " ) ;
1995-04-10 15:28:03 +00:00
_p = _rec - > string ( ) + rd - > Fd [ nf ] . RecOff + from ;
_dec = rd - > Fd [ nf ] . Dec ;
_type = rd - > Fd [ nf ] . TypeF ;
1994-08-22 11:10:49 +00:00
if ( to > = 0 )
{
1995-04-10 15:28:03 +00:00
CHECK ( from < = to & & to < = rd - > Fd [ nf ] . Len ,
1994-08-22 11:10:49 +00:00
" Invalid Range " ) ;
_len = to - from + 1 ;
}
1995-04-10 15:28:03 +00:00
else _len = rd - > Fd [ nf ] . Len - from ;
1994-08-22 11:10:49 +00:00
}
1994-08-12 10:52:49 +00:00
}
TRecfield : : TRecfield ( TRectype & rec , const char * name , int from , int to )
{
1995-04-10 15:28:03 +00:00
// NOT_LINKED(rec.filehnd(), "TRecfield");
1994-08-22 11:10:49 +00:00
strcpy ( _name , name ) ;
_rec = & rec ;
set ( from , to ) ;
1994-08-12 10:52:49 +00:00
}
HIDDEN void __getfieldbuff ( byte l , byte t , const char * recin , char * s )
{
1994-08-22 11:10:49 +00:00
if ( recin = = NULL )
{
* s = ' \0 ' ;
return ;
}
if ( ( t ! = _alfafld ) & & ( t ! = _datefld ) )
{
while ( ( * recin = = ' ' ) & & ( l ) )
{
recin + + ;
l - - ;
}
if ( ( t ! = _realfld ) & & ( t ! = _charfld ) & &
( t ! = _intzerofld ) & & ( t ! = _longzerofld ) )
{
while ( ( * recin = = ' 0 ' ) & & ( l ) )
{
recin + + ;
l - - ;
}
}
}
if ( l )
{
strncpy ( s , recin , l ) ;
1995-02-09 14:47:31 +00:00
for ( int i = l - 1 ; i > = 0 & & s [ i ] = = ' ' ; i - - ) ;
l = byte ( i + 1 ) ;
1994-08-22 11:10:49 +00:00
}
s [ l ] = ' \0 ' ;
if ( t = = _datefld & & * s )
{
TDate dt ( atol ( s ) ) ;
1995-05-09 09:12:26 +00:00
strcpy ( s , dt . string ( full ) ) ;
1994-08-22 11:10:49 +00:00
}
1994-08-12 10:52:49 +00:00
}
HIDDEN void __putfieldbuff ( byte l , byte d , byte t , const char * s , char * recout )
{
1994-08-22 11:10:49 +00:00
int len , i ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
if ( recout = = NULL ) return ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
char s2 [ 256 ] ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
strcpy ( s2 , s ) ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
if ( t = = _datefld )
{
if ( * s2 )
{
TDate dt ( s2 ) ;
sprintf ( s2 , " %06ld " , ( long ) dt ) ;
}
} else
if ( t = = _realfld ) setdec ( s2 , d ) ;
len = strlen ( s2 ) ;
if ( len > l ) return ;
if ( ( t = = _intfld ) | |
( t = = _longfld ) | |
( t = = _wordfld ) | |
( t = = _realfld ) )
{
if ( len = = 0 )
{
strcpy ( s2 , " 0 " ) ;
len = 1 ;
}
i = 0 ;
while ( i < l - len - 1 ) recout [ i + + ] = ' ' ;
strncpy ( & recout [ l - len - 1 ] , s2 , len ) ;
}
else
{
strncpy ( recout , s2 , len ) ;
while ( len < l ) recout [ len + + ] = ' ' ;
}
1994-08-12 10:52:49 +00:00
}
int TRecfield : : operator = ( int i )
{
1994-08-22 11:10:49 +00:00
if ( _type = = _intzerofld )
1995-04-20 14:35:14 +00:00
sprintf ( _isam_string , " %0*d " , _len , i ) ;
1994-08-22 11:10:49 +00:00
else
1995-04-20 14:35:14 +00:00
sprintf ( _isam_string , " %d " , i ) ;
1995-04-10 15:28:03 +00:00
__putfieldbuff ( _len , _dec , _type , _isam_string , _p ) ;
1994-08-22 11:10:49 +00:00
_rec - > setempty ( FALSE ) ;
return i ;
1994-08-12 10:52:49 +00:00
}
long TRecfield : : operator = ( long l )
{
1994-08-22 11:10:49 +00:00
if ( _type = = _longzerofld )
1995-04-20 14:35:14 +00:00
sprintf ( _isam_string , " %0*ld " , _len , l ) ;
1994-08-22 11:10:49 +00:00
else
1995-04-20 14:35:14 +00:00
sprintf ( _isam_string , " %ld " , l ) ;
1995-04-10 15:28:03 +00:00
__putfieldbuff ( _len , _dec , _type , _isam_string , _p ) ;
1994-08-22 11:10:49 +00:00
_rec - > setempty ( FALSE ) ;
return l ;
1994-08-12 10:52:49 +00:00
}
# ifndef FOXPRO
1995-05-09 13:33:34 +00:00
1994-08-12 10:52:49 +00:00
const real & TRecfield : : operator = ( const real & r )
{
1995-04-20 14:35:14 +00:00
strcpy ( _isam_string , r . string ( ) ) ;
1995-04-10 15:28:03 +00:00
__putfieldbuff ( _len , _dec , _type , _isam_string , _p ) ;
1994-08-22 11:10:49 +00:00
_rec - > setempty ( FALSE ) ;
return r ;
1994-08-12 10:52:49 +00:00
}
1995-04-20 14:35:14 +00:00
1994-08-12 10:52:49 +00:00
# endif // FOXPRO
const TDate & TRecfield : : operator = ( const TDate & d )
{
1995-04-20 14:35:14 +00:00
strcpy ( _isam_string , ( const char * ) d ) ;
1995-04-10 15:28:03 +00:00
__putfieldbuff ( _len , _dec , _type , _isam_string , _p ) ;
1994-08-22 11:10:49 +00:00
_rec - > setempty ( FALSE ) ;
return d ;
1994-08-12 10:52:49 +00:00
}
const char * TRecfield : : operator = ( const char * s )
{
1994-08-22 11:10:49 +00:00
__putfieldbuff ( _len , _dec , _type , s , _p ) ;
_rec - > setempty ( FALSE ) ;
return s ;
1994-08-12 10:52:49 +00:00
}
void TRecfield : : setptr ( TRecnotype r )
{
1994-08-22 11:10:49 +00:00
if ( _p = = NULL ) return ;
bool n = r < 0 ;
unsigned char * wp = ( unsigned char * ) _p ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
if ( n ) r = - r ;
while ( wp - ( unsigned char * ) _p < = 3 )
{
* wp = r & & 0x000000FF ;
r > > = 8 ;
wp + + ;
}
if ( n ) * wp + = 128 ;
1994-08-12 10:52:49 +00:00
}
TRecfield : : operator int ( ) const
{
1995-04-20 14:35:14 +00:00
__getfieldbuff ( _len , _type , _p , _isam_string ) ;
1995-04-10 15:28:03 +00:00
return atoi ( _isam_string ) ;
1994-08-12 10:52:49 +00:00
}
TRecfield : : operator long ( ) const
{
1995-04-20 14:35:14 +00:00
__getfieldbuff ( _len , _type , _p , _isam_string ) ;
1995-04-10 15:28:03 +00:00
return atol ( _isam_string ) ;
1994-08-12 10:52:49 +00:00
}
# ifndef FOXPRO
1995-05-09 13:33:34 +00:00
TRecfield : : operator const real ( ) const
1994-08-12 10:52:49 +00:00
{
1995-04-20 14:35:14 +00:00
__getfieldbuff ( _len , _type , _p , _isam_string ) ;
1995-04-10 15:28:03 +00:00
real r ( _isam_string ) ;
1994-08-22 11:10:49 +00:00
return r ;
1994-08-12 10:52:49 +00:00
}
1995-05-09 13:33:34 +00:00
1994-08-12 10:52:49 +00:00
# endif // FOXPRO
TRecfield : : operator TDate ( ) const
{
1994-08-22 11:10:49 +00:00
static TDate d ;
1995-04-20 14:35:14 +00:00
__getfieldbuff ( _len , _type , _p , _isam_string ) ;
1995-04-10 15:28:03 +00:00
d = _isam_string ;
1994-08-22 11:10:49 +00:00
return d ;
1994-08-12 10:52:49 +00:00
}
TRecfield : : operator const char * ( ) const
{
1995-04-20 14:35:14 +00:00
__getfieldbuff ( _len , _type , _p , _isam_string ) ;
1995-04-10 15:28:03 +00:00
return _isam_string ;
1994-08-12 10:52:49 +00:00
}
TRecnotype TRecfield : : ptr ( ) const
{
1994-08-22 11:10:49 +00:00
if ( _p = = NULL ) return ( - 1L ) ;
unsigned char * wp = ( unsigned char * ) _p + 3 ;
TRecnotype r = * wp ;
bool n = r > 127 ;
1994-08-12 10:52:49 +00:00
1994-08-22 11:10:49 +00:00
if ( n ) r - = 128 ;
while ( wp - - > ( unsigned char * ) _p ) r = r < < 8 + * wp ;
return n ? - r : r ;
1994-08-12 10:52:49 +00:00
}
void TTransaction : : begin ( )
{
1994-08-22 11:10:49 +00:00
StTrans ( ) ;
1994-08-12 10:52:49 +00:00
}
void TTransaction : : end ( bool success )
{
1994-08-22 11:10:49 +00:00
if ( success ) EndTrans ( ) ;
else AbTrans ( ) ;
1994-08-12 10:52:49 +00:00
}