~ubuntu-branches/ubuntu/precise/nvidia-settings/precise-proposed

« back to all changes in this revision

Viewing changes to src/jansson/value.c

  • Committer: Package Import Robot
  • Author(s): Alberto Milone
  • Date: 2013-12-11 15:23:40 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20131211152340-6j6x4ldvu4ll1bu1
Tags: 331.20-0ubuntu0.0.1
* debian/patches/series:
  - Do not apply 01_allow_dark_themes.dpatch.
* debian/patches/07_remove_features_for_legacy.patch:
  - Do not expose features that are not available in the legacy
    drivers.
* debian/patches/08_add_prime_support.patch:
  - Add support for PRIME switching. An additional
    tab provides support for switching between GPUs.
    This is only visible if nvidia-prime (>= 0.5) is
    installed and reports that the system supports
    hybrid graphics. No hard dependency on nvidia-prime
    is therefore required (LP: #1259237).
* debian/patches/09_do_not_complain_if_nvidia_is_missing.patch:
  - Disable the warning dialog since it suggests to run
    nvidia-xconfig, which we don't need. This would also break
    PRIME.
* debian/control.in, debian/postinst.in, debian/postrm.in,
  debian/prerm.in, debian/rules:
  - Drop alternatives and remove templates, as we only have
    one nvidia-settings for all the driver flavours.
* debian/dirs, debian/install,
  debian/nvidia-settings-autostart.desktop,
  debian/nvidia-settings.desktop:
  - Install the icon and the desktop files.
* debian/control:
  - Add ${misc:Depends}.
  - Build depend on libvdpau-dev and depend on libvdpau1.
  - Create transitional packages for 319, 319-updates, 313-updates,
    310, 310-updates, 304, 304-updates, experimental-304, updates.
  - Remove lpia.
  - Depend on screen-resolution-extra (>= 0.14ubuntu2.1).
* debian/rules:
  - Pass the destdir argument in uppercase to match the variable.
  - Add download-sources target.
  - Clean action in rules to target "clean" instead of "distclean".
  - Do not compress .c and .mk files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
 
3
 *
 
4
 * Jansson is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the MIT license. See LICENSE for details.
 
6
 */
 
7
 
 
8
#define _GNU_SOURCE
 
9
 
 
10
#include <stddef.h>
 
11
#include <stdlib.h>
 
12
#include <string.h>
 
13
#include <math.h>
 
14
 
 
15
#include "jansson.h"
 
16
#include "hashtable.h"
 
17
#include "jansson_private.h"
 
18
#include "utf.h"
 
19
 
 
20
/* Work around nonstandard isnan() and isinf() implementations */
 
21
#ifndef isnan
 
22
static JSON_INLINE int jansson_isnan(double x) { return x != x; }
 
23
# define isnan jansson_isnan
 
24
#endif
 
25
#ifndef isinf
 
26
static JSON_INLINE int jansson_isinf(double x) { return !isnan(x) && isnan(x - x); }
 
27
# define isinf jansson_isinf
 
28
#endif
 
29
 
 
30
static JSON_INLINE void json_init(json_t *json, json_type type)
 
31
{
 
32
    json->type = type;
 
33
    json->refcount = 1;
 
34
}
 
35
 
 
36
 
 
37
/*** object ***/
 
38
 
 
39
json_t *json_object(void)
 
40
{
 
41
    json_object_t *object = jsonp_malloc(sizeof(json_object_t));
 
42
    if(!object)
 
43
        return NULL;
 
44
    json_init(&object->json, JSON_OBJECT);
 
45
 
 
46
    if(hashtable_init(&object->hashtable))
 
47
    {
 
48
        jsonp_free(object);
 
49
        return NULL;
 
50
    }
 
51
 
 
52
    object->serial = 0;
 
53
    object->visited = 0;
 
54
 
 
55
    return &object->json;
 
56
}
 
57
 
 
58
static void json_delete_object(json_object_t *object)
 
59
{
 
60
    hashtable_close(&object->hashtable);
 
61
    jsonp_free(object);
 
62
}
 
63
 
 
64
size_t json_object_size(const json_t *json)
 
65
{
 
66
    json_object_t *object;
 
67
 
 
68
    if(!json_is_object(json))
 
69
        return 0;
 
70
 
 
71
    object = json_to_object(json);
 
72
    return object->hashtable.size;
 
73
}
 
74
 
 
75
json_t *json_object_get(const json_t *json, const char *key)
 
76
{
 
77
    json_object_t *object;
 
78
 
 
79
    if(!json_is_object(json))
 
80
        return NULL;
 
81
 
 
82
    object = json_to_object(json);
 
83
    return hashtable_get(&object->hashtable, key);
 
84
}
 
85
 
 
86
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
 
87
{
 
88
    json_object_t *object;
 
89
 
 
90
    if(!value)
 
91
        return -1;
 
92
 
 
93
    if(!key || !json_is_object(json) || json == value)
 
94
    {
 
95
        json_decref(value);
 
96
        return -1;
 
97
    }
 
98
    object = json_to_object(json);
 
99
 
 
100
    if(hashtable_set(&object->hashtable, key, object->serial++, value))
 
101
    {
 
102
        json_decref(value);
 
103
        return -1;
 
104
    }
 
105
 
 
106
    return 0;
 
107
}
 
108
 
 
109
int json_object_set_new(json_t *json, const char *key, json_t *value)
 
110
{
 
111
    if(!key || !utf8_check_string(key, -1))
 
112
    {
 
113
        json_decref(value);
 
114
        return -1;
 
115
    }
 
116
 
 
117
    return json_object_set_new_nocheck(json, key, value);
 
118
}
 
119
 
 
120
int json_object_del(json_t *json, const char *key)
 
121
{
 
122
    json_object_t *object;
 
123
 
 
124
    if(!json_is_object(json))
 
125
        return -1;
 
126
 
 
127
    object = json_to_object(json);
 
128
    return hashtable_del(&object->hashtable, key);
 
129
}
 
130
 
 
131
int json_object_clear(json_t *json)
 
132
{
 
133
    json_object_t *object;
 
134
 
 
135
    if(!json_is_object(json))
 
136
        return -1;
 
137
 
 
138
    object = json_to_object(json);
 
139
 
 
140
    hashtable_clear(&object->hashtable);
 
141
    object->serial = 0;
 
142
 
 
143
    return 0;
 
144
}
 
145
 
 
146
int json_object_update(json_t *object, json_t *other)
 
147
{
 
148
    const char *key;
 
149
    json_t *value;
 
150
 
 
151
    if(!json_is_object(object) || !json_is_object(other))
 
152
        return -1;
 
153
 
 
154
    json_object_foreach(other, key, value) {
 
155
        if(json_object_set_nocheck(object, key, value))
 
156
            return -1;
 
157
    }
 
158
 
 
159
    return 0;
 
160
}
 
161
 
 
162
int json_object_update_existing(json_t *object, json_t *other)
 
163
{
 
164
    const char *key;
 
165
    json_t *value;
 
166
 
 
167
    if(!json_is_object(object) || !json_is_object(other))
 
168
        return -1;
 
169
 
 
170
    json_object_foreach(other, key, value) {
 
171
        if(json_object_get(object, key))
 
172
            json_object_set_nocheck(object, key, value);
 
173
    }
 
174
 
 
175
    return 0;
 
176
}
 
177
 
 
178
int json_object_update_missing(json_t *object, json_t *other)
 
179
{
 
180
    const char *key;
 
181
    json_t *value;
 
182
 
 
183
    if(!json_is_object(object) || !json_is_object(other))
 
184
        return -1;
 
185
 
 
186
    json_object_foreach(other, key, value) {
 
187
        if(!json_object_get(object, key))
 
188
            json_object_set_nocheck(object, key, value);
 
189
    }
 
190
 
 
191
    return 0;
 
192
}
 
193
 
 
194
void *json_object_iter(json_t *json)
 
195
{
 
196
    json_object_t *object;
 
197
 
 
198
    if(!json_is_object(json))
 
199
        return NULL;
 
200
 
 
201
    object = json_to_object(json);
 
202
    return hashtable_iter(&object->hashtable);
 
203
}
 
204
 
 
205
void *json_object_iter_at(json_t *json, const char *key)
 
206
{
 
207
    json_object_t *object;
 
208
 
 
209
    if(!key || !json_is_object(json))
 
210
        return NULL;
 
211
 
 
212
    object = json_to_object(json);
 
213
    return hashtable_iter_at(&object->hashtable, key);
 
214
}
 
215
 
 
216
void *json_object_iter_next(json_t *json, void *iter)
 
217
{
 
218
    json_object_t *object;
 
219
 
 
220
    if(!json_is_object(json) || iter == NULL)
 
221
        return NULL;
 
222
 
 
223
    object = json_to_object(json);
 
224
    return hashtable_iter_next(&object->hashtable, iter);
 
225
}
 
226
 
 
227
const char *json_object_iter_key(void *iter)
 
228
{
 
229
    if(!iter)
 
230
        return NULL;
 
231
 
 
232
    return hashtable_iter_key(iter);
 
233
}
 
234
 
 
235
json_t *json_object_iter_value(void *iter)
 
236
{
 
237
    if(!iter)
 
238
        return NULL;
 
239
 
 
240
    return (json_t *)hashtable_iter_value(iter);
 
241
}
 
242
 
 
243
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
 
244
{
 
245
    if(!json_is_object(json) || !iter || !value)
 
246
        return -1;
 
247
 
 
248
    hashtable_iter_set(iter, value);
 
249
    return 0;
 
250
}
 
251
 
 
252
void *json_object_key_to_iter(const char *key)
 
253
{
 
254
    if(!key)
 
255
        return NULL;
 
256
 
 
257
    return hashtable_key_to_iter(key);
 
258
}
 
259
 
 
260
static int json_object_equal(json_t *object1, json_t *object2)
 
261
{
 
262
    const char *key;
 
263
    json_t *value1, *value2;
 
264
 
 
265
    if(json_object_size(object1) != json_object_size(object2))
 
266
        return 0;
 
267
 
 
268
    json_object_foreach(object1, key, value1) {
 
269
        value2 = json_object_get(object2, key);
 
270
 
 
271
        if(!json_equal(value1, value2))
 
272
            return 0;
 
273
    }
 
274
 
 
275
    return 1;
 
276
}
 
277
 
 
278
static json_t *json_object_copy(json_t *object)
 
279
{
 
280
    json_t *result;
 
281
 
 
282
    const char *key;
 
283
    json_t *value;
 
284
 
 
285
    result = json_object();
 
286
    if(!result)
 
287
        return NULL;
 
288
 
 
289
    json_object_foreach(object, key, value)
 
290
        json_object_set_nocheck(result, key, value);
 
291
 
 
292
    return result;
 
293
}
 
294
 
 
295
static json_t *json_object_deep_copy(json_t *object)
 
296
{
 
297
    json_t *result;
 
298
 
 
299
    const char *key;
 
300
    json_t *value;
 
301
 
 
302
    result = json_object();
 
303
    if(!result)
 
304
        return NULL;
 
305
 
 
306
    json_object_foreach(object, key, value)
 
307
        json_object_set_new_nocheck(result, key, json_deep_copy(value));
 
308
 
 
309
    return result;
 
310
}
 
311
 
 
312
 
 
313
/*** array ***/
 
314
 
 
315
json_t *json_array(void)
 
316
{
 
317
    json_array_t *array = jsonp_malloc(sizeof(json_array_t));
 
318
    if(!array)
 
319
        return NULL;
 
320
    json_init(&array->json, JSON_ARRAY);
 
321
 
 
322
    array->entries = 0;
 
323
    array->size = 8;
 
324
 
 
325
    array->table = jsonp_malloc(array->size * sizeof(json_t *));
 
326
    if(!array->table) {
 
327
        jsonp_free(array);
 
328
        return NULL;
 
329
    }
 
330
 
 
331
    array->visited = 0;
 
332
 
 
333
    return &array->json;
 
334
}
 
335
 
 
336
static void json_delete_array(json_array_t *array)
 
337
{
 
338
    size_t i;
 
339
 
 
340
    for(i = 0; i < array->entries; i++)
 
341
        json_decref(array->table[i]);
 
342
 
 
343
    jsonp_free(array->table);
 
344
    jsonp_free(array);
 
345
}
 
346
 
 
347
size_t json_array_size(const json_t *json)
 
348
{
 
349
    if(!json_is_array(json))
 
350
        return 0;
 
351
 
 
352
    return json_to_array(json)->entries;
 
353
}
 
354
 
 
355
json_t *json_array_get(const json_t *json, size_t index)
 
356
{
 
357
    json_array_t *array;
 
358
    if(!json_is_array(json))
 
359
        return NULL;
 
360
    array = json_to_array(json);
 
361
 
 
362
    if(index >= array->entries)
 
363
        return NULL;
 
364
 
 
365
    return array->table[index];
 
366
}
 
367
 
 
368
int json_array_set_new(json_t *json, size_t index, json_t *value)
 
369
{
 
370
    json_array_t *array;
 
371
 
 
372
    if(!value)
 
373
        return -1;
 
374
 
 
375
    if(!json_is_array(json) || json == value)
 
376
    {
 
377
        json_decref(value);
 
378
        return -1;
 
379
    }
 
380
    array = json_to_array(json);
 
381
 
 
382
    if(index >= array->entries)
 
383
    {
 
384
        json_decref(value);
 
385
        return -1;
 
386
    }
 
387
 
 
388
    json_decref(array->table[index]);
 
389
    array->table[index] = value;
 
390
 
 
391
    return 0;
 
392
}
 
393
 
 
394
static void array_move(json_array_t *array, size_t dest,
 
395
                       size_t src, size_t count)
 
396
{
 
397
    memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
 
398
}
 
399
 
 
400
static void array_copy(json_t **dest, size_t dpos,
 
401
                       json_t **src, size_t spos,
 
402
                       size_t count)
 
403
{
 
404
    memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
 
405
}
 
406
 
 
407
static json_t **json_array_grow(json_array_t *array,
 
408
                                size_t amount,
 
409
                                int copy)
 
410
{
 
411
    size_t new_size;
 
412
    json_t **old_table, **new_table;
 
413
 
 
414
    if(array->entries + amount <= array->size)
 
415
        return array->table;
 
416
 
 
417
    old_table = array->table;
 
418
 
 
419
    new_size = max(array->size + amount, array->size * 2);
 
420
    new_table = jsonp_malloc(new_size * sizeof(json_t *));
 
421
    if(!new_table)
 
422
        return NULL;
 
423
 
 
424
    array->size = new_size;
 
425
    array->table = new_table;
 
426
 
 
427
    if(copy) {
 
428
        array_copy(array->table, 0, old_table, 0, array->entries);
 
429
        jsonp_free(old_table);
 
430
        return array->table;
 
431
    }
 
432
 
 
433
    return old_table;
 
434
}
 
435
 
 
436
int json_array_append_new(json_t *json, json_t *value)
 
437
{
 
438
    json_array_t *array;
 
439
 
 
440
    if(!value)
 
441
        return -1;
 
442
 
 
443
    if(!json_is_array(json) || json == value)
 
444
    {
 
445
        json_decref(value);
 
446
        return -1;
 
447
    }
 
448
    array = json_to_array(json);
 
449
 
 
450
    if(!json_array_grow(array, 1, 1)) {
 
451
        json_decref(value);
 
452
        return -1;
 
453
    }
 
454
 
 
455
    array->table[array->entries] = value;
 
456
    array->entries++;
 
457
 
 
458
    return 0;
 
459
}
 
460
 
 
461
int json_array_insert_new(json_t *json, size_t index, json_t *value)
 
462
{
 
463
    json_array_t *array;
 
464
    json_t **old_table;
 
465
 
 
466
    if(!value)
 
467
        return -1;
 
468
 
 
469
    if(!json_is_array(json) || json == value) {
 
470
        json_decref(value);
 
471
        return -1;
 
472
    }
 
473
    array = json_to_array(json);
 
474
 
 
475
    if(index > array->entries) {
 
476
        json_decref(value);
 
477
        return -1;
 
478
    }
 
479
 
 
480
    old_table = json_array_grow(array, 1, 0);
 
481
    if(!old_table) {
 
482
        json_decref(value);
 
483
        return -1;
 
484
    }
 
485
 
 
486
    if(old_table != array->table) {
 
487
        array_copy(array->table, 0, old_table, 0, index);
 
488
        array_copy(array->table, index + 1, old_table, index,
 
489
                   array->entries - index);
 
490
        jsonp_free(old_table);
 
491
    }
 
492
    else
 
493
        array_move(array, index + 1, index, array->entries - index);
 
494
 
 
495
    array->table[index] = value;
 
496
    array->entries++;
 
497
 
 
498
    return 0;
 
499
}
 
500
 
 
501
int json_array_remove(json_t *json, size_t index)
 
502
{
 
503
    json_array_t *array;
 
504
 
 
505
    if(!json_is_array(json))
 
506
        return -1;
 
507
    array = json_to_array(json);
 
508
 
 
509
    if(index >= array->entries)
 
510
        return -1;
 
511
 
 
512
    json_decref(array->table[index]);
 
513
 
 
514
    array_move(array, index, index + 1, array->entries - index);
 
515
    array->entries--;
 
516
 
 
517
    return 0;
 
518
}
 
519
 
 
520
int json_array_clear(json_t *json)
 
521
{
 
522
    json_array_t *array;
 
523
    size_t i;
 
524
 
 
525
    if(!json_is_array(json))
 
526
        return -1;
 
527
    array = json_to_array(json);
 
528
 
 
529
    for(i = 0; i < array->entries; i++)
 
530
        json_decref(array->table[i]);
 
531
 
 
532
    array->entries = 0;
 
533
    return 0;
 
534
}
 
535
 
 
536
int json_array_extend(json_t *json, json_t *other_json)
 
537
{
 
538
    json_array_t *array, *other;
 
539
    size_t i;
 
540
 
 
541
    if(!json_is_array(json) || !json_is_array(other_json))
 
542
        return -1;
 
543
    array = json_to_array(json);
 
544
    other = json_to_array(other_json);
 
545
 
 
546
    if(!json_array_grow(array, other->entries, 1))
 
547
        return -1;
 
548
 
 
549
    for(i = 0; i < other->entries; i++)
 
550
        json_incref(other->table[i]);
 
551
 
 
552
    array_copy(array->table, array->entries, other->table, 0, other->entries);
 
553
 
 
554
    array->entries += other->entries;
 
555
    return 0;
 
556
}
 
557
 
 
558
static int json_array_equal(json_t *array1, json_t *array2)
 
559
{
 
560
    size_t i, size;
 
561
 
 
562
    size = json_array_size(array1);
 
563
    if(size != json_array_size(array2))
 
564
        return 0;
 
565
 
 
566
    for(i = 0; i < size; i++)
 
567
    {
 
568
        json_t *value1, *value2;
 
569
 
 
570
        value1 = json_array_get(array1, i);
 
571
        value2 = json_array_get(array2, i);
 
572
 
 
573
        if(!json_equal(value1, value2))
 
574
            return 0;
 
575
    }
 
576
 
 
577
    return 1;
 
578
}
 
579
 
 
580
static json_t *json_array_copy(json_t *array)
 
581
{
 
582
    json_t *result;
 
583
    size_t i;
 
584
 
 
585
    result = json_array();
 
586
    if(!result)
 
587
        return NULL;
 
588
 
 
589
    for(i = 0; i < json_array_size(array); i++)
 
590
        json_array_append(result, json_array_get(array, i));
 
591
 
 
592
    return result;
 
593
}
 
594
 
 
595
static json_t *json_array_deep_copy(json_t *array)
 
596
{
 
597
    json_t *result;
 
598
    size_t i;
 
599
 
 
600
    result = json_array();
 
601
    if(!result)
 
602
        return NULL;
 
603
 
 
604
    for(i = 0; i < json_array_size(array); i++)
 
605
        json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
 
606
 
 
607
    return result;
 
608
}
 
609
 
 
610
/*** string ***/
 
611
 
 
612
json_t *json_string_nocheck(const char *value)
 
613
{
 
614
    json_string_t *string;
 
615
 
 
616
    if(!value)
 
617
        return NULL;
 
618
 
 
619
    string = jsonp_malloc(sizeof(json_string_t));
 
620
    if(!string)
 
621
        return NULL;
 
622
    json_init(&string->json, JSON_STRING);
 
623
 
 
624
    string->value = jsonp_strdup(value);
 
625
    if(!string->value) {
 
626
        jsonp_free(string);
 
627
        return NULL;
 
628
    }
 
629
 
 
630
    return &string->json;
 
631
}
 
632
 
 
633
json_t *json_string(const char *value)
 
634
{
 
635
    if(!value || !utf8_check_string(value, -1))
 
636
        return NULL;
 
637
 
 
638
    return json_string_nocheck(value);
 
639
}
 
640
 
 
641
const char *json_string_value(const json_t *json)
 
642
{
 
643
    if(!json_is_string(json))
 
644
        return NULL;
 
645
 
 
646
    return json_to_string(json)->value;
 
647
}
 
648
 
 
649
int json_string_set_nocheck(json_t *json, const char *value)
 
650
{
 
651
    char *dup;
 
652
    json_string_t *string;
 
653
 
 
654
    if(!json_is_string(json) || !value)
 
655
        return -1;
 
656
 
 
657
    dup = jsonp_strdup(value);
 
658
    if(!dup)
 
659
        return -1;
 
660
 
 
661
    string = json_to_string(json);
 
662
    jsonp_free(string->value);
 
663
    string->value = dup;
 
664
 
 
665
    return 0;
 
666
}
 
667
 
 
668
int json_string_set(json_t *json, const char *value)
 
669
{
 
670
    if(!value || !utf8_check_string(value, -1))
 
671
        return -1;
 
672
 
 
673
    return json_string_set_nocheck(json, value);
 
674
}
 
675
 
 
676
static void json_delete_string(json_string_t *string)
 
677
{
 
678
    jsonp_free(string->value);
 
679
    jsonp_free(string);
 
680
}
 
681
 
 
682
static int json_string_equal(json_t *string1, json_t *string2)
 
683
{
 
684
    return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
 
685
}
 
686
 
 
687
static json_t *json_string_copy(json_t *string)
 
688
{
 
689
    return json_string_nocheck(json_string_value(string));
 
690
}
 
691
 
 
692
 
 
693
/*** integer ***/
 
694
 
 
695
json_t *json_integer(json_int_t value)
 
696
{
 
697
    json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
 
698
    if(!integer)
 
699
        return NULL;
 
700
    json_init(&integer->json, JSON_INTEGER);
 
701
 
 
702
    integer->value = value;
 
703
    return &integer->json;
 
704
}
 
705
 
 
706
json_int_t json_integer_value(const json_t *json)
 
707
{
 
708
    if(!json_is_integer(json))
 
709
        return 0;
 
710
 
 
711
    return json_to_integer(json)->value;
 
712
}
 
713
 
 
714
int json_integer_set(json_t *json, json_int_t value)
 
715
{
 
716
    if(!json_is_integer(json))
 
717
        return -1;
 
718
 
 
719
    json_to_integer(json)->value = value;
 
720
 
 
721
    return 0;
 
722
}
 
723
 
 
724
static void json_delete_integer(json_integer_t *integer)
 
725
{
 
726
    jsonp_free(integer);
 
727
}
 
728
 
 
729
static int json_integer_equal(json_t *integer1, json_t *integer2)
 
730
{
 
731
    return json_integer_value(integer1) == json_integer_value(integer2);
 
732
}
 
733
 
 
734
static json_t *json_integer_copy(json_t *integer)
 
735
{
 
736
    return json_integer(json_integer_value(integer));
 
737
}
 
738
 
 
739
 
 
740
/*** real ***/
 
741
 
 
742
json_t *json_real(double value)
 
743
{
 
744
    json_real_t *real;
 
745
 
 
746
    if(isnan(value) || isinf(value))
 
747
        return NULL;
 
748
 
 
749
    real = jsonp_malloc(sizeof(json_real_t));
 
750
    if(!real)
 
751
        return NULL;
 
752
    json_init(&real->json, JSON_REAL);
 
753
 
 
754
    real->value = value;
 
755
    return &real->json;
 
756
}
 
757
 
 
758
double json_real_value(const json_t *json)
 
759
{
 
760
    if(!json_is_real(json))
 
761
        return 0;
 
762
 
 
763
    return json_to_real(json)->value;
 
764
}
 
765
 
 
766
int json_real_set(json_t *json, double value)
 
767
{
 
768
    if(!json_is_real(json) || isnan(value) || isinf(value))
 
769
        return -1;
 
770
 
 
771
    json_to_real(json)->value = value;
 
772
 
 
773
    return 0;
 
774
}
 
775
 
 
776
static void json_delete_real(json_real_t *real)
 
777
{
 
778
    jsonp_free(real);
 
779
}
 
780
 
 
781
static int json_real_equal(json_t *real1, json_t *real2)
 
782
{
 
783
    return json_real_value(real1) == json_real_value(real2);
 
784
}
 
785
 
 
786
static json_t *json_real_copy(json_t *real)
 
787
{
 
788
    return json_real(json_real_value(real));
 
789
}
 
790
 
 
791
 
 
792
/*** number ***/
 
793
 
 
794
double json_number_value(const json_t *json)
 
795
{
 
796
    if(json_is_integer(json))
 
797
        return (double)json_integer_value(json);
 
798
    else if(json_is_real(json))
 
799
        return json_real_value(json);
 
800
    else
 
801
        return 0.0;
 
802
}
 
803
 
 
804
 
 
805
/*** simple values ***/
 
806
 
 
807
json_t *json_true(void)
 
808
{
 
809
    static json_t the_true = {JSON_TRUE, (size_t)-1};
 
810
    return &the_true;
 
811
}
 
812
 
 
813
 
 
814
json_t *json_false(void)
 
815
{
 
816
    static json_t the_false = {JSON_FALSE, (size_t)-1};
 
817
    return &the_false;
 
818
}
 
819
 
 
820
 
 
821
json_t *json_null(void)
 
822
{
 
823
    static json_t the_null = {JSON_NULL, (size_t)-1};
 
824
    return &the_null;
 
825
}
 
826
 
 
827
 
 
828
/*** deletion ***/
 
829
 
 
830
void json_delete(json_t *json)
 
831
{
 
832
    if(json_is_object(json))
 
833
        json_delete_object(json_to_object(json));
 
834
 
 
835
    else if(json_is_array(json))
 
836
        json_delete_array(json_to_array(json));
 
837
 
 
838
    else if(json_is_string(json))
 
839
        json_delete_string(json_to_string(json));
 
840
 
 
841
    else if(json_is_integer(json))
 
842
        json_delete_integer(json_to_integer(json));
 
843
 
 
844
    else if(json_is_real(json))
 
845
        json_delete_real(json_to_real(json));
 
846
 
 
847
    /* json_delete is not called for true, false or null */
 
848
}
 
849
 
 
850
 
 
851
/*** equality ***/
 
852
 
 
853
int json_equal(json_t *json1, json_t *json2)
 
854
{
 
855
    if(!json1 || !json2)
 
856
        return 0;
 
857
 
 
858
    if(json_typeof(json1) != json_typeof(json2))
 
859
        return 0;
 
860
 
 
861
    /* this covers true, false and null as they are singletons */
 
862
    if(json1 == json2)
 
863
        return 1;
 
864
 
 
865
    if(json_is_object(json1))
 
866
        return json_object_equal(json1, json2);
 
867
 
 
868
    if(json_is_array(json1))
 
869
        return json_array_equal(json1, json2);
 
870
 
 
871
    if(json_is_string(json1))
 
872
        return json_string_equal(json1, json2);
 
873
 
 
874
    if(json_is_integer(json1))
 
875
        return json_integer_equal(json1, json2);
 
876
 
 
877
    if(json_is_real(json1))
 
878
        return json_real_equal(json1, json2);
 
879
 
 
880
    return 0;
 
881
}
 
882
 
 
883
 
 
884
/*** copying ***/
 
885
 
 
886
json_t *json_copy(json_t *json)
 
887
{
 
888
    if(!json)
 
889
        return NULL;
 
890
 
 
891
    if(json_is_object(json))
 
892
        return json_object_copy(json);
 
893
 
 
894
    if(json_is_array(json))
 
895
        return json_array_copy(json);
 
896
 
 
897
    if(json_is_string(json))
 
898
        return json_string_copy(json);
 
899
 
 
900
    if(json_is_integer(json))
 
901
        return json_integer_copy(json);
 
902
 
 
903
    if(json_is_real(json))
 
904
        return json_real_copy(json);
 
905
 
 
906
    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
 
907
        return json;
 
908
 
 
909
    return NULL;
 
910
}
 
911
 
 
912
json_t *json_deep_copy(json_t *json)
 
913
{
 
914
    if(!json)
 
915
        return NULL;
 
916
 
 
917
    if(json_is_object(json))
 
918
        return json_object_deep_copy(json);
 
919
 
 
920
    if(json_is_array(json))
 
921
        return json_array_deep_copy(json);
 
922
 
 
923
    /* for the rest of the types, deep copying doesn't differ from
 
924
       shallow copying */
 
925
 
 
926
    if(json_is_string(json))
 
927
        return json_string_copy(json);
 
928
 
 
929
    if(json_is_integer(json))
 
930
        return json_integer_copy(json);
 
931
 
 
932
    if(json_is_real(json))
 
933
        return json_real_copy(json);
 
934
 
 
935
    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
 
936
        return json;
 
937
 
 
938
    return NULL;
 
939
}