JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
JST_update.c
Go to the documentation of this file.
1 #include "jstools.h"
2 #include "JST_private.h"
3 
4 #include <string.h>
5 #include <stdlib.h>
6 
7 static JST_Error element_copy( JST_Element * dest, const JST_Element * src );
8 
9 static JST_Error array_item_copy( JST_Array * parent, JST_ArrayItem * src, JST_ArrayItem ** pdest ) {
10  *pdest = calloc( 1, sizeof( JST_ArrayItem ));
11  JST_ArrayItem * dest = *pdest;
12  if( dest == NULL ) {
13  return JST_ERR_ERRNO;
14  }
15  dest->parent = parent;
16  return element_copy( &(dest->element), &(src->element));
17 }
18 
19 static JST_Error pair_copy( JST_Object * parent, JST_Pair * src, JST_Pair ** pdest ) {
20  *pdest = calloc( 1, sizeof( JST_Pair ));
21  JST_Pair * dest = *pdest;
22  if( dest == NULL ) {
23  return JST_ERR_ERRNO;
24  }
25  dest->parent = parent;
26  dest->name = strdup( src->name );
27  return element_copy( &(dest->element), &(src->element));
28 }
29 
30 static JST_Error element_copy( JST_Element * dest, const JST_Element * src ) {
31  dest->type = src->type;
32  switch( src->type ) {
33  case JST_OBJECT:
34  dest->value.object.parent = dest;
35  dest->value.object.count = src->value.object.count;
36  dest->value.object.items = calloc( dest->value.object.count, sizeof( JST_Pair * ));
37  for( unsigned i = 0; i < src->value.object.count; ++i ) {
38  pair_copy( &(dest->value.object), src->value.object.items[i], &(dest->value.object.items[i]));
39  }
40  break;
41  case JST_ARRAY:
42  dest->value.array.parent = dest;
43  dest->value.array.count = src->value.array.count;
44  dest->value.array.items = calloc( dest->value.array.count, sizeof( JST_ArrayItem * ));
45  for( unsigned i = 0; i < src->value.array.count; ++i ) {
46  array_item_copy( &(dest->value.array), src->value.array.items[i], &(dest->value.array.items[i]));
47  }
48  break;
49  case JST_BOOLEAN: dest->value.boolean = src->value.boolean; break;
50  case JST_INTEGER: dest->value.integer = src->value.integer; break;
51  case JST_DOUBLE : dest->value.dbl = src->value.dbl; break;
52  case JST_STRING : dest->value.string = strdup( src->value.string ); break;
53  case JST_NULL : dest->value.string = NULL; break;
54  default:
55  return JST_ERR_NULL_TYPE;
56  }
57  return JST_ERR_NONE;
58 }
59 
60 JST_Error JST_add_property( JST_Object * object, unsigned index, const char * name, const JST_Element * value ) {
61  (void)index;
62  JST_Pair * item = calloc( 1, sizeof( JST_Pair ));
63  if( ! item ) {
64  return JST_ERR_ERRNO;
65  }
66  item->name = strdup( name );
67  item->parent = object;
68  JST_Error error = element_copy( &(item->element), value );
69  if( error == JST_ERR_NONE ) {
70  ++(object->count);
71  object->items = realloc( object->items, object->count * sizeof( JST_Pair * ));
72  if( object->items == NULL ) {
73  object->count = 0;
74  error = JST_ERR_ERRNO;
75  }
76  else {
77  object->items[object->count-1] = item;
78  qsort( object->items, object->count, sizeof( JST_Pair *), JST_pairs_compare );
79  }
80  }
81  if( error != JST_ERR_NONE ) {
82  JST_delete_element( &(item->element));
83  free( item->name );
84  free( item );
85  }
86  return error;
87 }
88 
89 JST_Error JST_add_item( JST_Array * array, const JST_Element * value ) {
90  JST_ArrayItem * item = calloc( 1, sizeof( JST_ArrayItem ));
91  if( ! item ) {
92  return JST_ERR_ERRNO;
93  }
94  item->parent = array;
95  JST_Error error = element_copy( &(item->element), value );
96  if( error == JST_ERR_NONE ) {
97  ++(array->count);
98  array->items = realloc( array->items, array->count * sizeof( JST_ArrayItem * ));
99  if( array->items == NULL ) {
100  array->count = 0;
101  error = JST_ERR_ERRNO;
102  }
103  else {
104  array->items[array->count-1] = item;
105  }
106  }
107  if( error != JST_ERR_NONE ) {
108  JST_delete_element( &(item->element));
109  free( item );
110  }
111  return error;
112 }
113 
114 JST_Error JST_replace_item( JST_Array * array, unsigned index, const JST_Element * src ) {
115  if(( array == NULL )||( src == NULL )) {
116  return JST_ERR_NULL_ARGUMENT;
117  }
118  if( index > array->count ) {
120  }
121  return element_copy( &(array->items[index]->element), src );
122 }
123 
124 JST_Error JST_remove_property( JST_Object * object, const char * name ) {
125  if(( object == NULL )||( name == NULL )) {
126  return JST_ERR_NULL_ARGUMENT;
127  }
128  for( unsigned i = 0; i < object->count; ++i ) {
129  JST_Pair * pair = object->items[i];
130  if( 0 == strcmp( pair->name, name )) {
131  memmove( object->items+i, object->items+i+1, sizeof( JST_Pair * )*( object->count - i - 1 ));
132  --(object->count);
133  JST_delete_pair( pair );
134  return JST_ERR_NONE;
135  }
136  }
137  return JST_ERR_NOT_FOUND;
138 }
139 
140 JST_Error JST_remove_item( JST_Array * array, unsigned index ) {
141  if( array == NULL ) {
142  return JST_ERR_NULL_ARGUMENT;
143  }
144  if( index >= array->count ) {
146  }
147  JST_ArrayItem * item = array->items[index];
148  memmove( array->items+index, array->items+index+1, sizeof( JST_ArrayItem * )*( array->count - index - 1 ));
149  --(array->count);
150  JST_delete_array_item( item );
151  return JST_ERR_NONE;
152 }
struct JST_Pair_ ** items
Array of JST_Pair, ordered by JST_Pair.name to ease search with bsearch()
Definition: jstools.h:25
char * name
This property&#39;s name.
Definition: jstools.h:96
int JST_pairs_compare(const void *left, const void *right)
JST_Error JST_remove_item(JST_Array *array, unsigned index)
Definition: JST_update.c:140
JST_Error JST_add_item(JST_Array *array, const JST_Element *value)
Definition: JST_update.c:89
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
JST_Error JST_replace_item(JST_Array *array, unsigned index, const JST_Element *src)
Definition: JST_update.c:114
A JSON array.
Definition: jstools.h:10
JST_Element element
Definition: jstools.h:85
An array item has a parent and a typed value.
Definition: jstools.h:83
A type field has been set to JST_NONE.
Definition: jstools.h:111
char * string
Definition: jstools.h:46
A JSON object, a sorted set of named-value pairs.
Definition: jstools.h:24
JST_Object object
Definition: jstools.h:41
index is out of range
Definition: jstools.h:127
bool boolean
Definition: jstools.h:43
JST_Value value
Definition: jstools.h:72
JST_Error JST_delete_pair(JST_Pair *element)
Definition: JST_delete.c:5
JST_Error JST_add_property(JST_Object *object, unsigned index, const char *name, const JST_Element *value)
Add a named-value pair to an object.
Definition: JST_update.c:60
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
JST_Error JST_remove_property(JST_Object *object, const char *name)
Definition: JST_update.c:124
An element is a typed value.
Definition: jstools.h:70
JST_Array array
Definition: jstools.h:42
double dbl
Definition: jstools.h:45
JST_Object * parent
This property&#39;s owner.
Definition: jstools.h:94
int64_t integer
Definition: jstools.h:44
struct JST_Element_ * parent
of type JST_ArrayItem or JST_Pair, case selector is parent->type
Definition: jstools.h:27
JST_Array * parent
Definition: jstools.h:84
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
JST_ValueType type
Definition: jstools.h:71
struct JST_Element_ * parent
of type JST_ArrayItem or JST_Pair, case selector is parent->type
Definition: jstools.h:13
JST_Error JST_delete_array_item(JST_ArrayItem *array)
Definition: JST_delete.c:31
JST_Element element
The value associated with the name.
Definition: jstools.h:95
JST_get() has no result.
Definition: jstools.h:126
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26