#ifndef __LFFILES_H
#include "lffiles.h"
#endif

#ifndef __DYNAREL_H
#include "dynarel.h"
#endif


TField_array::TField_array( const TRectype& rec )
{
  int last = rec.items( );
  for( int i = 0; i  < last;  i ++ )
  {
    add( rec.fieldname( i ), rec.get( rec.fieldname( i ) ) );  
  }   
}

const TString& TField_array::get( const TString& nome_campo )
{
  return ( TString& )(*this)[ nome_campo ];
}

int TField_array::get_int( const TString& nome_campo )
{
  return atoi( get( nome_campo ) );
}

long TField_array::get_long( const TString& nome_campo )
{
  return atoi( get( nome_campo ) );
}

bool TField_array::get_bool( const TString& nome_campo )
{ 
  TString p( get( nome_campo ) );
  char fc = toupper( p[ 0 ] );
  return ( fc == 'T' || fc == 'Y' || fc == 'S' || fc == 'X' );
}

real TField_array::get_real( const TString& nome_campo )
{
  real r( get( nome_campo ) );
  return r;
}


TDyna_rel::TDyna_rel( const int main_file_index )
{
  _main_file = main_file_index;
  load_file( main_file_index );
};

bool TDyna_rel::file_added( const int file_index )
{
  return ( &_rules[ file_index ] != NULL );
}

bool TDyna_rel::file_loaded( const int file_index )
{
  return ( &_file_array[ file_index ] != NULL );
}

void TDyna_rel::load_file( const int file_index )
{
  CHECK( file_loaded( file_index ), "Utilizzo di un file non aggiunto alla dyna_rel!" );
  TToken_string rule( ( ( TString& ) _rules[ file_index ] ) );

  // Il file principale � gi� caricato
  TLocalisamfile to( file_index );
  to.setkey( _keys[ file_index ] );
  to.curr( ).zero( );
  int last = rule.items( );
  // Carico i valori per la ricerca
  for( int i = 0; i < last; i += 3 )
    to.curr( ).put( rule.get( i ), get( ( rule.get_int( i + 1 ) == 0 ? rule.get_int( i + 1 ) : _main_file ), rule.get( i + 2 ) ) );
  to.read ( _isequal );
  // Se il posizionamento riesce ...
  if( !to.bad( ) )
    {
       //  Carica tutti i campi                                   
       _file_array.add( new TField_array( to.curr( ) ), file_index );
    }
}  

void TDyna_rel::add_file( const int file_index, const TToken_string rule, const int key )
{
  _rules[ file_index ] = rule;
  _keys[ file_index ] = key;

};

const TString& TDyna_rel::get( const int file_index, const TString16 nome_campo )
{
  CHECK( file_loaded( file_index ), "Tentativo di accesso ad un campo di un file non caricato!" );
  return ( file( file_index ).get( nome_campo ) );
};

int TDyna_rel::get_int( const int file_index, const TString16 nome_campo )
{
  CHECK( file_loaded( file_index ), "Tentativo di accesso ad un campo di un file non caricato!" );
  return ( file( file_index ).get_int( nome_campo ) );
};

long TDyna_rel::get_long( const int file_index, const TString16 nome_campo )
{
  CHECK( file_loaded( file_index ), "Tentativo di accesso ad un campo di un file non caricato!" );
  return ( file( file_index ).get_long( nome_campo ) );
};

bool TDyna_rel::get_bool( const int file_index, const TString16 nome_campo )
{
  CHECK( file_loaded( file_index ), "Tentativo di accesso ad un campo di un file non caricato!" );
  return ( file( file_index ).get_bool( nome_campo ) );
};

real TDyna_rel::get_real( const int file_index, const TString16 nome_campo )
{
  CHECK( file_loaded( file_index ), "Tentativo di accesso ad un campo di un file non caricato!" );
  return ( file( file_index ).get_real( nome_campo ) );
};

class TCF : public TDyna_rel
{
  public:
    
    TCF( );
    

};

TCF::TCF( ) : TDyna_rel( LF_CLIFO )
{
  add_file( LF_CFVEN, "", 1 );
}