struct.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus Library: 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:  struct.c
00020 
00021  Description:
00022 
00023  Version:  1.0
00024  Created:  09/07/07 12:22:17 CEST
00025  Revision:
00026     0 - created
00027 
00028  Author:   Daniele Rondina aka ge@@ru (geaaru@gmail.com) 
00029  License:  GPL 2.0
00030 */
00031 
00037 #include <stdlib.h>
00038 #include <string.h>
00039 // easydbus includes
00040 #include "easydbus-core.h"
00041 #include "elem_internal.h"
00042 #include "debug.h"
00043 
00044 
00053 int 
00054 easydbus_struct_add_string (EasyDbus_container * container, 
00055                             char *string) 
00056 {
00057    int string_length = 0;
00058    char *p_string = NULL;
00059    struct EasyDbus_elem *el = NULL;
00060    struct EasyDbus_elem *el2 = NULL;
00061 
00062    if (!container || !string ||
00063        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00064       return -1;
00065 
00066    el = easydbus_elem_create (EASYDBUS_ELTYPE_STRING);
00067    if (!el)
00068       return -1;
00069 
00070    string_length = strlen (string) + 1;
00071    p_string = malloc (string_length);
00072    if (!p_string)
00073       goto error;
00074 
00075    EASYDBUS_MEMCOPY (p_string, string, string_length);
00076 
00077    el->payload.string = p_string;
00078 
00079    if (container->nel) {
00080       el2 = (struct EasyDbus_elem *) container->p_data;
00081       while (el2->next)
00082          el2 = el2->next;
00083       el2->next = el;
00084    }
00085    else
00086       container->p_data = el;
00087 
00088    container->nel++;
00089    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00090    return 0;
00091 
00092  error:
00093    easydbus_elem_free (el);
00094    return -1;
00095 }
00096 
00097 
00105 int 
00106 easydbus_struct_add_signature (EasyDbus_container * container, 
00107                                char *string) 
00108 {
00109    int string_length = 0;
00110    char *p_string = NULL;
00111    struct EasyDbus_elem *el = NULL;
00112    struct EasyDbus_elem *el2 = NULL;
00113 
00114    if (!container || !string ||
00115        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00116       return -1;
00117 
00118    el = easydbus_elem_create (EASYDBUS_ELTYPE_SIGNATURE);
00119    if (!el)
00120       return -1;
00121 
00122    string_length = strlen (string) + 1;
00123    p_string = malloc (string_length);
00124    if (!p_string)
00125       goto error;
00126 
00127    EASYDBUS_MEMCOPY (p_string, string, string_length);
00128 
00129    el->payload.string = p_string;
00130 
00131    if (container->nel) {
00132       el2 = (struct EasyDbus_elem *) container->p_data;
00133       while (el2->next)
00134          el2 = el2->next;
00135       el2->next = el;
00136    }
00137    else
00138       container->p_data = el;
00139 
00140    container->nel++;
00141    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00142    return 0;
00143 
00144  error:
00145    easydbus_elem_free (el);
00146    return -1;
00147 }
00148 
00149 
00158 int 
00159 easydbus_struct_add_obj_path (EasyDbus_container * container, 
00160                               char *string) 
00161 {
00162    int string_length = 0;
00163    char *p_string = NULL;
00164    struct EasyDbus_elem *el = NULL;
00165    struct EasyDbus_elem *el2 = NULL;
00166 
00167    if (!container || !string ||
00168        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00169       return -1;
00170 
00171    el = easydbus_elem_create (EASYDBUS_ELTYPE_OBJECT_PATH);
00172    if (!el)
00173       return -1;
00174 
00175    string_length = strlen (string) + 1;
00176    p_string = malloc (string_length);
00177    if (!p_string)
00178       goto error;
00179 
00180    EASYDBUS_MEMCOPY (p_string, string, string_length);
00181 
00182    el->payload.string = p_string;
00183 
00184    if (container->nel) {
00185       el2 = (struct EasyDbus_elem *) container->p_data;
00186       while (el2->next)
00187          el2 = el2->next;
00188       el2->next = el;
00189    }
00190    else
00191       container->p_data = el;
00192 
00193    container->nel++;
00194    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00195    return 0;
00196 
00197  error:
00198    easydbus_elem_free (el);
00199    return -1;
00200 }
00201 
00202 
00210 int easydbus_struct_add_i32 (EasyDbus_container * container, 
00211                              int i32) 
00212 {
00213    struct EasyDbus_elem *el = NULL;
00214    struct EasyDbus_elem *el2 = NULL;
00215 
00216    if (!container ||
00217        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00218       return -1;
00219 
00220    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT32);
00221    if (!el)
00222       return -1;
00223 
00224    el->payload.i32 = i32;
00225 
00226    if (container->nel) {
00227       el2 = (struct EasyDbus_elem *) container->p_data;
00228       while (el2->next)
00229          el2 = el2->next;
00230       el2->next = el;
00231    }
00232    else
00233       container->p_data = (void *) el;
00234    container->nel++;
00235    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00236    return 0;
00237 }
00238 
00246 int
00247 easydbus_struct_add_ui32 (EasyDbus_container * container, 
00248                           unsigned int ui32) 
00249 {
00250    struct EasyDbus_elem *el = NULL;
00251    struct EasyDbus_elem *el2 = NULL;
00252 
00253    if (!container ||
00254        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00255       return -1;
00256 
00257    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT32);
00258    if (!el)
00259       return -1;
00260 
00261    el->payload.ui32 = ui32;
00262 
00263    if (container->nel) {
00264       el2 = (struct EasyDbus_elem *) container->p_data;
00265       while (el2->next)
00266          el2 = el2->next;
00267       el2->next = el;
00268    }
00269    else
00270       container->p_data = (void *) el;
00271 
00272    container->nel++;
00273    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00274    return 0;
00275 }
00276 
00284 int easydbus_struct_add_i16 (EasyDbus_container * container, 
00285                              short i16) 
00286 {
00287    struct EasyDbus_elem *el = NULL;
00288    struct EasyDbus_elem *el2 = NULL;
00289 
00290    if (!container ||
00291        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00292       return -1;
00293 
00294    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT16);
00295    if (!el)
00296       return -1;;
00297 
00298    el->payload.i16 = i16;
00299 
00300    if (container->nel) {
00301       el2 = (struct EasyDbus_elem *) container->p_data;
00302       while (el2->next)
00303          el2 = el2->next;
00304       el2->next = el;
00305    }
00306    else
00307       container->p_data = (void *) el;
00308 
00309    container->nel++;
00310    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00311    return 0;
00312 }
00313 
00321 int
00322 easydbus_struct_add_ui16 (EasyDbus_container * container, 
00323                           unsigned short ui16) 
00324 {
00325    struct EasyDbus_elem *el = NULL;
00326    struct EasyDbus_elem *el2 = NULL;
00327 
00328    if (!container ||
00329        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00330       return -1;
00331 
00332    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT16);
00333    if (!el)
00334       return -1;
00335 
00336    el->payload.ui16 = ui16;
00337 
00338    if (container->nel) {
00339       el2 = (struct EasyDbus_elem *) container->p_data;
00340       while (el2->next)
00341          el2 = el2->next;
00342       el2->next = el;
00343    }
00344    else
00345       container->p_data = (void *) el;
00346 
00347    container->nel++;
00348    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00349    return 0;
00350 }
00351 
00359 int
00360 easydbus_struct_add_byte (EasyDbus_container * container, 
00361                           char byte) 
00362 {
00363    struct EasyDbus_elem *el = NULL;
00364    struct EasyDbus_elem *el2 = NULL;
00365 
00366    if (!container ||
00367        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00368       return -1;
00369 
00370    el = easydbus_elem_create (EASYDBUS_ELTYPE_BYTE);
00371    if (!el)
00372       return -1;
00373 
00374    el->payload.byte = byte;
00375 
00376    if (container->nel) {
00377       el2 = (struct EasyDbus_elem *) container->p_data;
00378       while (el2->next)
00379          el2 = el2->next;
00380       el2->next = el;
00381    }
00382    else
00383       container->p_data = (void *) el;
00384 
00385    container->nel++;
00386    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00387    return 0;
00388 }
00389 
00397 int
00398 easydbus_struct_add_boolean (EasyDbus_container * container, 
00399                              char boolean) 
00400 {
00401    struct EasyDbus_elem *el = NULL;
00402    struct EasyDbus_elem *el2 = NULL;
00403 
00404    if (!container ||
00405        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00406       return -1;
00407 
00408    el = easydbus_elem_create (EASYDBUS_ELTYPE_BOOLEAN);
00409    if (!el)
00410       return -1;
00411 
00412    el->payload.boolean = boolean;
00413 
00414    if (container->nel) {
00415       el2 = (struct EasyDbus_elem *) container->p_data;
00416       while (el2->next)
00417          el2 = el2->next;
00418       el2->next = el;
00419    }
00420    else
00421       container->p_data = (void *) el;
00422 
00423    container->nel++;
00424    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00425    return 0;
00426 }
00427 
00435 int
00436 easydbus_struct_add_ui64 (EasyDbus_container * container, 
00437                           unsigned long long ui64)
00438 {
00439    struct EasyDbus_elem *el = NULL;
00440    struct EasyDbus_elem *el2 = NULL;
00441 
00442    if (!container ||
00443        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00444       return -1;
00445 
00446    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT64);
00447    if (!el)
00448       return -1;
00449 
00450    el->payload.ui64 = ui64;
00451 
00452    if (container->nel) {
00453       el2 = (struct EasyDbus_elem *) container->p_data;
00454       while (el2->next)
00455          el2 = el2->next;
00456       el2->next = el;
00457    }
00458    else
00459       container->p_data = (void *) el;
00460 
00461    container->nel++;
00462    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00463    return 0;
00464 }
00465 
00473 int
00474 easydbus_struct_add_i64 (EasyDbus_container * container, 
00475                          long long i64) 
00476 {
00477    struct EasyDbus_elem *el = NULL;
00478    struct EasyDbus_elem *el2 = NULL;
00479 
00480    if (!container ||
00481        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00482       return -1;
00483 
00484    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT64);
00485    if (!el)
00486       return -1;;
00487 
00488    el->payload.i64 = i64;
00489 
00490    if (container->nel) {
00491       el2 = (struct EasyDbus_elem *) container->p_data;
00492       while (el2->next)
00493          el2 = el2->next;
00494       el2->next = el;
00495    }
00496    else
00497       container->p_data = (void *) el;
00498 
00499    container->nel++;
00500    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00501    return 0;
00502 }
00503 
00511 int
00512 easydbus_struct_add_double (EasyDbus_container * container, 
00513                             double d) 
00514 {
00515    struct EasyDbus_elem *el = NULL;
00516    struct EasyDbus_elem *el2 = NULL;
00517 
00518    if (!container ||
00519        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00520       return -1;
00521 
00522    el = easydbus_elem_create (EASYDBUS_ELTYPE_DOUBLE);
00523    if (!el)
00524       return -1;;
00525 
00526    el->payload.Double = d;
00527 
00528    if (container->nel) {
00529       el2 = (struct EasyDbus_elem *) container->p_data;
00530       while (el2->next)
00531          el2 = el2->next;
00532       el2->next = el;
00533    }
00534    else
00535       container->p_data = (void *) el;
00536 
00537    container->nel++;
00538    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00539    return 0;
00540 }
00541 
00549 int 
00550 easydbus_struct_add_dict_entry (EasyDbus_container * container, 
00551                                 EasyDbus_container * dict_entry) 
00552 {
00553    struct EasyDbus_elem *el = NULL;
00554    struct EasyDbus_elem *el2 = NULL;
00555 
00556    if (!container || !dict_entry ||
00557        container->container_type != EASYDBUS_ELTYPE_STRUCT ||
00558        dict_entry->container_type != EASYDBUS_ELTYPE_DICT_ENTRY)
00559       return -1;
00560 
00561    el = easydbus_elem_create (EASYDBUS_ELTYPE_DICT_ENTRY);
00562    if (!el)
00563       return -1;;
00564 
00565    el->payload.p_container = dict_entry;
00566 
00567    if (container->nel) {
00568       el2 = (struct EasyDbus_elem *) container->p_data;
00569       while (el2->next)
00570          el2 = el2->next;
00571       el2->next = el;
00572    }
00573    else
00574       container->p_data = (void *) el;
00575 
00576    container->nel++;
00577    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00578    return 0;
00579 }
00580 
00581 
00589 int
00590 easydbus_struct_add_array (EasyDbus_container * container, 
00591                            EasyDbus_container * array) 
00592 {
00593    struct EasyDbus_elem *el = NULL;
00594    struct EasyDbus_elem *el2 = NULL;
00595 
00596    if (!container || !array ||
00597        container->container_type != EASYDBUS_ELTYPE_STRUCT ||
00598        array->container_type != EASYDBUS_ELTYPE_ARRAY)
00599       return -1;
00600 
00601    el = easydbus_elem_create (EASYDBUS_ELTYPE_ARRAY);
00602    if (!el)
00603       return -1;
00604 
00605    el->payload.p_container = array;
00606 
00607    if (container->nel) {
00608       el2 = (struct EasyDbus_elem *) container->p_data;
00609       while (el2->next)
00610          el2 = el2->next;
00611       el2->next = el;
00612    }
00613    else
00614       container->p_data = (void *) el;
00615 
00616    container->nel++;
00617    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00618    return 0;
00619 }
00620 
00628 int 
00629 easydbus_struct_add_variant (EasyDbus_container * container, 
00630                              EasyDbus_container * variant) 
00631 {
00632    struct EasyDbus_elem *el = NULL;
00633    struct EasyDbus_elem *el2 = NULL;
00634 
00635    if (!container || !variant ||
00636        container->container_type != EASYDBUS_ELTYPE_STRUCT ||
00637        variant->container_type != EASYDBUS_ELTYPE_VARIANT)
00638       return -1;
00639 
00640    el = easydbus_elem_create (EASYDBUS_ELTYPE_VARIANT);
00641    if (!el)
00642       return -1;;
00643 
00644    el->payload.p_container = variant;
00645 
00646    if (container->nel) {
00647       el2 = (struct EasyDbus_elem *) container->p_data;
00648       while (el2->next)
00649          el2 = el2->next;
00650       el2->next = el;
00651    }
00652    else
00653       container->p_data = (void *) el;
00654 
00655    container->nel++;
00656    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00657    return 0;
00658 }
00659 
00667 int
00668 easydbus_struct_add_struct (EasyDbus_container * container,
00669                             EasyDbus_container * struct_internal) 
00670 {
00671    struct EasyDbus_elem *el = NULL;
00672    struct EasyDbus_elem *el2 = NULL;
00673 
00674    if (!container || !struct_internal ||
00675        container->container_type != EASYDBUS_ELTYPE_STRUCT ||
00676        struct_internal->container_type != EASYDBUS_ELTYPE_STRUCT)
00677       return -1;
00678 
00679    el = easydbus_elem_create (EASYDBUS_ELTYPE_STRUCT);
00680    if (!el)
00681       return -1;;
00682 
00683    el->payload.p_container = struct_internal;
00684 
00685    if (container->nel) {
00686       el2 = (struct EasyDbus_elem *) container->p_data;
00687       while (el2->next)
00688          el2 = el2->next;
00689       el2->next = el;
00690    }
00691    else
00692       container->p_data = (void *) el;
00693 
00694    container->nel++;
00695    container->elem_type = EASYDBUS_ELTYPE_INVALID;
00696    return 0;
00697 }
00698 
00709 const EasyDbus_elem *
00710 easydbus_struct_get_element (EasyDbus_container * container,
00711                              unsigned int n)
00712 {
00713    unsigned int i;
00714    EasyDbus_elem *el = NULL;
00715 
00716    if (!container ||
00717        container->container_type != EASYDBUS_ELTYPE_STRUCT ||
00718        n > container->nel)
00719       return NULL;
00720 
00721    for (i = 1, el = (EasyDbus_elem *) container->p_data;
00722         i < n; el = el->next, i++);
00723 
00724    return (const EasyDbus_elem *) el;
00725 }
00726 
00727 // vim: ts=3 shiftwidth=3 expandtab

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