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