/* @lay:4386l2 */

/* Listing 2 */

/**************************************************
Function: GetFormIntegerValue 

Input:    FieldName: Pointer to a NULL 
          terminated string containing  
          the field name to get from the 
          form data. 

Return:   The integer contained in the form 
          data field with the specified
          name. If the field does not exist, 
          is null or contains any characters 
          that are not numbers or spaces, -1 
          is returned.  

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

int GetFormIntegerValue(const char *FieldName) {
    char  *FieldData  = NULL;
    int    Index      = 0;
    flag   DataValid  = TRUE;     

    /* make sure a field name was passed in */
    if (FieldName == NULL) return(-1);

    /* make sure the field exists */
    FieldData = GetFormStringValue(FieldName);
    if (FieldData == NULL)    return(-1);
    if (FieldData[0] == 0x00) return(-1);


    /* make sure there are only numbers and 
       spaces in the field */

    while (FieldData[Index] != 0x00 
        && DataValid == TRUE) {

        if ((FieldData[Index] < '0' 
            || FieldData[Index] > '9') &&
             FieldData[Index] != ' '
           )
            DataValid = FALSE;
         Index += 1;

         } /* end while */

    if (DataValid == FALSE) return(-1);
        /* get out successfully */
        return(atoi(FieldData));

      } /* end function GetFormIntegerValue() */




/**************************************************
Function: GetFormFloatValue

Input:    FieldName: Pointer to a NULL terminated 
          string containing the field name to get 
          from the form data.

Return:   The float value contained in the form 
          data field with the specified name. If 
          the field does not exist, is null or
          contains any characters that are not 
          numbers or spaces, -1.00 is returned.

**************************************************/
   
float GetFormFloatValue(const char *FieldName) {    
    char   *FieldData  = NULL;
    int     Index      = 0;
    flag    DataValid  = TRUE;

    /* make sure a field name was passed in */
    if (FieldName == NULL) return(-1.00);

    /* make sure the field exists */
    FieldData = GetFormStringValue(FieldName);
    if (FieldData == NULL)    return(-1.00);
    if (FieldData[0] == 0x00) return(-1.00);  

    /* make sure there are only numbers, spaces
       and decimal points in the field. */

    while (FieldData[Index] != 0x00 
        && DataValid == TRUE) {

        if ((FieldData[Index] < '0' 
            || FieldData[Index] > '9') 

            && FieldData[Index] != ' ' 
            && FieldData[Index] != '.'   
           )
            DataValid = FALSE;
         Index += 1;
      } /* end while */

      if (DataValid == FALSE) return(-1.00);

      /* get out successfully */
      return(atof(FieldData));
    
      } /* end function GetFormIntegerValue() */


/**************************************************
Function: GetFormStringValue() 

Input:   FormData - Pointer to a NULL terminated 
         string containing the concatenated form 
         data. 
         FieldName - Pointer to a NULL terminated
         string containing the name of the field 
         whose value we want to fetch.

Return:  Pointer to the data area where the NULL 
         terminated string representing the value 
         of the field we wanted to fetch is
         stored.

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

char *GetFormStringValue(const char *FieldName) {
    int     Index       = 0;
    char  **FormData    = GetFormData(TRUE);
    char   *FieldValue  = NULL;
    char   *Trail       = NULL;
    char   *Delimiter   = NULL;

   /* make sure we have both a field name and the 
      form data */

    if (FieldName == NULL || FormData == NULL) 
        return(NULL); 

    /* find the start of the field in the 
       form data */

    while (FormData[Index] != NULL 
        && FieldValue == NULL) {
        Delimiter = strchr(FormData[Index], 
            (int) '=');
        if (Delimiter != NULL) {
            *Delimiter = 0x00;
            if (strcmp(FormData[Index], FieldName) 
                == 0)
               FieldValue = Delimiter+sizeof(char);
            *Delimiter = '=';
         } /* end if */

         Index += 1;
         
    } /* end while */


    /* strip out leading and trailing spaces */

    if (FieldValue != NULL) {
        while (*FieldValue == ' ')
            FieldValue += sizeof(char);
        if (strlen(FieldValue) > 0) {
            Trail = FieldValue +
            (strlen(FieldValue) * sizeof(char)) - 1;

            while(*Trail == ' ' && Trail 
                > FieldValue) {
                *Trail = 0x00;
                Trail -= sizeof(char);
            } /* end while */

        } /* end if */

    } /* end if */

      /* get out successfully */
      return(FieldValue);

} /* end function GetFormStringValue() */          
