gst/: More guards against bad input; typo fix; some minor clean-ups.
[platform/upstream/gstreamer.git] / gst / gstparse.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2002 Andy Wingo <wingo@pobox.com>
5  *
6  * gstparse.c: get a pipeline from a text pipeline description
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstparse
26  * @short_description: Get a pipeline from a text pipeline description
27  *
28  * These function allow to create a pipeline based on the syntax used in the
29  * gst-launch utility.
30  */
31
32
33 #include "gst_private.h"
34 #include <string.h>
35
36 #include "gstparse.h"
37 #include "gsterror.h"
38 #include "gstinfo.h"
39
40 extern GstElement *_gst_parse_launch (const gchar *, GError **);
41
42 /**
43  * gst_parse_error_quark:
44  *
45  * Get the error quark used by the parsing subsystem.
46  *
47  * Returns: the quark of the parse errors.
48  */
49 GQuark
50 gst_parse_error_quark (void)
51 {
52   static GQuark quark = 0;
53
54   if (!quark)
55     quark = g_quark_from_static_string ("gst_parse_error");
56   return quark;
57 }
58
59 #ifndef GST_DISABLE_PARSE
60 static gchar *
61 _gst_parse_escape (const gchar * str)
62 {
63   GString *gstr = NULL;
64
65   g_return_val_if_fail (str != NULL, NULL);
66
67   gstr = g_string_sized_new (strlen (str));
68
69   while (*str) {
70     if (*str == ' ')
71       g_string_append_c (gstr, '\\');
72     g_string_append_c (gstr, *str);
73     str++;
74   }
75
76   return g_string_free (gstr, FALSE);
77 }
78 #endif /* !GST_DISABLE_PARSE */
79
80 /**
81  * gst_parse_launchv:
82  * @argv: null-terminated array of arguments
83  * @error: pointer to a #GError
84  *
85  * Create a new element based on command line syntax.
86  * @error will contain an error message if an erroneuos pipeline is specified.
87  * An error does not mean that the pipeline could not be constructed.
88  *
89  * Returns: a new element on success and %NULL on failure.
90  */
91 GstElement *
92 gst_parse_launchv (const gchar ** argv, GError ** error)
93 {
94 #ifndef GST_DISABLE_PARSE
95   GstElement *element;
96   GString *str;
97   const gchar **argvp, *arg;
98   gchar *tmp;
99
100   g_return_val_if_fail (argv != NULL, NULL);
101   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
102
103   /* let's give it a nice size. */
104   str = g_string_sized_new (1024);
105
106   argvp = argv;
107   while (*argvp) {
108     arg = *argvp;
109     tmp = _gst_parse_escape (arg);
110     g_string_append (str, tmp);
111     g_free (tmp);
112     g_string_append_c (str, ' ');
113     argvp++;
114   }
115
116   element = gst_parse_launch (str->str, error);
117
118   g_string_free (str, TRUE);
119
120   return element;
121 #else
122   gchar *msg;
123
124   GST_WARNING ("Disabled API called");
125
126   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
127   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
128   g_free (msg);
129
130   return NULL;
131 #endif
132 }
133
134 /**
135  * gst_parse_launch:
136  * @pipeline_description: the command line describing the pipeline
137  * @error: the error message in case of an erroneous pipeline.
138  *
139  * Create a new pipeline based on command line syntax.
140  * Please note that you might get a return value that is not %NULL even though
141  * the @error is set. In this case there was a recoverable parsing error and you
142  * can try to play the pipeline.
143  *
144  * Returns: a new element on success, %NULL on failure. If more than one toplevel
145  * element is specified by the @pipeline_description, all elements are put into
146  * a #GstPipeline, which than is returned.
147  */
148 GstElement *
149 gst_parse_launch (const gchar * pipeline_description, GError ** error)
150 {
151 #ifndef GST_DISABLE_PARSE
152   GstElement *element;
153
154   g_return_val_if_fail (pipeline_description != NULL, NULL);
155
156   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
157       pipeline_description);
158
159   element = _gst_parse_launch (pipeline_description, error);
160
161   return element;
162 #else
163   gchar *msg;
164
165   GST_WARNING ("Disabled API called: gst_parse_launch()");
166
167   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
168   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
169   g_free (msg);
170
171   return NULL;
172 #endif
173 }