libgphoto2 photo camera library (libgphoto2) Internals  2.5.26
gphoto2-list.c
Go to the documentation of this file.
1 
24 #define _DEFAULT_SOURCE
25 
26 #include "config.h"
27 #include <gphoto2/gphoto2-list.h>
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <gphoto2/gphoto2-result.h>
35 
36 #define CHECK_RESULT(result) {int r = (result); if (r < 0) return (r);}
37 
42 struct _entry {
43  char *name;
44  char *value;
45 };
46 
47 struct _CameraList {
48  int used; /* used entries */
49  int max; /* allocated entries */
50  struct _entry *entry;
51  int ref_count;
52 };
53 
54 
62 int
64 {
65  C_PARAMS (list);
66 
67  C_MEM (*list = calloc (1, sizeof (CameraList)));
68 
69  (*list)->ref_count = 1;
70 
71  return (GP_OK);
72 }
73 
81 int
83 {
84  C_PARAMS (list && list->ref_count);
85 
86  list->ref_count++;
87 
88  return (GP_OK);
89 }
90 
100 int
102 {
103  C_PARAMS (list && list->ref_count);
104 
105  if (list->ref_count == 1) /* time to free */
106  gp_list_free (list);
107  else
108  list->ref_count--;
109  return (GP_OK);
110 }
111 
119 int
121 {
122  int i;
123  C_PARAMS (list && list->ref_count);
124 
125  for (i=0;i<list->used;i++) {
126  free (list->entry[i].name);
127  list->entry[i].name = NULL;
128  free (list->entry[i].value);
129  list->entry[i].value = NULL;
130  }
131  free (list->entry);
132  /* Mark this list as having been freed. That may help us
133  * prevent access to already freed lists.
134  */
135  list->ref_count = 0;
136  free (list);
137  return (GP_OK);
138 }
139 
147 int
149 {
150  int i;
151  C_PARAMS (list && list->ref_count);
152 
153  for (i=0;i<list->used;i++) {
154  free (list->entry[i].name);
155  list->entry[i].name = NULL;
156  free (list->entry[i].value);
157  list->entry[i].value = NULL;
158  }
159  /* keeps -> entry allocated for reuse. */
160  list->used = 0;
161  return (GP_OK);
162 }
163 
173 int
174 gp_list_append (CameraList *list, const char *name, const char *value)
175 {
176  C_PARAMS (list && list->ref_count);
177 
178  if (list->used == list->max) {
179  C_MEM (list->entry = realloc(list->entry,(list->max+100)*sizeof(struct _entry)));
180  list->max += 100;
181  }
182 
183  if (name) {
184  C_MEM (list->entry[list->used].name = strdup (name));
185  } else {
186  list->entry[list->used].name = NULL;
187  }
188  if (value) {
189  C_MEM (list->entry[list->used].value = strdup (value));
190  } else {
191  list->entry[list->used].value = NULL;
192  }
193  list->used++;
194  return (GP_OK);
195 }
196 
197 static int
198 cmp_list (const void *a, const void *b) {
199  const struct _entry *ca = a;
200  const struct _entry *cb = b;
201 
202  return strcmp (ca->name, cb->name);
203 }
204 
212 int
214 {
215  C_PARAMS (list && list->ref_count);
216 
217  qsort (list->entry, list->used, sizeof(list->entry[0]), cmp_list);
218  return GP_OK;
219 }
220 
228 int
230 {
231  C_PARAMS (list && list->ref_count);
232 
233  return (list->used);
234 }
235 
248 int
249 gp_list_find_by_name (CameraList *list, int *index, const char *name)
250 {
251  int i;
252  C_PARAMS (list && list->ref_count);
253  C_PARAMS (name);
254 
255  /* We search backwards because our only known user
256  * camlibs/ptp2/library.c thinks this is faster
257  */
258  for (i=list->used-1; i >= 0; i--) {
259  if (0==strcmp(list->entry[i].name, name)) {
260  if (index) {
261  (*index) = i;
262  }
263  return (GP_OK);
264  }
265  }
266 
267  return (GP_ERROR);
268 }
269 
279 int
280 gp_list_get_name (CameraList *list, int index, const char **name)
281 {
282  C_PARAMS (list && list->ref_count);
283  C_PARAMS (name);
284  C_PARAMS (0 <= index && index < list->used);
285 
286  *name = list->entry[index].name;
287 
288  return (GP_OK);
289 }
290 
300 int
301 gp_list_get_value (CameraList *list, int index, const char **value)
302 {
303  C_PARAMS (list && list->ref_count);
304  C_PARAMS (value);
305  C_PARAMS (0 <= index && index < list->used);
306 
307  *value = list->entry[index].value;
308 
309  return (GP_OK);
310 }
311 
321 int
322 gp_list_set_value (CameraList *list, int index, const char *value)
323 {
324  char *newval;
325  C_PARAMS (list && list->ref_count);
326  C_PARAMS (value);
327  C_PARAMS (0 <= index && index < list->used);
328 
329  C_MEM (newval = strdup(value));
330  free (list->entry[index].value);
331  list->entry[index].value = newval;
332  return (GP_OK);
333 }
334 
344 int
345 gp_list_set_name (CameraList *list, int index, const char *name)
346 {
347  char *newname;
348  C_PARAMS (list && list->ref_count);
349  C_PARAMS (name);
350  C_PARAMS (0 <= index && index < list->used);
351 
352  C_MEM (newname = strdup(name));
353  free (list->entry[index].name);
354  list->entry[index].name = newname;
355  return (GP_OK);
356 }
357 
372 int
373 gp_list_populate (CameraList *list, const char *format, int count)
374 {
375  int x;
376  char buf[1024];
377 
378  C_PARAMS (list && list->ref_count);
379  C_PARAMS (format);
380 
381  gp_list_reset (list);
382  for (x = 0; x < count; x++) {
383  snprintf (buf, sizeof (buf), format, x + 1);
384  CHECK_RESULT (gp_list_append (list, buf, NULL));
385  }
386 
387  return (GP_OK);
388 }
389 
390 
391 
392 /*
393  * Local Variables:
394  * c-file-style:"linux"
395  * indent-tabs-mode:nil
396  * End:
397  */
int gp_list_new(CameraList **list)
Creates a new CameraList.
Definition: gphoto2-list.c:63
#define CHECK_RESULT(result)
Definition: gphoto2-list.c:36
int gp_list_populate(CameraList *list, const char *format, int count)
Definition: gphoto2-list.c:373
int gp_list_reset(CameraList *list)
Definition: gphoto2-list.c:148
int gp_list_set_name(CameraList *list, int index, const char *name)
Definition: gphoto2-list.c:345
static int cmp_list(const void *a, const void *b)
Definition: gphoto2-list.c:198
int gp_list_free(CameraList *list)
Definition: gphoto2-list.c:120
int gp_list_sort(CameraList *list)
Definition: gphoto2-list.c:213
int gp_list_set_value(CameraList *list, int index, const char *value)
Definition: gphoto2-list.c:322
int gp_list_unref(CameraList *list)
Decrements the reference count of the list.
Definition: gphoto2-list.c:101
int gp_list_append(CameraList *list, const char *name, const char *value)
Definition: gphoto2-list.c:174
int gp_list_get_value(CameraList *list, int index, const char **value)
Definition: gphoto2-list.c:301
int gp_list_find_by_name(CameraList *list, int *index, const char *name)
Definition: gphoto2-list.c:249
int gp_list_count(CameraList *list)
Definition: gphoto2-list.c:229
int gp_list_ref(CameraList *list)
Increments the reference count of the list.
Definition: gphoto2-list.c:82
int gp_list_get_name(CameraList *list, int index, const char **name)
Definition: gphoto2-list.c:280
#define GP_OK
Everything is OK.
#define GP_ERROR
Generic Error.
struct _entry * entry
Definition: gphoto2-list.c:50
Definition: gphoto2-list.c:42
char * name
Definition: gphoto2-list.c:43
char * value
Definition: gphoto2-list.c:44