validate: Move GstStructure file parsing into utils
authorThibault Saunier <tsaunier@gnome.org>
Fri, 3 Oct 2014 16:42:04 +0000 (18:42 +0200)
committerMathieu Duponchelle <mathieu.duponchelle@opencreed.com>
Tue, 21 Oct 2014 18:21:52 +0000 (20:21 +0200)
So it can be reused, at least in GstValidate.

validate/gst/validate/gst-validate-scenario.c
validate/gst/validate/gst-validate-utils.c
validate/gst/validate/gst-validate-utils.h

index 731ee05..803ad0e 100644 (file)
@@ -73,7 +73,6 @@ enum
 static GList *action_types = NULL;
 static void gst_validate_scenario_dispose (GObject * object);
 static void gst_validate_scenario_finalize (GObject * object);
-static GRegex *clean_action_str;
 
 struct _GstValidateScenarioPrivate
 {
@@ -1259,133 +1258,6 @@ _pipeline_freed_cb (GstValidateScenario * scenario,
   GST_DEBUG_OBJECT (scenario, "pipeline was freed");
 }
 
-static gchar **
-_scenario_file_get_lines (GFile * file)
-{
-  gsize size;
-
-  GError *err = NULL;
-  gchar *content = NULL, *escaped_content = NULL, **lines = NULL;
-
-  /* TODO Handle GCancellable */
-  if (!g_file_load_contents (file, NULL, &content, &size, NULL, &err))
-    goto failed;
-
-  if (g_strcmp0 (content, "") == 0)
-    goto failed;
-
-  escaped_content = g_regex_replace (clean_action_str, content, -1, 0, "", 0,
-      NULL);
-  g_free (content);
-
-  lines = g_strsplit (escaped_content, "\n", 0);
-  g_free (escaped_content);
-
-done:
-
-  return lines;
-
-failed:
-  if (err) {
-    GST_WARNING ("Failed to load contents: %d %s", err->code, err->message);
-    g_error_free (err);
-  }
-
-  if (content)
-    g_free (content);
-  content = NULL;
-
-  if (escaped_content)
-    g_free (escaped_content);
-  escaped_content = NULL;
-
-  if (lines)
-    g_strfreev (lines);
-  lines = NULL;
-
-  goto done;
-}
-
-static gchar **
-_scenario_get_lines (const gchar * scenario_file)
-{
-  GFile *file = NULL;
-  gchar **lines = NULL;
-
-  GST_DEBUG ("Trying to load %s", scenario_file);
-  if ((file = g_file_new_for_path (scenario_file)) == NULL) {
-    GST_WARNING ("%s wrong uri", scenario_file);
-    return NULL;
-  }
-
-  lines = _scenario_file_get_lines (file);
-
-  g_object_unref (file);
-
-  return lines;
-}
-
-static GList *
-_scenario_lines_get_strutures (gchar ** lines)
-{
-  gint i;
-  GList *structures = NULL;
-
-  for (i = 0; lines[i]; i++) {
-    GstStructure *structure;
-
-    if (g_strcmp0 (lines[i], "") == 0)
-      continue;
-
-    structure = gst_structure_from_string (lines[i], NULL);
-    if (structure == NULL) {
-      GST_ERROR ("Could not parse action %s", lines[i]);
-      goto failed;
-    }
-
-    structures = g_list_append (structures, structure);
-  }
-
-done:
-  if (lines)
-    g_strfreev (lines);
-
-  return structures;
-
-failed:
-  if (structures)
-    g_list_free_full (structures, (GDestroyNotify) gst_structure_free);
-  structures = NULL;
-
-  goto done;
-}
-
-static GList *
-_scenario_get_structures (const gchar * scenario_file)
-{
-  gchar **lines;
-
-  lines = _scenario_get_lines (scenario_file);
-
-  if (lines == NULL)
-    return NULL;
-
-  return _scenario_lines_get_strutures (lines);
-}
-
-static GList *
-_scenario_file_get_structures (GFile * scenario_file)
-{
-  gchar **lines;
-
-  lines = _scenario_file_get_lines (scenario_file);
-
-  if (lines == NULL)
-    return NULL;
-
-  return _scenario_lines_get_strutures (lines);
-}
-
 static gboolean
 _load_scenario_file (GstValidateScenario * scenario,
     const gchar * scenario_file, gboolean * is_config)
@@ -1396,7 +1268,7 @@ _load_scenario_file (GstValidateScenario * scenario,
 
   *is_config = FALSE;
 
-  structures = _scenario_get_structures (scenario_file);
+  structures = structs_parse_from_filename (scenario_file);
   if (structures == NULL)
     goto failed;
 
@@ -1765,7 +1637,7 @@ _parse_scenario (GFile * f, GKeyFile * kf)
     GstStructure *desc = NULL;
 
     gchar **name = g_strsplit (fname, GST_VALIDATE_SCENARIO_SUFFIX, 0);
-    GList *tmp, *structures = _scenario_file_get_structures (f);
+    GList *tmp, *structures = structs_parse_from_gfile (f);
 
     for (tmp = structures; tmp; tmp = tmp->next) {
       if (gst_structure_has_name (tmp->data, "description")) {
@@ -2043,8 +1915,6 @@ init_scenarios (void)
   _gst_validate_action_type = gst_validate_action_get_type ();
   _gst_validate_action_type_type = gst_validate_action_type_get_type ();
 
-  clean_action_str = g_regex_new ("\\\\\n|#.*\n", G_REGEX_CASELESS, 0, NULL);
-
   /*  *INDENT-OFF* */
   REGISTER_ACTION_TYPE ("description", NULL,
       ((GstValidateActionParameter [])  {
index b2527a7..60d300a 100644 (file)
@@ -33,6 +33,8 @@
 #define PARSER_MAX_TOKEN_SIZE 256
 #define PARSER_MAX_ARGUMENT_COUNT 10
 
+static GRegex *_clean_structs_lines = NULL;
+
 typedef struct
 {
   const gchar *str;
@@ -507,3 +509,137 @@ gst_validate_utils_enum_from_str (GType type, const gchar * str_enum,
 
   return ret;
 }
+
+/* Parse file that contains a list of GStructures */
+static gchar **
+_file_get_lines (GFile * file)
+{
+  gsize size;
+
+  GError *err = NULL;
+  gchar *content = NULL, *escaped_content = NULL, **lines = NULL;
+
+  /* TODO Handle GCancellable */
+  if (!g_file_load_contents (file, NULL, &content, &size, NULL, &err))
+    goto failed;
+
+  if (g_strcmp0 (content, "") == 0)
+    goto failed;
+
+  if (_clean_structs_lines == NULL)
+    _clean_structs_lines =
+        g_regex_new ("\\\\\n|#.*\n", G_REGEX_CASELESS, 0, NULL);
+
+  escaped_content =
+      g_regex_replace (_clean_structs_lines, content, -1, 0, "", 0, NULL);
+  g_free (content);
+
+  lines = g_strsplit (escaped_content, "\n", 0);
+  g_free (escaped_content);
+
+done:
+
+  return lines;
+
+failed:
+  if (err) {
+    GST_WARNING ("Failed to load contents: %d %s", err->code, err->message);
+    g_error_free (err);
+  }
+
+  if (content)
+    g_free (content);
+  content = NULL;
+
+  if (escaped_content)
+    g_free (escaped_content);
+  escaped_content = NULL;
+
+  if (lines)
+    g_strfreev (lines);
+  lines = NULL;
+
+  goto done;
+}
+
+static gchar **
+_get_lines (const gchar * scenario_file)
+{
+  GFile *file = NULL;
+  gchar **lines = NULL;
+
+  GST_DEBUG ("Trying to load %s", scenario_file);
+  if ((file = g_file_new_for_path (scenario_file)) == NULL) {
+    GST_WARNING ("%s wrong uri", scenario_file);
+    return NULL;
+  }
+
+  lines = _file_get_lines (file);
+
+  g_object_unref (file);
+
+  return lines;
+}
+
+static GList *
+_lines_get_strutures (gchar ** lines)
+{
+  gint i;
+  GList *structures = NULL;
+
+  for (i = 0; lines[i]; i++) {
+    GstStructure *structure;
+
+    if (g_strcmp0 (lines[i], "") == 0)
+      continue;
+
+    structure = gst_structure_from_string (lines[i], NULL);
+    if (structure == NULL) {
+      GST_ERROR ("Could not parse action %s", lines[i]);
+      goto failed;
+    }
+
+    structures = g_list_append (structures, structure);
+  }
+
+done:
+  if (lines)
+    g_strfreev (lines);
+
+  return structures;
+
+failed:
+  if (structures)
+    g_list_free_full (structures, (GDestroyNotify) gst_structure_free);
+  structures = NULL;
+
+  goto done;
+}
+
+GList *
+structs_parse_from_filename (const gchar * scenario_file)
+{
+  gchar **lines;
+
+  lines = _get_lines (scenario_file);
+
+  if (lines == NULL) {
+    GST_ERROR ("Got no line for file: %s", scenario_file);
+    return NULL;
+  }
+
+  return _lines_get_strutures (lines);
+}
+
+GList *
+structs_parse_from_gfile (GFile * scenario_file)
+{
+  gchar **lines;
+
+  lines = _file_get_lines (scenario_file);
+
+  if (lines == NULL)
+    return NULL;
+
+  return _lines_get_strutures (lines);
+}
index 62d6ba4..7de4c51 100644 (file)
@@ -26,6 +26,7 @@
 #include<setjmp.h>
 #include<stdlib.h>
 #include<glib.h>
+#include<gio/gio.h>
 #include <gst/gst.h>
 
 typedef int (*ParseVariableFunc) (const gchar *name,
@@ -39,4 +40,8 @@ guint gst_validate_utils_flags_from_str     (GType type, const gchar * str_flags
 gboolean gst_validate_utils_enum_from_str   (GType type,
                                              const gchar * str_enum,
                                              guint * enum_value);
+
+GList * structs_parse_from_filename         (const gchar * scenario_file);
+GList * structs_parse_from_gfile            (GFile * scenario_file);
+
 #endif