gmyth/src/gmyth_stringlist.c
author morphbr
Wed Sep 12 16:47:02 2007 +0100 (2007-09-12)
branchtrunk
changeset 841 25fb81d23ef5
parent 754 cb885ee44618
permissions -rw-r--r--
[svn r847] - Fixed several bugs
- Included some help functions
     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 *
    44                                                  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 
   113 /** Appends a gdouble to the string list.
   114  *
   115  * @param strlist The GMythStringList instance.
   116  * @param value The gdouble to be appended.
   117  *
   118  * @return The appended gdouble converted to a GString object.
   119  */
   120 GString        *
   121 gmyth_string_list_append_float(GMythStringList * strlist, const gdouble value)
   122 {
   123     GString        *value_str = g_string_new("");
   124     g_string_printf(value_str, "%f", value);
   125     strlist->glist = g_list_append(strlist->glist, value_str);
   126     return value_str;
   127 }
   128 
   129 
   130 /** Appends a guint64 to the string list.
   131  * 
   132  * @param strlist The GMythStringList instance.
   133  * @param value The guint64 to be appended.
   134  * 
   135  * @return The appended guint64 converted to a GString object.
   136  */
   137 GString        *
   138 gmyth_string_list_append_uint64(GMythStringList * strlist,
   139                                 const guint64 value)
   140 {
   141     GString        *tmp_str1 = g_string_new("");
   142     GString        *tmp_str2 = g_string_new("");
   143 
   144     gmyth_debug("value = %llu.\n", value);
   145 
   146     gulong          l2 = ((guint64) value & 0xffffffff);
   147     gulong          l1 = ((guint64) value >> 32);
   148 
   149     /*
   150      * high order part of guint64 value 
   151      */
   152     g_string_printf(tmp_str1, "%lu", l1);
   153 
   154     gmyth_debug("[%s] uint64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
   155 
   156     strlist->glist = g_list_append(strlist->glist, tmp_str1);
   157 
   158     /*
   159      * low order part of guint64 value 
   160      */
   161     g_string_printf(tmp_str2, "%lu", l2);
   162 
   163     gmyth_debug("[%s] uint64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
   164 
   165     strlist->glist = g_list_append(strlist->glist, tmp_str2);
   166 
   167     return tmp_str2;
   168 }
   169 
   170 /** Appends a gint64 to the string list.
   171  * 
   172  * @param strlist The GMythStringList instance.
   173  * @param value The gint64 to be appended.
   174  * 
   175  * @return The appended gint64 converted to a GString object.
   176  */
   177 GString        *
   178 gmyth_string_list_append_int64(GMythStringList * strlist,
   179                                const gint64 value)
   180 {
   181     GString        *tmp_str1 = g_string_new("");
   182     GString        *tmp_str2 = g_string_new("");
   183 
   184     gmyth_debug("value = %lld.\n", value);
   185 
   186     glong           l2 = ((gint64) value & 0xffffffff);
   187     glong           l1 = ((gint64) value >> 32);
   188 
   189     /*
   190      * high order part of gint64 value 
   191      */
   192     g_string_printf(tmp_str1, "%ld", l1);
   193 
   194     gmyth_debug("[%s] int64 (high) = %s\n", __FUNCTION__, tmp_str1->str);
   195 
   196     strlist->glist = g_list_append(strlist->glist, tmp_str1);
   197 
   198     /*
   199      * low order part of gint64 value 
   200      */
   201     g_string_printf(tmp_str2, "%ld", l2);
   202 
   203     gmyth_debug("[%s] int64 (low) = %s\n", __FUNCTION__, tmp_str2->str);
   204 
   205     strlist->glist = g_list_append(strlist->glist, tmp_str2);
   206 
   207     return tmp_str2;
   208 }
   209 
   210 /** Appends a char array to the string list.
   211  * 
   212  * @param strlist The GMythStringList instance.
   213  * @param value The char array to be appended.
   214  * 
   215  * @return The appended char array converted to a GString object.
   216  */
   217 GString        *
   218 gmyth_string_list_append_char_array(GMythStringList * strlist,
   219                                     const gchar * value)
   220 {
   221     GString        *tmp_str = NULL;
   222 
   223     g_return_val_if_fail(strlist != NULL, NULL);
   224 
   225     tmp_str = g_string_new(value);
   226 
   227     strlist->glist = g_list_append(strlist->glist, tmp_str);
   228 
   229     return tmp_str;
   230 }
   231 
   232 /** Appends a string to the string list.
   233  * 
   234  * @param strlist The GMythStringList instance.
   235  * @param value The string to be appended.
   236  * 
   237  * @return The appended string itself. 
   238  */
   239 GString        *
   240 gmyth_string_list_append_string(GMythStringList * strlist, GString * value)
   241 {
   242     g_return_val_if_fail(strlist != NULL, NULL);
   243 
   244     if (value != NULL) {
   245         strlist->glist =
   246             g_list_append(strlist->glist, g_string_new(value->str));
   247     } else {
   248         strlist->glist = g_list_append(strlist->glist, NULL);
   249     }
   250 
   251     return value;
   252 }
   253 
   254 /** Gets an integer value from the string list at the given position.
   255  * 
   256  * @param strlist The GMythStringList instance.
   257  * @param index the integer position in the list, starting with zero.
   258  * @return The integer value.
   259  */
   260 gint
   261 gmyth_string_list_get_int(GMythStringList * strlist, const gint index)
   262 {
   263     // TODO: Create static method check_index()
   264     GString        *tmp_str = NULL;
   265 
   266     g_return_val_if_fail(strlist != NULL, 0);
   267 
   268     tmp_str = (GString *) g_list_nth_data(strlist->glist, index);
   269 
   270     if (NULL == tmp_str || NULL == tmp_str->str
   271         || strlen(tmp_str->str) <= 0)
   272         return 0;
   273 
   274     return (gint) (             /* 0x00000000ffffffffL & (gint64) */
   275                       g_ascii_strtoull(tmp_str->str, NULL, 10));
   276 }
   277 
   278 /** Gets a guint64 value from the string list at the given position.
   279  * According to the Mythtv protocol, the 64 bits value is formed by
   280  * two strings.
   281  * 
   282  * @param strlist The GMythStringList instance.
   283  * @param index the index of the first string forming the 64 bits value. 
   284  * Index starts with zero.
   285  * @return The guint64 value.
   286  */
   287 guint64
   288 gmyth_string_list_get_uint64(GMythStringList * strlist, const gint index)
   289 {
   290     // TODO: Create static method check_index()
   291     guint64         ret_value = 0;
   292     guint64         l2 = 0;
   293 
   294     g_return_val_if_fail(strlist != NULL, 0);
   295 
   296     const GString  *tmp_str1 =
   297         (GString *) g_list_nth_data(strlist->glist, index);
   298     const GString  *tmp_str2 =
   299         (GString *) g_list_nth_data(strlist->glist, index + 1);
   300 
   301     if (tmp_str1 != NULL)
   302         gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
   303                     tmp_str1->str);
   304     if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
   305     } else {
   306         gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
   307                     tmp_str2->str);
   308     }
   309 
   310     guint64         l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10)   /* & 
   311                                                                                  * 0xffffffff 
   312                                                                                  */
   313         );
   314 
   315     if (tmp_str2 != NULL && tmp_str2->str != NULL
   316         && strlen(tmp_str2->str) > 0) {
   317         l2 = ((guint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
   318               /*
   319                * & 0xffffffff 
   320                */
   321             );
   322     } else {
   323         l2 = l1;
   324         l1 = 0;
   325     }
   326 
   327     gmyth_debug("[%s]\t[l1 == %llu, l2 == %llu]\n", __FUNCTION__, l1, l2);
   328 
   329     ret_value =
   330         ((guint64) (l2) /* & 0xffffffff */ ) | ((guint64) l1 << 32);
   331 
   332     gmyth_debug("[%s] returning uint64 value = %llu\n", __FUNCTION__,
   333                 ret_value);
   334 
   335     return ret_value;
   336 }
   337 
   338 /** Gets a gint64 value from the string list at the given position.
   339  * According to the Mythtv protocol, the 64 bits value is formed by
   340  * two strings.
   341  * 
   342  * @param strlist The GMythStringList instance.
   343  * @param index the index of the first string forming the 64 bits value. 
   344  * Index starts with zero.
   345  * @return The gint64 value.
   346  */
   347 gint64
   348 gmyth_string_list_get_int64(GMythStringList * strlist, const gint index)
   349 {
   350     // TODO: Create static method check_index()
   351     gint64          ret_value = 0;
   352     gint64          l2 = 0;
   353 
   354     g_return_val_if_fail(strlist != NULL, 0);
   355 
   356     const GString  *tmp_str1 =
   357         (GString *) g_list_nth_data(strlist->glist, index);
   358     const GString  *tmp_str2 =
   359         (GString *) g_list_nth_data(strlist->glist, index + 1);
   360 
   361     if (tmp_str1 != NULL)
   362         gmyth_debug("[%s] seek high bytes = %s\n", __FUNCTION__,
   363                     tmp_str1->str);
   364     if (tmp_str2 == NULL || strlen(tmp_str2->str) > 0) {
   365     } else {
   366         gmyth_debug("[%s] seek low bytes = %s\n", __FUNCTION__,
   367                     tmp_str2->str);
   368     }
   369 
   370     gint64          l1 = ((guint64) g_ascii_strtoull(tmp_str1->str, NULL, 10)   /* & 
   371                                                                                  * 0xffffffff 
   372                                                                                  */
   373         );
   374 
   375     if (tmp_str2 != NULL && tmp_str2->str != NULL
   376         && strlen(tmp_str2->str) > 0) {
   377         l2 = ((gint64) g_ascii_strtoull(tmp_str2->str, NULL, 10)
   378               /*
   379                * & 0xffffffff 
   380                */
   381             );
   382     } else {
   383         l2 = l1;
   384         l1 = 0;
   385     }
   386 
   387     gmyth_debug("[%s]\t[l1 == %lld, l2 == %lld]\n", __FUNCTION__, l1, l2);
   388 
   389     ret_value = ((gint64) (l2) /* & 0xffffffff */ ) | ((gint64) l1 << 32);
   390 
   391     gmyth_debug("[%s] returning int64 value = %lld\n", __FUNCTION__,
   392                 ret_value);
   393 
   394     return ret_value;
   395 }
   396 
   397 
   398 /** Gets a string from the string list at the given position. The GString must be deallocated.
   399  * 
   400  * @param strlist The GMythStringList instance.
   401  * @param index the string position in the list, starting with zero.
   402  * @return A pointer to the string data.
   403  */
   404 GString        *
   405 gmyth_string_list_get_string(GMythStringList * strlist, const gint index)
   406 {
   407     GString        *ret;
   408 
   409     if (!strlist || !(strlist->glist)) {
   410         gmyth_debug("%s received Null arguments", __FUNCTION__);
   411         return NULL;
   412     }
   413 
   414     ret = (GString *) g_list_nth_data(strlist->glist, index);
   415 
   416     return g_string_new(ret->str);
   417 }
   418 
   419 
   420 static void
   421 gmyth_string_list_clear_element(GString * str_elem, void *data_aux)
   422 {
   423     if (str_elem != NULL)
   424         g_string_free(str_elem, TRUE);
   425 }
   426 
   427 /** Removes all strings from the string list.
   428  * 
   429  * @param strlist The GMythStringList instance.
   430  */
   431 void
   432 gmyth_string_list_clear_all(GMythStringList * strlist)
   433 {
   434     if (strlist != NULL && strlist->glist) {
   435         g_list_foreach(strlist->glist,
   436                        (GFunc) gmyth_string_list_clear_element, NULL);
   437         g_list_free(strlist->glist);
   438         strlist->glist = NULL;
   439     }
   440 }
   441 
   442 /** Retrieves the number of elements in the string list.
   443  * 
   444  * @param strlist The GMythStringList instance.
   445  * @return the string list length.
   446  */
   447 gint
   448 gmyth_string_list_length(GMythStringList * strlist)
   449 {
   450     if (!(strlist != NULL && strlist->glist != NULL))
   451         return 0;
   452 
   453     return g_list_length(strlist->glist);
   454 }