#include "dbinc.h"
/*****************************************************************************
*                                                                            *
*      COMMON OCEANOGRAPHIC DATA ACCESS SYSTEM (CODAS)                       *
*                                                                            *
*      WRITTEN BY:  RAMON CABRERA, ERIC FIRING, and JULIE RANADA             *
*                   JOINT INSTITUTE FOR MARINE AND ATMOSPHERIC RESEARCH      *
*                   1000 POPE ROAD  MSB 404                                  *
*                   HONOLULU, HI 96822                                       *
*                                                                            *
*      VERSION:     3.00                                                     *
*                                                                            *
*      DATE:        APRIL 1989                                               *
*                                                                            *
*****************************************************************************/
/*

       FILE:  dbmove.c

*/
/*----------------------------------------------------------------------------

   FUNCTION:  DBMOVE

      It resets the database pointer to *steps profiles from the current profile.

   PARAMETERS:
   
      *steps = number of profiles to advance

      *ierr = error code

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
void DBMOVE(int *steps, int *ierr)
#else
void DBMOVE(steps, ierr)
int *steps, *ierr;
#endif
{
   int iprofile, jprofile, nprofiles, iblock, n;
   int profile_found = 0;

   if ((!(db)) || first_database_call)
   {
      *ierr = DB_IS_NOT_OPEN;
      return;
   }
   if (db->create_mode)
   {
      *ierr = OPEN_FOR_CREATE;
      return;
   }
   if (!(db->block_is_open))
   {
      *ierr = NO_BLOCK_IS_OPEN;
      return;
   }
   *ierr = 0;
   n = *steps;
   iblock = db->block_dir_index;
   jprofile = db->profile_dir_index;
   nprofiles = db->block_hdr.dir_nentries;
   while (!profile_found)
   {
      iprofile = jprofile + n;
      if ((iprofile >= 0) && (iprofile < nprofiles))
         profile_found = 1;
      else
      {
         if (iprofile > 0)
         {
            iblock++;
            if (iblock >= (int)db->block_dir_hdr.dir_nentries)
            {
               *ierr = SEARCH_BEYOND_END;
               iprofile = db->block_hdr.dir_nentries - 1;
               profile_found = 1;
            }
            else
            {
               n = n - (nprofiles - jprofile);
               if (close_block())
                  goto error_found;
               db->block_dir_index = iblock;
               if (lock_on_block())
                  goto error_found;
               nprofiles = db->block_hdr.dir_nentries;
               jprofile = 0;
            }
         }
         else
         {
            iblock--;
            if (iblock < 0)
            {
               *ierr = SEARCH_BEFORE_BEGINNING;
               iprofile = 0;
               profile_found = 1;
            }
            else
            {
               n = n + jprofile + 1;
               if (close_block())
                  goto error_found;
               db->block_dir_index = iblock;
               if (lock_on_block())
                  goto error_found;
               nprofiles = db->block_hdr.dir_nentries;
               jprofile = nprofiles - 1;
            }
         }
      }
   }
   db->profile_dir_index = iprofile;
   if (lock_on_profile())
      goto error_found;
   return;

   error_found:
      report_db_error("DBMOVE");
      *ierr = db->error_code * 1000 - db->error_data;
}

