don't mix tabs and spaces
[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 <string.h>
24
25 #include "gstlog.h"
26
27 #include "gstformat.h"
28
29 static GList *_gst_formats = NULL;
30 static GHashTable *_nick_to_format = NULL;
31 static GHashTable *_format_to_nick = NULL;
32 static gint _n_values = 1;      /* we start from 1 because 0 reserved for UNDEFINED */
33
34 static GstFormatDefinition standard_definitions[] = {
35   {GST_FORMAT_DEFAULT, "default", "Default format for the media type"},
36   {GST_FORMAT_BYTES, "bytes", "Bytes"},
37   {GST_FORMAT_TIME, "time", "Time"},
38   {GST_FORMAT_BUFFERS, "buffers", "Buffers"},
39   {GST_FORMAT_PERCENT, "percent", "Percent"},
40   {0, NULL, NULL}
41 };
42
43 void
44 _gst_format_initialize (void)
45 {
46   GstFormatDefinition *standards = standard_definitions;
47
48   if (_nick_to_format == NULL) {
49     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
50     _format_to_nick = g_hash_table_new (NULL, NULL);
51   }
52
53   while (standards->nick) {
54     g_hash_table_insert (_nick_to_format, standards->nick, standards);
55     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
56         standards);
57
58     _gst_formats = g_list_append (_gst_formats, standards);
59     standards++;
60     _n_values++;
61   }
62 }
63
64 /**
65  * gst_format_register:
66  * @nick: The nick of the new format
67  * @description: The description of the new format
68  *
69  * Create a new GstFormat based on the nick or return an
70  * allrady registered format with that nick
71  *
72  * Returns: A new GstFormat or an already registered format
73  * with the same nick.
74  */
75 GstFormat
76 gst_format_register (const gchar * nick, const gchar * description)
77 {
78   GstFormatDefinition *format;
79   GstFormat query;
80
81   g_return_val_if_fail (nick != NULL, 0);
82   g_return_val_if_fail (description != NULL, 0);
83
84   query = gst_format_get_by_nick (nick);
85   if (query != GST_FORMAT_UNDEFINED)
86     return query;
87
88   format = g_new0 (GstFormatDefinition, 1);
89   format->value = _n_values;
90   format->nick = g_strdup (nick);
91   format->description = g_strdup (description);
92
93   g_hash_table_insert (_nick_to_format, format->nick, format);
94   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
95       format);
96   _gst_formats = g_list_append (_gst_formats, format);
97   _n_values++;
98
99   return format->value;
100 }
101
102 /**
103  * gst_format_get_by_nick:
104  * @nick: The nick of the format
105  *
106  * Return the format registered with the given nick. 
107  *
108  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
109  * if the format was not registered.
110  */
111 GstFormat
112 gst_format_get_by_nick (const gchar * nick)
113 {
114   GstFormatDefinition *format;
115
116   g_return_val_if_fail (nick != NULL, 0);
117
118   format = g_hash_table_lookup (_nick_to_format, nick);
119
120   if (format != NULL)
121     return format->value;
122   else
123     return GST_FORMAT_UNDEFINED;
124 }
125
126 /**
127  * gst_formats_contains:
128  * @formats: The format array to search
129  * @format: the format to find
130  *
131  * See if the given format is inside the format array.
132  *
133  * Returns: TRUE if the format is found inside the array
134  */
135 gboolean
136 gst_formats_contains (const GstFormat * formats, GstFormat format)
137 {
138   if (!formats)
139     return FALSE;
140
141   while (*formats) {
142     if (*formats == format)
143       return TRUE;
144
145     formats++;
146   }
147   return FALSE;
148 }
149
150
151 /**
152  * gst_format_get_details:
153  * @format: The format to get details of
154  *
155  * Get details about the given format.
156  *
157  * Returns: The #GstFormatDefinition for @format or NULL on failure.
158  */
159 const GstFormatDefinition *
160 gst_format_get_details (GstFormat format)
161 {
162   return g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
163 }
164
165 /**
166  * gst_format_get_definitions:
167  *
168  * Get a list of all the registered formats.
169  *
170  * Returns: A GList of #GstFormatDefinition.
171  */
172 const GList *
173 gst_format_get_definitions (void)
174 {
175   return _gst_formats;
176 }