gmyth/src/gmyth_stringlist.c
author renatofilho
Thu Jun 14 18:19:52 2007 +0100 (2007-06-14)
branchtrunk
changeset 750 312d6bc514f3
parent 701 2f28edb4d804
child 754 cb885ee44618
permissions -rw-r--r--
[svn r756] fixed indent using GNU Style
     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  * 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.
    17  *
    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.
    22  *
    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
    26  */
    27 
    28 #ifdef HAVE_CONFIG_H
    29 #include "config.h"
    30 #endif
    31 
    32 #include "gmyth_stringlist.h"
    33 
    34 #include "gmyth_debug.h"
    35 
    36 static void gmyth_string_list_class_init(GMythStringListClass * klass);
    37 static void gmyth_string_list_init(GMythStringList * object);
    38 
    39 static void gmyth_string_list_dispose(GObject * object);
    40 static void gmyth_string_list_finalize(GObject * object);
    41 
    42 G_DEFINE_TYPE(GMythStringList, gmyth_string_list, G_TYPE_OBJECT)
    43 	 static void gmyth_string_list_class_init(GMythStringListClass * klass)
    44 {
    45   GObjectClass *gobject_class;
    46 
    47   gobject_class = (GObjectClass *) klass;
    48 
    49   gobject_class->dispose = gmyth_string_list_dispose;
    50   gobject_class->finalize = gmyth_string_list_finalize;
    51 }
    52 
    53 static void
    54 gmyth_string_list_init(GMythStringList * gmyth_string_list)
    55 {
    56   gmyth_string_list->glist = NULL;
    57 }
    58 
    59 static void
    60 gmyth_string_list_dispose(GObject * object)
    61 {
    62   GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    63 
    64   gmyth_string_list_clear_all(gmyth_string_list);
    65 
    66   G_OBJECT_CLASS(gmyth_string_list_parent_class)->dispose(object);
    67 }
    68 
    69 static void
    70 gmyth_string_list_finalize(GObject * object)
    71 {
    72  //GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    73 
    74   g_signal_handlers_destroy(object);
    75 
    76   G_OBJECT_CLASS(gmyth_string_list_parent_class)->finalize(object);
    77 }
    78 
    79 /** Creates a new instance of GStringList.
    80  * 
    81  * @return a new instance of GStringList.
    82  */
    83 GMythStringList *
    84 gmyth_string_list_new()
    85 {
    86   GMythStringList *gmyth_string_list =
    87 	GMYTH_STRING_LIST(g_object_new(GMYTH_STRING_LIST_TYPE, NULL));
    88 
    89   return gmyth_string_list;
    90 }
    91 
    92 /** Appends a guint64 to the string list.
    93  * 
    94  * @param strlist The GMythStringList instance.
    95  * @param value The guint64 to be appended.
    96  * 
    97  * @return The appended guint64 converted to a GString object.
    98  */
    99 GString *
   100 gmyth_string_list_append_int(GMythStringList * strlist, const gint value)
   101 {
   102   GString *value_str = g_string_new("");
   103 
   104   g_string_printf(value_str, "%d", value);
   105 
   106   strlist->glist = g_list_append(strlist->glist, value_str);
   107 
   108   return value_str;
   109 }
   110 
   111 /** Appends a guint64 to the string list.
   112  * 
   113  * @param strlist The GMythStringList instance.
   114  * @param value The guint64 to be appended.
   115  * 
   116  * @return The appended guint64 converted to a GString object.
   117  */
   118 GString *
   119 gmyth_string_list_append_uint64(GMythStringList * strlist,
   120 								const guint64 value)
   121 {
   122   GString *tmp_str1 = g_string_new("");
   123   GString *tmp_str2 = g_string_new("");
   124 
   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   strlist->glist = g_list_append(strlist->glist, tmp_str1);
   136 
   137  /* low order part of guint64 value */
   138   g_string_printf(tmp_str2, "%lu", l2);
   139 
   140   gmyth_debug("[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
   141 
   142   strlist->glist = g_list_append(strlist->glist, tmp_str2);
   143 
   144   return tmp_str2;
   145 }
   146 
   147 /** Appends a gint64 to the string list.
   148  * 
   149  * @param strlist The GMythStringList instance.
   150  * @param value The gint64 to be appended.
   151  * 
   152  * @return The appended gint64 converted to a GString object.
   153  */
   154 GString *
   155 gmyth_string_list_append_int64(GMythStringList * strlist, const gint64 value)
   156 {
   157   GString *tmp_str1 = g_string_new("");
   158   GString *tmp_str2 = g_string_new("");
   159 
   160   gmyth_debug("value = %lld.\n", value);
   161 
   162   glong l2 = ((gint64) value & 0xffffffff);
   163   glong l1 = ((gint64) value >> 32);
   164 
   165  /* high order part of gint64 value */
   166   g_string_printf(tmp_str1, "%ld", l1);
   167 
   168   gmyth_debug("[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
   169 
   170   strlist->glist = g_list_append(strlist->glist, tmp_str1);
   171 
   172  /* low order part of gint64 value */
   173   g_string_printf(tmp_str2, "%ld", l2);
   174 
   175   gmyth_debug("[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
   176 
   177   strlist->glist = g_list_append(strlist->glist, tmp_str2);
   178 
   179   return tmp_str2;
   180 }
   181 
   182 /** Appends a char array to the string list.
   183  * 
   184  * @param strlist The GMythStringList instance.
   185  * @param value The char array to be appended.
   186  * 
   187  * @return The appended char array converted to a GString object.
   188  */
   189 GString *
   190 gmyth_string_list_append_char_array(GMythStringList * strlist,
   191 									const gchar * value)
   192 {
   193   GString *tmp_str = NULL;
   194 
   195   g_return_val_if_fail(strlist != NULL, NULL);
   196 
   197   tmp_str = g_string_new(value);
   198 
   199   strlist->glist = g_list_append(strlist->glist, tmp_str);
   200 
   201   return tmp_str;
   202 }
   203 
   204 /** Appends a string to the string list.
   205  * 
   206  * @param strlist The GMythStringList instance.
   207  * @param value The string to be appended.
   208  * 
   209  * @return The appended string itself. 
   210  */
   211 GString *
   212 gmyth_string_list_append_string(GMythStringList * strlist, GString * value)
   213 {
   214   g_return_val_if_fail(strlist != NULL, NULL);
   215 
   216   if (value != NULL)
   217 	{
   218 	  strlist->glist =
   219 		g_list_append(strlist->glist, g_string_new(value->str));
   220 	}
   221   else
   222 	{
   223 	  strlist->glist = g_list_append(strlist->glist, NULL);
   224 	}
   225 
   226   return value;
   227 }
   228 
   229 /** Gets an integer value from the string list at the given position.
   230  * 
   231  * @param strlist The GMythStringList instance.
   232  * @param index the integer position in the list, starting with zero.
   233  * @return The integer value.
   234  */
   235 gint
   236 gmyth_string_list_get_int(GMythStringList * strlist, const gint index)
   237 {
   238  //TODO: Create static method check_index()
   239   GString *tmp_str = NULL;
   240 
   241   g_return_val_if_fail(strlist != NULL, 0);
   242 
   243   tmp_str = (GString *) g_list_nth_data(strlist->glist, index);
   244 
   245   if (NULL == tmp_str || NULL == tmp_str->str || strlen(tmp_str->str) <= 0)
   246 	return 0;
   247 
   248   return (gint) (				/* 0x00000000ffffffffL &  (gint64) */
   249 				  g_ascii_strtoull(tmp_str->str, NULL, 10));
   250 }
   251 
   252 /** Gets a guint64 value from the string list at the given position.
   253  * According to the Mythtv protocol, the 64 bits value is formed by
   254  * two strings.
   255  * 
   256  * @param strlist The GMythStringList instance.
   257  * @param index the index of the first string forming the 64 bits value. 
   258  * Index starts with zero.
   259  * @return The guint64 value.
   260  */
   261 guint64
   262 gmyth_string_list_get_uint64(GMythStringList * strlist, const gint index)
   263 {
   264  //TODO: Create static method check_index()
   265   guint64 ret_value = 0;
   266   guint64 l2 = 0;
   267 
   268   g_return_val_if_fail(strlist != NULL, 0);
   269 
   270   const GString *tmp_str1 =
   271 	(GString *) g_list_nth_data(strlist->glist, index);
   272   const GString *tmp_str2 =
   273 	(GString *) g_list_nth_data(strlist->glist, index + 1);
   274 
   275   if (tmp_str1 != NULL)
   276 	gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str);
   277   if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0)
   278 	{
   279 	}
   280   else
   281 	{
   282 	  gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str);
   283 	}
   284 
   285   guint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10)	/*& 0xffffffff */
   286 	);
   287 
   288   if (tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0)
   289 	{
   290 	  l2 = ((guint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
   291 		   /*& 0xffffffff */
   292 		);
   293 	}
   294   else
   295 	{
   296 	  l2 = l1;
   297 	  l1 = 0;
   298 	}
   299 
   300   gmyth_debug("[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2);
   301 
   302   ret_value = ((guint64) (l2) /*& 0xffffffff */ ) | ((guint64) l1 << 32);
   303 
   304   gmyth_debug("[%s] returning uint64 value = %llu\n", __FUNCTION__,
   305 			  ret_value);
   306 
   307   return ret_value;
   308 }
   309 
   310 /** Gets a gint64 value from the string list at the given position.
   311  * According to the Mythtv protocol, the 64 bits value is formed by
   312  * two strings.
   313  * 
   314  * @param strlist The GMythStringList instance.
   315  * @param index the index of the first string forming the 64 bits value. 
   316  * Index starts with zero.
   317  * @return The gint64 value.
   318  */
   319 gint64
   320 gmyth_string_list_get_int64(GMythStringList * strlist, const gint index)
   321 {
   322  //TODO: Create static method check_index()
   323   gint64 ret_value = 0;
   324   gint64 l2 = 0;
   325 
   326   g_return_val_if_fail(strlist != NULL, 0);
   327 
   328   const GString *tmp_str1 =
   329 	(GString *) g_list_nth_data(strlist->glist, index);
   330   const GString *tmp_str2 =
   331 	(GString *) g_list_nth_data(strlist->glist, index + 1);
   332 
   333   if (tmp_str1 != NULL)
   334 	gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__, tmp_str1->str);
   335   if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0)
   336 	{
   337 	}
   338   else
   339 	{
   340 	  gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str);
   341 	}
   342 
   343   gint64 l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10)	/*& 0xffffffff */
   344 	);
   345 
   346   if (tmp_str2 != NULL && tmp_str2->str != NULL && strlen(tmp_str2->str) > 0)
   347 	{
   348 	  l2 = ((gint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
   349 		   /*& 0xffffffff */
   350 		);
   351 	}
   352   else
   353 	{
   354 	  l2 = l1;
   355 	  l1 = 0;
   356 	}
   357 
   358   gmyth_debug("[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2);
   359 
   360   ret_value = ((gint64) (l2) /*& 0xffffffff */ ) | ((gint64) l1 << 32);
   361 
   362   gmyth_debug("[%s] returning int64 value = %lld\n", __FUNCTION__, ret_value);
   363 
   364   return ret_value;
   365 }
   366 
   367 
   368 /** Gets a string from the string list at the given position. The GString must be deallocated.
   369  * 
   370  * @param strlist The GMythStringList instance.
   371  * @param index the string position in the list, starting with zero.
   372  * @return A pointer to the string data.
   373  */
   374 GString *
   375 gmyth_string_list_get_string(GMythStringList * strlist, const gint index)
   376 {
   377   GString *ret;
   378 
   379   if (!strlist || !(strlist->glist))
   380 	{
   381 	  gmyth_debug("%s received Null arguments", __FUNCTION__);
   382 	  return NULL;
   383 	}
   384 
   385   ret = (GString *) g_list_nth_data(strlist->glist, index);
   386 
   387   return g_string_new(ret->str);
   388 }
   389 
   390 
   391 static void
   392 gmyth_string_list_clear_element(GString * str_elem, void *data_aux)
   393 {
   394   if (str_elem != NULL)
   395 	g_string_free(str_elem, TRUE);
   396 }
   397 
   398 /** Removes all strings from the string list.
   399  * 
   400  * @param strlist The GMythStringList instance.
   401  */
   402 void
   403 gmyth_string_list_clear_all(GMythStringList * strlist)
   404 {
   405   if (strlist != NULL && strlist->glist)
   406 	{
   407 	  g_list_foreach(strlist->glist,
   408 					 (GFunc) gmyth_string_list_clear_element, NULL);
   409 	  g_list_free(strlist->glist);
   410 	  strlist->glist = NULL;
   411 	}
   412 }
   413 
   414 /** Retrieves the number of elements in the string list.
   415  * 
   416  * @param strlist The GMythStringList instance.
   417  * @return the string list length.
   418  */
   419 gint
   420 gmyth_string_list_length(GMythStringList * strlist)
   421 {
   422   if (!(strlist != NULL && strlist->glist != NULL))
   423 	return 0;
   424 
   425   return g_list_length(strlist->glist);
   426 }