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