diff options
author | Deven Patel <cdevenp@codeaurora.org> | 2016-03-07 10:44:34 -0800 |
---|---|---|
committer | Deven Patel <cdevenp@codeaurora.org> | 2016-03-07 10:55:20 -0800 |
commit | a29688ff347be4972133eb11ccecaf03b0d3445e (patch) | |
tree | 45dc8adb0d08a30bc542075042884cef831bcfdf /utils/loc_cfg.cpp | |
parent | fa7a874eb0586c7844fe3a4cb8d0063ce53464f0 (diff) | |
download | gps-a29688ff347be4972133eb11ccecaf03b0d3445e.tar.gz |
Revert "Merging m_master changes to oe_master".
This reverts commit 1aeb6bad84c0afd0e032c3d4d7e33959501e18f1
Change-Id: Iec0a96e7cdfe55ef5836c92a2ae1cce407f6cd5e
Diffstat (limited to 'utils/loc_cfg.cpp')
-rw-r--r-- | utils/loc_cfg.cpp | 456 |
1 files changed, 171 insertions, 285 deletions
diff --git a/utils/loc_cfg.cpp b/utils/loc_cfg.cpp index 967d2f3..8c76d1f 100644 --- a/utils/loc_cfg.cpp +++ b/utils/loc_cfg.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. +/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -38,9 +38,8 @@ #include <unistd.h> #include <time.h> #include <loc_cfg.h> -#include <log_util.h> -#include <loc_misc_utils.h> -#ifdef USE_GLIB +#include <platform_lib_includes.h> +#if defined(USE_GLIB) && !defined(OFF_TARGET) #include <glib.h> #endif #include "platform_lib_includes.h" @@ -52,36 +51,22 @@ *============================================================================*/ /* Parameter data */ -static uint32_t DEBUG_LEVEL = 0xff; -static uint32_t TIMESTAMP = 0; +static uint8_t DEBUG_LEVEL = 0xff; +static uint8_t TIMESTAMP = 0; /* Parameter spec table */ -static const loc_param_s_type loc_param_table[] = +static loc_param_s_type loc_parameter_table[] = { - {"DEBUG_LEVEL", &DEBUG_LEVEL, NULL, 'n'}, - {"TIMESTAMP", &TIMESTAMP, NULL, 'n'}, + {"DEBUG_LEVEL", &DEBUG_LEVEL, NULL, 'n'}, + {"TIMESTAMP", &TIMESTAMP, NULL, 'n'}, }; -static const int loc_param_num = sizeof(loc_param_table) / sizeof(loc_param_s_type); - -typedef struct loc_param_v_type -{ - char* param_name; - char* param_str_value; - int param_int_value; - double param_double_value; -}loc_param_v_type; +int loc_param_num = sizeof(loc_parameter_table) / sizeof(loc_param_s_type); /*=========================================================================== -FUNCTION loc_set_config_entry +FUNCTION trim_space DESCRIPTION - Potentially sets a given configuration table entry based on the passed in - configuration value. This is done by using a string comparison of the - parameter names and those found in the configuration file. - -PARAMETERS: - config_entry: configuration entry in the table to possibly set - config_value: value to store in the entry if the parameter names match + Removes leading and trailing spaces of the string DEPENDENCIES N/A @@ -92,267 +77,120 @@ RETURN VALUE SIDE EFFECTS N/A ===========================================================================*/ -int loc_set_config_entry(const loc_param_s_type* config_entry, loc_param_v_type* config_value) +void trim_space(char *org_string) { - int ret=-1; - if(NULL == config_entry || NULL == config_value) - { - LOC_LOGE("%s: INVALID config entry or parameter", __FUNCTION__); - return ret; - } - - if (strcmp(config_entry->param_name, config_value->param_name) == 0 && - config_entry->param_ptr) - { - switch (config_entry->param_type) - { - case 's': - if (strcmp(config_value->param_str_value, "NULL") == 0) - { - *((char*)config_entry->param_ptr) = '\0'; - } - else { - strlcpy((char*) config_entry->param_ptr, - config_value->param_str_value, - LOC_MAX_PARAM_STRING + 1); - } - /* Log INI values */ - LOC_LOGD("%s: PARAM %s = %s", __FUNCTION__, - config_entry->param_name, (char*)config_entry->param_ptr); - - if(NULL != config_entry->param_set) - { - *(config_entry->param_set) = 1; - } - ret = 0; - break; - case 'n': - *((int *)config_entry->param_ptr) = config_value->param_int_value; - /* Log INI values */ - LOC_LOGD("%s: PARAM %s = %d", __FUNCTION__, - config_entry->param_name, config_value->param_int_value); - - if(NULL != config_entry->param_set) - { - *(config_entry->param_set) = 1; - } - ret = 0; - break; - case 'f': - *((double *)config_entry->param_ptr) = config_value->param_double_value; - /* Log INI values */ - LOC_LOGD("%s: PARAM %s = %f", __FUNCTION__, - config_entry->param_name, config_value->param_double_value); - - if(NULL != config_entry->param_set) - { - *(config_entry->param_set) = 1; - } - ret = 0; - break; - default: - LOC_LOGE("%s: PARAM %s parameter type must be n, f, or s", - __FUNCTION__, config_entry->param_name); - } - } - return ret; + char *scan_ptr, *write_ptr; + char *first_nonspace = NULL, *last_nonspace = NULL; + + scan_ptr = write_ptr = org_string; + + while (*scan_ptr) + { + if ( !isspace(*scan_ptr) && first_nonspace == NULL) + { + first_nonspace = scan_ptr; + } + + if (first_nonspace != NULL) + { + *(write_ptr++) = *scan_ptr; + if ( !isspace(*scan_ptr)) + { + last_nonspace = write_ptr; + } + } + + scan_ptr++; + } + + if (last_nonspace) { *last_nonspace = '\0'; } } -/*=========================================================================== -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) +typedef struct loc_param_v_type { - 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_r (repetitive) + char* param_name; -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, const loc_param_s_type* config_table, uint32_t table_length) -{ - 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(uint32_t i = 0; NULL != config_table && i < table_length; i++) - { - if(NULL != config_table[i].param_set) - { - *(config_table[i].param_set) = 0; - } - } - - char input_buf[LOC_MAX_PARAM_LINE]; /* declare a char array */ - - 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; - } - - num_params -= loc_fill_conf_item(input_buf, config_table, table_length); - } - -err: - return ret; -} + char* param_str_value; + int param_int_value; + double param_double_value; +}loc_param_v_type; /*=========================================================================== -FUNCTION loc_udpate_conf +FUNCTION loc_set_config_entry DESCRIPTION - Parses the passed in buffer for configuration items, and update the table - that is also passed in. - -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. + Potentially sets a given configuration table entry based on the passed in + configuration value. This is done by using a string comparison of the + parameter names and those found in the configuration file. PARAMETERS: - conf_data: configuration items in bufferas a string - length: strlen(conf_data) - config_table: table definition of strings to places to store information - table_length: length of the configuration table + config_entry: configuration entry in the table to possibly set + config_value: value to store in the entry if the parameter names match DEPENDENCIES N/A RETURN VALUE - number of the records in the table that is updated at time of return. + None SIDE EFFECTS N/A ===========================================================================*/ -int loc_update_conf(const char* conf_data, int32_t length, - const loc_param_s_type* config_table, uint32_t table_length) +void loc_set_config_entry(loc_param_s_type* config_entry, loc_param_v_type* config_value) { - int ret = -1; - - if (conf_data && length && config_table && table_length) { - // make a copy, so we do not tokenize the original data - char* conf_copy = (char*)malloc(length+1); - - if (conf_copy != NULL) - { - memcpy(conf_copy, conf_data, length); - // we hard NULL the end of string to be safe - conf_copy[length] = 0; - - // start with one record off - uint32_t num_params = table_length - 1; - char* saveptr = NULL; - char* input_buf = strtok_r(conf_copy, "\n", &saveptr); - ret = 0; - - LOC_LOGD("%s:%d]: num_params: %d\n", __func__, __LINE__, num_params); - while(num_params && input_buf) { - ret++; - num_params -= loc_fill_conf_item(input_buf, config_table, table_length); - input_buf = strtok_r(NULL, "\n", &saveptr); - } - free(conf_copy); - } - } - - return ret; + if(NULL == config_entry || NULL == config_value) + { + LOC_LOGE("%s: INVALID config entry or parameter", __FUNCTION__); + return; + } + + if (strcmp(config_entry->param_name, config_value->param_name) == 0 && + config_entry->param_ptr) + { + switch (config_entry->param_type) + { + case 's': + if (strcmp(config_value->param_str_value, "NULL") == 0) + { + *((char*)config_entry->param_ptr) = '\0'; + } + else { + strlcpy((char*) config_entry->param_ptr, + config_value->param_str_value, + LOC_MAX_PARAM_STRING + 1); + } + /* Log INI values */ + LOC_LOGD("%s: PARAM %s = %s", __FUNCTION__, config_entry->param_name, (char*)config_entry->param_ptr); + + if(NULL != config_entry->param_set) + { + *(config_entry->param_set) = 1; + } + break; + case 'n': + *((int *)config_entry->param_ptr) = config_value->param_int_value; + /* Log INI values */ + LOC_LOGD("%s: PARAM %s = %d", __FUNCTION__, config_entry->param_name, config_value->param_int_value); + + if(NULL != config_entry->param_set) + { + *(config_entry->param_set) = 1; + } + break; + case 'f': + *((double *)config_entry->param_ptr) = config_value->param_double_value; + /* Log INI values */ + LOC_LOGD("%s: PARAM %s = %f", __FUNCTION__, config_entry->param_name, config_value->param_double_value); + + if(NULL != config_entry->param_set) + { + *(config_entry->param_set) = 1; + } + break; + default: + LOC_LOGE("%s: PARAM %s parameter type must be n, f, or s", __FUNCTION__, config_entry->param_name); + } + } } /*=========================================================================== @@ -377,24 +215,72 @@ RETURN VALUE SIDE EFFECTS N/A ===========================================================================*/ -void loc_read_conf(const char* conf_file_name, const loc_param_s_type* config_table, - uint32_t table_length) +void loc_read_conf(const char* conf_file_name, loc_param_s_type* config_table, uint32_t table_length) { - FILE *conf_fp = NULL; - char *lasts; - loc_param_v_type config_value; - uint32_t i; - - if((conf_fp = fopen(conf_file_name, "r")) != NULL) - { - LOC_LOGD("%s: using %s", __FUNCTION__, conf_file_name); - if(table_length && config_table) { - loc_read_conf_r(conf_fp, config_table, table_length); - rewind(conf_fp); - } - loc_read_conf_r(conf_fp, loc_param_table, loc_param_num); - fclose(conf_fp); - } - /* Initialize logging mechanism with parsed data */ - loc_logger_init(DEBUG_LEVEL, TIMESTAMP); + 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); } |