leo_sobral@1: /** leo_sobral@1: * GMyth Library leo_sobral@1: * leo_sobral@1: * @file gmyth/gmyth_stringlist.c leo_sobral@1: * leo_sobral@1: * @brief

This component contains functions for dealing with the stringlist leo_sobral@1: * format of the mythprotocol. leo_sobral@1: * leo_sobral@1: * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. leo_sobral@1: * @author Hallyson Luiz de Morais Melo leo_sobral@1: * leo_sobral@1: *//* leo_sobral@1: * leo_sobral@1: * This program is free software; you can redistribute it and/or modify leo_sobral@1: * it under the terms of the GNU Lesser General Public License as published by leo_sobral@1: * the Free Software Foundation; either version 2 of the License, or leo_sobral@1: * (at your option) any later version. leo_sobral@1: * leo_sobral@1: * This program is distributed in the hope that it will be useful, leo_sobral@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of leo_sobral@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the leo_sobral@1: * GNU General Public License for more details. leo_sobral@1: * leo_sobral@1: * You should have received a copy of the GNU Lesser General Public License leo_sobral@1: * along with this program; if not, write to the Free Software leo_sobral@1: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA leo_sobral@1: */ leo_sobral@213: leo_sobral@213: #ifdef HAVE_CONFIG_H leo_sobral@213: #include "config.h" leo_sobral@213: #endif leo_sobral@1: leo_sobral@1: #include "gmyth_stringlist.h" leo_sobral@213: renatofilho@131: #include "gmyth_debug.h" leo_sobral@1: leo_sobral@1: static void gmyth_string_list_class_init (GMythStringListClass *klass); leo_sobral@1: static void gmyth_string_list_init (GMythStringList *object); leo_sobral@1: leo_sobral@1: static void gmyth_string_list_dispose (GObject *object); leo_sobral@1: static void gmyth_string_list_finalize (GObject *object); leo_sobral@1: leo_sobral@1: G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT) leo_sobral@1: leo_sobral@1: static void leo_sobral@1: gmyth_string_list_class_init (GMythStringListClass *klass) leo_sobral@1: { leo_sobral@1: GObjectClass *gobject_class; leo_sobral@1: leo_sobral@1: gobject_class = (GObjectClass *) klass; leo_sobral@1: leo_sobral@1: gobject_class->dispose = gmyth_string_list_dispose; leo_sobral@1: gobject_class->finalize = gmyth_string_list_finalize; leo_sobral@1: } leo_sobral@1: leo_sobral@1: static void leo_sobral@1: gmyth_string_list_init (GMythStringList *gmyth_string_list) leo_sobral@1: { leo_sobral@1: gmyth_string_list->glist = NULL; leo_sobral@1: } leo_sobral@1: leo_sobral@1: static void leo_sobral@1: gmyth_string_list_dispose (GObject *object) leo_sobral@1: { leo_sobral@1: GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object); leo_sobral@1: leo_sobral@1: if (gmyth_string_list->glist) leo_sobral@1: gmyth_string_list_clear_all(gmyth_string_list); leo_sobral@1: leo_sobral@1: G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object); leo_sobral@1: } leo_sobral@1: leo_sobral@1: static void leo_sobral@1: gmyth_string_list_finalize (GObject *object) leo_sobral@1: { leo_sobral@1: //GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object); leo_sobral@1: leo_sobral@1: g_signal_handlers_destroy (object); leo_sobral@1: leo_sobral@1: G_OBJECT_CLASS (gmyth_string_list_parent_class)->finalize (object); leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Creates a new instance of GStringList. leo_sobral@1: * leo_sobral@1: * @return a new instance of GStringList. leo_sobral@1: */ leo_sobral@1: GMythStringList * leo_sobral@1: gmyth_string_list_new () leo_sobral@1: { rosfran@29: GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL)); leo_sobral@1: return gmyth_string_list; leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Appends a guint64 to the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param value The guint64 to be appended. leo_sobral@1: * leo_sobral@1: * @return The appended guint64 converted to a GString object. leo_sobral@1: */ leo_sobral@1: GString* leo_sobral@1: gmyth_string_list_append_int ( GMythStringList *strlist, const gint value ) leo_sobral@1: { leo_sobral@1: GString *tmp_str = g_string_new (""); leo_sobral@1: leo_sobral@1: g_string_printf (tmp_str, "%d", value); leo_sobral@1: leo_sobral@1: gmyth_string_list_append_string (strlist, tmp_str); leo_sobral@1: leo_sobral@1: return tmp_str; leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Appends a guint64 to the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param value The guint64 to be appended. leo_sobral@1: * leo_sobral@1: * @return The appended guint64 converted to a GString object. leo_sobral@1: */ leo_sobral@1: GString* leo_sobral@1: gmyth_string_list_append_uint64 ( GMythStringList *strlist, const guint64 value) leo_sobral@1: { rosfran@29: GString *tmp_str1 = g_string_new (""); rosfran@29: GString *tmp_str2 = g_string_new (""); renatofilho@319: gmyth_debug ( "value = %llu.\n", value); leo_sobral@1: rosfran@33: gulong l2 = ( (guint64)value & 0xffffffff ); rosfran@33: gulong l1 = ( (guint64)value >> 32 ); leo_sobral@1: leo_sobral@1: /* high order part of guint64 value */ rosfran@33: g_string_printf (tmp_str1, "%lu", l1); leo_sobral@1: renatofilho@131: gmyth_debug( "[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str ); leo_sobral@1: rosfran@29: //gmyth_string_list_append_string (strlist, tmp_str1); rosfran@29: strlist->glist = g_list_append( strlist->glist, tmp_str1 ); leo_sobral@1: leo_sobral@1: /* low order part of guint64 value */ rosfran@33: g_string_printf (tmp_str2, "%lu", l2); leo_sobral@1: renatofilho@131: gmyth_debug( "[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str ); leo_sobral@1: rosfran@29: strlist->glist = g_list_append( strlist->glist, tmp_str2 ); leo_sobral@1: rosfran@29: //gmyth_string_list_append_string (strlist, tmp_str2); rosfran@29: rosfran@29: return tmp_str2; leo_sobral@1: } leo_sobral@1: rosfran@35: /** Appends a gint64 to the string list. rosfran@35: * rosfran@35: * @param strlist The GMythStringList instance. rosfran@35: * @param value The gint64 to be appended. rosfran@35: * rosfran@35: * @return The appended gint64 converted to a GString object. rosfran@35: */ rosfran@35: GString* rosfran@35: gmyth_string_list_append_int64 ( GMythStringList *strlist, const gint64 value) rosfran@35: { rosfran@35: GString *tmp_str1 = g_string_new (""); rosfran@35: GString *tmp_str2 = g_string_new (""); renatofilho@319: gmyth_debug ( "value = %lld.\n", value ); rosfran@35: rosfran@35: glong l2 = ( (gint64)value & 0xffffffff ); rosfran@35: glong l1 = ( (gint64)value >> 32 ); rosfran@35: rosfran@35: /* high order part of gint64 value */ rosfran@35: g_string_printf (tmp_str1, "%ld", l1); rosfran@35: renatofilho@131: gmyth_debug( "[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str ); rosfran@35: rosfran@35: //gmyth_string_list_append_string (strlist, tmp_str1); rosfran@35: strlist->glist = g_list_append( strlist->glist, tmp_str1 ); rosfran@35: rosfran@35: /* low order part of gint64 value */ rosfran@35: g_string_printf (tmp_str2, "%ld", l2); rosfran@35: renatofilho@131: gmyth_debug( "[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str ); rosfran@35: rosfran@35: strlist->glist = g_list_append( strlist->glist, tmp_str2 ); rosfran@35: rosfran@35: return tmp_str2; rosfran@35: } rosfran@35: leo_sobral@1: /** Appends a char array to the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param value The char array to be appended. leo_sobral@1: * leo_sobral@1: * @return The appended char array converted to a GString object. leo_sobral@1: */ leo_sobral@1: GString* leo_sobral@1: gmyth_string_list_append_char_array ( GMythStringList *strlist, const gchar* value ) leo_sobral@1: { leo_sobral@1: GString *tmp_str = NULL; leo_sobral@1: leo_sobral@1: g_return_val_if_fail( strlist != NULL, NULL ); leo_sobral@1: leo_sobral@1: tmp_str = g_string_new (value); leo_sobral@1: leo_sobral@1: g_return_val_if_fail( tmp_str != NULL, NULL ); leo_sobral@1: leo_sobral@1: gmyth_string_list_append_string (strlist, tmp_str); leo_sobral@1: leo_sobral@1: return tmp_str; leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Appends a string to the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param value The string to be appended. leo_sobral@1: * leo_sobral@1: * @return The appended string itself. leo_sobral@1: */ leo_sobral@1: GString* leo_sobral@1: gmyth_string_list_append_string ( GMythStringList *strlist, GString *value ) leo_sobral@1: { leo_sobral@1: g_return_val_if_fail( strlist != NULL, NULL ); leo_sobral@1: leo_sobral@1: strlist->glist = g_list_append (strlist->glist, value); leo_sobral@1: leo_sobral@1: return value; leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Gets an integer value from the string list at the given position. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param index the integer position in the list, starting with zero. leo_sobral@1: * @return The integer value. leo_sobral@1: */ leo_sobral@1: gint leo_sobral@1: gmyth_string_list_get_int ( GMythStringList *strlist, const gint index ) leo_sobral@1: { leo_sobral@1: //TODO: Create static method check_index() leo_sobral@1: GString *tmp_str = NULL; leo_sobral@1: leo_sobral@1: g_return_val_if_fail( strlist != NULL, 0 ); leo_sobral@1: leo_sobral@1: tmp_str = (GString *) g_list_nth_data (strlist->glist, index); leo_sobral@1: leo_sobral@1: g_return_val_if_fail( tmp_str != NULL && tmp_str->str != NULL, 0 ); leo_sobral@1: rosfran@35: return (gint) ( /* 0x00000000ffffffffL & (gint64)*/g_ascii_strtoull ( tmp_str->str, NULL, 10 ) ); leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Gets a guint64 value from the string list at the given position. leo_sobral@1: * According to the Mythtv protocol, the 64 bits value is formed by leo_sobral@1: * two strings. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param index the index of the first string forming the 64 bits value. leo_sobral@1: * Index starts with zero. leo_sobral@1: * @return The guint64 value. leo_sobral@1: */ leo_sobral@1: guint64 leo_sobral@1: gmyth_string_list_get_uint64 ( GMythStringList *strlist, const gint index ) leo_sobral@1: { leo_sobral@1: //TODO: Create static method check_index() leo_sobral@1: guint64 ret_value = 0; rosfran@33: guint64 l2 = 0; leo_sobral@1: leo_sobral@1: g_return_val_if_fail( strlist != NULL, 0 ); leo_sobral@1: leo_sobral@1: const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index); leo_sobral@1: const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1); rosfran@33: rosfran@33: if ( tmp_str1 != NULL ) renatofilho@319: gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str ); rosfran@33: if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) { rosfran@33: } else { renatofilho@319: gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str ); rosfran@33: } leo_sobral@1: rosfran@35: guint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/ ); rosfran@33: if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) { rosfran@35: l2 = ( (guint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/ ); rosfran@33: } else { rosfran@33: l2 = l1; rosfran@33: l1 = 0; rosfran@33: } rosfran@29: renatofilho@319: gmyth_debug ( "[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2 ); leo_sobral@1: rosfran@29: ret_value = ((guint64)(l2) /*& 0xffffffff*/) | ((guint64)l1 << 32); leo_sobral@1: renatofilho@131: gmyth_debug( "[%s] returning uint64 value = %llu\n", __FUNCTION__, ret_value ); leo_sobral@1: leo_sobral@1: return ret_value; leo_sobral@1: } leo_sobral@1: rosfran@35: /** Gets a gint64 value from the string list at the given position. rosfran@35: * According to the Mythtv protocol, the 64 bits value is formed by rosfran@35: * two strings. rosfran@35: * rosfran@35: * @param strlist The GMythStringList instance. rosfran@35: * @param index the index of the first string forming the 64 bits value. rosfran@35: * Index starts with zero. rosfran@35: * @return The gint64 value. rosfran@35: */ rosfran@35: gint64 rosfran@35: gmyth_string_list_get_int64 ( GMythStringList *strlist, const gint index ) rosfran@35: { rosfran@35: //TODO: Create static method check_index() rosfran@35: gint64 ret_value = 0; rosfran@35: gint64 l2 = 0; rosfran@35: rosfran@35: g_return_val_if_fail( strlist != NULL, 0 ); rosfran@35: rosfran@35: const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index); rosfran@35: const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1); rosfran@35: rosfran@35: if ( tmp_str1 != NULL ) renatofilho@319: gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str ); rosfran@35: if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) { rosfran@35: } else { renatofilho@319: gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str ); rosfran@35: } rosfran@35: rosfran@35: gint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/ ); rosfran@35: if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) { rosfran@35: l2 = ( (gint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/ ); rosfran@35: } else { rosfran@35: l2 = l1; rosfran@35: l1 = 0; rosfran@35: } rosfran@35: renatofilho@319: gmyth_debug ( "[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2 ); rosfran@35: rosfran@35: ret_value = ((gint64)(l2) /*& 0xffffffff*/) | ((gint64)l1 << 32); rosfran@35: renatofilho@131: gmyth_debug( "[%s] returning int64 value = %lld\n", __FUNCTION__, ret_value ); rosfran@35: rosfran@35: return ret_value; rosfran@35: } rosfran@35: rosfran@35: leo_sobral@1: /** Gets a string from the string list at the given position. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @param index the string position in the list, starting with zero. leo_sobral@1: * @return A pointer to the string data. leo_sobral@1: */ leo_sobral@1: GString* leo_sobral@1: gmyth_string_list_get_string ( GMythStringList *strlist, const gint index ) leo_sobral@1: { leo_sobral@1: if (!strlist || !(strlist->glist)) { leo_sobral@1: g_warning ("%s received Null arguments", __FUNCTION__); leo_sobral@1: return NULL; leo_sobral@1: } leo_sobral@1: leo_sobral@1: return (GString *) g_list_nth_data (strlist->glist, index); leo_sobral@1: } leo_sobral@1: rosfran@24: rosfran@29: #if 0 rosfran@24: static void rosfran@24: gmyth_string_list_clear_element( GString *str_elem, void *data_aux ) rosfran@24: { rosfran@24: if ( str_elem != NULL ) { rosfran@29: g_string_free( str_elem, TRUE ); rosfran@24: } rosfran@24: } rosfran@29: #endif rosfran@24: leo_sobral@1: /** Removes all strings from the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: */ leo_sobral@1: void leo_sobral@1: gmyth_string_list_clear_all ( GMythStringList *strlist ) leo_sobral@1: { leo_sobral@1: if ( strlist != NULL && strlist->glist ) { rosfran@29: //g_list_foreach( strlist->glist, (GFunc)gmyth_string_list_clear_element, NULL ); leo_sobral@1: g_list_free (strlist->glist); leo_sobral@1: strlist->glist = NULL; leo_sobral@1: } leo_sobral@1: } leo_sobral@1: leo_sobral@1: /** Retrieves the number of elements in the string list. leo_sobral@1: * leo_sobral@1: * @param strlist The GMythStringList instance. leo_sobral@1: * @return the string list length. leo_sobral@1: */ leo_sobral@1: gint leo_sobral@1: gmyth_string_list_length ( GMythStringList *strlist ) leo_sobral@1: { leo_sobral@1: g_return_val_if_fail( strlist != NULL && strlist->glist != NULL, 0 ); leo_sobral@1: leo_sobral@1: return g_list_length (strlist->glist); leo_sobral@1: }