// file: pfil_parse_0.cc
//

// system include files
//
#include <string.h>

// isip include files
//
#include "param_file.h"
#include "param_file_constants.h"
#include <integral_constants.h>

// method: parse_cc
//
// arguments:
//  char_1* buffer: (input) statement to be parsed
//  int_4 length: (input) size of the buffer
//
// return: a logical_1 indicating status
//
logical_1 Param_file::parse_cc(char_1* buffer, int_4 length) {
  
  if (length < 1) {
    return ISIP_FALSE;
  }
  if (buffer == (char_1*)NULL) {
    error_handler_cc((char_1*)"parse_cc",
		     (char_1*)"input buffer is null");
    return ISIP_FALSE;
  }
		     
  char_1* param;
  char_1* value;
  
  // terminate the buffer with a newline
  //
  buffer[length] = (char_1)NULL;
  
  // parse the statement into "param = value" sets
  //
  char_1* tmp = (char_1*)strchr((char*)buffer, (char)assignment_char_d);

  *tmp = (char_1)NULL;
  tmp++;

  param = buffer;
  value = tmp;
  
  // trim whitespace off of the end of param
  //
  if (param[strlen((char*)param) - 1] == (char_1)PFIL_SPACE_CHR) {
    param[strlen((char*)param) - 1] = (char_1)NULL;
  }

  if (value != (char_1*)NULL) {
    
    // trim the terminator off the end of the value
    //
    if (value[strlen((char*)value) -1] == terminator_char_d) {
      value[strlen((char*)value) -1] = (char_1)NULL;
    }
    
    
    // trim whitespace off of the beginning of value
    //
    if (value != (char_1*)NULL) {
      if (*value == PFIL_SPACE_CHR) {
	value++;
      }
    }
    
    // trim whitespace off the end of the value
    //
    if (value[strlen((char*)value) - 1] == (char_1)PFIL_SPACE_CHR) {
      value[strlen((char*)value) - 1] = (char_1)NULL;
    }
  }
  
  // give debuging output
  //
  if (debug_level_d == ISIP_DEBUG_FULL) {
    if (value != (char_1*)NULL) {
      fprintf(stdout,"param = '%s'; value = '%s'\n",(char*)param,(char*)value);
    }
    else {
      fprintf(stdout,"param = '%s'; value = NULL\n",(char*)param);
    }
  }

  // set the parameter
  //
  if (set_cc(param,value) != ISIP_TRUE) {

    error_handler_cc((char_1*)"parse_cc",
		     (char_1*)"could not set paramater");
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
