Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

property.c

00001 /***************************************************************************
00002  * CVSID: $Id: property.c,v 1.3 2004/04/20 20:54:15 joe Exp $
00003  *
00004  * property.c : HalProperty methods
00005  *
00006  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00007  * Copyright (C) 2004 Novell, Inc.
00008  *
00009  * Licensed under the Academic Free License version 2.0
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  **************************************************************************/
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <glib.h>
00032 
00033 #include "property.h"
00034 
00035 struct _HalProperty {
00036     char *key;
00037 
00038     int type;
00039     union {
00040         char *str_value;
00041         dbus_int32_t int_value;
00042         dbus_bool_t bool_value;
00043         double double_value;
00044     };
00045 };
00046 
00047 void
00048 hal_property_free (HalProperty *prop)
00049 {
00050     g_free (prop->key);
00051     
00052     if (prop->type == DBUS_TYPE_STRING)
00053         g_free (prop->str_value);
00054 
00055     g_free (prop);
00056 }
00057 
00058 HalProperty *
00059 hal_property_new_string (const char *key, const char *value)
00060 {
00061     HalProperty *prop;
00062 
00063     prop = g_new0 (HalProperty, 1);
00064 
00065     prop->type = DBUS_TYPE_STRING;
00066     prop->key = g_strdup (key);
00067     prop->str_value = g_strdup (value);
00068 
00069     return prop;
00070 }
00071 
00072 HalProperty *
00073 hal_property_new_int (const char *key, dbus_int32_t value)
00074 {
00075     HalProperty *prop;
00076 
00077     prop = g_new0 (HalProperty, 1);
00078 
00079     prop->type = DBUS_TYPE_INT32;
00080     prop->key = g_strdup (key);
00081     prop->int_value = value;
00082 
00083     return prop;
00084 }
00085 
00086 HalProperty *
00087 hal_property_new_bool (const char *key, dbus_bool_t value)
00088 {
00089     HalProperty *prop;
00090 
00091     prop = g_new0 (HalProperty, 1);
00092 
00093     prop->type = DBUS_TYPE_BOOLEAN;
00094     prop->key = g_strdup (key);
00095     prop->bool_value = value;
00096 
00097     return prop;
00098 }
00099 
00100 HalProperty *
00101 hal_property_new_double (const char *key, double value)
00102 {
00103     HalProperty *prop;
00104 
00105     prop = g_new0 (HalProperty, 1);
00106 
00107     prop->type = DBUS_TYPE_DOUBLE;
00108     prop->key = g_strdup (key);
00109     prop->double_value = value;
00110 
00111     return prop;
00112 }
00113 
00114 const char *
00115 hal_property_get_key (HalProperty *prop)
00116 {
00117     g_return_val_if_fail (prop != NULL, NULL);
00118 
00119     return prop->key;
00120 }
00121 
00122 int
00123 hal_property_get_type (HalProperty *prop)
00124 {
00125     g_return_val_if_fail (prop != NULL, DBUS_TYPE_INVALID);
00126 
00127     return prop->type;
00128 }
00129 
00130 const char *
00131 hal_property_get_string (HalProperty *prop)
00132 {
00133     g_return_val_if_fail (prop != NULL, NULL);
00134     g_return_val_if_fail (prop->type == DBUS_TYPE_STRING, NULL);
00135 
00136     return prop->str_value;
00137 }
00138 
00139 dbus_int32_t
00140 hal_property_get_int (HalProperty *prop)
00141 {
00142     g_return_val_if_fail (prop != NULL, -1);
00143     g_return_val_if_fail (prop->type == DBUS_TYPE_INT32, -1);
00144 
00145     return prop->int_value;
00146 }
00147 
00148 dbus_bool_t
00149 hal_property_get_bool (HalProperty *prop)
00150 {
00151     g_return_val_if_fail (prop != NULL, -1);
00152     g_return_val_if_fail (prop->type == DBUS_TYPE_BOOLEAN, -1);
00153 
00154     return prop->bool_value;
00155 }
00156 
00157 char *
00158 hal_property_to_string (HalProperty *prop)
00159 {
00160     g_return_val_if_fail (prop != NULL, NULL);
00161 
00162     switch (prop->type) {
00163     case DBUS_TYPE_STRING:
00164         return g_strdup (prop->str_value);
00165     case DBUS_TYPE_INT32:
00166         return g_strdup_printf ("%d", prop->int_value);
00167     case DBUS_TYPE_BOOLEAN:
00168         /* FIXME: Maybe use 1 and 0 here instead? */
00169         return g_strdup (prop->bool_value ? "true" : "false");
00170     case DBUS_TYPE_DOUBLE:
00171         return g_strdup_printf ("%f", prop->double_value);
00172     default:
00173         return NULL;
00174     }
00175 }
00176 
00177 double
00178 hal_property_get_double (HalProperty *prop)
00179 {
00180     g_return_val_if_fail (prop != NULL, -1.0);
00181     g_return_val_if_fail (prop->type == DBUS_TYPE_DOUBLE, -1.0);
00182 
00183     return prop->double_value;
00184 }
00185 
00186 void
00187 hal_property_set_string (HalProperty *prop, const char *value)
00188 {
00189     g_return_if_fail (prop != NULL);
00190     g_return_if_fail (prop->type == DBUS_TYPE_STRING ||
00191               prop->type == DBUS_TYPE_NIL);
00192 
00193     prop->type = DBUS_TYPE_STRING;
00194     prop->str_value = g_strdup (value);
00195 }
00196 
00197 void
00198 hal_property_set_int (HalProperty *prop, dbus_int32_t value)
00199 {
00200     g_return_if_fail (prop != NULL);
00201     g_return_if_fail (prop->type == DBUS_TYPE_INT32 ||
00202               prop->type == DBUS_TYPE_NIL);
00203 
00204     prop->type = DBUS_TYPE_INT32;
00205     prop->int_value = value;
00206 }
00207 
00208 void
00209 hal_property_set_bool (HalProperty *prop, dbus_bool_t value)
00210 {
00211     g_return_if_fail (prop != NULL);
00212     g_return_if_fail (prop->type == DBUS_TYPE_BOOLEAN ||
00213               prop->type == DBUS_TYPE_NIL);
00214 
00215     prop->type = DBUS_TYPE_BOOLEAN;
00216     prop->bool_value = value;
00217 }
00218 
00219 void
00220 hal_property_set_double (HalProperty *prop, double value)
00221 {
00222     g_return_if_fail (prop != NULL);
00223     g_return_if_fail (prop->type == DBUS_TYPE_DOUBLE ||
00224               prop->type == DBUS_TYPE_NIL);
00225 
00226     prop->type = DBUS_TYPE_DOUBLE;
00227     prop->double_value = value;
00228 }

Generated on Sat Apr 24 19:57:45 2004 for HAL by doxygen 1.3.6-20040222