dict_entry.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:  dict_entry.c
00020  
00021   Description:  
00022  
00023   Version:  1.0
00024   Created:  06/10/07 22:00:27 CEST
00025   Revision:  none
00026  
00027   Author:   Daniele Rondina aka Ge@@ru (geaaru@gmail.com) 
00028   License:  GPL 2.0
00029 */
00030 
00031 /*@file dict_entry.c Functions for manage easydbus dict_entry containers.
00032  * @date    Created:  06/10/07 22:00:27 CEST
00033  * @author  Daniele Rondina aka ge\@\@ru <geaaru@gmail.com>
00034  *
00035  * \todo
00036  *  See why doxygen doesn't work fine with dict entry easydbus_add_i* functions,
00037  *  in particular why these functions can be inserted on dict entry group with 
00038  *  "{" "}".
00039  */
00040 
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044 // easydbus includes 
00045 #include "easydbus-core.h"
00046 #include "elem_internal.h"
00047 #include "utils.h"
00048 #include "debug.h"
00049 
00063 int
00064 easydbus_dict_entry_add_obj_path (EasyDbus_container * container, 
00065                                   char *string) 
00066 {
00067    int string_length = 0;
00068    char *p_string = NULL;
00069    struct EasyDbus_elem *el = NULL;
00070    struct EasyDbus_elem *el2 = NULL;
00071 
00072    if (container == NULL || string == NULL)
00073       return -1;
00074 
00075    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00076       return -1;
00077 
00078    if (container->nel > 1)
00079       return -1;
00080 
00081    el = easydbus_elem_create (EASYDBUS_ELTYPE_OBJECT_PATH);
00082    if (el == NULL)
00083       return -1;
00084 
00085    string_length = strlen (string) + 1;
00086    p_string = malloc (string_length);
00087    if (p_string == NULL)
00088       goto error;
00089 
00090    EASYDBUS_MEMCOPY (p_string, string, string_length);
00091 
00092    el->payload.string = p_string;
00093 
00094    if (container->nel) {
00095       el2 = (struct EasyDbus_elem *) container->p_data;
00096       el2->next = el;
00097    }
00098    else {
00099       container->p_data = el;
00100    }
00101    container->nel++;
00102    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00103    return 0;
00104 
00105  error:
00106    easydbus_elem_free (el);
00107    return -1;
00108 }
00109 
00122 int
00123 easydbus_dict_entry_add_string (EasyDbus_container * container, 
00124                                 char *string)
00125 {
00126    int string_length = 0;
00127    char *p_string = NULL;
00128    struct EasyDbus_elem *el = NULL;
00129    struct EasyDbus_elem *el2 = NULL;
00130 
00131    if (container == NULL || string == NULL)
00132       return -1;
00133 
00134    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00135       return -1;
00136 
00137    if (container->nel > 1)
00138       return -1;
00139 
00140    el = easydbus_elem_create (EASYDBUS_ELTYPE_STRING);
00141    if (el == NULL)
00142       return -1;
00143 
00144    string_length = strlen (string) + 1;
00145    p_string = malloc (string_length);
00146    if (p_string == NULL)
00147       goto error;
00148 
00149    EASYDBUS_MEMCOPY (p_string, string, string_length);
00150 
00151    el->payload.string = p_string;
00152 
00153    if (container->nel) {
00154       el2 = (struct EasyDbus_elem *) container->p_data;
00155       el2->next = el;
00156    }
00157    else {
00158       container->p_data = el;
00159    }
00160    container->nel++;
00161    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00162    return 0;
00163 
00164  error:
00165    easydbus_elem_free (el);
00166    return -1;
00167 }
00168 
00182 int
00183 easydbus_dict_entry_add_signature (EasyDbus_container * container, 
00184                                    char *string)
00185 {
00186    int string_length = 0;
00187    char *p_string = NULL;
00188    struct EasyDbus_elem *el = NULL;
00189    struct EasyDbus_elem *el2 = NULL;
00190 
00191    if (container == NULL || string == NULL)
00192       return -1;
00193 
00194    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00195       return -1;
00196 
00197    if (container->nel > 1)
00198       return -1;
00199 
00200    el = easydbus_elem_create (EASYDBUS_ELTYPE_SIGNATURE);
00201    if (el == NULL)
00202       return -1;
00203 
00204    string_length = strlen (string) + 1;
00205    p_string = malloc (string_length);
00206    if (p_string == NULL)
00207       goto error;
00208 
00209    EASYDBUS_MEMCOPY (p_string, string, string_length);
00210 
00211    el->payload.string = p_string;
00212 
00213    if (container->nel) {
00214       el2 = (struct EasyDbus_elem *) container->p_data;
00215       el2->next = el;
00216    }
00217    else {
00218       container->p_data = el;
00219    }
00220    container->nel++;
00221    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00222    return 0;
00223 
00224  error:
00225    easydbus_elem_free (el);
00226    return -1;
00227 }
00228 
00241 int 
00242 easydbus_dict_entry_add_byte (EasyDbus_container * container, 
00243                               char byte) 
00244 {
00245    struct EasyDbus_elem *el = NULL;
00246    struct EasyDbus_elem *el2 = NULL;
00247 
00248    if (container == NULL)
00249       return -1;
00250 
00251    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00252       return -1;
00253 
00254    if (container->nel > 1)
00255       return -1;
00256 
00257    el = easydbus_elem_create (EASYDBUS_ELTYPE_BYTE);
00258    if (el == NULL)
00259       return -1;
00260 
00261    el->payload.byte = byte;
00262 
00263    if (container->nel) {
00264       el2 = (struct EasyDbus_elem *) container->p_data;
00265       el2->next = el;
00266    }
00267    else {
00268       container->p_data = el;
00269    }
00270    container->nel++;
00271    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00272    return 0;
00273 }
00274 
00287 int
00288 easydbus_dict_entry_add_boolean (EasyDbus_container * container, 
00289                                  char boolean)
00290 {
00291    struct EasyDbus_elem *el = NULL;
00292    struct EasyDbus_elem *el2 = NULL;
00293 
00294    if (container == NULL)
00295       return -1;
00296 
00297    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00298       return -1;
00299 
00300    if (container->nel > 1)
00301       return -1;
00302 
00303    el = easydbus_elem_create (EASYDBUS_ELTYPE_BOOLEAN);
00304    if (el == NULL)
00305       return -1;
00306 
00307    el->payload.boolean = boolean;
00308 
00309    if (container->nel) {
00310       el2 = (struct EasyDbus_elem *) container->p_data;
00311       el2->next = el;
00312    }
00313    else {
00314       container->p_data = el;
00315    }
00316    container->nel++;
00317    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00318    return 0;
00319 }
00320 
00321 
00336 int 
00337 easydbus_dict_entry_add_variant (EasyDbus_container * dict_entry, 
00338                                  EasyDbus_container * variant)
00339 {
00340    struct EasyDbus_elem *el = NULL;
00341    struct EasyDbus_elem *el2 = NULL;
00342 
00343    if (dict_entry == NULL || variant == NULL)
00344       return -1;
00345 
00346    if (dict_entry->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00347       return -1;
00348 
00349    if (dict_entry->nel > 1)
00350       return -1;
00351 
00352    el = easydbus_elem_create (EASYDBUS_ELTYPE_VARIANT);
00353    if (el == NULL)
00354       return -1;
00355 
00356    el->payload.p_container = variant;
00357 
00358    if (dict_entry->nel) {
00359       el2 = (struct EasyDbus_elem *) dict_entry->p_data;
00360       el2->next = el;
00361    }
00362    else {
00363       dict_entry->p_data = el;
00364       dict_entry->elem_type = EASYDBUS_ELTYPE_INVALID;
00365    }
00366    dict_entry->nel++;
00367    return 0;
00368 }
00369 
00382 int
00383 easydbus_dict_entry_add_double (EasyDbus_container * container, 
00384                                 double d) 
00385 {
00386    struct EasyDbus_elem *el = NULL;
00387    struct EasyDbus_elem *el2 = NULL;
00388 
00389    if (container == NULL)
00390       return -1;
00391 
00392    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00393       return -1;
00394 
00395    if (container->nel > 1)
00396       return -1;
00397 
00398    el = easydbus_elem_create (EASYDBUS_ELTYPE_DOUBLE);
00399    if (el == NULL)
00400       return -1;
00401 
00402    el->payload.Double = d;
00403 
00404    if (container->nel) {
00405       el2 = (struct EasyDbus_elem *) container->p_data;
00406       el2->next = el;
00407    }
00408    else {
00409       container->p_data = el;
00410    }
00411    container->nel++;
00412    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00413    return 0;
00414 }
00415 
00416 
00429 int 
00430 easydbus_dict_entry_add_ui32 (EasyDbus_container * container, 
00431                               unsigned int ui32) 
00432 {
00433    struct EasyDbus_elem *el = NULL;
00434    struct EasyDbus_elem *el2 = NULL;
00435 
00436    if (container == NULL)
00437       return -1;
00438 
00439    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00440       return -1;
00441 
00442    if (container->nel > 1)
00443       return -1;
00444 
00445    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT32);
00446    if (el == NULL)
00447       return -1;
00448 
00449    el->payload.ui32 = ui32;
00450 
00451    if (container->nel) {
00452       el2 = (struct EasyDbus_elem *) container->p_data;
00453       el2->next = el;
00454    }
00455    else {
00456       container->p_data = el;
00457    }
00458    container->nel++;
00459    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00460    return 0;
00461 }
00462 
00475 int 
00476 easydbus_dict_entry_add_i16 (struct EasyDbus_container *container, 
00477                              short i16) 
00478 {
00479    struct EasyDbus_elem *el = NULL;
00480    struct EasyDbus_elem *el2 = NULL;
00481 
00482    if (container == NULL)
00483       return -1;
00484 
00485    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00486       return -1;
00487 
00488    if (container->nel > 1)
00489       return -1;
00490 
00491    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT16);
00492    if (el == NULL)
00493       return -1;
00494 
00495    el->payload.i16 = i16;
00496 
00497    if (container->nel) {
00498       el2 = (struct EasyDbus_elem *) container->p_data;
00499       el2->next = el;
00500    }
00501    else {
00502       container->p_data = el;
00503    }
00504    container->nel++;
00505    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00506    return 0;
00507 }
00508 
00521 int 
00522 easydbus_dict_entry_add_i32 (EasyDbus_container * container, 
00523                              int i32) 
00524 {
00525    struct EasyDbus_elem *el = NULL;
00526    struct EasyDbus_elem *el2 = NULL;
00527 
00528    if (container == NULL)
00529       return -1;
00530 
00531    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00532       return -1;
00533 
00534    if (container->nel > 1)
00535       return -1;
00536 
00537    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT32);
00538    if (el == NULL)
00539       return -1;
00540 
00541    el->payload.i32 = i32;
00542 
00543    if (container->nel) {
00544       el2 = (struct EasyDbus_elem *) container->p_data;
00545       el2->next = el;
00546    }
00547    else {
00548       container->p_data = el;
00549    }
00550    container->nel++;
00551    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00552    return 0;
00553 }
00554 
00567 int 
00568 easydbus_dict_entry_add_ui16 (EasyDbus_container * container, 
00569                               unsigned short ui16) 
00570 {
00571    struct EasyDbus_elem *el = NULL;
00572    struct EasyDbus_elem *el2 = NULL;
00573 
00574    if (container == NULL)
00575       return -1;
00576 
00577    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00578       return -1;
00579 
00580    if (container->nel > 1)
00581       return -1;
00582 
00583    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT16);
00584    if (el == NULL)
00585       return -1;
00586 
00587    el->payload.ui16 = ui16;
00588 
00589    if (container->nel) {
00590       el2 = (struct EasyDbus_elem *) container->p_data;
00591       el2->next = el;
00592    }
00593    else {
00594       container->p_data = el;
00595    }
00596    container->nel++;
00597    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00598    return 0;
00599 }
00600 
00613 int easydbus_dict_entry_add_i64 (EasyDbus_container * container, 
00614                                  long long i64) 
00615 {
00616    struct EasyDbus_elem *el = NULL;
00617    struct EasyDbus_elem *el2 = NULL;
00618 
00619    if (container == NULL)
00620       return -1;
00621 
00622    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00623       return -1;
00624 
00625    if (container->nel > 1)
00626       return -1;
00627 
00628    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT64);
00629    if (el == NULL)
00630       return -1;
00631 
00632    el->payload.i64 = i64;
00633 
00634    if (container->nel) {
00635       el2 = (struct EasyDbus_elem *) container->p_data;
00636       el2->next = el;
00637    }
00638    else {
00639       container->p_data = el;
00640    }
00641    container->nel++;
00642    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00643    return 0;
00644 }
00645 
00658 int easydbus_dict_entry_add_ui64 (EasyDbus_container * container, 
00659                                   unsigned long long ui64) 
00660 {
00661    struct EasyDbus_elem *el = NULL;
00662    struct EasyDbus_elem *el2 = NULL;
00663 
00664    if (container == NULL)
00665       return -1;
00666 
00667    if (container->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00668       return -1;
00669 
00670    if (container->nel > 1)
00671       return -1;
00672 
00673    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT64);
00674    if (el == NULL)
00675       return -1;
00676 
00677    el->payload.ui64 = ui64;
00678 
00679    if (container->nel) {
00680       el2 = (struct EasyDbus_elem *) container->p_data;
00681       el2->next = el;
00682    }
00683    else {
00684       container->p_data = el;
00685    }
00686    container->nel++;
00687    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00688    return 0;
00689 }
00690 
00698 const EasyDbus_elem *
00699 easydbus_dict_entry_get_first_element (EasyDbus_container * dict_entry) 
00700 {
00701    if (dict_entry == NULL ||
00702        dict_entry->container_type != EASYDBUS_ELTYPE_DICT_ENTRY ||
00703        dict_entry->nel == 0)
00704       return NULL;
00705 
00706    return (const EasyDbus_elem *) dict_entry->p_data;
00707 }
00708 
00716 const EasyDbus_elem *
00717 easydbus_dict_entry_get_second_element (EasyDbus_container * dict_entry) 
00718 {
00719    if (dict_entry == NULL ||
00720        dict_entry->container_type != EASYDBUS_ELTYPE_DICT_ENTRY ||
00721        dict_entry->nel < 2)
00722       return NULL;
00723 
00724    return (const EasyDbus_elem *)
00725       (((const EasyDbus_elem *) dict_entry->p_data)->next);
00726 }
00727 // vim: ts=3 shiftwidth=3 expandtab

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