branches/gmyth-0.1b/src/gmyth_stringlist.c
author rosfran
Wed Feb 07 20:38:39 2007 +0000 (2007-02-07)
branchtrunk
changeset 333 f9d778bb88a2
permissions -rw-r--r--
[svn r335] Some fixes to the do_get_file_info.
     1 /**
     2  * GMyth Library
     3  *
     4  * @file gmyth/gmyth_stringlist.c
     5  * 
     6  * @brief <p> This component contains functions for dealing with the stringlist
     7  * format of the mythprotocol.
     8  * 
     9  * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    10  * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
    11  *
    12  *//*
    13  * 
    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.
    18  *
    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.
    23  *
    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
    27  */
    28  
    29 #ifdef HAVE_CONFIG_H
    30 #include "config.h"
    31 #endif
    32 
    33 #include "gmyth_stringlist.h"
    34  
    35 #include "gmyth_debug.h"
    36 
    37 static void gmyth_string_list_class_init          (GMythStringListClass *klass);
    38 static void gmyth_string_list_init                (GMythStringList *object);
    39 
    40 static void gmyth_string_list_dispose  (GObject *object);
    41 static void gmyth_string_list_finalize (GObject *object);
    42 
    43 G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT)
    44     
    45 static void
    46 gmyth_string_list_class_init (GMythStringListClass *klass)
    47 {
    48 	GObjectClass *gobject_class;
    49 
    50     gobject_class = (GObjectClass *) klass;
    51 	
    52     gobject_class->dispose  = gmyth_string_list_dispose;
    53     gobject_class->finalize = gmyth_string_list_finalize;	
    54 }
    55 
    56 static void
    57 gmyth_string_list_init (GMythStringList *gmyth_string_list)
    58 {
    59 	gmyth_string_list->glist = NULL;
    60 }
    61 
    62 static void
    63 gmyth_string_list_dispose  (GObject *object)
    64 {
    65 	GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    66 
    67 	if (gmyth_string_list->glist)
    68 		gmyth_string_list_clear_all(gmyth_string_list);
    69 
    70 	G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object);
    71 }
    72 
    73 static void
    74 gmyth_string_list_finalize (GObject *object)
    75 {
    76 	//GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    77 
    78 	g_signal_handlers_destroy (object);
    79 
    80 	G_OBJECT_CLASS (gmyth_string_list_parent_class)->finalize (object);
    81 }
    82 
    83 /** Creates a new instance of GStringList.
    84  * 
    85  * @return a new instance of GStringList.
    86  */
    87 GMythStringList *
    88 gmyth_string_list_new ()
    89 {
    90     GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL));
    91     return gmyth_string_list;
    92 }
    93 
    94 /** Appends a guint64 to the string list.
    95  * 
    96  * @param strlist The GMythStringList instance.
    97  * @param value The guint64 to be appended.
    98  * 
    99  * @return The appended guint64 converted to a GString object.
   100  */
   101 GString*
   102 gmyth_string_list_append_int ( GMythStringList *strlist, const gint value )
   103 {
   104 	GString *tmp_str = g_string_new ("");
   105 
   106 	g_string_printf (tmp_str, "%d", value);
   107 
   108 	gmyth_string_list_append_string (strlist, tmp_str);
   109 
   110 	return tmp_str;	
   111 }
   112 
   113 /** Appends a guint64 to the string list.
   114  * 
   115  * @param strlist The GMythStringList instance.
   116  * @param value The guint64 to be appended.
   117  * 
   118  * @return The appended guint64 converted to a GString object.
   119  */
   120 GString*
   121 gmyth_string_list_append_uint64 ( GMythStringList *strlist, const guint64 value)
   122 {
   123 	GString *tmp_str1 = g_string_new ("");
   124 	GString *tmp_str2 = g_string_new ("");
   125 	gmyth_debug ( "value = %llu.\n", value);
   126 
   127 	gulong l2 = ( (guint64)value & 0xffffffff  );
   128 	gulong l1 = ( (guint64)value >> 32 );
   129   
   130 	/* high order part of guint64 value */
   131 	g_string_printf (tmp_str1, "%lu", l1);
   132 	
   133 	gmyth_debug( "[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
   134 
   135 	//gmyth_string_list_append_string (strlist, tmp_str1);
   136 	strlist->glist = g_list_append( strlist->glist, tmp_str1 );
   137 
   138  	/* low order part of guint64 value */
   139 	g_string_printf (tmp_str2, "%lu", l2);
   140 
   141 	gmyth_debug( "[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
   142 
   143 	strlist->glist = g_list_append( strlist->glist, tmp_str2 );
   144 
   145 	//gmyth_string_list_append_string (strlist, tmp_str2);
   146 
   147 	return tmp_str2;	
   148 }
   149 
   150 /** Appends a gint64 to the string list.
   151  * 
   152  * @param strlist The GMythStringList instance.
   153  * @param value The gint64 to be appended.
   154  * 
   155  * @return The appended gint64 converted to a GString object.
   156  */
   157 GString*
   158 gmyth_string_list_append_int64 ( GMythStringList *strlist, const gint64 value)
   159 {
   160 	GString *tmp_str1 = g_string_new ("");
   161 	GString *tmp_str2 = g_string_new ("");
   162 	gmyth_debug ( "value = %lld.\n", value );
   163 
   164 	glong l2 = ( (gint64)value & 0xffffffff  );
   165 	glong l1 = ( (gint64)value >> 32 );
   166   
   167 	/* high order part of gint64 value */
   168 	g_string_printf (tmp_str1, "%ld", l1);
   169 	
   170 	gmyth_debug( "[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str );
   171 
   172 	//gmyth_string_list_append_string (strlist, tmp_str1);
   173 	strlist->glist = g_list_append( strlist->glist, tmp_str1 );
   174 
   175  	/* low order part of gint64 value */
   176 	g_string_printf (tmp_str2, "%ld", l2);
   177 
   178 	gmyth_debug( "[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str );
   179 
   180 	strlist->glist = g_list_append( strlist->glist, tmp_str2 );
   181 
   182 	return tmp_str2;
   183 }
   184 
   185 /** Appends a char array to the string list.
   186  * 
   187  * @param strlist The GMythStringList instance.
   188  * @param value The char array to be appended.
   189  * 
   190  * @return The appended char array converted to a GString object.
   191  */
   192 GString*
   193 gmyth_string_list_append_char_array ( GMythStringList *strlist, const gchar* value )
   194 {
   195 	GString *tmp_str = NULL;
   196 	
   197 	g_return_val_if_fail( strlist != NULL, NULL );
   198 	
   199 	tmp_str = g_string_new (value);
   200 
   201 	g_return_val_if_fail( tmp_str != NULL, NULL );
   202 
   203 	gmyth_string_list_append_string (strlist, tmp_str);
   204 
   205 	return tmp_str;		
   206 }
   207 
   208 /** Appends a string to the string list.
   209  * 
   210  * @param strlist The GMythStringList instance.
   211  * @param value The string to be appended.
   212  * 
   213  * @return The appended string itself. 
   214  */
   215 GString*
   216 gmyth_string_list_append_string ( GMythStringList *strlist, GString *value )
   217 {
   218 	g_return_val_if_fail( strlist != NULL, NULL );
   219 
   220 	strlist->glist = g_list_append (strlist->glist, value);
   221 
   222 	return value;
   223 }
   224 
   225 /** Gets an integer value from the string list at the given position.
   226  * 
   227  * @param strlist The GMythStringList instance.
   228  * @param index the integer position in the list, starting with zero.
   229  * @return The integer value.
   230  */
   231 gint
   232 gmyth_string_list_get_int ( GMythStringList *strlist, const gint index )
   233 {
   234 	//TODO: Create static method check_index()
   235 	GString *tmp_str = NULL;
   236 	
   237 	g_return_val_if_fail( strlist != NULL, 0 );
   238 
   239 	tmp_str = (GString *) g_list_nth_data (strlist->glist, index);
   240 
   241 	g_return_val_if_fail( tmp_str != NULL && tmp_str->str != NULL, 0 );
   242 	
   243 	return (gint) ( /* 0x00000000ffffffffL &  (gint64)*/g_ascii_strtoull ( tmp_str->str, NULL, 10 ) );
   244 }
   245 
   246 /** Gets a guint64 value from the string list at the given position.
   247  * According to the Mythtv protocol, the 64 bits value is formed by
   248  * two strings.
   249  * 
   250  * @param strlist The GMythStringList instance.
   251  * @param index the index of the first string forming the 64 bits value. 
   252  * Index starts with zero.
   253  * @return The guint64 value.
   254  */
   255 guint64
   256 gmyth_string_list_get_uint64 ( GMythStringList *strlist, const gint index )
   257 {
   258 	//TODO: Create static method check_index()
   259 	guint64 ret_value = 0;
   260 	guint64 l2 = 0;
   261 
   262 	g_return_val_if_fail( strlist != NULL, 0 );
   263   
   264 	const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
   265 	const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
   266 
   267 	if ( tmp_str1 != NULL )
   268 		gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
   269 	if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
   270 	} else {
   271 		gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
   272 	}
   273 	
   274 	guint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/  );
   275 	if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
   276 		l2 = ( (guint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/  );
   277 	} else {
   278 		l2 = l1;
   279 		l1 = 0;
   280 	}
   281 
   282 	gmyth_debug ( "[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2 );
   283 	
   284 	ret_value = ((guint64)(l2) /*& 0xffffffff*/) | ((guint64)l1 << 32);
   285   
   286 	gmyth_debug( "[%s] returning uint64 value = %llu\n", __FUNCTION__, ret_value );	
   287 	
   288 	return ret_value;
   289 }
   290 
   291 /** Gets a gint64 value from the string list at the given position.
   292  * According to the Mythtv protocol, the 64 bits value is formed by
   293  * two strings.
   294  * 
   295  * @param strlist The GMythStringList instance.
   296  * @param index the index of the first string forming the 64 bits value. 
   297  * Index starts with zero.
   298  * @return The gint64 value.
   299  */
   300 gint64
   301 gmyth_string_list_get_int64 ( GMythStringList *strlist, const gint index )
   302 {
   303 	//TODO: Create static method check_index()
   304 	gint64 ret_value = 0;
   305 	gint64 l2 = 0;
   306 
   307 	g_return_val_if_fail( strlist != NULL, 0 );
   308   
   309 	const GString *tmp_str1 = (GString *) g_list_nth_data (strlist->glist, index);
   310 	const GString *tmp_str2 = (GString *) g_list_nth_data (strlist->glist, index+1);
   311 
   312 	if ( tmp_str1 != NULL )
   313 		gmyth_debug ( "[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str );
   314 	if ( tmp_str2 == NULL || strlen( tmp_str2->str ) > 0 ) {
   315 	} else {
   316 		gmyth_debug ( "[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str );
   317 	}
   318 	
   319 	gint64 l1 = ( (guint64)g_ascii_strtoull (tmp_str1->str, NULL, 10) /*& 0xffffffff*/  );
   320 	if ( tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0 ) {
   321 		l2 = ( (gint64)g_ascii_strtoull (tmp_str2->str, NULL, 10) /*& 0xffffffff*/  );
   322 	} else {
   323 		l2 = l1;
   324 		l1 = 0;
   325 	}
   326 
   327 	gmyth_debug ( "[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2 );
   328 	
   329 	ret_value = ((gint64)(l2) /*& 0xffffffff*/) | ((gint64)l1 << 32);
   330   
   331 	gmyth_debug( "[%s] returning int64 value = %lld\n", __FUNCTION__, ret_value );	
   332 	
   333 	return ret_value;
   334 }
   335 
   336 
   337 /** Gets a string from the string list at the given position.
   338  * 
   339  * @param strlist The GMythStringList instance.
   340  * @param index the string position in the list, starting with zero.
   341  * @return A pointer to the string data.
   342  */
   343 GString*
   344 gmyth_string_list_get_string ( GMythStringList *strlist, const gint index )
   345 {
   346 	if (!strlist || !(strlist->glist)) {
   347 		g_warning ("%s received Null arguments", __FUNCTION__);
   348 		return NULL;
   349 	}
   350 
   351 	return (GString *) g_list_nth_data (strlist->glist, index);
   352 }
   353 
   354 
   355 #if 0
   356 static void
   357 gmyth_string_list_clear_element( GString *str_elem, void *data_aux )
   358 {
   359 	if ( str_elem != NULL ) {
   360 		g_string_free( str_elem, TRUE );
   361 	}
   362 }
   363 #endif
   364 
   365 /** Removes all strings from the string list.
   366  * 
   367  * @param strlist The GMythStringList instance.
   368  */
   369 void
   370 gmyth_string_list_clear_all ( GMythStringList *strlist )
   371 {
   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;
   376 	}
   377 }
   378 
   379 /** Retrieves the number of elements in the string list.
   380  * 
   381  * @param strlist The GMythStringList instance.
   382  * @return the string list length.
   383  */
   384 gint
   385 gmyth_string_list_length ( GMythStringList *strlist )
   386 {
   387 	g_return_val_if_fail( strlist != NULL && strlist->glist != NULL, 0 );
   388 
   389 	return g_list_length (strlist->glist);
   390 }