2002-12-11 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
3  * 
4  * Copyright (C) 2002  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-sysdeps.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 /**
32  * @addtogroup DBusInternalsUtils
33  * @{
34  */
35 /**
36  * Aborts the program with SIGABRT (dumping core).
37  */
38 void
39 _dbus_abort (void)
40 {
41   abort ();
42   _exit (1); /* in case someone manages to ignore SIGABRT */
43 }
44
45 /**
46  * Wrapper for getenv().
47  *
48  * @param varname name of environment variable
49  * @returns value of environment variable or #NULL if unset
50  */
51 const char*
52 _dbus_getenv (const char *varname)
53 {  
54   return getenv (varname);
55 }
56
57 /** @} */
58
59 /**
60  * @addtogroup DBusString
61  *
62  * @{
63  */
64 /**
65  * Appends an integer to a DBusString.
66  * 
67  * @param str the string
68  * @param value the integer value
69  * @returns #FALSE if not enough memory or other failure.
70  */
71 dbus_bool_t
72 _dbus_string_append_int (DBusString *str,
73                          long        value)
74 {
75   /* this calculation is from comp.lang.c faq */
76 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1)  /* +1 for '-' */
77   int orig_len;
78   int i;
79   char *buf;
80   
81   orig_len = _dbus_string_get_length (str);
82
83   if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
84     return FALSE;
85
86   _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
87
88   snprintf (buf, MAX_LONG_LEN, "%ld", value);
89
90   i = 0;
91   while (*buf)
92     {
93       ++buf;
94       ++i;
95     }
96   
97   _dbus_string_shorten (str, MAX_LONG_LEN - i);
98   
99   return TRUE;
100 }
101
102 /**
103  * Appends a double to a DBusString.
104  * 
105  * @param str the string
106  * @param value the floating point value
107  * @returns #FALSE if not enough memory or other failure.
108  */
109 dbus_bool_t
110 _dbus_string_append_double (DBusString *str,
111                             double      value)
112 {
113 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
114   int orig_len;
115   char *buf;
116   int i;
117   
118   orig_len = _dbus_string_get_length (str);
119
120   if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
121     return FALSE;
122
123   _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
124
125   snprintf (buf, MAX_LONG_LEN, "%g", value);
126
127   i = 0;
128   while (*buf)
129     {
130       ++buf;
131       ++i;
132     }
133   
134   _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
135   
136   return TRUE;
137 }
138
139 /**
140  * Parses an integer contained in a DBusString. Either return parameter
141  * may be #NULL if you aren't interested in it. The integer is parsed
142  * and stored in value_return. Return parameters are not initialized
143  * if the function returns #FALSE.
144  *
145  * @param str the string
146  * @param start the byte index of the start of the integer
147  * @param value_return return location of the integer value or #NULL
148  * @param end_return return location of the end of the integer, or #NULL
149  * @returns #TRUE on success
150  */
151 dbus_bool_t
152 _dbus_string_parse_int (const DBusString *str,
153                         int               start,
154                         long             *value_return,
155                         int              *end_return)
156 {
157   long v;
158   const char *p;
159   char *end;
160
161   _dbus_string_get_const_data_len (str, &p, start,
162                                    _dbus_string_get_length (str) - start);
163
164   end = NULL;
165   errno = 0;
166   v = strtol (p, &end, 0);
167   if (end == NULL || end == p || errno != 0)
168     return FALSE;
169
170   if (value_return)
171     *value_return = v;
172   if (end_return)
173     *end_return = (end - p);
174
175   return TRUE;
176 }
177
178 /**
179  * Parses a floating point number contained in a DBusString. Either
180  * return parameter may be #NULL if you aren't interested in it. The
181  * integer is parsed and stored in value_return. Return parameters are
182  * not initialized if the function returns #FALSE.
183  *
184  * @todo this function is currently locale-dependent. Should
185  * ask alexl to relicense g_ascii_strtod() code and put that in
186  * here instead, so it's locale-independent.
187  *
188  * @param str the string
189  * @param start the byte index of the start of the float
190  * @param value_return return location of the float value or #NULL
191  * @param end_return return location of the end of the float, or #NULL
192  * @returns #TRUE on success
193  */
194 dbus_bool_t
195 _dbus_string_parse_double (const DBusString *str,
196                            int               start,
197                            double           *value_return,
198                            int              *end_return)
199 {
200   double v;
201   const char *p;
202   char *end;
203
204   _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
205   
206   _dbus_string_get_const_data_len (str, &p, start,
207                                    _dbus_string_get_length (str) - start);
208
209   end = NULL;
210   errno = 0;
211   v = strtod (p, &end);
212   if (end == NULL || end == p || errno != 0)
213     return FALSE;
214
215   if (value_return)
216     *value_return = v;
217   if (end_return)
218     *end_return = (end - p);
219
220   return TRUE;
221 }
222
223 /** @} end of DBusString */