[Conf] Custom conf keys
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 25 Mar 2019 06:01:51 +0000 (15:01 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 27 Mar 2019 04:10:13 +0000 (13:10 +0900)
Any subplugins (/ext/) may use this to make custom
configuration keys.

To support custom filters, the header should be exported.
However, for now, let's forget about the need from
custom filters.

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
gst/nnstreamer/nnstreamer_conf.c
gst/nnstreamer/nnstreamer_conf.h

index a70ff01..9cde03a 100644 (file)
@@ -395,3 +395,79 @@ nnsconf_get_value_bool (nnsconf_type_value type)
 
   return ret;
 }
+
+/**
+ * @brief Internal cache for the custom key-values
+ */
+static GHashTable *custom_table = NULL;
+
+/** @brief Public function defined in the header */
+gchar *
+nnsconf_get_custom_value_string (const gchar * group, const gchar * key)
+{
+  gchar *hashkey = g_strdup_printf ("[%s]%s", group, key);
+  gchar *value = NULL;
+
+  nnsconf_loadconf (FALSE);     /* Load .ini file path */
+
+  if (NULL == custom_table)
+    custom_table =
+        g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+  value = g_hash_table_lookup (custom_table, hashkey);
+
+  if (NULL == value) {
+    gchar *envkey = g_strdup_printf ("NNSTREAMER_%s_%s", group, key);
+
+    /* 1. Read envvar */
+    value = _strdup_getenv (envkey);
+    g_free (envkey);
+
+    /* 2. Read ini */
+    if (NULL == value && conf.conffile) {
+      g_autoptr (GError) error = NULL;
+      g_autoptr (GKeyFile) key_file = g_key_file_new ();
+
+      if (g_key_file_load_from_file (key_file, conf.conffile, G_KEY_FILE_NONE,
+              &error)) {
+        value = g_key_file_get_string (key_file, group, key, &error);
+      }
+
+      g_key_file_free (key_file);
+
+    }
+
+    if (value)
+      g_hash_table_insert (custom_table, hashkey, value);
+  }
+
+  if (NULL == value)
+    return NULL;
+
+  return g_strdup (value);
+}
+
+/** @brief Public function defined in the header */
+gboolean
+nnsconf_get_custom_value_bool (const gchar * group, const gchar * key,
+    gboolean def)
+{
+  gchar *strval = nnsconf_get_custom_value_string (group, key);
+  gboolean ret = def;
+
+  if (NULL == strval)
+    return ret;
+
+  /** 1/0, true/false, t/f, yes/no, on/off. case incensitive. */
+  if (strval[0] == '1' || strval[0] == 't' || strval[0] == 'T' ||
+      strval[0] == 'y' || strval[0] == 'Y' ||
+      !g_ascii_strncasecmp ("on", strval, 2))
+    ret = TRUE;
+  if (strval[0] == '0' || strval[0] == 'f' || strval[0] == 'F' ||
+      strval[0] == 'n' || strval[0] == 'N' ||
+      !g_ascii_strncasecmp ("of", strval, 2))
+    ret = FALSE;
+
+  g_free (strval);
+  return ret;
+}
index 1f27c36..dfabcda 100644 (file)
@@ -115,4 +115,43 @@ nnsconf_get_fullpath (const gchar *subpluginname, nnsconf_type_path type);
 extern const gboolean
 nnsconf_get_value_bool (nnsconf_type_value type);
 
+/**
+ * @brief Get the custom configuration value from .ini and envvar.
+ * @detail For predefined configurations defined in this header,
+ *         use the given enum for faster configuration processing.
+ *         For custom configurations not defined in this header,
+ *         you may use this API to access your own custom configurations.
+ *         Configuration values may be loaded only once during runtime,
+ *         thus, if the values are changed in run-time, the changes are
+ *         not guaranteed to be reflected.
+ *         The ENVVAR is supposed to be NNSTREAMER_${group}_${key}, which
+ *         has higher priority than the .ini configuration.
+ *         Be careful not to use special characters in group name ([, ], _).
+ * @param[in] group The group name, [group], in .ini file.
+ * @param[in] key The key name, key = value, in .ini file.
+ * @return The newly allocated string. A caller must free it. NULL if it's not available.
+ */
+extern gchar *
+nnsconf_get_custom_value_string (const gchar *group, const gchar *key);
+
+/**
+ * @brief Get the custom configuration value from .ini and envvar.
+ * @detail For predefined configurations defined in this header,
+ *         use the given enum for faster configuration processing.
+ *         For custom configurations not defined in this header,
+ *         you may use this API to access your own custom configurations.
+ *         Configuration values may be loaded only once during runtime,
+ *         thus, if the values are changed in run-time, the changes are
+ *         not guaranteed to be reflected.
+ *         The ENVVAR is supposed to be NNSTREAMER_${group}_${key}, which
+ *         has higher priority than the .ini configuration.
+ *         Be careful not to use special characters in group name ([, ], _).
+ * @param[in] group The group name, [group], in .ini file.
+ * @param[in] key The key name, key = value, in .ini file.
+ * @param[in] def The default return value in case there is no value available.
+ * @return The value interpreted as TRUE/FALSE.
+ */
+extern gboolean
+nnsconf_get_custom_value_bool (const gchar *group, const gchar *key, gboolean def);
+
 #endif /* __GST_NNSTREAMER_CONF_H__ */