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

 Module:      STRUTIL.C

 Description: A collection of general purpose string manipulation
              primitives.

 Version:     2.5a1 (SuSE Linux 9.0, gcc compiler)

 Author:      Michael Campbell

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

#include <stdlib.h>
#include <string.h>

/**/

void strcpy_right_justify ( src, dst, dst_size, pattern )

/**************************************************************************
 This function initializes the destination buffer to the indicated pattern,
 and then copies the src buffer into it, such that it is right justified 
 within the dst buffer. The src buffer MUST contain a null terminated 
 string. This function returns no value.
 **************************************************************************/

char *src, *dst;        /* The source and destination buffers             */
int  dst_size;          /* The length in bytes of the destination buffer  */
char pattern;           /* The pattern the dst buffer is prefilled with   */
{
   int src_size;        /* The length of the source string                */
   int src_ix, dst_ix;  /* Source and destination indices                 */

   src_size = strlen ( src ) + 1;  /* Include the nul at the end          */

   /* If either string is null, simply return                             */

   if ( (src_size < 2) || (dst_size < 2) ) {
      dst[0] = '\0';
      return;
   }

   /* Set up the destination buffer, by filling and then nul terminating  */

   setmem ( dst, dst_size, pattern );
   dst[dst_size - 1] = '\0';

   /* Copy the strings in reverse                                         */

   for (src_ix=src_size-2, dst_ix=dst_size-2;((src_ix >= 0) && (dst_ix >= 0));
        src_ix--,dst_ix--)
        dst[dst_ix] = src[src_ix];

  /* Check for a partial copy. If the entire source would not fit into the */
  /* destination, indicate this by setting the first char to '<'           */

  if ( src_ix > 0 ) 
     dst[0] = '<';

} /* strcpy_right_justify */

/**/

int findstr ( str, strtable, table_entries )

/**************************************************************************
 This function searches the supplied string table for the supplied string, 
 and returns either -1, if the string has not been found, or the index of 
 the string entry within the string table, if it was found.            
 **************************************************************************/

char *str;               /* String to be searched for                     */
char *strtable[];        /* Table of strings to be searched               */
int table_entries;       /* The number of etnries in strtable             */

{
   int result = -1;      /* The returned result                           */
   int i;                /* Loop counter                                  */

   if ( table_entries > 0 ) {

      /* Loop over whole string table, and compare each element against   */
      /* the target string. If found, set the return var and break out.   */

      for ( i = 0; i < table_entries; i++ ) 
         if ( strcmp(str, strtable[i]) == 0 ) {
            result = i;
            break;
      }
   }
   return ( result );

} /* findstr */

