Simple JSON  0.1
A free simple json library for C
simple_json_list.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "simple_json_list.h"
5 #include "simple_json_error.h"
6 
7 void sj_list_delete(SJList *list)
8 {
9  if (!list)return;
10  if (list->elements)
11  {
12  free(list->elements);
13  }
14  free(list);
15 }
16 
18 {
19  return sj_list_new_size(16);
20 }
21 
22 SJList *sj_list_new_size(unsigned int count)
23 {
24  SJList *l;
25  if (!count)
26  {
27  sj_set_error("cannot make a list of size zero");
28  return NULL;
29  }
30  l = (SJList *)malloc(sizeof(SJList));
31  if (!l)
32  {
33  sj_set_error("failed to allocate space for the list");
34  return NULL;
35  }
36  memset(l,0,sizeof(SJList));
37  l->size = count;
38  l->elements = (SJListElementData*)malloc(sizeof(SJListElementData)*count);
39  if (!l->elements)
40  {
41  sj_set_error("failed to allocate space for list elements");
42  free(l);
43  return NULL;
44  }
45  memset(l->elements,0,sizeof(SJListElementData)*count);
46  return l;
47 }
48 
49 void *sj_list_get_nth(SJList *list,unsigned int n)
50 {
51  if (!list)
52  {
53  sj_set_error("no list provided");
54  return NULL;
55  }
56  if ((n >= list->count)||(n >= list->size))return NULL;
57  return list->elements[n].data;
58 }
59 
61 {
62  SJList *l;
63  if (!list)
64  {
65  sj_set_error("no list provided");
66  return NULL;
67  }
68  if (!list->size)list->size = 8;
69  l = sj_list_new_size(list->size * 2);
70  if (!l)
71  {
72  return list;
73  }
74  if (list->count > 0)
75  {
76  memcpy(l->elements,list->elements,sizeof(SJListElementData)*list->count);
77  }
78  l->count = list->count;
79  sj_list_delete(list);
80  return l;
81 }
82 
83 void sj_list_append(SJList *list,void *data)
84 {
85  if (!list)
86  {
87  sj_set_error("no list provided");
88  printf("no list provided\n");
89  return;
90  }
91  if (list->count >= list->size)
92  {
93  list = sj_list_expand(list);
94  if (!list)
95  {
96  sj_set_error("append failed due to lack of memory");
97  printf("append failed due to lack of memory\n");
98  return;
99  }
100  }
101  list->elements[list->count++].data = data;
102 }
103 
104 SJList *sj_list_prepend(SJList *list,void *data)
105 {
106  return sj_list_insert(list,data,0);
107 }
108 
109 SJList *sj_list_insert(SJList *list,void *data,unsigned int n)
110 {
111  if (!list)
112  {
113  sj_set_error("no list provided");
114  return NULL;
115  }
116  if (n > list->size + 1)
117  {
118  sj_set_error("attempting to insert element beyond length of list");
119  return list;
120  }
121  if (list->count >= list->size)
122  {
123  list = sj_list_expand(list);
124  if (!list)return NULL;
125  }
126  memmove(&list->elements[n+1],&list->elements[n],sizeof(SJListElementData)*(list->count - n));//copy all elements after n
127  list->elements[n].data = data;
128  list->count++;
129  return list;
130 }
131 
132 
134 {
135  return sj_list_delete_nth(list,0);
136 }
137 
139 {
140  if (!list)
141  {
142  sj_set_error("no list provided");
143  return NULL;
144  }
145  return sj_list_delete_nth(list,list->count-1);
146 }
147 
148 int sj_list_delete_data(SJList *list,void *data)
149 {
150  int i;
151  if (!list)
152  {
153  sj_set_error("no list provided");
154  return -1;
155  }
156  if (!data)return 0;
157  for (i = 0; i < list->count;i++)
158  {
159  if (list->elements[i].data == data)
160  {
161  // found it, now delete it
162  sj_list_delete_nth(list,i);
163  return 0;
164  }
165  }
166  sj_set_error("data not found");
167  return -1;
168 }
169 
170 SJList *sj_list_delete_nth(SJList *list,unsigned int n)
171 {
172  if (!list)
173  {
174  sj_set_error("no list provided");
175  return NULL;
176  }
177  if (n >= list->count)
178  {
179  sj_set_error("attempting to delete beyond the length of the list");
180  return list;
181  }
182  if (n == (list->count - 1))
183  {
184  list->count--;// last element in the list, this is easy
185  return list;
186  }
187  memmove(&list->elements[n],&list->elements[n+1],sizeof(SJListElementData)*(list->count - n));//copy all elements after n
188  list->count--;
189  return list;
190 }
191 
192 unsigned int sj_list_get_count(SJList *list)
193 {
194  if (!list)return 0;
195  return list->count;
196 }
197 
198 void sj_list_foreach(SJList *list,void (*function)(void *data,void *context),void *contextData)
199 {
200  int i;
201  if (!list)
202  {
203  sj_set_error("no list provided");
204  return;
205  }
206  if (!function)
207  {
208  sj_set_error("no function provided");
209  }
210  for (i = 0;i < list->count;i++)
211  {
212  function(list->elements[i].data,contextData);
213  }
214 }
215 
216 /*eol@eof*/
void sj_list_foreach(SJList *list, void(*function)(void *data, void *context), void *contextData)
void sj_list_append(SJList *list, void *data)
add an element to the end of the list
simple datatype abstracting the data held.
SJListElementData * elements
void * sj_list_get_nth(SJList *list, unsigned int n)
get the data stored at the nth element
SJList * sj_list_prepend(SJList *list, void *data)
SJList * sj_list_new()
create a new list
SJList * sj_list_delete_nth(SJList *list, unsigned int n)
delete the element at the nth position in the list
this is a simple list structure intended to hold an arbitrary number of elements list will automatica...
void sj_set_error(char *er)
used internally to se the error message
unsigned int size
SJList * sj_list_insert(SJList *list, void *data, unsigned int n)
instert a new element at the position provided
int sj_list_delete_data(SJList *list, void *data)
delete the first element in the list pointing to the address of data
void sj_list_delete(SJList *list)
deletes a list that has been previously allocated
SJList * sj_list_delete_last(SJList *list)
SJList * sj_list_expand(SJList *list)
SJList * sj_list_new_size(unsigned int count)
allocate a new empty list of size &#39;count&#39;
SJList * sj_list_delete_first(SJList *list)
unsigned int sj_list_get_count(SJList *list)
get the number of tracked elements in the list
unsigned int count