Increment counter when registering standard formats
[platform/upstream/gstreamer.git] / gst / gstformat.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstformat.c: GstFormat registration
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "gstlog.h"
24
25 #include "gstformat.h"
26
27 static GList *_gst_formats = NULL;
28 static gint  _n_values = 1; /* we start from 1 because 0 reserved for UNDEFINED */
29
30 typedef struct _GstFormatDefinition GstFormatDefinition;
31
32 struct _GstFormatDefinition 
33 {
34   GstFormat  value;
35   gchar     *nick;
36   gchar     *description;
37 };
38
39 static GstFormatDefinition standard_definitions[] = {
40   { GST_FORMAT_DEFAULT, "default", "Default" },
41   { GST_FORMAT_BYTES,   "bytes",   "Bytes" },
42   { GST_FORMAT_TIME,    "time",    "Time" }, 
43   { GST_FORMAT_BUFFERS, "buffers", "Buffers" },
44   { GST_FORMAT_PERCENT, "percent", "Percent" },
45   { GST_FORMAT_UNITS,   "units",   "Units as defined by the media type" },
46   { 0, NULL, NULL }
47 };
48
49 void            
50 _gst_format_initialize (void)
51 {
52   GstFormatDefinition *standards = standard_definitions;
53
54   while (standards->nick) {
55     _gst_formats = g_list_prepend (_gst_formats, standards);
56     standards++;
57     _n_values++;
58   }
59 }
60
61 /**
62  * gst_format_register:
63  * @nick: The nick of the new format
64  * @description: The description of the new format
65  *
66  * Create a new GstFormat based on the nick or return an
67  * allrady registered format with that nick
68  *
69  * Returns: A new GstFormat or an already registered format
70  * with the same nick.
71  */
72 GstFormat
73 gst_format_register (const gchar *nick, const gchar *description)
74 {
75   GstFormatDefinition *format;
76   GstFormat query;
77   
78   g_return_val_if_fail (nick != NULL, 0);
79   g_return_val_if_fail (description != NULL, 0);
80
81   query = gst_format_get_by_nick (nick);
82   if (query != GST_FORMAT_UNDEFINED)
83     return query;
84   
85   format = g_new0 (GstFormatDefinition, 1);
86   format->value = _n_values;
87   format->nick = g_strdup (nick);
88   format->description = g_strdup (description);
89
90   _gst_formats = g_list_prepend (_gst_formats, format);
91
92   _n_values++;
93   
94   return format->value;
95 }
96
97 /**
98  * gst_format_get_by_nick:
99  * @nick: The nick of the format
100  *
101  * Return the format registered with the given nick. 
102  *
103  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
104  * if the format was not registered.
105  */
106 GstFormat
107 gst_format_get_by_nick (const gchar *nick)
108 {
109   GList *walk;
110   GstFormatDefinition *format;
111   
112   g_return_val_if_fail (nick != NULL, 0);
113
114   walk = _gst_formats;
115
116   while (walk) {
117     format = (GstFormatDefinition *) walk->data;
118     
119     if (!strcmp (format->nick, nick))
120       return format->value;
121
122     walk = g_list_next (walk);
123   }
124
125   return GST_FORMAT_UNDEFINED;
126 }
127
128 /**
129  * gst_format_get_details:
130  * @format: The format to get details of
131  * @nick: The nick of the format
132  * @description: The description of the format
133  *
134  * Get details about the given format.
135  *
136  * Returns: TRUE if the format was registered, FALSE otherwise
137  */
138 gboolean
139 gst_format_get_details (GstFormat format, const gchar **nick, const gchar **description)
140 {
141   GList *walk;
142   GstFormatDefinition *walk_format;
143
144   g_return_val_if_fail (nick != NULL, FALSE);
145   g_return_val_if_fail (description != NULL, FALSE);
146
147   walk = _gst_formats;
148
149   while (walk) {
150     walk_format = (GstFormatDefinition *) walk->data;
151     
152     if (walk_format->value == format) {
153       *nick = walk_format->nick;
154       *description = walk_format->description;
155
156       return TRUE;
157     }
158
159     walk = g_list_next (walk);
160   }
161   return FALSE;
162 }