gmyth/src/gmyth_stringlist.c
author rosfran
Wed May 23 16:11:29 2007 +0100 (2007-05-23)
branchtrunk
changeset 698 9019388af980
parent 467 bc13aaf7561d
child 701 2f28edb4d804
permissions -rw-r--r--
[svn r704] Added gmyth-upnp-search, to search for MythTV UPnP devices.
     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      static void gmyth_string_list_class_init (GMythStringListClass * klass)
    45 {
    46     GObjectClass *gobject_class;
    47 
    48     gobject_class = (GObjectClass *) klass;
    49 
    50     gobject_class->dispose = gmyth_string_list_dispose;
    51     gobject_class->finalize = gmyth_string_list_finalize;
    52 }
    53 
    54 static void
    55 gmyth_string_list_init (GMythStringList * gmyth_string_list)
    56 {
    57     gmyth_string_list->glist = NULL;
    58 }
    59 
    60 static void
    61 gmyth_string_list_dispose (GObject * object)
    62 {
    63     GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (object);
    64 
    65     gmyth_string_list_clear_all (gmyth_string_list);
    66 
    67     G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object);
    68 }
    69 
    70 static void
    71 gmyth_string_list_finalize (GObject * object)
    72 {
    73     //GMythStringList *gmyth_string_list = GMYTH_STRING_LIST(object);
    74 
    75     g_signal_handlers_destroy (object);
    76 
    77     G_OBJECT_CLASS (gmyth_string_list_parent_class)->finalize (object);
    78 }
    79 
    80 /** Creates a new instance of GStringList.
    81  * 
    82  * @return a new instance of GStringList.
    83  */
    84 GMythStringList *
    85 gmyth_string_list_new ()
    86 {
    87     GMythStringList *gmyth_string_list =
    88         GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL));
    89 
    90     return gmyth_string_list;
    91 }
    92 
    93 /** Appends a guint64 to the string list.
    94  * 
    95  * @param strlist The GMythStringList instance.
    96  * @param value The guint64 to be appended.
    97  * 
    98  * @return The appended guint64 converted to a GString object.
    99  */
   100 GString *
   101 gmyth_string_list_append_int (GMythStringList * strlist, const gint value)
   102 {
   103     GString *value_str = g_string_new ("");
   104 
   105     g_string_printf (value_str, "%d", value);
   106 
   107     strlist->glist = g_list_append (strlist->glist, value_str);
   108 
   109     return value_str;
   110 }
   111 
   112 /** Appends a guint64 to the string list.
   113  * 
   114  * @param strlist The GMythStringList instance.
   115  * @param value The guint64 to be appended.
   116  * 
   117  * @return The appended guint64 converted to a GString object.
   118  */
   119 GString *
   120 gmyth_string_list_append_uint64 (GMythStringList * strlist, 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         strlist->glist =
   218             g_list_append (strlist->glist, g_string_new (value->str));
   219     } else {
   220         strlist->glist = g_list_append (strlist->glist, NULL);
   221     }
   222 
   223     return value;
   224 }
   225 
   226 /** Gets an integer value from the string list at the given position.
   227  * 
   228  * @param strlist The GMythStringList instance.
   229  * @param index the integer position in the list, starting with zero.
   230  * @return The integer value.
   231  */
   232 gint
   233 gmyth_string_list_get_int (GMythStringList * strlist, const gint index)
   234 {
   235     //TODO: Create static method check_index()
   236     GString *tmp_str = NULL;
   237 
   238     g_return_val_if_fail (strlist != NULL, 0);
   239 
   240     tmp_str = (GString *) g_list_nth_data (strlist->glist, index);
   241 
   242     if (NULL == tmp_str || NULL == tmp_str->str || strlen (tmp_str->str) <= 0)
   243         return 0;
   244 
   245     return (gint) (             /* 0x00000000ffffffffL &  (gint64) */
   246         g_ascii_strtoull (tmp_str->str, NULL, 10));
   247 }
   248 
   249 /** Gets a guint64 value from the string list at the given position.
   250  * According to the Mythtv protocol, the 64 bits value is formed by
   251  * two strings.
   252  * 
   253  * @param strlist The GMythStringList instance.
   254  * @param index the index of the first string forming the 64 bits value. 
   255  * Index starts with zero.
   256  * @return The guint64 value.
   257  */
   258 guint64
   259 gmyth_string_list_get_uint64 (GMythStringList * strlist, const gint index)
   260 {
   261     //TODO: Create static method check_index()
   262     guint64 ret_value = 0;
   263     guint64 l2 = 0;
   264 
   265     g_return_val_if_fail (strlist != NULL, 0);
   266 
   267     const GString *tmp_str1 =
   268         (GString *) g_list_nth_data (strlist->glist, index);
   269     const GString *tmp_str2 =
   270         (GString *) g_list_nth_data (strlist->glist, index + 1);
   271 
   272     if (tmp_str1 != NULL)
   273         gmyth_debug ("[%s] seek high bytes = %s\n", __FUNCTION__,
   274             tmp_str1->str);
   275     if (tmp_str2 == NULL || strlen (tmp_str2->str) > 0) {
   276     } else {
   277         gmyth_debug ("[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str);
   278     }
   279 
   280     guint64 l1 = ((guint64) g_ascii_strtoull (tmp_str1->str, NULL, 10)  /*& 0xffffffff */
   281         );
   282 
   283     if (tmp_str2 != NULL && tmp_str2->str != NULL && strlen (tmp_str2->str) > 0) {
   284         l2 = ((guint64) g_ascii_strtoull (tmp_str2->str, NULL, 10)
   285             /*& 0xffffffff */
   286             );
   287     } else {
   288         l2 = l1;
   289         l1 = 0;
   290     }
   291 
   292     gmyth_debug ("[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2);
   293 
   294     ret_value = ((guint64) (l2) /*& 0xffffffff */ ) | ((guint64) l1 << 32);
   295 
   296     gmyth_debug ("[%s] returning uint64 value = %llu\n", __FUNCTION__,
   297         ret_value);
   298 
   299     return ret_value;
   300 }
   301 
   302 /** Gets a gint64 value from the string list at the given position.
   303  * According to the Mythtv protocol, the 64 bits value is formed by
   304  * two strings.
   305  * 
   306  * @param strlist The GMythStringList instance.
   307  * @param index the index of the first string forming the 64 bits value. 
   308  * Index starts with zero.
   309  * @return The gint64 value.
   310  */
   311 gint64
   312 gmyth_string_list_get_int64 (GMythStringList * strlist, const gint index)
   313 {
   314     //TODO: Create static method check_index()
   315     gint64 ret_value = 0;
   316     gint64 l2 = 0;
   317 
   318     g_return_val_if_fail (strlist != NULL, 0);
   319 
   320     const GString *tmp_str1 =
   321         (GString *) g_list_nth_data (strlist->glist, index);
   322     const GString *tmp_str2 =
   323         (GString *) g_list_nth_data (strlist->glist, index + 1);
   324 
   325     if (tmp_str1 != NULL)
   326         gmyth_debug ("[%s] seek high bytes = %s\n", __FUNCTION__,
   327             tmp_str1->str);
   328     if (tmp_str2 == NULL || strlen (tmp_str2->str) > 0) {
   329     } else {
   330         gmyth_debug ("[%s] seek low bytes = %s\n", __FUNCTION__, tmp_str2->str);
   331     }
   332 
   333     gint64 l1 = ((guint64) g_ascii_strtoull (tmp_str1->str, NULL, 10)   /*& 0xffffffff */
   334         );
   335 
   336     if (tmp_str2 != NULL && tmp_str2->str != NULL && strlen (tmp_str2->str) > 0) {
   337         l2 = ((gint64) g_ascii_strtoull (tmp_str2->str, NULL, 10)
   338             /*& 0xffffffff */
   339             );
   340     } else {
   341         l2 = l1;
   342         l1 = 0;
   343     }
   344 
   345     gmyth_debug ("[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2);
   346 
   347     ret_value = ((gint64) (l2) /*& 0xffffffff */ ) | ((gint64) l1 << 32);
   348 
   349     gmyth_debug ("[%s] returning int64 value = %lld\n", __FUNCTION__,
   350         ret_value);
   351 
   352     return ret_value;
   353 }
   354 
   355 
   356 /** Gets a string from the string list at the given position. The GString must be deallocated.
   357  * 
   358  * @param strlist The GMythStringList instance.
   359  * @param index the string position in the list, starting with zero.
   360  * @return A pointer to the string data.
   361  */
   362 GString *
   363 gmyth_string_list_get_string (GMythStringList * strlist, const gint index)
   364 {
   365     GString *ret;
   366 
   367     if (!strlist || !(strlist->glist)) {
   368         gmyth_debug ("%s received Null arguments", __FUNCTION__);
   369         return NULL;
   370     }
   371 
   372     ret = (GString *) g_list_nth_data (strlist->glist, index);
   373 
   374     return g_string_new (ret->str);
   375 }
   376 
   377 
   378 static void
   379 gmyth_string_list_clear_element (GString * str_elem, void *data_aux)
   380 {
   381     if (str_elem != NULL)
   382         g_string_free (str_elem, TRUE);
   383 }
   384 
   385 /** Removes all strings from the string list.
   386  * 
   387  * @param strlist The GMythStringList instance.
   388  */
   389 void
   390 gmyth_string_list_clear_all (GMythStringList * strlist)
   391 {
   392     if (strlist != NULL && strlist->glist) {
   393         g_list_foreach (strlist->glist,
   394             (GFunc) gmyth_string_list_clear_element, NULL);
   395         g_list_free (strlist->glist);
   396         strlist->glist = NULL;
   397     }
   398 }
   399 
   400 /** Retrieves the number of elements in the string list.
   401  * 
   402  * @param strlist The GMythStringList instance.
   403  * @return the string list length.
   404  */
   405 gint
   406 gmyth_string_list_length (GMythStringList * strlist)
   407 {
   408     if (!(strlist != NULL && strlist->glist != NULL))
   409         return 0;
   410 
   411     return g_list_length (strlist->glist);
   412 }