/*===========================================================================
FUNCTION loc_fill_conf_item

DESCRIPTION
   Takes a line of configuration item and sets defined values based on
   the passed in configuration table. This table maps strings to values to
   set along with the type of each of these values.

PARAMETERS:
   input_buf : buffer contanis config item
   config_table: table definition of strings to places to store information
   table_length: length of the configuration table

DEPENDENCIES
   N/A

RETURN VALUE
   0: Number of records in the config_table filled with input_buf

SIDE EFFECTS
   N/A
===========================================================================*/
int loc_fill_conf_item(char* input_buf,
                       const loc_param_s_type* config_table, uint32_t table_length)
{
    int ret = 0;

    if (input_buf && config_table) {
        char *lasts;
        loc_param_v_type config_value;
        memset(&config_value, 0, sizeof(config_value));

        /* Separate variable and value */
        config_value.param_name = strtok_r(input_buf, "=", &lasts);
        /* skip lines that do not contain "=" */
        if (config_value.param_name) {
            config_value.param_str_value = strtok_r(NULL, "=", &lasts);

            /* skip lines that do not contain two operands */
            if (config_value.param_str_value) {
                /* Trim leading and trailing spaces */
                loc_util_trim_space(config_value.param_name);
                loc_util_trim_space(config_value.param_str_value);

                /* Parse numerical value */
                if ((strlen(config_value.param_str_value) >=3) &&
                    (config_value.param_str_value[0] == '0') &&
                    (tolower(config_value.param_str_value[1]) == 'x'))
                {
                    /* hex */
                    config_value.param_int_value = (int) strtol(&config_value.param_str_value[2],
                                                                (char**) NULL, 16);
                }
                else {
                    config_value.param_double_value = (double) atof(config_value.param_str_value); /* float */
                    config_value.param_int_value = atoi(config_value.param_str_value); /* dec */
                }

                for(uint32_t i = 0; NULL != config_table && i < table_length; i++)
                {
                    if(!loc_set_config_entry(&config_table[i], &config_value)) {
                        ret += 1;
                    }
                }
            }
        }
    }

    return ret;
}
/*===========================================================================
FUNCTION loc_read_conf

DESCRIPTION
   Reads the specified configuration file and sets defined values based on
   the passed in configuration table. This table maps strings to values to
   set along with the type of each of these values.

PARAMETERS:
   conf_file_name: configuration file to read
   config_table: table definition of strings to places to store information
   table_length: length of the configuration table

DEPENDENCIES
   N/A

RETURN VALUE
   None

SIDE EFFECTS
   N/A
===========================================================================*/
void loc_read_conf(const char* conf_file_name, loc_param_s_type* config_table, uint32_t table_length)
{
   FILE *gps_conf_fp = NULL;
   char input_buf[LOC_MAX_PARAM_LINE];  /* declare a char array */
   char *lasts;
   loc_param_v_type config_value;
   uint32_t i;

   if((gps_conf_fp = fopen(conf_file_name, "r")) != NULL)
   {
      LOC_LOGD("%s: using %s", __FUNCTION__, conf_file_name);
   }
   else
   {
      LOC_LOGW("%s: no %s file found", __FUNCTION__, conf_file_name);
      loc_logger_init(DEBUG_LEVEL, TIMESTAMP);
      return; /* no parameter file */
   }

   /* Clear all validity bits */
   for(i = 0; NULL != config_table && i < table_length; i++)
   {
      if(NULL != config_table[i].param_set)
      {
         *(config_table[i].param_set) = 0;
      }
   }

   while(fgets(input_buf, LOC_MAX_PARAM_LINE, gps_conf_fp) != NULL)
   {
      memset(&config_value, 0, sizeof(config_value));

      /* Separate variable and value */
      config_value.param_name = strtok_r(input_buf, "=", &lasts);
      if (config_value.param_name == NULL) continue;       /* skip lines that do not contain "=" */
      config_value.param_str_value = strtok_r(NULL, "=", &lasts);
      if (config_value.param_str_value == NULL) continue;  /* skip lines that do not contain two operands */

      /* Trim leading and trailing spaces */
      trim_space(config_value.param_name);
      trim_space(config_value.param_str_value);

      /* Parse numerical value */
      if (config_value.param_str_value[0] == '0' && tolower(config_value.param_str_value[1]) == 'x')
      {
         /* hex */
         config_value.param_int_value = (int) strtol(&config_value.param_str_value[2], (char**) NULL, 16);
      }
      else {
         config_value.param_double_value = (double) atof(config_value.param_str_value); /* float */
         config_value.param_int_value = atoi(config_value.param_str_value); /* dec */
      }

      for(i = 0; NULL != config_table && i < table_length; i++)
      {
         loc_set_config_entry(&config_table[i], &config_value);
      }

      for(i = 0; i < loc_param_num; i++)
      {
         loc_set_config_entry(&loc_parameter_table[i], &config_value);
      }
   }

   fclose(gps_conf_fp);

   /* Initialize logging mechanism with parsed data */
   loc_logger_init(DEBUG_LEVEL, TIMESTAMP);
}
/*===========================================================================
FUNCTION loc_read_conf_r (repetitive)

DESCRIPTION
   Reads the specified configuration file and sets defined values based on
   the passed in configuration table. This table maps strings to values to
   set along with the type of each of these values.
   The difference between this and loc_read_conf is that this function returns
   the file pointer position at the end of filling a config table. Also, it
   reads a fixed number of parameters at a time which is equal to the length
   of the configuration table. This functionality enables the caller to
   repeatedly call the function to read data from the same file.

PARAMETERS:
   conf_fp : file pointer
   config_table: table definition of strings to places to store information
   table_length: length of the configuration table

DEPENDENCIES
   N/A

RETURN VALUE
   0: Table filled successfully
   1: No more parameters to read
  -1: Error filling table

SIDE EFFECTS
   N/A
===========================================================================*/
int loc_read_conf_r(FILE *conf_fp, loc_param_s_type* config_table, uint32_t table_length)
{
    char input_buf[LOC_MAX_PARAM_LINE];  /* declare a char array */
    char *lasts;
    loc_param_v_type config_value;
    uint32_t i;
    int ret=0;

    unsigned int num_params=table_length;
    if(conf_fp == NULL) {
        LOC_LOGE("%s:%d]: ERROR: File pointer is NULL\n", __func__, __LINE__);
        ret = -1;
        goto err;
    }

    /* Clear all validity bits */
    for(i = 0; NULL != config_table && i < table_length; i++)
    {
        if(NULL != config_table[i].param_set)
        {
            *(config_table[i].param_set) = 0;
        }
    }
    LOC_LOGD("%s:%d]: num_params: %d\n", __func__, __LINE__, num_params);
    while(num_params)
    {
        if(!fgets(input_buf, LOC_MAX_PARAM_LINE, conf_fp)) {
            LOC_LOGD("%s:%d]: fgets returned NULL\n", __func__, __LINE__);
            break;
        }

        memset(&config_value, 0, sizeof(config_value));

        /* Separate variable and value */
        config_value.param_name = strtok_r(input_buf, "=", &lasts);
        /* skip lines that do not contain "=" */
        if (config_value.param_name == NULL) continue;
        config_value.param_str_value = strtok_r(NULL, "=", &lasts);
        /* skip lines that do not contain two operands */
        if (config_value.param_str_value == NULL) continue;

        /* Trim leading and trailing spaces */
        loc_util_trim_space(config_value.param_name);
        loc_util_trim_space(config_value.param_str_value);

        /* Parse numerical value */
        if ((strlen(config_value.param_str_value) >=3) &&
            (config_value.param_str_value[0] == '0') &&
            (tolower(config_value.param_str_value[1]) == 'x'))
        {
            /* hex */
            config_value.param_int_value = (int) strtol(&config_value.param_str_value[2],
                                                        (char**) NULL, 16);
        }
        else {
            config_value.param_double_value = (double) atof(config_value.param_str_value); /* float */
            config_value.param_int_value = atoi(config_value.param_str_value); /* dec */
        }

        for(i = 0; NULL != config_table && i < table_length; i++)
        {
            if(!loc_set_config_entry(&config_table[i], &config_value)) {
                num_params--;
            }
        }
    }

err:
    return ret;
}