#include "dbinc.h"

#if PROTOTYPE_ALLOWED
void DBPRTSTR(char *data, char *name, unsigned int *n)
#else
void DBPRTSTR(data, name, n)
char *data, *name;
unsigned int *n;
#endif
{
   if (!db->block_strdef_loaded)
      if (load_block_strdef() != 0)
      {
         report_db_error("DBPRTSTR");
         return;
      }
   if (!db->formats_matched)
      if ((db->error_code = match_formats(db->block_strdef, db->format_list)) != 0)
      {
         db->error_data = STRUCTURE_FORMATS;
         report_db_error("DBPRTSTR");
         return;
      }
   print_structure(stdout, data, name, *n, db->struct_def);
}
   
/*-----------------------------------------------------------------------------

    FUNCTION:  match_formats

       It sets the format_ptr fields of the structure element definitions to
       point to a format specification if found in a linked list of structure
       printing format definitions pointed to by format_list.

    PARAMETERS:

       strdef_list = linked list of structure definitions
       format_list = linked list of format specifications

    RETURNS:  0 if okay
              FORMAT_ERROR if element name in format specification
                           does not match element name in structure definition

*/
#if PROTOTYPE_ALLOWED
int match_formats(STRUCT_DEF_HDR_TYPE *strdef_list, FORMAT_HDR_TYPE *format_list)
#else
int match_formats(strdef_list, format_list)
STRUCT_DEF_HDR_TYPE *strdef_list;
FORMAT_HDR_TYPE *format_list;
#endif
{
   STRUCT_DEF_ENTRY_TYPE *strdef_ptr;
   FORMAT_ENTRY_TYPE *format_ptr;
   USHORT nelem, i;

   strdef_ptr = (STRUCT_DEF_ENTRY_TYPE *) strdef_list;

   if (format_list)
   {
      while (*(strdef_ptr->hdr.name))
      {
         nelem = strdef_ptr->hdr.nelem;
         if ((format_ptr = (FORMAT_ENTRY_TYPE *)
            find_format(strdef_ptr->hdr.name, format_list)) != NULL)
         {
            for (i = 0; i < nelem; i++)
            {
               format_ptr += 1;
               strdef_ptr += 1;
               if (!strcmp(strdef_ptr->elem.name, format_ptr->elem.name))
                  (*(FORMAT_SPEC **) strdef_ptr->elem.format_ptr) =
                  format_ptr->elem.format_ptr;
               else
                  return(FORMAT_ERROR);
            }
            strdef_ptr += 1;
         }
         else
            strdef_ptr += nelem + 1;
      }
   }
   db->formats_matched = 1;
   return(0);
}

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

    FUNCTION:  find_format

       It searches a linked list of structure printing format definitions
       for the definition corresponding to a specified structure.

    PARAMETERS:

       struct_name = name of structure to search for
       format_list = pointer to head of linked list

    RETURNS:

       pointer to structure printing format definition if found
       NULL otherwise

*/
#if PROTOTYPE_ALLOWED
FORMAT_HDR_TYPE *find_format(char *struct_name, FORMAT_HDR_TYPE *format_list)
#else
FORMAT_HDR_TYPE *find_format(struct_name, format_list)
char *struct_name;
FORMAT_HDR_TYPE *format_list;
#endif
{
   FORMAT_HDR_TYPE *format_ptr;

   format_ptr = format_list;
   while (format_ptr->name)
   {
      if (!strcmp(format_ptr->name, struct_name))
         return(format_ptr);
      format_ptr += format_ptr->nelem + 1;
   }
   return(NULL);
}

