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);
112 /** Appends a guint64 to the string list.
114 * @param strlist The GMythStringList instance.
115 * @param value The guint64 to be appended.
117 * @return The appended guint64 converted to a GString object.
120 gmyth_string_list_append_uint64(GMythStringList * strlist,
123 GString *tmp_str1 = g_string_new("");
124 GString *tmp_str2 = g_string_new("");
126 gmyth_debug("value = %llu.\n", value);
128 gulong l2 = ((guint64) value & 0xffffffff);
129 gulong l1 = ((guint64) value >> 32);
132 * high order part of guint64 value
134 g_string_printf(tmp_str1, "%lu", l1);
136 gmyth_debug("[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
138 strlist->glist = g_list_append(strlist->glist, tmp_str1);
141 * low order part of guint64 value
143 g_string_printf(tmp_str2, "%lu", l2);
145 gmyth_debug("[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
147 strlist->glist = g_list_append(strlist->glist, tmp_str2);
152 /** Appends a gint64 to the string list.
154 * @param strlist The GMythStringList instance.
155 * @param value The gint64 to be appended.
157 * @return The appended gint64 converted to a GString object.
160 gmyth_string_list_append_int64(GMythStringList * strlist,
163 GString *tmp_str1 = g_string_new("");
164 GString *tmp_str2 = g_string_new("");
166 gmyth_debug("value = %lld.\n", value);
168 glong l2 = ((gint64) value & 0xffffffff);
169 glong l1 = ((gint64) value >> 32);
172 * high order part of gint64 value
174 g_string_printf(tmp_str1, "%ld", l1);
176 gmyth_debug("[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
178 strlist->glist = g_list_append(strlist->glist, tmp_str1);
181 * low order part of gint64 value
183 g_string_printf(tmp_str2, "%ld", l2);
185 gmyth_debug("[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
187 strlist->glist = g_list_append(strlist->glist, tmp_str2);
192 /** Appends a char array to the string list.
194 * @param strlist The GMythStringList instance.
195 * @param value The char array to be appended.
197 * @return The appended char array converted to a GString object.
200 gmyth_string_list_append_char_array(GMythStringList * strlist,
203 GString *tmp_str = NULL;
205 g_return_val_if_fail(strlist != NULL, NULL);
207 tmp_str = g_string_new(value);
209 strlist->glist = g_list_append(strlist->glist, tmp_str);
214 /** Appends a string to the string list.
216 * @param strlist The GMythStringList instance.
217 * @param value The string to be appended.
219 * @return The appended string itself.
222 gmyth_string_list_append_string(GMythStringList * strlist, GString * value)
224 g_return_val_if_fail(strlist != NULL, NULL);
228 g_list_append(strlist->glist, g_string_new(value->str));
230 strlist->glist = g_list_append(strlist->glist, NULL);
236 /** Gets an integer value from the string list at the given position.
238 * @param strlist The GMythStringList instance.
239 * @param index the integer position in the list, starting with zero.
240 * @return The integer value.
243 gmyth_string_list_get_int(GMythStringList * strlist, const gint index)
245 // TODO: Create static method check_index()
246 GString *tmp_str = NULL;
248 g_return_val_if_fail(strlist != NULL, 0);
250 tmp_str = (GString *) g_list_nth_data(strlist->glist, index);
252 if (NULL == tmp_str || NULL == tmp_str->str
253 || strlen(tmp_str->str) <= 0)
256 return (gint) ( /* 0x00000000ffffffffL & (gint64) */
257 g_ascii_strtoull(tmp_str->str, NULL, 10));
260 /** Gets a guint64 value from the string list at the given position.
261 * According to the Mythtv protocol, the 64 bits value is formed by
264 * @param strlist The GMythStringList instance.
265 * @param index the index of the first string forming the 64 bits value.
266 * Index starts with zero.
267 * @return The guint64 value.
270 gmyth_string_list_get_uint64(GMythStringList * strlist, const gint index)
272 // TODO: Create static method check_index()
273 guint64 ret_value = 0;
276 g_return_val_if_fail(strlist != NULL, 0);
278 const GString *tmp_str1 =
279 (GString *) g_list_nth_data(strlist->glist, index);
280 const GString *tmp_str2 =
281 (GString *) g_list_nth_data(strlist->glist, index + 1);
283 if (tmp_str1 != NULL)
284 gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
286 if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
288 gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
292 guint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10) /* &
297 if (tmp_str2 != NULL && tmp_str2->str != NULL
298 && strlen(tmp_str2->str) > 0) {
299 l2 = ((guint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
309 gmyth_debug("[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2);
312 ((guint64) (l2) /* & 0xffffffff */ ) | ((guint64) l1 << 32);
314 gmyth_debug("[%s] returning uint64 value = %llu\n", __FUNCTION__,
320 /** Gets a gint64 value from the string list at the given position.
321 * According to the Mythtv protocol, the 64 bits value is formed by
324 * @param strlist The GMythStringList instance.
325 * @param index the index of the first string forming the 64 bits value.
326 * Index starts with zero.
327 * @return The gint64 value.
330 gmyth_string_list_get_int64(GMythStringList * strlist, const gint index)
332 // TODO: Create static method check_index()
333 gint64 ret_value = 0;
336 g_return_val_if_fail(strlist != NULL, 0);
338 const GString *tmp_str1 =
339 (GString *) g_list_nth_data(strlist->glist, index);
340 const GString *tmp_str2 =
341 (GString *) g_list_nth_data(strlist->glist, index + 1);
343 if (tmp_str1 != NULL)
344 gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
346 if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
348 gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
352 gint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10) /* &
357 if (tmp_str2 != NULL && tmp_str2->str != NULL
358 && strlen(tmp_str2->str) > 0) {
359 l2 = ((gint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
369 gmyth_debug("[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2);
371 ret_value = ((gint64) (l2) /* & 0xffffffff */ ) | ((gint64) l1 << 32);
373 gmyth_debug("[%s] returning int64 value = %lld\n", __FUNCTION__,
380 /** Gets a string from the string list at the given position. The GString must be deallocated.
382 * @param strlist The GMythStringList instance.
383 * @param index the string position in the list, starting with zero.
384 * @return A pointer to the string data.
387 gmyth_string_list_get_string(GMythStringList * strlist, const gint index)
391 if (!strlist || !(strlist->glist)) {
392 gmyth_debug("%s received Null arguments", __FUNCTION__);
396 ret = (GString *) g_list_nth_data(strlist->glist, index);
398 return g_string_new(ret->str);
403 gmyth_string_list_clear_element(GString * str_elem, void *data_aux)
405 if (str_elem != NULL)
406 g_string_free(str_elem, TRUE);
409 /** Removes all strings from the string list.
411 * @param strlist The GMythStringList instance.
414 gmyth_string_list_clear_all(GMythStringList * strlist)
416 if (strlist != NULL && strlist->glist) {
417 g_list_foreach(strlist->glist,
418 (GFunc) gmyth_string_list_clear_element, NULL);
419 g_list_free(strlist->glist);
420 strlist->glist = NULL;
424 /** Retrieves the number of elements in the string list.
426 * @param strlist The GMythStringList instance.
427 * @return the string list length.
430 gmyth_string_list_length(GMythStringList * strlist)
432 if (!(strlist != NULL && strlist->glist != NULL))
435 return g_list_length(strlist->glist);