#include <stdio.h>
#include <string.h>  /* str*() */
#include <ctype.h>    /* toupper, islower */ 
#include "dbhost.h"  /* COMPILER */
#include "dbext.h"   /* BADINT */
#include "misc.h"    /* NAME_LIST_ENTRY_TYPE */
/*****************************************************************************
*                                                                            *
*      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:  misc.c

              MISCELLANEOUS FUNCTIONS

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

   FUNCTION:  strtrim   **MODIFIED** 88-01-09 by EF

      Given a string s1 (not neccesarily NUL terminated) it copies s1 into
      s2 less any trailing blanks

      modification: s2 will be NUL terminated
      modification: the order of the arguments has been changed
            to be consistent with the other str functions

   PARAMETERS:
   
      s1 = pointer to character array (string) (source)

      s2 = pointer to character array where s1 will be copied to 
           without trailing blanks    (destination)
        
      n = max number of chars to copy, including the NUL

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
void strtrim(char *s2, char *s1, int n)
#else
void strtrim(s2, s1, n)
char *s1, *s2;
int n;
#endif
{
   int i = 0;
   n--;
   while ((s1[i] != ' ') && (s1[i] != '\0') && (i < n))
      i++;
   strncpy(s2, s1, i);
   s2[i] = '\0';
}

#ifdef LACKS_STRICMP

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

    FUNCTION:  stricmp

       A case-insensitive string comparison function.
       Note:  This function is available for most C compilers except the VAX,
       hence the compiler-dependent #if directive.

    PARAMETERS:

       str1, str2 = pointers to strings to be compared

    RETURNS:

       +1 if str1 > str2
        0 if str1 = str2
       -1 if str1 < str2

*/
static char __toupper(char c)        /* ALLIANT toupper assumes lowercase argument */
{
   if (islower(c))
      return(c - 'a' + 'A');
   else
      return(c);
}

#if PROTOTYPE_ALLOWED
int stricmp(char *str1, char *str2)               /* case-insensitive strcmp */
#else
int stricmp(str1, str2)               /* case-insensitive strcmp */
char *str1, *str2;
#endif
{
   int i = 0;
   char c1, c2;

   while (str1[i])                   /* while not end of first string */
      if ((c1 = __toupper(str1[i])) > (c2 = __toupper(str2[i])))
         return(1);
      else if (c1 < c2)
         return(-1);
      else i++;
   if (str2[i])               /* string 1 is exhausted before string 2 */
      return(-1);
   return(0);
}
#endif

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

   FUNCTION:   split_path

      It takes a database name that includes a path and
      splits it into the path part and the name part.
      The name is restricted to MAX_DBNAME_LENGTH
      characters.

   ARGUMENTS:

      all = the name including path: e.g. C:\data\adcp

      path = the path part of all    e.g. C:\data\

      name = the name part of all    e.g. adcp

   RETURNS: void

*/
#define isname(c) (isalpha(c) || isdigit(c) || c=='_')

#if PROTOTYPE_ALLOWED
void split_path(char *all, char *path, char *name)
#else
void split_path(all, path, name)
char *all, *path, *name;
#endif
{
   int i;

   for (i=strlen(all)-1; i>0 && isname(all[i]); i--)
      ;                   /* i is 0 or index of last path character */
   if (i>0)
      i++;         /* first character that is part of name; number of
                        path characters  */
   strncpy(path, all, i);
   path[i] = '\0';
   strcpy(name, all+i);
}

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

   FUNCTION:  get_code

      It returns the code, given the name from a NAME_LIST_ENTRY_TYPE structure.

   PARAMETERS:

      list = array of NAME_LIST_ENTRY_TYPE structures
      name = pointer to name to search for in name field of list

   RETURNS:  code if okay
             BADINT if name is not in list (used to be -1 -- 2/22/91)

*/
#if PROTOTYPE_ALLOWED
int get_code(NAME_LIST_ENTRY_TYPE *list, char *name)
#else
int get_code(list, name)
NAME_LIST_ENTRY_TYPE *list;
char                 *name;
#endif
{
   int i;

   for (i = 0; list[i].name; i++)
      if (!strcmp(list[i].name, name))
         return(list[i].code);                /* match */
   return(BADINT);                            /* no match */
}

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

   FUNCTION:  get_name

      Inverse function for get_code():  Given the integer code, it returns
      the corresponding string in the specified array of NAME_LIST_ENTRY_TYPE
      structures.

   PARAMETERS:

      list = array of NAME_LIST_ENTRY_TYPE structures

      code = integer code to search for in list

   RETURNS:  name corresponding to integer code, if found in list
             NULL otherwise

*/
#if PROTOTYPE_ALLOWED
char *get_name(NAME_LIST_ENTRY_TYPE *list, int code)
#else
char *get_name(list, code)
NAME_LIST_ENTRY_TYPE *list;
int code;
#endif
{
   int i;

   for (i = 0; list[i].name; i++)
      if (list[i].code == code)
         return(list[i].name);         /* match */
   return(NULL);                       /* no match */
}




