[svn r457] added check != NULL before g_string_free to avoid GLib Critical warnings
4 * @file gmyth/gmyth_stringlist.c
6 * @brief <p> This component contains functions for dealing with the stringlist
7 * format of the mythprotocol.
9 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
10 * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "gmyth_stringlist.h"
35 #include "gmyth_debug.h"
37 static void gmyth_string_list_class_init (GMythStringListClass *klass);
38 static void gmyth_string_list_init (GMythStringList *object);
40 static void gmyth_string_list_dispose (GObject *object);
41 static void gmyth_string_list_finalize (GObject *object);
43 G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT)
46 gmyth_string_list_class_init (GMythStringListClass *klass)
48 GObjectClass *gobject_class;
50 gobject_class = (GObjectClass *) klass;
52 gobject_class->dispose = gmyth_string_list_dispose;
53 gobject_class->finalize = gmyth_string_list_finalize;
57 gmyth_string_list_init (GMythStringList *gmyth_string_list)
59 gmyth_string_list->glist = NULL;
63 gmyth_string_list_dispose (GObject *object)
65 GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
67 gmyth_string_list_clear_all(gmyth_string_list);
69 G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object);
73 gmyth_string_list_finalize (GObject *object)
75 //GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
77 g_signal_handlers_destroy (object);
79 G_OBJECT_CLASS (gmyth_string_list_parent_class)->finalize (object);
82 /** Creates a new instance of GStringList.
84 * @return a new instance of GStringList.
87 gmyth_string_list_new ()
89 GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL));
91 return gmyth_string_list;
94 /** Appends a guint64 to the string list.
96 * @param strlist The GMythStringList instance.
97 * @param value The guint64 to be appended.
99 * @return The appended guint64 converted to a GString object.
102 gmyth_string_list_append_int ( GMythStringList *strlist, const gint value )
104 GString *value_str = g_string_new ("");
106 g_string_printf (value_str, "%d", value);
108 strlist->glist = g_list_append (strlist->glist, value_str);
113 /** Appends a guint64 to the string list.
115 * @param strlist The GMythStringList instance.
116 * @param value The guint64 to be appended.
118 * @return The appended guint64 converted to a GString object.
121 gmyth_string_list_append_uint64 ( GMythStringList *strlist, const guint64 value)
123 GString *tmp_str1 = g_string_new ("");
124 GString *tmp_str2 = g_string_new ("");
125 gmyth_debug ( "value = %llu.\n", value);
127 gulong l2 = ( (guint64)value & 0xffffffff );
128 gulong l1 = ( (guint64)value >> 32 );
130 /* high order part of guint64 value */
131 g_string_printf (tmp_str1, "%lu", l1);
133 gmyth_debug( "[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
135 strlist->glist = g_list_append( strlist->glist, tmp_str1 );
137 /* low order part of guint64 value */
138 g_string_printf (tmp_str2, "%lu", l2);
140 gmyth_debug( "[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
142 strlist->glist = g_list_append( strlist->glist, tmp_str2 );
147 /** Appends a gint64 to the string list.
149 * @param strlist The GMythStringList instance.
150 * @param value The gint64 to be appended.
152 * @return The appended gint64 converted to a GString object.
155 gmyth_string_list_append_int64 ( GMythStringList *strlist, const gint64 value)
157 GString *tmp_str1 = g_string_new ("");
158 GString *tmp_str2 = g_string_new ("");
159 gmyth_debug ( "value = %lld.\n", value );
161 glong l2 = ( (gint64)value & 0xffffffff );
162 glong l1 = ( (gint64)value >> 32 );
164 /* high order part of gint64 value */
165 g_string_printf (tmp_str1, "%ld", l1);
167 gmyth_debug( "[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
169 strlist->glist = g_list_append( strlist->glist, tmp_str1 );
171 /* low order part of gint64 value */
172 g_string_printf (tmp_str2, "%ld", l2);
174 gmyth_debug( "[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
176 strlist->glist = g_list_append( strlist->glist, tmp_str2 );
181 /** Appends a char array to the string list.
183 * @param strlist The GMythStringList instance.
184 * @param value The char array to be appended.
186 * @return The appended char array converted to a GString object.
189 gmyth_string_list_append_char_array ( GMythStringList *strlist, const gchar* value )
191 GString *tmp_str = NULL;
193 g_return_val_if_fail( strlist != NULL, NULL );
195 tmp_str = g_string_new (value);
197 strlist->glist = g_list_append (strlist->glist, tmp_str);
202 /** Appends a string to the string list.
204 * @param strlist The GMythStringList instance.
205 * @param value The string to be appended.
207 * @return The appended string itself.
210 gmyth_string_list_append_string ( GMythStringList *strlist, GString *value )
212 g_return_val_if_fail (strlist != NULL, NULL );
215 strlist->glist = g_list_append (strlist->glist, g_string_new (value->str));
217 strlist->glist = g_list_append (strlist->glist, NULL);
223 /** Gets an integer value from the string list at the given position.
225 * @param strlist The GMythStringList instance.
226 * @param index the integer position in the list, starting with zero.
227 * @return The integer value.
230 gmyth_string_list_get_int ( GMythStringList *strlist, const gint index )
232 //TODO: Create static method check_index()
233 GString *tmp_str = NULL;
235 g_return_val_if_fail( strlist != NULL, 0 );
237 tmp_str = (GString *) g_list_nth_data (strlist->glist, index);
239 if ( NULL == tmp_str || NULL == tmp_str->str || strlen( tmp_str->str ) <= 0 )
242 return (gint) ( /* 0x00000000ffffffffL & (gint64)*/g_ascii_strtoull ( tmp_str->str, NULL, 10 ) );
245 /** Gets a guint64 value from the string list at the given position.
246 * According to the Mythtv protocol, the 64 bits value is formed by
249 * @param strlist The GMythStringList instance.
250 * @param index the index of the first string forming the 64 bits value.
251 * Index starts with zero.
252 * @return The guint64 value.
255 gmyth_string_list_get_uint64 ( GMythStringList *strlist, const gint index )
257 //TODO: Create static method check_index()
258 guint64 ret_value = 0;
261 g_return_val_if_fail( strlist != NULL, 0 );
263 const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
264 const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
266 if ( tmp_str1 != NULL )
267 gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
268 if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
270 gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
273 guint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/ );
274 if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
275 l2 = ( (guint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/ );
281 gmyth_debug ( "[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2 );
283 ret_value = ((guint64)(l2) /*& 0xffffffff*/) | ((guint64)l1 << 32);
285 gmyth_debug( "[%s] returning uint64 value = %llu\n", __FUNCTION__, ret_value );
290 /** Gets a gint64 value from the string list at the given position.
291 * According to the Mythtv protocol, the 64 bits value is formed by
294 * @param strlist The GMythStringList instance.
295 * @param index the index of the first string forming the 64 bits value.
296 * Index starts with zero.
297 * @return The gint64 value.
300 gmyth_string_list_get_int64 ( GMythStringList *strlist, const gint index )
302 //TODO: Create static method check_index()
303 gint64 ret_value = 0;
306 g_return_val_if_fail( strlist != NULL, 0 );
308 const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
309 const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
311 if ( tmp_str1 != NULL )
312 gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
313 if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
315 gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
318 gint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/ );
319 if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
320 l2 = ( (gint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/ );
326 gmyth_debug ( "[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2 );
328 ret_value = ((gint64)(l2) /*& 0xffffffff*/) | ((gint64)l1 << 32);
330 gmyth_debug( "[%s] returning int64 value = %lld\n", __FUNCTION__, ret_value );
336 /** Gets a string from the string list at the given position. The GString must be deallocated.
338 * @param strlist The GMythStringList instance.
339 * @param index the string position in the list, starting with zero.
340 * @return A pointer to the string data.
343 gmyth_string_list_get_string ( GMythStringList *strlist, const gint index )
347 if (!strlist || !(strlist->glist)) {
348 g_warning ("%s received Null arguments", __FUNCTION__);
352 ret = (GString *) g_list_nth_data (strlist->glist, index);
354 return g_string_new (ret->str);
359 gmyth_string_list_clear_element( GString *str_elem, void *data_aux )
361 if (str_elem != NULL)
362 g_string_free( str_elem, TRUE );
365 /** Removes all strings from the string list.
367 * @param strlist The GMythStringList instance.
370 gmyth_string_list_clear_all ( GMythStringList *strlist )
372 if ( strlist != NULL && strlist->glist ) {
373 g_list_foreach( strlist->glist, (GFunc)gmyth_string_list_clear_element, NULL );
374 g_list_free (strlist->glist);
375 strlist->glist = NULL;
379 /** Retrieves the number of elements in the string list.
381 * @param strlist The GMythStringList instance.
382 * @return the string list length.
385 gmyth_string_list_length ( GMythStringList *strlist )
387 if ( !(strlist != NULL && strlist->glist != NULL) )
390 return g_list_length (strlist->glist);