/**---------------------------------------------------------------------------
 ** 
 ** manip.c -- Utilities for data manipulation in database
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/15 22:39:41
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/07/16 19:33:27
 ** Update Count    : 4
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/libs/db/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    The purpose of these utilities is to make it even easier to manipulate
 **    data in CODAS databases. They are designed to perform some actions
 **    instead of raising error messages, as functions DB* and db*_cnf do. 
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/

#include "manip.h"

/* ------------------------------------------------------------------
	 Close current block. If a profile is open, it is closed prior to close the
   block. 

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int close_blk()
#else
int close_blk()
#endif
{

	int ierr;

	/* Check if profile is open */
	if(db->new_profile_is_open){
		ierr = close_prf();
		if(DBERROR(&ierr, "close_prf()"))
			return(ierr);
	}
	
	/* Close block */
	if(db->new_block_is_open){
		DBENDBLK(&ierr);
		if(DBERROR(&ierr, "DBENDBLK"))
			return(ierr);
	}

	return(0);

}

/* ------------------------------------------------------------------
	 Close current profile. This is only performed if a profile is open. 

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int close_prf()
#else
int close_prf()
#endif
{

	int ierr;

	if(db->new_profile_is_open){
		DBENDPRF(&ierr);
		if(DBERROR(&ierr, "DBENDPRF"))
			return(ierr);
	}

	return(0);
}

/* ------------------------------------------------------------------
   Open a new block. Closes current block and profile before opening a new
   block 

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */

#if PROTOTYPE_ALLOWED
int open_blk()
#else
int open_blk()
#endif
{

	int ierr;

	/* check if current block is open */
	if(db->new_block_is_open){
		ierr = close_blk();
		if(DBERROR(&ierr, "close_blk()"))
			return(ierr);
	}
	
	report_msg("\n%%% OPENING NEW BLOCK\n");
	DBNEWBLK(&ierr);
	if(DBERROR(&ierr, "DBNEWBLK"))
		return(ierr);

	return(0);

}

/* ------------------------------------------------------------------
	 Open a new profile. If a profile is currently opened, it closed before
   openeing a new one. If the maximal number of allowed profiles is reached, a 
   new block is opened.

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int open_prf(/* Profile time */
             YMDHMS_TIME_TYPE *time)
#else
int open_prf(time)
#endif
{

	int ierr=0;

	/* check if profile is open */
	if(db->new_profile_is_open){
		ierr = close_prf();
		if(DBERROR(&ierr, "close_prf()"))
			return(ierr);
	}

	/* Check current number of profiles */
	if(db->block_hdr.dir_nentries >= MAX_PROFILE_DIR_SIZE){
		/* Open new block */
		ierr = open_blk();
		if(DBERROR(&ierr, "open_blk()"))
			return(ierr);
	}
	
	/* Open new profile */
	DBNEWPRF(time, &ierr);
	if(DBERROR(&ierr, "DBNEWPRF"))
		return(ierr);
	
	return(0);

}

/* ------------------------------------------------------------------
	 Close current database. Current profile and block are closed before closing 
   the database. 

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int close_db()
#else
int close_db()
#endif
{

	int ierr=0;

	/* Close current block and profile */
	close_prf();
	close_blk();
	
	/* Close the data base */
	DBCLOSE(&ierr);
	if(DBERROR(&ierr, "DBCLOSE"))
		 return(ierr);
	
	return(0);

}

/* ------------------------------------------------------------------
	 Create a database. The default memory mode is DIR_ON_DISK.

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int create_db(/* Database identifier */
              int db_id, 
              /* Database name */
              FILE_NAME_TYPE dbname, 
              /* Producer definition file name */
              FILE_NAME_TYPE prdname)
#else
int create_db(db_id, dbname, prdname)
#endif
{

	FILE_NAME_TYPE dirblk;

	int mem_mode = 0;
	int ierr=0;

	/* Build the directory name */
	strcpy(dirblk, dbname);
	strcat(dirblk, "dir.blk");
	
	/* Open data base */
	if(EXISTS(dirblk))
		report_msg("\n%%% APPENDING TO DATABASE\n");
	else
		report_msg("\n%%% CREATING DATABASE\n");
		
	DBCREATE(&db_id, dbname, prdname, &mem_mode, &ierr);
	if(DBERROR(&ierr, "DBCREATE"))
		return(ierr);
		
	return(0);
}

/* ------------------------------------------------------------------
	 Open a database. The default memory mode is DIR_ON_DISK.

   RETURNS: non-zero on error
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int open_db(/* Database identifier */
            int dbid, 
            /* Database name */
            char *dbname, 
            /* Database access mode */
            int dba)
#else
int ()
#endif
{

	int dbm = DIR_ON_DISK;
	int ierr = 0;
	
	DBOPEN(&dbid, dbname, &dba, &dbm, &ierr);
	DBERROR(&ierr, "DBOPEN");

	return(ierr);

}

/* ------------------------------------------------------------------
   Searches and position the database. This is equivalent to function
   dbsrch_cnf(), but no messages are displayed if searching occurs before
   beginning or beyond end of database.

	 RETURNS: Error status, or 0 if successfull.
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int search_db(/* Type of searching */
							int search_type, 
							/* Parameter to search for */
							char *search_param
							)
#else
int ()
#endif
{

	int ierr;

	DBSRCH(&search_type, search_param, &ierr);
	ierr = db_not_beof(ierr);
	DBERROR(&ierr, "DBSRCH()");
	
	return(ierr);

}
