/* d4data_p.cpp (c)Copyright Sequiter Software Inc., 1989-1993.  All rights reserved. */

#include "d4all.h"
#ifndef S4UNIX
#ifdef __TURBOC__
#pragma hdrstop
#endif  /* __TUROBC__ */
#endif  /* S4UNIX */

#include "d4data.hpp"

int Code4::optAll()
{
  for( Data4 data = Data4((DATA4 *)l4first( &this->data_list )); data.isValid();
      data = Data4((DATA4 *)l4next( &this->data_list, data.data)) )
  {
    int rc ;
    if( (rc = data.lockAll()) != 0 )  return rc ;
    data.optimize( 1 ) ;
    data.optimizeWrite( 1 ) ;
  }
  if( errorCode < 0 )  return -1 ;
  if( optStart() < 0 )
    return error( e4memory ) ;
  return 0 ;
}


#ifdef S4OLD_CODE
/*   CodeBase++ 1.0
     Field4 Data4::field( char *name )
     {
     Field4 temp( this, name ) ;
     return temp ;
     }

     Field4 Data4::field( int j )
     {
     Field4 temp( this, j ) ;
     return temp ;
     } */
#endif


void Field4memo::changed()
{
  field->data->record_changed = 1 ;
  if ( field->memo != 0 )
    field->memo->is_changed =  1 ;
}
#ifdef JUNK
Field4memo::Field4memo()
{
}
#endif

Field4memo::Field4memo( Data4& data, int j ) : Field4( data, j )
{
}

Field4memo::Field4memo( Data4& data, char *name ) : Field4( data, name )
{
}

Field4memo::Field4memo( Field4 f )
{
  field = f.field ;
}


REPORT4 * S4FUNCTION  report4retrieve(Code4 &cb, char *file_name,
                                      int open_files, char *pathname)
{

  char *buf, *name_buf;
  REPORT4  *retvalue;

  buf = (char *) u4alloc_free( (CODE4 *) &cb, 2048 );
  if( !buf )
    return NULL;

  name_buf = (char *) u4alloc_free( (CODE4 *) &cb, 512 );
  if( !name_buf )
  {
    u4free( buf );
    return NULL;
  }
  retvalue = report4retrieve2( (CODE4 *) &cb, file_name, open_files, pathname, buf, name_buf  );
  u4free( buf );
  u4free( name_buf );

  return retvalue;

}

RELATE4 * S4FUNCTION relate4retrieve( Code4 &cb, char *file_name,
                                     int open_files, char *pathname )
{
  char *buf, *name_buf;
  RELATE4* retvalue;

  buf = (char *) u4alloc_free( (CODE4 *) &cb, 2048 );
  if( !buf )
    return (RELATE4*)NULL;

  name_buf = (char *) u4alloc_free( (CODE4 *) &cb, 512 );
  if( !name_buf )
  {
    u4free( buf );
    return (RELATE4*)NULL;
  }

  retvalue = relate4retrieve2( (CODE4 *) &cb, file_name, open_files, pathname, buf, name_buf  );

  u4free( buf );
  u4free( name_buf );

  return retvalue;

}

int S4FUNCTION relate4save(Relate4set &relSet, char *file_name,
                           int save_paths )
{
  char *buf, *name_buf;
  int retvalue;

  buf = (char *) u4alloc_free( relSet.relate->code_base, 2048 );
  if( !buf )
    return -1;

  name_buf = (char *) u4alloc_free( relSet.relate->code_base, 512 );
  if( !name_buf )
  {
    u4free( buf );
    return -1;
  }

  retvalue = relate4save2( relSet.relate, file_name, save_paths, buf, name_buf  );

  u4free( buf );
  u4free( name_buf );

  return retvalue;

}
