327b2a2c3fa43062665a6ec7f1661b0bff90c49d
[platform/upstream/gst-editing-services.git] / ges / ges-structure-parser.c
1 /* GStreamer Editing Services
2  *
3  * Copyright (C) <2015> Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "ges-structure-parser.h"
25
26 #include <ges/ges.h>
27
28 G_DEFINE_TYPE (GESStructureParser, ges_structure_parser, G_TYPE_OBJECT);
29
30 static void
31 ges_structure_parser_init (GESStructureParser * self)
32 {
33 }
34
35 static void
36 _finalize (GObject * self)
37 {
38   GESStructureParser *parser = GES_STRUCTURE_PARSER (self);
39
40   g_list_free_full (parser->structures, (GDestroyNotify) gst_structure_free);
41   g_list_free_full (parser->wrong_strings, (GDestroyNotify) g_free);
42
43   G_OBJECT_CLASS (ges_structure_parser_parent_class)->finalize (self);
44 }
45
46 static void
47 ges_structure_parser_class_init (GESStructureParserClass * klass)
48 {
49   G_OBJECT_CLASS (klass)->finalize = _finalize;
50 }
51
52 void
53 ges_structure_parser_parse_string (GESStructureParser * self,
54     const gchar * text, gboolean is_symbol)
55 {
56   gchar *new_string = NULL;
57
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);
62   }
63
64   g_free (self->current_string);
65   self->current_string = new_string;
66 }
67
68 void
69 ges_structure_parser_parse_value (GESStructureParser * self, const gchar * text)
70 {
71   /* text starts with '=' */
72   gchar *val_string = g_strconcat ("=(string)", text + 1, NULL);
73   ges_structure_parser_parse_string (self, val_string, FALSE);
74   g_free (val_string);
75 }
76
77 void
78 ges_structure_parser_parse_default (GESStructureParser * self,
79     const gchar * text)
80 {
81   gchar *new_string = NULL;
82
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;
88   } else {
89     ges_structure_parser_parse_string (self, text, FALSE);
90   }
91 }
92
93 void
94 ges_structure_parser_parse_whitespace (GESStructureParser * self)
95 {
96   self->add_comma = TRUE;
97 }
98
99 static void
100 _finish_structure (GESStructureParser * self)
101 {
102   GstStructure *structure;
103
104   if (!self->current_string)
105     return;
106
107   structure = gst_structure_new_from_string (self->current_string);
108
109   if (structure == NULL) {
110     GST_ERROR ("Could not parse %s", self->current_string);
111
112     self->wrong_strings = g_list_append (self->wrong_strings,
113         g_strdup (self->current_string));
114
115     return;
116   }
117
118   self->structures = g_list_append (self->structures, structure);
119   g_free (self->current_string);
120   self->current_string = NULL;
121 }
122
123
124 void
125 ges_structure_parser_end_of_file (GESStructureParser * self)
126 {
127   _finish_structure (self);
128 }
129
130 void
131 ges_structure_parser_parse_symbol (GESStructureParser * self,
132     const gchar * symbol)
133 {
134   _finish_structure (self);
135
136   while (*symbol == ' ' || *symbol == '+')
137     symbol++;
138
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)",
144         TRUE);
145   else if (!g_ascii_strncasecmp (symbol, "effect", 6))
146     ges_structure_parser_parse_string (self, "effect, bin-description=(string)",
147         TRUE);
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);
154 }
155
156 void
157 ges_structure_parser_parse_setter (GESStructureParser * self,
158     const gchar * setter)
159 {
160   gchar *parsed_setter;
161
162   _finish_structure (self);
163
164   while (*setter == '-' || *setter == ' ')
165     setter++;
166
167   while (*setter != '-')
168     setter++;
169
170   setter++;
171
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);
177 }
178
179 GESStructureParser *
180 ges_structure_parser_new (void)
181 {
182   return (g_object_new (GES_TYPE_STRUCTURE_PARSER, NULL));
183 }
184
185 GError *
186 ges_structure_parser_get_error (GESStructureParser * self)
187 {
188   GList *tmp;
189   GError *error = NULL;
190   GString *msg = NULL;
191
192   if (self->wrong_strings == NULL)
193     return NULL;
194
195   msg = g_string_new ("Could not parse: ");
196
197
198   for (tmp = self->wrong_strings; tmp; tmp = tmp->next) {
199     g_string_append_printf (msg, " %s", (gchar *) tmp->data);
200   }
201
202   error = g_error_new_literal (GES_ERROR, 0, msg->str);
203   g_string_free (msg, TRUE);
204
205   return error;
206 }