container.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding Library. 
00003  Copyright (C) 2007  Daniele Rondina aka Ge@@ru , geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019   Filename:  container.c
00020  
00021   Description:  
00022  
00023   Version:  0.1
00024   Created:  06/10/07 17:08:57 CEST
00025   Revision:  
00026     0 - created
00027     1 - insert doxygen documentation 07/10/30 (Ge@@ru)
00028  
00029   Author:   Daniele Rondina aka Ge@@ru (geaaru@gmial.com) 
00030   License:  GPL 2.0
00031 */
00032 
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <stdio.h>
00046 // easydbus includes
00047 #include "easydbus-core.h"
00048 #include "array_internal.h"
00049 #include "elem_internal.h"
00050 #include "utils.h"
00051 #include "debug.h"
00052 
00057 void
00058 easydbus_container_free (EasyDbus_container * container)
00059 {
00060    unsigned int i = 0;
00061    char **temp, **p_char;
00062    struct EasyDbus_elem *el, *el2;
00063 
00064    if (container == NULL)
00065       return;
00066 
00067    el = el2 = NULL;
00068    temp = p_char = NULL;
00069 
00070    switch (container->container_type) {
00071       case EASYDBUS_ELTYPE_ARRAY:
00072          switch (container->elem_type) {
00073             case EASYDBUS_ELTYPE_STRING:
00074             case EASYDBUS_ELTYPE_OBJECT_PATH:
00075             case EASYDBUS_ELTYPE_SIGNATURE:
00076                /* see if use p_data instead of temp */
00077                p_char = (char **) container->p_data;
00078                temp = p_char;
00079                for (i = 0; i < container->nel; i++) {
00080                   p_char++;
00081                   free (*temp);
00082                   temp = p_char;
00083                }
00084                free (container->p_data);
00085                break;
00086             case EASYDBUS_ELTYPE_INT16:
00087             case EASYDBUS_ELTYPE_UINT16:
00088             case EASYDBUS_ELTYPE_INT32:
00089             case EASYDBUS_ELTYPE_UINT32:
00090             case EASYDBUS_ELTYPE_INT64:
00091             case EASYDBUS_ELTYPE_UINT64:
00092             case EASYDBUS_ELTYPE_BYTE:
00093             case EASYDBUS_ELTYPE_BOOLEAN:
00094             case EASYDBUS_ELTYPE_DOUBLE:
00095                free (container->p_data);
00096                break;
00097             case EASYDBUS_ELTYPE_VARIANT:
00098                el = el2 = (struct EasyDbus_elem *) container->p_data;
00099                while (el != NULL) {
00100                   el = el2->next;
00101                   EasyDbusDebug ("Free of variant element on array");
00102                   easydbus_elem_free (el2);
00103                }
00104                break;
00105             case EASYDBUS_ELTYPE_ARRAY:
00106                el = el2 = (struct EasyDbus_elem *) container->p_data;
00107                for (i = 0; i < container->nel; i++) {
00108                   el2++;
00109                   free_array_from_container (el);
00110                   el = el2;
00111                }
00112                free (container->p_data);
00113                break;
00114             case EASYDBUS_ELTYPE_STRUCT:
00115                el = el2 = (struct EasyDbus_elem *) container->p_data;
00116                i = 1;
00117                while (el != NULL) {
00118                   el = el2->next;
00119                   easydbus_elem_free (el2);
00120                   EasyDbusDebug ("Free of struct element %d", i++);
00121                }
00122                break;
00123             case EASYDBUS_ELTYPE_INVALID:
00124                EasyDbusDebug ("Error or empty array");
00125                break;
00126             case EASYDBUS_ELTYPE_DICT_ENTRY:
00127                el = (struct EasyDbus_elem *) container->p_data;
00128                if (el != NULL) {
00129                   if (el->next != NULL)
00130                      easydbus_elem_free (el->next);
00131                   easydbus_elem_free (el);
00132                }
00133                break;
00134          }
00135          break;
00136       case EASYDBUS_ELTYPE_VARIANT:
00137          switch (container->elem_type) {
00138             case EASYDBUS_ELTYPE_STRING:
00139             case EASYDBUS_ELTYPE_OBJECT_PATH:
00140             case EASYDBUS_ELTYPE_INT16:
00141             case EASYDBUS_ELTYPE_UINT16:
00142             case EASYDBUS_ELTYPE_INT32:
00143             case EASYDBUS_ELTYPE_UINT32:
00144             case EASYDBUS_ELTYPE_INT64:
00145             case EASYDBUS_ELTYPE_UINT64:
00146             case EASYDBUS_ELTYPE_BYTE:
00147             case EASYDBUS_ELTYPE_BOOLEAN:
00148             case EASYDBUS_ELTYPE_DOUBLE:
00149             case EASYDBUS_ELTYPE_SIGNATURE:
00150                free (container->p_data);
00151                break;
00152             case EASYDBUS_ELTYPE_VARIANT:
00153             case EASYDBUS_ELTYPE_ARRAY:
00154             case EASYDBUS_ELTYPE_STRUCT:
00155             case EASYDBUS_ELTYPE_DICT_ENTRY:
00156             case EASYDBUS_ELTYPE_INVALID:
00157                EasyDbusDebug ("Error");
00158                break;
00159          }
00160          EasyDbusDebug ("Free of variant container");
00161          break;
00162       case EASYDBUS_ELTYPE_STRUCT:
00163          el = el2 = (struct EasyDbus_elem *) container->p_data;
00164          i = 1;
00165          while (el != NULL) {
00166             el = el2->next;
00167             easydbus_elem_free (el2);
00168             el2 = el;
00169             EasyDbusDebug ("Free of struct element %d", i++);
00170          }
00171          break;
00172       case EASYDBUS_ELTYPE_DICT_ENTRY:
00173          el = (struct EasyDbus_elem *) container->p_data;
00174          if (el != NULL) {
00175             if (el->next != NULL)
00176                easydbus_elem_free (el->next);
00177             easydbus_elem_free (el);
00178          }
00179          break;
00180       default:
00181          break;
00182    }
00183    if (container->signature != NULL)
00184       free (container->signature);
00185    free (container);
00186 }
00187 
00193 inline void
00194 free_array_from_container (EasyDbus_elem * el)
00195 {
00196    if (el == NULL)
00197       return;
00198    if (el->type == EASYDBUS_ELTYPE_ARRAY) {
00199       if (el->payload.p_container != NULL) {
00200          easydbus_container_free (el->payload.p_container);
00201       }
00202    }
00203 }
00204 
00215 EasyDbus_container *
00216 easydbus_container_create_skeleton (enum el_type type)
00217 {
00218    struct EasyDbus_container *container = NULL;
00219 
00220    switch (type) {
00221       case EASYDBUS_ELTYPE_VARIANT:
00222       case EASYDBUS_ELTYPE_ARRAY:
00223       case EASYDBUS_ELTYPE_STRUCT:
00224       case EASYDBUS_ELTYPE_DICT_ENTRY:
00225          container = (struct EasyDbus_container *)
00226             malloc (sizeof (struct EasyDbus_container));
00227          break;
00228       default:
00229          return NULL;
00230          break;
00231    }
00232 
00233    if (container != NULL) {
00234       memset (container, 0, sizeof (struct EasyDbus_container));
00235       container->container_type = type;
00236       container->elem_type = EASYDBUS_ELTYPE_INVALID;
00237       container->p_data = NULL;
00238       container->signature = NULL;
00239    }
00240 
00241    return container;
00242 }
00243 
00244 
00245 
00264 int 
00265 easydbus_container_add_signature_to_empty_container
00266    (EasyDbus_container * container, ...) 
00267 {
00268    va_list ap;
00269    enum el_type elem = EASYDBUS_ELTYPE_INVALID;
00270    struct easydbus_signature_elem_stack *list_elem;
00271    struct easydbus_signature_elem_stack *tmp_elem;
00272    char *signature = NULL;
00273    int i = 0;
00274 
00275    if (container == NULL)
00276       return -1;
00277 
00278    list_elem = tmp_elem = NULL;
00279    va_start (ap, container);
00280 
00281    /* recover data from input params list */
00282    elem = va_arg (ap, enum el_type);
00283 
00284    while (elem != EASYDBUS_ELTYPE_INVALID) {
00285       EasyDbusDebug ("elem %d", elem);
00286       i++;
00287       list_elem = easydbus_add_elem_to_stack (elem, list_elem);
00288       elem = va_arg (ap, enum el_type);
00289    }
00290 
00291    va_end (ap);
00292 
00293    while (list_elem != NULL) {
00294       if (easydbus_manage_list_signature (list_elem, &signature,
00295                                           EASYDBUS_SIGNATURE_NORMAL))
00296          goto error;
00297       tmp_elem = list_elem->next;
00298       easydbus_free_elem_from_stack (list_elem);
00299       list_elem = tmp_elem;
00300    }
00301 
00302    container->signature = signature;
00303    EasyDbusDebug ("signature added = %s\n", container->signature);
00304    EasyDbusDebug ("Elements founded = %d", i);
00305 
00306    return 0;
00307  error:
00308    EasyDbusDebug ("Error on create signature for" " empty container");
00309    if (signature != NULL)
00310       free (signature);
00311    while (list_elem != NULL) {
00312       tmp_elem = list_elem;
00313       list_elem = list_elem->next;
00314       easydbus_free_elem_from_stack (tmp_elem);
00315    }
00316    return -1;
00317 }
00318 
00327 inline enum el_type
00328 easydbus_container_get_type (const EasyDbus_container * container)
00329 {
00330    if (container == NULL)
00331       return EASYDBUS_ELTYPE_INVALID;
00332    return container->container_type;
00333 }
00334 
00341 inline enum el_type
00342 easydbus_container_get_elem_type 
00343   (const EasyDbus_container * container)
00344 {
00345    if (container == NULL)
00346       return EASYDBUS_ELTYPE_INVALID;
00347    return container->elem_type;
00348 }
00349 
00356 inline unsigned int
00357 easydbus_container_get_nel (const EasyDbus_container * container)
00358 {
00359    if (container == NULL)
00360       return -1;
00361    return container->nel;
00362 }
00363 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:17 2008 for EasyDbus-0.2 by  doxygen 1.5.4