/******************************************************************************

    FILE:  SHOWDB.C
    USAGE: SHOWDB <database name> [supplementary definition file] [flag]

    This progam is used for examining the contents of a CODAS database
    specified by <database name>.  Together with the SHDB function in
    the CODAS\SOURCE file DB4.C, it provides an example of how to
    write menu-driven routines for displaying the contents of a CODAS
    database.  The contents are displayed to the screen using default
    printing formats.

    If the user wants to override the default formats for printing
    structures with specifications of his own, an external text file
    with DEFINE_FORMAT statements can be specified as a
    [supplementary definition file].  See the CODAS documentation file
    CODAS.DOC for the syntax of the DEFINE_FORMAT statement and the
    file MORE_SD.DAT for some examples.

    If the CODAS database includes user-defined data of structure value type,
    the user may want also want to use DEFINE_STRUCT statements in the
    [supplementary definition file] to specify the members of the structures
    in order to view the structure members properly, rather than as a
    sequence of unsigned bytes.  See CODAS.DOC for the syntax of the
    DEFINE_STRUCT statement and MORE_SD.DAT for examples.

    In the case where such structures have already been defined during loading,
    the original structure definitions may be overriden by using DEFINE_STRUCT
    statements for those structures in the [supplementary definition file] and
    setting the [flag] to 'y'.  If overrides are not desired, then no [flag]
    argument must be provided--the definitions set up during loading will take
    precedence over the [supplementary definition file] contents.

    89/2 - J. Ranada - University of Hawaii JIMAR

*/
#include "geninc.h"

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   char buff[80], ans = 'N';
   FILE_NAME_TYPE db_name, sd_file;
   int accmode = READ_ONLY, memmode = 0, db_id = 1, ier = 0;

   strcpy(db_name, "");
   strcpy(sd_file, "");
   if (argc < 2)
   {
      do
      {
         printf("\n ENTER DATABASE NAME: ");
         gets(buff);
         sscanf(buff, "%79s", db_name);
      } while (strlen(db_name) < 1);
      printf("\n ENTER EXTERNAL STRUCTURE DEFINITION FILE");
      printf("\n              <press return key for none>: ");
      gets(buff);
      if (strlen(buff) >= 1)
      {
         sscanf(buff, "%79s", sd_file);
         printf("\n LOAD BEFORE BLOCK STRUCTURE DEFINITIONS <y/n>? ");
         gets(buff);
         sscanf(buff, "%c", &ans);
         ans = toupper(ans);
      }
   }
   else
   {
      strcpy(db_name, argv[1]);
      if (argc > 2)
         strcpy(sd_file, argv[2]);
      if (argc > 3)  /* presence of third argument means load */
         ans = 'Y';  /* block structure definitions first     */
   }

   printf("\n DBOPEN DATABASE NAME = %s", db_name);
   DBOPEN(&db_id, db_name, &accmode, &memmode, &ier);
   if (DBERROR(&ier, "opening database")) return;
   if (ans != 'Y')    /* load block structure definitions first */
   {
      if (load_block_strdef())
         return;
   }
   if (strcmp(sd_file, ""))
   {
      DBLOADSD(sd_file, &ier);
      if (DBERROR(&ier, "loading structure definition file")) return;
   }

   SHDB();

   DBCLOSE(&ier);
   DBERROR(&ier, "closing database");
}

