1 /* GStreamer Editing Services
3 * Copyright (C) <2015> Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "ges-structure-parser.h"
28 G_DEFINE_TYPE (GESStructureParser, ges_structure_parser, G_TYPE_OBJECT);
31 ges_structure_parser_init (GESStructureParser * self)
36 _finalize (GObject * self)
38 GESStructureParser *parser = GES_STRUCTURE_PARSER (self);
40 g_list_free_full (parser->structures, (GDestroyNotify) gst_structure_free);
41 g_list_free_full (parser->wrong_strings, (GDestroyNotify) g_free);
43 G_OBJECT_CLASS (ges_structure_parser_parent_class)->finalize (self);
47 ges_structure_parser_class_init (GESStructureParserClass * klass)
49 G_OBJECT_CLASS (klass)->finalize = _finalize;
53 ges_structure_parser_parse_string (GESStructureParser * self,
54 const gchar * text, gboolean is_symbol)
56 gchar *new_string = NULL;
58 if (self->current_string) {
59 new_string = g_strconcat (self->current_string, text, NULL);
60 } else if (is_symbol) {
61 new_string = g_strdup (text);
64 g_free (self->current_string);
65 self->current_string = new_string;
69 ges_structure_parser_parse_value (GESStructureParser * self, const gchar * text)
71 /* text starts with '=' */
72 gchar *val_string = g_strconcat ("=(string)", text + 1, NULL);
73 ges_structure_parser_parse_string (self, val_string, FALSE);
78 ges_structure_parser_parse_default (GESStructureParser * self,
81 gchar *new_string = NULL;
83 if (self->add_comma && self->current_string) {
84 new_string = g_strconcat (self->current_string, ",", text, NULL);
85 g_free (self->current_string);
86 self->current_string = new_string;
87 self->add_comma = FALSE;
89 ges_structure_parser_parse_string (self, text, FALSE);
94 ges_structure_parser_parse_whitespace (GESStructureParser * self)
96 self->add_comma = TRUE;
100 _finish_structure (GESStructureParser * self)
102 GstStructure *structure;
104 if (!self->current_string)
107 structure = gst_structure_new_from_string (self->current_string);
109 if (structure == NULL) {
110 GST_ERROR ("Could not parse %s", self->current_string);
112 self->wrong_strings = g_list_append (self->wrong_strings,
113 g_strdup (self->current_string));
118 self->structures = g_list_append (self->structures, structure);
119 g_free (self->current_string);
120 self->current_string = NULL;
125 ges_structure_parser_end_of_file (GESStructureParser * self)
127 _finish_structure (self);
131 ges_structure_parser_parse_symbol (GESStructureParser * self,
132 const gchar * symbol)
134 _finish_structure (self);
136 while (*symbol == ' ' || *symbol == '+')
139 self->add_comma = FALSE;
140 if (!g_ascii_strncasecmp (symbol, "clip", 4))
141 ges_structure_parser_parse_string (self, "clip, uri=(string)", TRUE);
142 else if (!g_ascii_strncasecmp (symbol, "test-clip", 9))
143 ges_structure_parser_parse_string (self, "test-clip, pattern=(string)",
145 else if (!g_ascii_strncasecmp (symbol, "effect", 6))
146 ges_structure_parser_parse_string (self, "effect, bin-description=(string)",
148 else if (!g_ascii_strncasecmp (symbol, "transition", 10))
149 ges_structure_parser_parse_string (self, "transition, type=(string)", TRUE);
150 else if (!g_ascii_strncasecmp (symbol, "title", 5))
151 ges_structure_parser_parse_string (self, "title, text=(string)", TRUE);
152 else if (!g_ascii_strncasecmp (symbol, "track", 5))
153 ges_structure_parser_parse_string (self, "track, type=(string)", TRUE);
157 ges_structure_parser_parse_setter (GESStructureParser * self,
158 const gchar * setter)
160 gchar *parsed_setter;
162 _finish_structure (self);
164 while (*setter == '-' || *setter == ' ')
167 while (*setter != '-')
172 parsed_setter = g_strdup_printf ("set-property, property=(string)%s, "
173 "value=(string)", setter);
174 self->add_comma = FALSE;
175 ges_structure_parser_parse_string (self, parsed_setter, TRUE);
176 g_free (parsed_setter);
180 ges_structure_parser_new (void)
182 return (g_object_new (GES_TYPE_STRUCTURE_PARSER, NULL));
186 ges_structure_parser_get_error (GESStructureParser * self)
189 GError *error = NULL;
192 if (self->wrong_strings == NULL)
195 msg = g_string_new ("Could not parse: ");
198 for (tmp = self->wrong_strings; tmp; tmp = tmp->next) {
199 g_string_append_printf (msg, " %s", (gchar *) tmp->data);
202 error = g_error_new_literal (GES_ERROR, 0, msg->str);
203 g_string_free (msg, TRUE);