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>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "gmyth_stringlist.h"
34 #include "gmyth_debug.h"
36 static void gmyth_string_list_class_init(GMythStringListClass * klass);
37 static void gmyth_string_list_init(GMythStringList * object);
39 static void gmyth_string_list_dispose(GObject * object);
40 static void gmyth_string_list_finalize(GObject * object);
42 G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT)
43 static void gmyth_string_list_class_init(GMythStringListClass *
46 GObjectClass *gobject_class;
48 gobject_class = (GObjectClass *) klass;
50 gobject_class->dispose = gmyth_string_list_dispose;
51 gobject_class->finalize = gmyth_string_list_finalize;
55 gmyth_string_list_init(GMythStringList * gmyth_string_list)
57 gmyth_string_list->glist = NULL;
61 gmyth_string_list_dispose(GObject * object)
63 GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
65 gmyth_string_list_clear_all(gmyth_string_list);
67 G_OBJECT_CLASS(gmyth_string_list_parent_class)->dispose(object);
71 gmyth_string_list_finalize(GObject * object)
73 // GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
75 g_signal_handlers_destroy(object);
77 G_OBJECT_CLASS(gmyth_string_list_parent_class)->finalize(object);
80 /** Creates a new instance of GStringList.
82 * @return a new instance of GStringList.
85 gmyth_string_list_new()
87 GMythStringList *gmyth_string_list =
88 GMYTH_STRING_LIST(g_object_new(GMYTH_STRING_LIST_TYPE, NULL));
90 return gmyth_string_list;
93 /** Appends a guint64 to the string list.
95 * @param strlist The GMythStringList instance.
96 * @param value The guint64 to be appended.
98 * @return The appended guint64 converted to a GString object.
101 gmyth_string_list_append_int(GMythStringList * strlist, const gint value)
103 GString *value_str = g_string_new("");
105 g_string_printf(value_str, "%d", value);
107 strlist->glist = g_list_append(strlist->glist, value_str);
113 /** Appends a gdouble to the string list.
115 * @param strlist The GMythStringList instance.
116 * @param value The gdouble to be appended.
118 * @return The appended gdouble converted to a GString object.
121 gmyth_string_list_append_float(GMythStringList * strlist, const gdouble value)
123 GString *value_str = g_string_new("");
124 g_string_printf(value_str, "%f", value);
125 strlist->glist = g_list_append(strlist->glist, value_str);
130 /** Appends a guint64 to the string list.
132 * @param strlist The GMythStringList instance.
133 * @param value The guint64 to be appended.
135 * @return The appended guint64 converted to a GString object.
138 gmyth_string_list_append_uint64(GMythStringList * strlist,
141 GString *tmp_str1 = g_string_new("");
142 GString *tmp_str2 = g_string_new("");
144 gmyth_debug("value = %llu.\n", value);
146 gulong l2 = ((guint64) value & 0xffffffff);
147 gulong l1 = ((guint64) value >> 32);
150 * high order part of guint64 value
152 g_string_printf(tmp_str1, "%lu", l1);
154 gmyth_debug("[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
156 strlist->glist = g_list_append(strlist->glist, tmp_str1);
159 * low order part of guint64 value
161 g_string_printf(tmp_str2, "%lu", l2);
163 gmyth_debug("[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
165 strlist->glist = g_list_append(strlist->glist, tmp_str2);
170 /** Appends a gint64 to the string list.
172 * @param strlist The GMythStringList instance.
173 * @param value The gint64 to be appended.
175 * @return The appended gint64 converted to a GString object.
178 gmyth_string_list_append_int64(GMythStringList * strlist,
181 GString *tmp_str1 = g_string_new("");
182 GString *tmp_str2 = g_string_new("");
184 gmyth_debug("value = %lld.\n", value);
186 glong l2 = ((gint64) value & 0xffffffff);
187 glong l1 = ((gint64) value >> 32);
190 * high order part of gint64 value
192 g_string_printf(tmp_str1, "%ld", l1);
194 gmyth_debug("[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
196 strlist->glist = g_list_append(strlist->glist, tmp_str1);
199 * low order part of gint64 value
201 g_string_printf(tmp_str2, "%ld", l2);
203 gmyth_debug("[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
205 strlist->glist = g_list_append(strlist->glist, tmp_str2);
210 /** Appends a char array to the string list.
212 * @param strlist The GMythStringList instance.
213 * @param value The char array to be appended.
215 * @return The appended char array converted to a GString object.
218 gmyth_string_list_append_char_array(GMythStringList * strlist,
221 GString *tmp_str = NULL;
223 g_return_val_if_fail(strlist != NULL, NULL);
225 tmp_str = g_string_new(value);
227 strlist->glist = g_list_append(strlist->glist, tmp_str);
232 /** Appends a string to the string list.
234 * @param strlist The GMythStringList instance.
235 * @param value The string to be appended.
237 * @return The appended string itself.
240 gmyth_string_list_append_string(GMythStringList * strlist, GString * value)
242 g_return_val_if_fail(strlist != NULL, NULL);
246 g_list_append(strlist->glist, g_string_new(value->str));
248 strlist->glist = g_list_append(strlist->glist, NULL);
254 /** Gets an integer value from the string list at the given position.
256 * @param strlist The GMythStringList instance.
257 * @param index the integer position in the list, starting with zero.
258 * @return The integer value.
261 gmyth_string_list_get_int(GMythStringList * strlist, const gint index)
263 // TODO: Create static method check_index()
264 GString *tmp_str = NULL;
266 g_return_val_if_fail(strlist != NULL, 0);
268 tmp_str = (GString *) g_list_nth_data(strlist->glist, index);
270 if (NULL == tmp_str || NULL == tmp_str->str
271 || strlen(tmp_str->str) <= 0)
274 return (gint) ( /* 0x00000000ffffffffL & (gint64) */
275 g_ascii_strtoull(tmp_str->str, NULL, 10));
278 /** Gets a guint64 value from the string list at the given position.
279 * According to the Mythtv protocol, the 64 bits value is formed by
282 * @param strlist The GMythStringList instance.
283 * @param index the index of the first string forming the 64 bits value.
284 * Index starts with zero.
285 * @return The guint64 value.
288 gmyth_string_list_get_uint64(GMythStringList * strlist, const gint index)
290 // TODO: Create static method check_index()
291 guint64 ret_value = 0;
294 g_return_val_if_fail(strlist != NULL, 0);
296 const GString *tmp_str1 =
297 (GString *) g_list_nth_data(strlist->glist, index);
298 const GString *tmp_str2 =
299 (GString *) g_list_nth_data(strlist->glist, index + 1);
301 if (tmp_str1 != NULL)
302 gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
304 if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
306 gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
310 guint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10) /* &
315 if (tmp_str2 != NULL && tmp_str2->str != NULL
316 && strlen(tmp_str2->str) > 0) {
317 l2 = ((guint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
327 gmyth_debug("[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2);
330 ((guint64) (l2) /* & 0xffffffff */ ) | ((guint64) l1 << 32);
332 gmyth_debug("[%s] returning uint64 value = %llu\n", __FUNCTION__,
338 /** Gets a gint64 value from the string list at the given position.
339 * According to the Mythtv protocol, the 64 bits value is formed by
342 * @param strlist The GMythStringList instance.
343 * @param index the index of the first string forming the 64 bits value.
344 * Index starts with zero.
345 * @return The gint64 value.
348 gmyth_string_list_get_int64(GMythStringList * strlist, const gint index)
350 // TODO: Create static method check_index()
351 gint64 ret_value = 0;
354 g_return_val_if_fail(strlist != NULL, 0);
356 const GString *tmp_str1 =
357 (GString *) g_list_nth_data(strlist->glist, index);
358 const GString *tmp_str2 =
359 (GString *) g_list_nth_data(strlist->glist, index + 1);
361 if (tmp_str1 != NULL)
362 gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
364 if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
366 gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
370 gint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10) /* &
375 if (tmp_str2 != NULL && tmp_str2->str != NULL
376 && strlen(tmp_str2->str) > 0) {
377 l2 = ((gint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
387 gmyth_debug("[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2);
389 ret_value = ((gint64) (l2) /* & 0xffffffff */ ) | ((gint64) l1 << 32);
391 gmyth_debug("[%s] returning int64 value = %lld\n", __FUNCTION__,
398 /** Gets a string from the string list at the given position. The GString must be deallocated.
400 * @param strlist The GMythStringList instance.
401 * @param index the string position in the list, starting with zero.
402 * @return A pointer to the string data.
405 gmyth_string_list_get_string(GMythStringList * strlist, const gint index)
409 if (!strlist || !(strlist->glist)) {
410 gmyth_debug("%s received Null arguments", __FUNCTION__);
414 ret = (GString *) g_list_nth_data(strlist->glist, index);
416 return g_string_new(ret->str);
421 gmyth_string_list_clear_element(GString * str_elem, void *data_aux)
423 if (str_elem != NULL)
424 g_string_free(str_elem, TRUE);
427 /** Removes all strings from the string list.
429 * @param strlist The GMythStringList instance.
432 gmyth_string_list_clear_all(GMythStringList * strlist)
434 if (strlist != NULL && strlist->glist) {
435 g_list_foreach(strlist->glist,
436 (GFunc) gmyth_string_list_clear_element, NULL);
437 g_list_free(strlist->glist);
438 strlist->glist = NULL;
442 /** Retrieves the number of elements in the string list.
444 * @param strlist The GMythStringList instance.
445 * @return the string list length.
448 gmyth_string_list_length(GMythStringList * strlist)
450 if (!(strlist != NULL && strlist->glist != NULL))
453 return g_list_length(strlist->glist);