Refactor/Conf: avoid using hardcoded categories
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 9 Dec 2019 10:01:33 +0000 (19:01 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Fri, 13 Dec 2019 08:56:46 +0000 (17:56 +0900)
To handle paths/files of each subplugin category,
use loops and enums instead of hardcoded variables.

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

index 49997ac..8a66996 100644 (file)
 
 #include "nnstreamer_conf.h"
 
+/**
+ * Note that users still can place their custom filters anywhere if they
+ * designate them with the full path.
+ */
+
+/* Subplugin Naming Rules */
+#define NNSTREAMER_PREFIX_DECODER      "libnnstreamer_decoder_"
+#define NNSTREAMER_PREFIX_FILTER       "libnnstreamer_filter_"
+#define NNSTREAMER_PREFIX_CUSTOMFILTERS        ""
+#define NNSTREAMER_PREFIX_CONVERTER    ""
+/* Custom filter does not have prefix */
+
+/* Env-var names */
+static const gchar *NNSTREAMER_ENVVAR[NNSCONF_PATH_END] = {
+  [NNSCONF_PATH_FILTERS] = "NNSTREAMER_FILTERS",
+  [NNSCONF_PATH_DECODERS] = "NNSTREAMER_DECODERS",
+  [NNSCONF_PATH_CUSTOM_FILTERS] = "NNSTREAMER_CUSTOMFILTERS",
+  [NNSCONF_PATH_CONVERTERS] = "NNSTREAMER_CONVERTERS"
+};
+
+static const gchar *NNSTREAMER_PATH[NNSCONF_PATH_END] = {
+  [NNSCONF_PATH_FILTERS] = "/usr/lib/nnstreamer/filters/",
+  [NNSCONF_PATH_DECODERS] = "/usr/lib/nnstreamer/decoders/",
+  [NNSCONF_PATH_CUSTOM_FILTERS] = "/usr/lib/nnstreamer/customfilters/",
+  [NNSCONF_PATH_CONVERTERS] = "/usr/lib/nnstreamer/converters/",
+};
+
 static const gchar *subplugin_prefixes[] = {
   [NNSCONF_PATH_FILTERS] = NNSTREAMER_PREFIX_FILTER,
   [NNSCONF_PATH_DECODERS] = NNSTREAMER_PREFIX_DECODER,
@@ -36,36 +63,38 @@ static const gchar *subplugin_prefixes[] = {
   [NNSCONF_PATH_END] = NULL
 };
 
-#define CONF_SOURCES (3)
-typedef struct
+typedef enum
 {
-  gboolean loaded;            /**< TRUE if loaded at least once */
-  gboolean enable_envvar;     /**< TRUE to parse env variables */
-  gboolean enable_symlink;    /**< TRUE to allow symbolic link file */
-
-  gchar *conffile;            /**< Location of conf file. */
+  CONF_SOURCE_ENVVAR = 0,
+  CONF_SOURCE_INI = 1,
+  CONF_SOURCE_HARDCODE = 2,
+  CONF_SOURCE_END,
+} conf_sources;
 
+typedef struct
+{
   /*************************************************
    * Cached Raw Values                             *
    * 0: ENVVAR. 1: CONFFILE, 2: Hardcoded          *
    *************************************************/
-  gchar *pathFILTERS[CONF_SOURCES];        /**< directory paths for FILTERS */
-  gchar *pathDECODERS[CONF_SOURCES];       /**< directory paths for DECODERS */
-  gchar *pathCUSTOM_FILTERS[CONF_SOURCES]; /**< directory paths for CUSTOM FILTERS */
-  gchar *pathCONVERTERS[CONF_SOURCES]; /**< directory paths for CONVERTERS */
+  gchar *path[CONF_SOURCE_END]; /**< directory paths */
 
   /*************************************************
    * Processed Values                              *
    *************************************************/
-  gchar **filesFILTERS;        /**< Null terminated list of full filepaths for FILTERS */
-  gchar **filesDECODERS;;      /**< Null terminated list of full filepaths for DECODERS */
-  gchar **filesCUSTOM_FILTERS; /**< Null terminated list of full filepaths for CUSTOM FILTERS */
-  gchar **filesCONVERTERS;     /**< Null terminated list of full filepaths for CONVERTERS */
-
-  gchar **basenameFILTERS;        /**< Null terminated list of basenames for FILTERS */
-  gchar **basenameDECODERS;;      /**< Null terminated list of basenames for DECODERS */
-  gchar **basenameCUSTOM_FILTERS; /**< Null terminated list of basenames for CUSTOM FILTERS */
-  gchar **basenameCONVERTERS;     /**< Null terminated list of basenames for CONVERTERS */
+  gchar **files; /**< Null terminated list of full filepaths */
+  gchar **basename; /**< Null terminated list of basenames */
+} subplugin_conf;
+
+typedef struct
+{
+  gboolean loaded;            /**< TRUE if loaded at least once */
+  gboolean enable_envvar;     /**< TRUE to parse env variables */
+  gboolean enable_symlink;    /**< TRUE to allow symbolic link file */
+
+  gchar *conffile;            /**< Location of conf file. */
+
+  subplugin_conf conf[NNSCONF_PATH_END];
 } confdata;
 
 static confdata conf = { 0 };
@@ -177,36 +206,18 @@ static gboolean
 _get_subplugin_with_type (nnsconf_type_path type, gchar *** basename,
     gchar *** filepath)
 {
-  gchar **vstr, **vstrFull;
-
-  switch (type) {
-    case NNSCONF_PATH_FILTERS:
-      vstr = conf.basenameFILTERS;
-      vstrFull = conf.filesFILTERS;
-      break;
-    case NNSCONF_PATH_DECODERS:
-      vstr = conf.basenameDECODERS;
-      vstrFull = conf.filesDECODERS;
-      break;
-    case NNSCONF_PATH_CUSTOM_FILTERS:
-      /** follow through */
-    case NNSCONF_PATH_EASY_CUSTOM_FILTERS:
-      /* Both use the same paths */
-      vstr = conf.basenameCUSTOM_FILTERS;
-      vstrFull = conf.filesCUSTOM_FILTERS;
-      break;
-    case NNSCONF_PATH_CONVERTERS:
-      vstr = conf.basenameCONVERTERS;
-      vstrFull = conf.filesCONVERTERS;
-      break;
-    default:
-      /* unknown type */
-      g_critical ("Failed to get sub-plugins, unknown sub-plugin type.");
-      return FALSE;
+  if (type >= NNSCONF_PATH_END) {
+    /* unknown type */
+    g_critical ("Failed to get sub-plugins, unknown sub-plugin type.");
+    return FALSE;
   }
 
-  *basename = vstr;
-  *filepath = vstrFull;
+  /* Easy custom uses the configuration of custom */
+  if (type == NNSCONF_PATH_EASY_CUSTOM_FILTERS)
+    type = NNSCONF_PATH_CUSTOM_FILTERS;
+
+  *basename = conf.conf[type].basename;
+  *filepath = conf.conf[type].files;
   return TRUE;
 }
 
@@ -239,14 +250,14 @@ _g_list_foreach_vstr_helper (gpointer data, gpointer user_data)
  */
 static void
 _fill_in_vstr (gchar *** fullpath_vstr, gchar *** basename_vstr,
-    gchar * searchpath[CONF_SOURCES], nnsconf_type_path type)
+    gchar * searchpath[CONF_SOURCE_END], nnsconf_type_path type)
 {
   GSList *lstF = NULL, *lstB = NULL;
   vstr_helper vstrF, vstrB;
   guint i, counter;
 
   counter = 0;
-  for (i = 0; i < CONF_SOURCES; i++) {
+  for (i = 0; i < CONF_SOURCE_END; i++) {
     if (searchpath[i]) {
       _get_filenames (type, searchpath[i], &lstF, &lstB, &counter);
     }
@@ -281,7 +292,7 @@ nnsconf_loadconf (gboolean force_reload)
 {
   const gchar root_path_prefix[] = NNSTREAMER_SYS_ROOT_PATH_PREFIX;
   g_autoptr (GKeyFile) key_file = NULL;
-  guint i;
+  guint i, t;
 
   if (FALSE == force_reload && TRUE == conf.loaded)
     return TRUE;
@@ -292,21 +303,15 @@ nnsconf_loadconf (gboolean force_reload)
     /* Do Clean Up */
     g_free (conf.conffile);
 
-    for (i = 0; i < CONF_SOURCES; i++) {
-      g_free (conf.pathFILTERS[i]);
-      g_free (conf.pathDECODERS[i]);
-      g_free (conf.pathCUSTOM_FILTERS[i]);
-      g_free (conf.pathCONVERTERS[i]);
-    }
+    for (t = 0; t < NNSCONF_PATH_END; t++) {
 
-    g_strfreev (conf.filesFILTERS);
-    g_strfreev (conf.filesDECODERS);
-    g_strfreev (conf.filesCUSTOM_FILTERS);
-    g_strfreev (conf.filesCONVERTERS);
-    g_strfreev (conf.basenameFILTERS);
-    g_strfreev (conf.basenameDECODERS);
-    g_strfreev (conf.basenameCUSTOM_FILTERS);
-    g_strfreev (conf.basenameCONVERTERS);
+      for (i = 0; i < CONF_SOURCE_END; i++) {
+        g_free (conf.conf[t].path[i]);
+      }
+
+      g_strfreev (conf.conf[t].files);
+      g_strfreev (conf.conf[t].basename);
+    }
 
     /* init with 0 */
     memset (&conf, 0, sizeof (confdata));
@@ -348,40 +353,33 @@ nnsconf_loadconf (gboolean force_reload)
     conf.enable_symlink = _parse_bool_string (value, FALSE);
     g_free (value);
 
-    conf.pathFILTERS[1] =
+    conf.conf[NNSCONF_PATH_FILTERS].path[CONF_SOURCE_INI] =
         g_key_file_get_string (key_file, "filter", "filters", NULL);
-    conf.pathDECODERS[1] =
+    conf.conf[NNSCONF_PATH_DECODERS].path[CONF_SOURCE_INI] =
         g_key_file_get_string (key_file, "decoder", "decoders", NULL);
-    conf.pathCUSTOM_FILTERS[1] =
+    conf.conf[NNSCONF_PATH_CUSTOM_FILTERS].path[CONF_SOURCE_INI] =
         g_key_file_get_string (key_file, "filter", "customfilters", NULL);
-    conf.pathCONVERTERS[1] =
+    conf.conf[NNSCONF_PATH_CONVERTERS].path[CONF_SOURCE_INI] =
         g_key_file_get_string (key_file, "converter", "converters", NULL);
   }
 
-  /* Read from env variables. */
-  if (conf.enable_envvar) {
-    conf.pathFILTERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_FILTERS);
-    conf.pathDECODERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_DECODERS);
-    conf.pathCUSTOM_FILTERS[0] =
-        _strdup_getenv (NNSTREAMER_ENVVAR_CUSTOMFILTERS);
-    conf.pathCONVERTERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_CONVERTERS);
-  }
 
-  /* Strdup the hardcoded */
-  conf.pathFILTERS[2] = g_strdup (NNSTREAMER_FILTERS);
-  conf.pathDECODERS[2] = g_strdup (NNSTREAMER_DECODERS);
-  conf.pathCUSTOM_FILTERS[2] = g_strdup (NNSTREAMER_CUSTOM_FILTERS);
-  conf.pathCONVERTERS[2] = g_strdup (NNSTREAMER_CONVERTERS);
-
-  /* Fill in conf.files* */
-  _fill_in_vstr (&conf.filesFILTERS, &conf.basenameFILTERS, conf.pathFILTERS,
-      NNSCONF_PATH_FILTERS);
-  _fill_in_vstr (&conf.filesDECODERS, &conf.basenameDECODERS, conf.pathDECODERS,
-      NNSCONF_PATH_DECODERS);
-  _fill_in_vstr (&conf.filesCUSTOM_FILTERS, &conf.basenameCUSTOM_FILTERS,
-      conf.pathCUSTOM_FILTERS, NNSCONF_PATH_CUSTOM_FILTERS);
-  _fill_in_vstr (&conf.filesCONVERTERS, &conf.basenameCONVERTERS,
-      conf.pathCONVERTERS, NNSCONF_PATH_CONVERTERS);
+  for (t = 0; t < NNSCONF_PATH_END; t++) {
+    if (t == NNSCONF_PATH_EASY_CUSTOM_FILTERS)
+      continue;                 /* It does not have its own configuration */
+
+    /* Read from env variables. */
+    if (conf.enable_envvar)
+      conf.conf[t].path[CONF_SOURCE_ENVVAR] =
+          _strdup_getenv (NNSTREAMER_ENVVAR[t]);
+
+    /* Strdup the hardcoded */
+    conf.conf[t].path[CONF_SOURCE_HARDCODE] = g_strdup (NNSTREAMER_PATH[t]);
+
+    /* Fill in conf.files* */
+    _fill_in_vstr (&conf.conf[t].files, &conf.conf[t].basename,
+        conf.conf[t].path, t);
+  }
 
   conf.loaded = TRUE;
   return TRUE;
index a7d77c6..75688e4 100644 (file)
@@ -53,40 +53,12 @@ G_BEGIN_DECLS
 #define NNSTREAMER_SO_FILE_EXTENSION   ".so"
 #endif
 
-/* Env-var names */
-#define NNSTREAMER_ENVVAR_CONF_FILE     "NNSTREAMER_CONF"
-#define NNSTREAMER_ENVVAR_FILTERS       "NNSTREAMER_FILTERS"
-#define NNSTREAMER_ENVVAR_DECODERS      "NNSTREAMER_DECODERS"
-#define NNSTREAMER_ENVVAR_CUSTOMFILTERS "NNSTREAMER_CUSTOMFILTERS"
-#define NNSTREAMER_ENVVAR_CONVERTERS   "NNSTREAMER_CONVERTERS"
-
 /* Internal Hardcoded Values */
 #define NNSTREAMER_DEFAULT_CONF_FILE    "/etc/nnstreamer.ini"
 #ifndef NNSTREAMER_CONF_FILE
 #define NNSTREAMER_CONF_FILE NNSTREAMER_DEFAULT_CONF_FILE
 #endif
-#define NNSTREAMER_FILTERS              "/usr/lib/nnstreamer/filters/"
-#define NNSTREAMER_DECODERS             "/usr/lib/nnstreamer/decoders/"
-#define NNSTREAMER_CUSTOM_FILTERS       "/usr/lib/nnstreamer/customfilters/"
-#define NNSTREAMER_CONVERTERS          "/usr/lib/nnstreamer/converters/"
-/**
- *  Note that users still can place their custom filters anywhere if they
- * designate them with the full path.
- */
-
-/* Subplugin Naming Rules */
-#define NNSTREAMER_PREFIX_DECODER      "libnnstreamer_decoder_"
-#define NNSTREAMER_PREFIX_FILTER       "libnnstreamer_filter_"
-#define NNSTREAMER_PREFIX_CUSTOMFILTERS        ""
-#define NNSTREAMER_PREFIX_CONVERTER    ""
-/* Custom filter does not have prefix */
-
-/* struct for sub-plugins info (name and full path) */
-typedef struct
-{
-  gchar **names;
-  gchar **paths;
-} subplugin_info_s;
+#define NNSTREAMER_ENVVAR_CONF_FILE     "NNSTREAMER_CONF"
 
 typedef enum {
   NNSCONF_PATH_FILTERS = 0,
@@ -98,6 +70,13 @@ typedef enum {
   NNSCONF_PATH_END,
 } nnsconf_type_path;
 
+/* struct for sub-plugins info (name and full path) */
+typedef struct
+{
+  gchar **names;
+  gchar **paths;
+} subplugin_info_s;
+
 /**
  * @brief Load the .ini file
  * @param[in] force_reload TRUE if you want to clean up and load conf again.