JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
Functions
JST_update.c File Reference
#include "jstools.h"
#include "JST_private.h"
#include <string.h>
#include <stdlib.h>
Include dependency graph for JST_update.c:

Go to the source code of this file.

Functions

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. More...
 
JST_Error JST_add_item (JST_Array *array, const JST_Element *value)
 
JST_Error JST_replace_item (JST_Array *array, unsigned index, const JST_Element *src)
 
JST_Error JST_remove_property (JST_Object *object, const char *name)
 
JST_Error JST_remove_item (JST_Array *array, unsigned index)
 

Function Documentation

◆ JST_add_item()

JST_Error JST_add_item ( JST_Array array,
const JST_Element value 
)

Definition at line 89 of file JST_update.c.

89  {
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 }
JST_Element element
Definition: jstools.h:85
An array item has a parent and a typed value.
Definition: jstools.h:83
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_Array * parent
Definition: jstools.h:84
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_add_property()

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.

Parameters
objectthe target object to modify
indexthe property's rank, -1U to add at the end
namethe property's name
valuethe property's value
Returns
JST_ERR_NONE on success, JST_ERR_NULL_TYPE or JST_ERR_ERRNO on failure.

Definition at line 60 of file JST_update.c.

60  {
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 }
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)
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
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_Object * parent
This property&#39;s owner.
Definition: jstools.h:94
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
JST_Element element
The value associated with the name.
Definition: jstools.h:95
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26

◆ JST_remove_item()

JST_Error JST_remove_item ( JST_Array array,
unsigned  index 
)

Definition at line 140 of file JST_update.c.

140  {
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 }
An array item has a parent and a typed value.
Definition: jstools.h:83
index is out of range
Definition: jstools.h:127
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
JST_Error JST_delete_array_item(JST_ArrayItem *array)
Definition: JST_delete.c:31
Here is the call graph for this function:

◆ JST_remove_property()

JST_Error JST_remove_property ( JST_Object object,
const char *  name 
)

Definition at line 124 of file JST_update.c.

124  {
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 }
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
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
JST_Error JST_delete_pair(JST_Pair *element)
Definition: JST_delete.c:5
Function misused.
Definition: jstools.h:110
JST_get() has no result.
Definition: jstools.h:126
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26
Here is the call graph for this function:

◆ JST_replace_item()

JST_Error JST_replace_item ( JST_Array array,
unsigned  index,
const JST_Element src 
)

Definition at line 114 of file JST_update.c.

114  {
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 }
JST_Element element
Definition: jstools.h:85
index is out of range
Definition: jstools.h:127
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12