Change the "length of bstream" data type to be a gsize, since it represents
[pidgin-git.git] / libpurple / value.c
blobc7002c60edd5393d10f6faaa0b2eee089815497b
1 /**
2 * @file value.c Value wrapper API
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "internal.h"
28 #include "value.h"
30 #define OUTGOING_FLAG 0x01
32 PurpleValue *
33 purple_value_new(PurpleType type, ...)
35 PurpleValue *value;
36 va_list args;
38 g_return_val_if_fail(type != PURPLE_TYPE_UNKNOWN, NULL);
40 value = g_new0(PurpleValue, 1);
42 value->type = type;
44 va_start(args, type);
46 if (type == PURPLE_TYPE_SUBTYPE)
47 value->u.subtype = va_arg(args, int);
48 else if (type == PURPLE_TYPE_BOXED)
49 value->u.specific_type = g_strdup(va_arg(args, char *));
51 va_end(args);
53 return value;
56 PurpleValue *
57 purple_value_new_outgoing(PurpleType type, ...)
59 PurpleValue *value;
60 va_list args;
62 g_return_val_if_fail(type != PURPLE_TYPE_UNKNOWN, NULL);
64 value = g_new0(PurpleValue, 1);
66 value->type = type;
68 va_start(args, type);
70 if (type == PURPLE_TYPE_SUBTYPE)
71 value->u.subtype = va_arg(args, int);
72 else if (type == PURPLE_TYPE_BOXED)
73 value->u.specific_type = g_strdup(va_arg(args, char *));
75 va_end(args);
77 value->flags |= OUTGOING_FLAG;
79 return value;
82 void
83 purple_value_destroy(PurpleValue *value)
85 g_return_if_fail(value != NULL);
87 if (purple_value_get_type(value) == PURPLE_TYPE_BOXED)
89 g_free(value->u.specific_type);
91 else if (purple_value_get_type(value) == PURPLE_TYPE_STRING)
93 g_free(value->data.string_data);
96 g_free(value);
99 PurpleValue *
100 purple_value_dup(const PurpleValue *value)
102 PurpleValue *new_value;
103 PurpleType type;
105 g_return_val_if_fail(value != NULL, NULL);
107 type = purple_value_get_type(value);
109 if (type == PURPLE_TYPE_SUBTYPE)
111 new_value = purple_value_new(PURPLE_TYPE_SUBTYPE,
112 purple_value_get_subtype(value));
114 else if (type == PURPLE_TYPE_BOXED)
116 new_value = purple_value_new(PURPLE_TYPE_BOXED,
117 purple_value_get_specific_type(value));
119 else
120 new_value = purple_value_new(type);
122 new_value->flags = value->flags;
124 switch (type)
126 case PURPLE_TYPE_CHAR:
127 purple_value_set_char(new_value, purple_value_get_char(value));
128 break;
130 case PURPLE_TYPE_UCHAR:
131 purple_value_set_uchar(new_value, purple_value_get_uchar(value));
132 break;
134 case PURPLE_TYPE_BOOLEAN:
135 purple_value_set_boolean(new_value, purple_value_get_boolean(value));
136 break;
138 case PURPLE_TYPE_SHORT:
139 purple_value_set_short(new_value, purple_value_get_short(value));
140 break;
142 case PURPLE_TYPE_USHORT:
143 purple_value_set_ushort(new_value, purple_value_get_ushort(value));
144 break;
146 case PURPLE_TYPE_INT:
147 purple_value_set_int(new_value, purple_value_get_int(value));
148 break;
150 case PURPLE_TYPE_UINT:
151 purple_value_set_uint(new_value, purple_value_get_uint(value));
152 break;
154 case PURPLE_TYPE_LONG:
155 purple_value_set_long(new_value, purple_value_get_long(value));
156 break;
158 case PURPLE_TYPE_ULONG:
159 purple_value_set_ulong(new_value, purple_value_get_ulong(value));
160 break;
162 case PURPLE_TYPE_INT64:
163 purple_value_set_int64(new_value, purple_value_get_int64(value));
164 break;
166 case PURPLE_TYPE_UINT64:
167 purple_value_set_uint64(new_value, purple_value_get_uint64(value));
168 break;
170 case PURPLE_TYPE_STRING:
171 purple_value_set_string(new_value, purple_value_get_string(value));
172 break;
174 case PURPLE_TYPE_OBJECT:
175 purple_value_set_object(new_value, purple_value_get_object(value));
176 break;
178 case PURPLE_TYPE_POINTER:
179 purple_value_set_pointer(new_value, purple_value_get_pointer(value));
180 break;
182 case PURPLE_TYPE_ENUM:
183 purple_value_set_enum(new_value, purple_value_get_enum(value));
184 break;
186 case PURPLE_TYPE_BOXED:
187 purple_value_set_boxed(new_value, purple_value_get_boxed(value));
188 break;
190 default:
191 break;
194 return new_value;
197 PurpleType
198 purple_value_get_type(const PurpleValue *value)
200 g_return_val_if_fail(value != NULL, PURPLE_TYPE_UNKNOWN);
202 return value->type;
205 unsigned int
206 purple_value_get_subtype(const PurpleValue *value)
208 g_return_val_if_fail(value != NULL, 0);
209 g_return_val_if_fail(purple_value_get_type(value) == PURPLE_TYPE_SUBTYPE, 0);
211 return value->u.subtype;
214 const char *
215 purple_value_get_specific_type(const PurpleValue *value)
217 g_return_val_if_fail(value != NULL, NULL);
218 g_return_val_if_fail(purple_value_get_type(value) == PURPLE_TYPE_BOXED, NULL);
220 return value->u.specific_type;
223 gboolean
224 purple_value_is_outgoing(const PurpleValue *value)
226 g_return_val_if_fail(value != NULL, FALSE);
228 return (value->flags & OUTGOING_FLAG);
231 void
232 purple_value_set_char(PurpleValue *value, char data)
234 g_return_if_fail(value != NULL);
236 value->data.char_data = data;
239 void
240 purple_value_set_uchar(PurpleValue *value, unsigned char data)
242 g_return_if_fail(value != NULL);
244 value->data.uchar_data = data;
247 void
248 purple_value_set_boolean(PurpleValue *value, gboolean data)
250 g_return_if_fail(value != NULL);
252 value->data.boolean_data = data;
255 void
256 purple_value_set_short(PurpleValue *value, short data)
258 g_return_if_fail(value != NULL);
260 value->data.short_data = data;
263 void
264 purple_value_set_ushort(PurpleValue *value, unsigned short data)
266 g_return_if_fail(value != NULL);
268 value->data.ushort_data = data;
271 void
272 purple_value_set_int(PurpleValue *value, int data)
274 g_return_if_fail(value != NULL);
276 value->data.int_data = data;
279 void
280 purple_value_set_uint(PurpleValue *value, unsigned int data)
282 g_return_if_fail(value != NULL);
284 value->data.int_data = data;
287 void
288 purple_value_set_long(PurpleValue *value, long data)
290 g_return_if_fail(value != NULL);
292 value->data.long_data = data;
295 void
296 purple_value_set_ulong(PurpleValue *value, unsigned long data)
298 g_return_if_fail(value != NULL);
300 value->data.long_data = data;
303 void
304 purple_value_set_int64(PurpleValue *value, gint64 data)
306 g_return_if_fail(value != NULL);
308 value->data.int64_data = data;
311 void
312 purple_value_set_uint64(PurpleValue *value, guint64 data)
314 g_return_if_fail(value != NULL);
316 value->data.uint64_data = data;
319 void
320 purple_value_set_string(PurpleValue *value, const char *data)
322 g_return_if_fail(value != NULL);
323 g_return_if_fail(data == NULL || g_utf8_validate(data, -1, NULL));
325 g_free(value->data.string_data);
326 value->data.string_data = g_strdup(data);
329 void
330 purple_value_set_object(PurpleValue *value, void *data)
332 g_return_if_fail(value != NULL);
334 value->data.object_data = data;
337 void
338 purple_value_set_pointer(PurpleValue *value, void *data)
340 g_return_if_fail(value != NULL);
342 value->data.pointer_data = data;
345 void
346 purple_value_set_enum(PurpleValue *value, int data)
348 g_return_if_fail(value != NULL);
350 value->data.enum_data = data;
353 void
354 purple_value_set_boxed(PurpleValue *value, void *data)
356 g_return_if_fail(value != NULL);
358 value->data.boxed_data = data;
361 char
362 purple_value_get_char(const PurpleValue *value)
364 g_return_val_if_fail(value != NULL, 0);
366 return value->data.char_data;
369 unsigned char
370 purple_value_get_uchar(const PurpleValue *value)
372 g_return_val_if_fail(value != NULL, 0);
374 return value->data.uchar_data;
377 gboolean
378 purple_value_get_boolean(const PurpleValue *value)
380 g_return_val_if_fail(value != NULL, FALSE);
382 return value->data.boolean_data;
385 short
386 purple_value_get_short(const PurpleValue *value)
388 g_return_val_if_fail(value != NULL, 0);
390 return value->data.short_data;
393 unsigned short
394 purple_value_get_ushort(const PurpleValue *value)
396 g_return_val_if_fail(value != NULL, 0);
398 return value->data.ushort_data;
402 purple_value_get_int(const PurpleValue *value)
404 g_return_val_if_fail(value != NULL, 0);
406 return value->data.int_data;
409 unsigned int
410 purple_value_get_uint(const PurpleValue *value)
412 g_return_val_if_fail(value != NULL, 0);
414 return value->data.int_data;
417 long
418 purple_value_get_long(const PurpleValue *value)
420 g_return_val_if_fail(value != NULL, 0);
422 return value->data.long_data;
425 unsigned long
426 purple_value_get_ulong(const PurpleValue *value)
428 g_return_val_if_fail(value != NULL, 0);
430 return value->data.long_data;
433 gint64
434 purple_value_get_int64(const PurpleValue *value)
436 g_return_val_if_fail(value != NULL, 0);
438 return value->data.int64_data;
441 guint64
442 purple_value_get_uint64(const PurpleValue *value)
444 g_return_val_if_fail(value != NULL, 0);
446 return value->data.uint64_data;
449 const char *
450 purple_value_get_string(const PurpleValue *value)
452 g_return_val_if_fail(value != NULL, NULL);
454 return value->data.string_data;
457 void *
458 purple_value_get_object(const PurpleValue *value)
460 g_return_val_if_fail(value != NULL, NULL);
462 return value->data.object_data;
465 void *
466 purple_value_get_pointer(const PurpleValue *value)
468 g_return_val_if_fail(value != NULL, NULL);
470 return value->data.pointer_data;
474 purple_value_get_enum(const PurpleValue *value)
476 g_return_val_if_fail(value != NULL, -1);
478 return value->data.enum_data;
481 void *
482 purple_value_get_boxed(const PurpleValue *value)
484 g_return_val_if_fail(value != NULL, NULL);
486 return value->data.boxed_data;