branches/gmyth-0.1b/src/gmyth_stringlist.c
branchtrunk
changeset 326 81b1f3006eb2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/branches/gmyth-0.1b/src/gmyth_stringlist.c	Tue Feb 06 00:33:35 2007 +0000
     1.3 @@ -0,0 +1,390 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_stringlist.c
     1.8 + * 
     1.9 + * @brief <p> This component contains functions for dealing with the stringlist
    1.10 + * format of the mythprotocol.
    1.11 + * 
    1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.13 + * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
    1.14 + *
    1.15 + *//*
    1.16 + * 
    1.17 + * This program is free software; you can redistribute it and/or modify
    1.18 + * it under the terms of the GNU Lesser General Public License as published by
    1.19 + * the Free Software Foundation; either version 2 of the License, or
    1.20 + * (at your option) any later version.
    1.21 + *
    1.22 + * This program is distributed in the hope that it will be useful,
    1.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.25 + * GNU General Public License for more details.
    1.26 + *
    1.27 + * You should have received a copy of the GNU Lesser General Public License
    1.28 + * along with this program; if not, write to the Free Software
    1.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.30 + */
    1.31 + 
    1.32 +#ifdef HAVE_CONFIG_H
    1.33 +#include "config.h"
    1.34 +#endif
    1.35 +
    1.36 +#include "gmyth_stringlist.h"
    1.37 + 
    1.38 +#include "gmyth_debug.h"
    1.39 +
    1.40 +static void gmyth_string_list_class_init          (GMythStringListClass *klass);
    1.41 +static void gmyth_string_list_init                (GMythStringList *object);
    1.42 +
    1.43 +static void gmyth_string_list_dispose  (GObject *object);
    1.44 +static void gmyth_string_list_finalize (GObject *object);
    1.45 +
    1.46 +G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT)
    1.47 +    
    1.48 +static void
    1.49 +gmyth_string_list_class_init (GMythStringListClass *klass)
    1.50 +{
    1.51 +	GObjectClass *gobject_class;
    1.52 +
    1.53 +    gobject_class = (GObjectClass *) klass;
    1.54 +	
    1.55 +    gobject_class->dispose  = gmyth_string_list_dispose;
    1.56 +    gobject_class->finalize = gmyth_string_list_finalize;	
    1.57 +}
    1.58 +
    1.59 +static void
    1.60 +gmyth_string_list_init (GMythStringList *gmyth_string_list)
    1.61 +{
    1.62 +	gmyth_string_list->glist = NULL;
    1.63 +}
    1.64 +
    1.65 +static void
    1.66 +gmyth_string_list_dispose  (GObject *object)
    1.67 +{
    1.68 +	GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    1.69 +
    1.70 +	if (gmyth_string_list->glist)
    1.71 +		gmyth_string_list_clear_all(gmyth_string_list);
    1.72 +
    1.73 +	G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object);
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +gmyth_string_list_finalize (GObject *object)
    1.78 +{
    1.79 +	//GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    1.80 +
    1.81 +	g_signal_handlers_destroy (object);
    1.82 +
    1.83 +	G_OBJECT_CLASS (gmyth_string_list_parent_class)->finalize (object);
    1.84 +}
    1.85 +
    1.86 +/** Creates a new instance of GStringList.
    1.87 + * 
    1.88 + * @return a new instance of GStringList.
    1.89 + */
    1.90 +GMythStringList *
    1.91 +gmyth_string_list_new ()
    1.92 +{
    1.93 +    GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL));
    1.94 +    return gmyth_string_list;
    1.95 +}
    1.96 +
    1.97 +/** Appends a guint64 to the string list.
    1.98 + * 
    1.99 + * @param strlist The GMythStringList instance.
   1.100 + * @param value The guint64 to be appended.
   1.101 + * 
   1.102 + * @return The appended guint64 converted to a GString object.
   1.103 + */
   1.104 +GString*
   1.105 +gmyth_string_list_append_int ( GMythStringList *strlist, const gint value )
   1.106 +{
   1.107 +	GString *tmp_str = g_string_new ("");
   1.108 +
   1.109 +	g_string_printf (tmp_str, "%d", value);
   1.110 +
   1.111 +	gmyth_string_list_append_string (strlist, tmp_str);
   1.112 +
   1.113 +	return tmp_str;	
   1.114 +}
   1.115 +
   1.116 +/** Appends a guint64 to the string list.
   1.117 + * 
   1.118 + * @param strlist The GMythStringList instance.
   1.119 + * @param value The guint64 to be appended.
   1.120 + * 
   1.121 + * @return The appended guint64 converted to a GString object.
   1.122 + */
   1.123 +GString*
   1.124 +gmyth_string_list_append_uint64 ( GMythStringList *strlist, const guint64 value)
   1.125 +{
   1.126 +	GString *tmp_str1 = g_string_new ("");
   1.127 +	GString *tmp_str2 = g_string_new ("");
   1.128 +	gmyth_debug ( "value = %llu.\n", value);
   1.129 +
   1.130 +	gulong l2 = ( (guint64)value & 0xffffffff  );
   1.131 +	gulong l1 = ( (guint64)value >> 32 );
   1.132 +  
   1.133 +	/* high order part of guint64 value */
   1.134 +	g_string_printf (tmp_str1, "%lu", l1);
   1.135 +	
   1.136 +	gmyth_debug( "[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
   1.137 +
   1.138 +	//gmyth_string_list_append_string (strlist, tmp_str1);
   1.139 +	strlist->glist = g_list_append( strlist->glist, tmp_str1 );
   1.140 +
   1.141 + 	/* low order part of guint64 value */
   1.142 +	g_string_printf (tmp_str2, "%lu", l2);
   1.143 +
   1.144 +	gmyth_debug( "[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
   1.145 +
   1.146 +	strlist->glist = g_list_append( strlist->glist, tmp_str2 );
   1.147 +
   1.148 +	//gmyth_string_list_append_string (strlist, tmp_str2);
   1.149 +
   1.150 +	return tmp_str2;	
   1.151 +}
   1.152 +
   1.153 +/** Appends a gint64 to the string list.
   1.154 + * 
   1.155 + * @param strlist The GMythStringList instance.
   1.156 + * @param value The gint64 to be appended.
   1.157 + * 
   1.158 + * @return The appended gint64 converted to a GString object.
   1.159 + */
   1.160 +GString*
   1.161 +gmyth_string_list_append_int64 ( GMythStringList *strlist, const gint64 value)
   1.162 +{
   1.163 +	GString *tmp_str1 = g_string_new ("");
   1.164 +	GString *tmp_str2 = g_string_new ("");
   1.165 +	gmyth_debug ( "value = %lld.\n", value );
   1.166 +
   1.167 +	glong l2 = ( (gint64)value & 0xffffffff  );
   1.168 +	glong l1 = ( (gint64)value >> 32 );
   1.169 +  
   1.170 +	/* high order part of gint64 value */
   1.171 +	g_string_printf (tmp_str1, "%ld", l1);
   1.172 +	
   1.173 +	gmyth_debug( "[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
   1.174 +
   1.175 +	//gmyth_string_list_append_string (strlist, tmp_str1);
   1.176 +	strlist->glist = g_list_append( strlist->glist, tmp_str1 );
   1.177 +
   1.178 + 	/* low order part of gint64 value */
   1.179 +	g_string_printf (tmp_str2, "%ld", l2);
   1.180 +
   1.181 +	gmyth_debug( "[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
   1.182 +
   1.183 +	strlist->glist = g_list_append( strlist->glist, tmp_str2 );
   1.184 +
   1.185 +	return tmp_str2;
   1.186 +}
   1.187 +
   1.188 +/** Appends a char array to the string list.
   1.189 + * 
   1.190 + * @param strlist The GMythStringList instance.
   1.191 + * @param value The char array to be appended.
   1.192 + * 
   1.193 + * @return The appended char array converted to a GString object.
   1.194 + */
   1.195 +GString*
   1.196 +gmyth_string_list_append_char_array ( GMythStringList *strlist, const gchar* value )
   1.197 +{
   1.198 +	GString *tmp_str = NULL;
   1.199 +	
   1.200 +	g_return_val_if_fail( strlist != NULL, NULL );
   1.201 +	
   1.202 +	tmp_str = g_string_new (value);
   1.203 +
   1.204 +	g_return_val_if_fail( tmp_str != NULL, NULL );
   1.205 +
   1.206 +	gmyth_string_list_append_string (strlist, tmp_str);
   1.207 +
   1.208 +	return tmp_str;		
   1.209 +}
   1.210 +
   1.211 +/** Appends a string to the string list.
   1.212 + * 
   1.213 + * @param strlist The GMythStringList instance.
   1.214 + * @param value The string to be appended.
   1.215 + * 
   1.216 + * @return The appended string itself. 
   1.217 + */
   1.218 +GString*
   1.219 +gmyth_string_list_append_string ( GMythStringList *strlist, GString *value )
   1.220 +{
   1.221 +	g_return_val_if_fail( strlist != NULL, NULL );
   1.222 +
   1.223 +	strlist->glist = g_list_append (strlist->glist, value);
   1.224 +
   1.225 +	return value;
   1.226 +}
   1.227 +
   1.228 +/** Gets an integer value from the string list at the given position.
   1.229 + * 
   1.230 + * @param strlist The GMythStringList instance.
   1.231 + * @param index the integer position in the list, starting with zero.
   1.232 + * @return The integer value.
   1.233 + */
   1.234 +gint
   1.235 +gmyth_string_list_get_int ( GMythStringList *strlist, const gint index )
   1.236 +{
   1.237 +	//TODO: Create static method check_index()
   1.238 +	GString *tmp_str = NULL;
   1.239 +	
   1.240 +	g_return_val_if_fail( strlist != NULL, 0 );
   1.241 +
   1.242 +	tmp_str = (GString *) g_list_nth_data (strlist->glist, index);
   1.243 +
   1.244 +	g_return_val_if_fail( tmp_str != NULL && tmp_str->str != NULL, 0 );
   1.245 +	
   1.246 +	return (gint) ( /* 0x00000000ffffffffL &  (gint64)*/g_ascii_strtoull ( tmp_str->str, NULL, 10 ) );
   1.247 +}
   1.248 +
   1.249 +/** Gets a guint64 value from the string list at the given position.
   1.250 + * According to the Mythtv protocol, the 64 bits value is formed by
   1.251 + * two strings.
   1.252 + * 
   1.253 + * @param strlist The GMythStringList instance.
   1.254 + * @param index the index of the first string forming the 64 bits value. 
   1.255 + * Index starts with zero.
   1.256 + * @return The guint64 value.
   1.257 + */
   1.258 +guint64
   1.259 +gmyth_string_list_get_uint64 ( GMythStringList *strlist, const gint index )
   1.260 +{
   1.261 +	//TODO: Create static method check_index()
   1.262 +	guint64 ret_value = 0;
   1.263 +	guint64 l2 = 0;
   1.264 +
   1.265 +	g_return_val_if_fail( strlist != NULL, 0 );
   1.266 +  
   1.267 +	const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
   1.268 +	const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
   1.269 +
   1.270 +	if ( tmp_str1 != NULL )
   1.271 +		gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
   1.272 +	if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
   1.273 +	} else {
   1.274 +		gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
   1.275 +	}
   1.276 +	
   1.277 +	guint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/  );
   1.278 +	if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
   1.279 +		l2 = ( (guint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/  );
   1.280 +	} else {
   1.281 +		l2 = l1;
   1.282 +		l1 = 0;
   1.283 +	}
   1.284 +
   1.285 +	gmyth_debug ( "[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2 );
   1.286 +	
   1.287 +	ret_value = ((guint64)(l2) /*& 0xffffffff*/) | ((guint64)l1 << 32);
   1.288 +  
   1.289 +	gmyth_debug( "[%s] returning uint64 value = %llu\n", __FUNCTION__, ret_value );	
   1.290 +	
   1.291 +	return ret_value;
   1.292 +}
   1.293 +
   1.294 +/** Gets a gint64 value from the string list at the given position.
   1.295 + * According to the Mythtv protocol, the 64 bits value is formed by
   1.296 + * two strings.
   1.297 + * 
   1.298 + * @param strlist The GMythStringList instance.
   1.299 + * @param index the index of the first string forming the 64 bits value. 
   1.300 + * Index starts with zero.
   1.301 + * @return The gint64 value.
   1.302 + */
   1.303 +gint64
   1.304 +gmyth_string_list_get_int64 ( GMythStringList *strlist, const gint index )
   1.305 +{
   1.306 +	//TODO: Create static method check_index()
   1.307 +	gint64 ret_value = 0;
   1.308 +	gint64 l2 = 0;
   1.309 +
   1.310 +	g_return_val_if_fail( strlist != NULL, 0 );
   1.311 +  
   1.312 +	const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
   1.313 +	const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
   1.314 +
   1.315 +	if ( tmp_str1 != NULL )
   1.316 +		gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
   1.317 +	if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
   1.318 +	} else {
   1.319 +		gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
   1.320 +	}
   1.321 +	
   1.322 +	gint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/  );
   1.323 +	if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
   1.324 +		l2 = ( (gint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/  );
   1.325 +	} else {
   1.326 +		l2 = l1;
   1.327 +		l1 = 0;
   1.328 +	}
   1.329 +
   1.330 +	gmyth_debug ( "[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2 );
   1.331 +	
   1.332 +	ret_value = ((gint64)(l2) /*& 0xffffffff*/) | ((gint64)l1 << 32);
   1.333 +  
   1.334 +	gmyth_debug( "[%s] returning int64 value = %lld\n", __FUNCTION__, ret_value );	
   1.335 +	
   1.336 +	return ret_value;
   1.337 +}
   1.338 +
   1.339 +
   1.340 +/** Gets a string from the string list at the given position.
   1.341 + * 
   1.342 + * @param strlist The GMythStringList instance.
   1.343 + * @param index the string position in the list, starting with zero.
   1.344 + * @return A pointer to the string data.
   1.345 + */
   1.346 +GString*
   1.347 +gmyth_string_list_get_string ( GMythStringList *strlist, const gint index )
   1.348 +{
   1.349 +	if (!strlist || !(strlist->glist)) {
   1.350 +		g_warning ("%s received Null arguments", __FUNCTION__);
   1.351 +		return NULL;
   1.352 +	}
   1.353 +
   1.354 +	return (GString *) g_list_nth_data (strlist->glist, index);
   1.355 +}
   1.356 +
   1.357 +
   1.358 +#if 0
   1.359 +static void
   1.360 +gmyth_string_list_clear_element( GString *str_elem, void *data_aux )
   1.361 +{
   1.362 +	if ( str_elem != NULL ) {
   1.363 +		g_string_free( str_elem, TRUE );
   1.364 +	}
   1.365 +}
   1.366 +#endif
   1.367 +
   1.368 +/** Removes all strings from the string list.
   1.369 + * 
   1.370 + * @param strlist The GMythStringList instance.
   1.371 + */
   1.372 +void
   1.373 +gmyth_string_list_clear_all ( GMythStringList *strlist )
   1.374 +{
   1.375 +	if ( strlist != NULL && strlist->glist ) {
   1.376 +	        //g_list_foreach( strlist->glist, (GFunc)gmyth_string_list_clear_element, NULL );
   1.377 +		g_list_free (strlist->glist);
   1.378 +		strlist->glist = NULL;
   1.379 +	}
   1.380 +}
   1.381 +
   1.382 +/** Retrieves the number of elements in the string list.
   1.383 + * 
   1.384 + * @param strlist The GMythStringList instance.
   1.385 + * @return the string list length.
   1.386 + */
   1.387 +gint
   1.388 +gmyth_string_list_length ( GMythStringList *strlist )
   1.389 +{
   1.390 +	g_return_val_if_fail( strlist != NULL && strlist->glist != NULL, 0 );
   1.391 +
   1.392 +	return g_list_length (strlist->glist);
   1.393 +}