 /* d4unlock.c   (c)Copyright Sequiter Software Inc., 1990-1994.  All rights reserved. */

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

 int S4FUNCTION d4unlock( DATA4 *data )
{
#ifndef S4SINGLE
  int rc ;

#ifdef S4VBASIC
  if ( c4parm_check( data, 2, E4_D4UNLOCK ) )
    return -1 ;
#endif  /* S4VBASIC */

  if ( data == 0 )
#ifdef S4DEBUG
    e4severe( e4parm, E4_D4UNLOCK ) ;
#else
  return -1 ;
#endif

#ifndef S4OFF_WRITE
  rc =  d4update( data ) ;  /* returns -1 if code_base->error_code < 0 */
  if ( rc < 0 )
    return -1 ;
#endif

  d4unlock_data( data ) ;
#ifndef N4OTHER
#ifndef S4MEMO_OFF
  if ( data->n_fields_memo > 0 && data->memo_file.file.hand != -1 )
    memo4file_unlock( &data->memo_file ) ;
#endif
#endif
  d4unlock_index( data ) ;
  if ( data->code_base->error_code < 0 )
    return -1 ;
  return rc ;
#else
  return 0 ;
#endif
}

/* only unlocks the append byte */
int S4FUNCTION d4unlock_append( DATA4 *data )
{
#ifndef S4SINGLE
#ifdef S4DEBUG
  if ( data == 0 )
    e4severe( e4parm, E4_D4UNLOCK_AP ) ;
#endif
  if ( data->append_lock )
  {
    if ( file4unlock( &data->file, L4LOCK_POS, 1L ) < 0 )
      return -1 ;
    data->append_lock =  0 ;
    data->num_recs = -1 ;
  }
  if ( data->code_base->error_code < 0 )
    return -1 ;
#endif
  return 0 ;
}

int S4FUNCTION d4unlock_data( DATA4 *data )
{
#ifndef S4SINGLE
#ifdef S4DEBUG
  if ( data == 0 )
    e4severe( e4parm, E4_D4UNLOCK_DATA ) ;
#endif
  d4unlock_file( data ) ;
  d4unlock_append( data ) ;
  d4unlock_records( data ) ;
  if ( data->code_base->error_code < 0 )
    return -1 ;
#endif
  return 0 ;
}

int S4FUNCTION d4unlock_file( DATA4 *data )
{
#ifndef S4SINGLE
#ifdef S4VBASIC
  if ( c4parm_check( data, 2, E4_D4UNLOCK_FILE ) )
    return -1 ;
#endif

#ifdef S4DEBUG
  if ( data == 0 )
    e4severe( e4parm, E4_D4UNLOCK_FILE ) ;
#endif

  if ( data->file_lock )
  {
#ifdef N4OTHER
    if ( file4unlock( &data->file, L4LOCK_POS, L4LOCK_POS ) < 0 )
      return -1 ;
#endif
#ifdef S4MDX
    if ( file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS - L4LOCK_POS_OLD + 1 ) < 0 )
      return -1 ;
#endif
#ifdef S4FOX
    /* codebase locks the append byte as well... */
    if ( file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS_OLD - 1L ) < 0 )
      return -1 ;
#endif
    data->rec_num_old =  -1 ;
    data->memo_validated =  0 ;
    data->file_lock =  0 ;
    data->num_recs = -1 ;
  }
  if ( data->code_base->error_code < 0 )
    return -1 ;
#endif
  return 0 ;
}

int S4FUNCTION d4unlock_files( CODE4 *c4 )
{
#ifndef S4SINGLE
  DATA4 *data_on ;

  if ( c4 == 0 )
#ifdef S4DEBUG
    e4severe( e4parm, E4_D4UNLOCK_FILES ) ;
#else
  return -1 ;
#endif

  for ( data_on = 0 ;; )
  {
    data_on = (DATA4 *)l4next( &c4->data_list, data_on ) ;
    if ( data_on == 0 )
      break ;
    d4unlock( data_on ) ;
  }

  if ( c4->error_code < 0 )
    return -1 ;
#endif
  return 0 ;
}

int S4FUNCTION d4unlock_index( DATA4 *data )
{
#ifdef S4SINGLE
  return 0 ;
#else
  INDEX4 *index_on ;

#ifdef S4DEBUG
  if ( data == 0 )
    e4severe( e4parm, E4_D4UNLOCK_INDEX ) ;
#endif

  for ( index_on = 0 ;; )
  {
    index_on = (INDEX4 *)l4next(&data->indexes,index_on) ;
    if ( index_on == 0 )
    {
      if ( data->code_base->error_code < 0 )
        return -1 ;
      return 0 ;
    }
    i4unlock(index_on) ;
  }
#endif
}

int S4FUNCTION d4unlock_records( DATA4 *data )
{
#ifndef S4SINGLE
#ifdef S4DEBUG
  if ( data == 0 )
    e4severe( e4parm, E4_D4UNLOCK_REC ) ;
#endif

  data->rec_num_old =  -1 ;
  data->memo_validated =  0 ;

  while ( data->num_locked > 0 )
  {
    data->num_locked-- ;
#ifdef N4OTHER
    if ( file4unlock( &data->file, L4LOCK_POS + data->locks[data->num_locked], 1L ) < 0 )
      return -1 ;
#endif
#ifdef S4MDX
    if ( file4unlock( &data->file, L4LOCK_POS - data->locks[data->num_locked] -1L, 1L ) < 0 )
      return -1 ;
#endif
#ifdef S4FOX
    if ( data->has_mdx )
    {
      if ( file4unlock( &data->file, L4LOCK_POS - data->locks[data->num_locked], 1L ) < 0 )
        return -1 ;
    }
    else
      if ( file4unlock( &data->file, L4LOCK_POS_OLD + d4record_position( data, data->locks[data->num_locked] ), 1L ) < 0 )
        return -1 ;
#endif
  }
#endif
  return 0 ;
}
