/*
 * parse.c
 *
$Author: dm3e $
$Date: 1992/12/01 23:42:05 $
$Revision: 1.2 $
$State: Exp $
$Locker:  $
$Log: parse.c,v $
 * Revision 1.2  1992/12/01  23:42:05  dm3e
 * *** empty log message ***
 *
 * Revision 1.1  1992/07/07  21:19:28  dm3e
 * Initial revision
 *
 *
 */

/***********************************************************
        Copyright 1992 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/

#include <stdio.h>
#include <errno.h>
#include "rxemt.h"
#include "tools.h"

/***************************************************************************
 * [exported] skip_whitespace
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Wed Jul 29 14:34:26 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long skip_whitespace(pos, data)
int *pos;
char *data;
{
    register int i;

    i = *pos;
    while((' ' == *(data+i) || '\t' == *(data+i)) &&
	  ('\0' != *(data+i)) && (i <= strlen(data))) i++;
    *pos = i;
}


/***************************************************************************
 * [exported] next_string
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Jul  7 17:14:44 1992 - created by David Allen Markley
 *
 ***************************************************************************/
char *next_string(pos, data)
int *pos;
char *data;
{
    char buffer[MAXBUFSIZE], *outstring;
    int i;

    if (*pos >= strlen(data)) return NULL;
    for (i = *pos; ((i < MAXBUFSIZE) && (*(data+i)!=DELIMITER)
		    && (*(data+i)!='\n') ); i++)
      {
	  buffer[i-*pos] = *(data+i);
      }
    buffer[i-*pos] = '\0';
    if ((outstring = (char *)calloc(strlen(buffer)+1,sizeof(char))) == NULL) {
	Log("can't malloc enough memory.\n");
	exit(-1);
    }
    *pos = ++i;
    strcpy(outstring, buffer);
    return outstring;
}

/***************************************************************************
 * [exported] next_string_d
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Jul  7 17:14:58 1992 - created by David Allen Markley
 *
 ***************************************************************************/
char *next_string_d(pos, data, delimit)
int *pos;
char *data;
char delimit;
{
    char buffer[MAXBUFSIZE], *outstring;
    int i;

    buffer[0] = '/0';
    for (i = *pos; (i < MAXBUFSIZE) && (*(data+i) != '\n') && (*(data+i) != '\0'); i++) {
	if (' ' != *(data+i) && '\t' != *(data+i)) {
	    *pos = i;
	    break;
	}
    }
    for (i = *pos; ((i < MAXBUFSIZE) && (*(data+i)!=delimit) && (*(data+i)!='\0')
		    && (*(data+i)!='\n') && (*(data+i)!='\t')); i++)
      {
	  buffer[i-*pos] = *(data+i);
      }
    buffer[i-*pos] = '\0';
    if (i == *pos) return NULL;
    if ((outstring = (char *)calloc(strlen(buffer)+1,sizeof(char))) == NULL) {
	Log("can't malloc enough memory.\n");
	exit(-1);
    }
    *pos = ++i;
    strcpy(outstring, buffer);
    return outstring;
}

/***************************************************************************
 * [exported] next_float
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Jul  7 17:15:09 1992 - created by David Allen Markley
 *
 ***************************************************************************/
float next_float(pos, data)
int *pos;
char *data;
{
    float value = 0.0;
    int dec = 0;
    while (data[*pos] != DELIMITER) {
	    switch (data[*pos]) {
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		if (dec) {
		    dec *= 10;
		    value += (((float)(data[*pos] - 48)) / dec);
		} else {
		    value *= 10.0;
		    value += (float)(data[*pos] - 48);
		}
		break;
	    case '.':
		dec = 1;
		break;
	    case '*':
		value = 0.0;
		break;
	    case '-':
		value *= -1.0;
		break;
	    case '\n':
		data[*pos + 1] = DELIMITER;
		break;
	    default:
		break;
	    }
	    ++*pos;
    }
    ++*pos;
    return value;
}

/***************************************************************************
 * [exported] next_int
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Jul  7 17:15:19 1992 - created by David Allen Markley
 *
 ***************************************************************************/
int next_int(pos, data, delimeter)
int *pos;
char *data;
char delimeter;
{
    int value = 0;
    short negative = FALSE;

    while (data[*pos] != delimeter) {
	    switch (data[*pos]) {
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		value *= 10;
		value += data[*pos] - 48;
		break;
	    case '*':
		value = 0;
		break;
	    case '-':
		negative = TRUE;
		break;
	    case '\n':
		data[*pos + 1] = delimeter;
		break;
	    default:
		break;
	    }
	    ++*pos;
    }
    ++*pos;
    if (negative) value *= -1;
    return value;
}

/***************************************************************************
 * [exported] next_long
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Jul  7 17:15:28 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long next_long(pos, data)
int *pos;
char *data;
{
    long value = 0;
    while (data[*pos] != DELIMITER) {
	    switch (data[*pos]) {
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		value *= 10;
		value += data[*pos] - 48;
		break;
	    case '*': case '\t': case ' ':
		value = 0;
		break;
	    case '-':
		value *= -1;
		break;
	    case '\n':
		data[*pos + 1] = DELIMITER;
		break;
	    default:
		break;
	    }
	    ++*pos;
    }
    ++*pos;
    return value;
}


