get config number/boolean helper functions added 68/121968/13
authorRafal Pietruch <r.pietruch@samsung.com>
Wed, 29 Mar 2017 14:06:32 +0000 (16:06 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 21 Apr 2017 10:48:25 +0000 (03:48 -0700)
 * log_config_get_int return given default value on error
 * log_config_get_boolean just calls log_config_get_int
   added to distinguish config parameter type

Change-Id: Iebbc1097ac399d48f016f6919b9b5b4a4ef113b4

include/logconfig.h
src/shared/logconfig.c

index 10a550a..14e2993 100644 (file)
@@ -48,6 +48,8 @@ typedef void (*configIter) (const char *key, const char* value, void *userdata);
 
 int log_config_set(struct log_config* config, const char* key, const char* value);
 const char* log_config_get(struct log_config* config, const char* key);
+int log_config_get_int(struct log_config* config, const char* key, int default_val);
+int log_config_get_boolean(struct log_config* config, const char* key, int default_val);
 int log_config_read(struct log_config* config);
 int log_config_read_file(struct log_config* config, char const* filename);
 int log_config_write(struct log_config* config, char const* filename);
index 0f1ad03..4a1d149 100644 (file)
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <logcommon.h>
+#include <limits.h>
 
 /**
  * @addtogroup SHARED_FUNCTIONS
@@ -50,6 +51,64 @@ const char * log_config_get(struct log_config* c, const char* key)
 }
 
 /**
+ * @brief Convert decimal number string to int
+ * @param[in] str string of decimal number
+ * @param[out] num a place to store the result
+ * @return 0 on success otherwise -errno
+ * @see strtol(3)
+ */
+static int str2num(const char *str, int *num)
+{
+       if (str == NULL)
+               return -EINVAL;
+
+       errno = 0; /* To distinguish success/failure after call */
+       char *endptr;
+       int val = strtol(str, &endptr, 10);
+
+       /* Check for various possible errors */
+       if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+               || (errno != 0 && val == 0))
+               return -errno;
+
+       else if (endptr == str)
+               return -EINVAL; /* No digits were found */
+
+       /* If we got here, strtol() successfully parsed a number */
+       *num = val;
+       return 0;
+}
+
+/**
+ * @brief Get a config value converted to int
+ * @details Returns a value with the given key from the given config
+ * @param[in] c The config to work with
+ * @param[in] key The key of the value entry to get
+ * @param[in] default_val Default int value to return when proper value not given in config
+ * @return The value of the entry if such exists, else default value
+ * @see log_config_get
+ */
+int log_config_get_int(struct log_config* c, const char* key, int default_val)
+{
+       int val;
+       return (str2num(log_config_get(c, key), &val) == 0 ? val : default_val);
+}
+
+/**
+ * @brief Get a config value converted to boolean
+ * @details Returns TRUE if config string is non-zero digital number
+ * @param[in] c The config to work with
+ * @param[in] key The key of the value entry to get
+ * @param[in] default_val Default boolean value to return when proper value not given in config
+ * @return The value of the entry if such exists, else default value
+ * @see log_config_get_int
+ */
+int log_config_get_boolean(struct log_config* c, const char* key, int default_val)
+{
+       return log_config_get_int(c, key, default_val);
+}
+
+/**
  * @brief Set a config value
  * @details Sets the entry with the given key in the given config to the given value
  * @param[in] c The config to work with