/****************************************************************************/
/* Copyright 1990 to 1994 MBARI                                             */
/****************************************************************************/
/* Summary  : Pan & Tilt Microcontroller Softare                            */
/* Filename : strlib.c                                                      */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV                                                   */
/* Version  : 1.0                                                           */
/* Created  : 05/02/94                                                      */
/* Modified : 05/02/94                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/lib/strlib.c,v 1.2 1997/05/07 15:53:03 pean Exp $
 * $Log: strlib.c,v $
 * Revision 1.2  1997/05/07 15:53:03  pean
 * Cleaned up Include files and makefile for new directory structure.
 *
 * Revision 1.1.1.1  1997/05/02 17:16:01  pean
 * Initial release of the microcontroller software after Tiburon
 * Moolpool Dive to test IView, Lapboxes, modified Power can
 * GF/5V using bus capacitance mode.
 *
 */
/****************************************************************************/

#pragma model(196)
#pragma nosignedchar

#include "C:/C96/INCLUDE/80C196.h"      /* 80196 Register mapping           */
#include "C:/C96/include/stdlib.h"      /* C Standard Library Definitions   */
#include "const.h"                      /* Misc. constants - TRUE/FALSE etc */
#include "types.h"                      /* MBARI style guide declarations   */
#include "strlib.h"                     /* My string library functions      */

#define deblank(s)      while(isspace(*s)) ((s)++)
#define toupper(c)      (c & 0x5f)

    char *
strchr(const char* s, int c)
{
    while (*s != '\0')
    {
        if (*s == c)
            return ((char*) s);
        s++;
    } /* while */
    return (NULL);
} /* strchr*/

/************************************************************************/
/* Function    : delimit                                                */
/* Purpose     : Determine if character is a delimiter                  */
/* Inputs      : Character                                              */
/* Outputs     : TRUE if character is ',', NULL, or space               */
/************************************************************************/
        Boolean
delimit( Reg Byte c )
{
    return( isspace(c) || (c == '\0') || (c == ',') || (c == '.') );

} /* delimit() */

/************************************************************************/
/* Function    : tohex                                                  */
/* Purpose     : Convert an ASCII character to hex equivalent           */
/* Inputs      : ASCII character                                        */
/* Outputs     : Hex number, or ERROR if not valid hex digit            */
/************************************************************************/
        Int16
tohex( Int16 c )
{
    if ( isdigit(c) )
        return( c - '0' );
    if ( isxdigit(c) )
        return( toupper(c) - 'A' + 10 );
    return( ERROR );

} /* tohex() */

/************************************************************************/
/* Function    : atoi                                                   */
/* Purpose     : Convert a number from ascii to an integer              */
/* Inputs      : Ptr to string for input                                */
/* Outputs     : Returns integer value                                  */
/************************************************************************/
    int
atoi(const char *s)
{
    Reg Int16   i, j, neg;

    deblank(s);

    neg = 1;
    if ( *s == '-' )
    {
        neg = -1;
        s++;
    }

    if ( (i = tohex(*s)) < 0 )
        return(0);

    while ( (j = tohex(*++s)) >= 0 )
        i = (i * 10) + j;

    if ( delimit(*s) )
        return(i * neg);

    return(i);
} /* atoi () */

/************************************************************************/
/* Function    : itoa                                                   */
/* Purpose     : Convert a number from an integer to ascii              */
/* Inputs      : Ptr to string for input                                */
/* Outputs     : Returns length of string                               */
/************************************************************************/
    Int16
itoa( char* buffer, Nat32 num, Int16 digits, Int16 radix,
    Int16 fillchar, enum signType signMode)
{
#define BLEN    14

    Byte    buf[BLEN];
    Int32   j;
    Int16   i, pos;
    Boolean neg = FALSE;

    if (signMode == DOSIGNED )
    {
        if ( (Int32) num < 0 )
        {
            if (digits)
                digits--;
            num = -num;
            neg = TRUE;
        } /* if */
    } /* if */

    else if ( signMode == SIGN )
    {
        if (digits)
            digits--;

        if ( (Int32) num < 0 )
        {
            num = -num;
            neg = TRUE;
        }
    } /* else */

    i = 0;
    do                      /* Fill buffer with digits*/
    {                       /* LSB in buf[0]      */
        j = num % radix;
        buf[i] = (j > 9) ? (j + 'A' - 10) : (j + '0');
        num /= radix;
        i++;
    } while ( (i < BLEN) && num );

    if ( digits && (i > digits) )
        i = digits;

    pos = 0;
    for( j = digits; j > i; j-- )
        buffer[pos++] = (char) fillchar;

    if (digits == 0)
            buffer[pos++] = (char) fillchar;

    if ( (signMode == DOSIGNED) && ( neg == TRUE ))
            buffer[pos++] = '-';

    else if (signMode == SIGN)
            buffer[pos++] = ( neg == TRUE ? '-' : '+');

    while ( --i >= 0 )              /* Print buffer from      */
        buffer[pos++] = (char) buf[i];  /*  MSB (buf[7])      */

    buffer[pos] = '\0';             /* Null terminate string  */
    return (pos);                   /* Return length of string*/
} /* itoa() */




