summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-06-03 21:52:11 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-07-10 23:41:15 +0200
commite3a9e5e57cdaeb4b71112cff014f6e6d90e5a94f (patch)
tree15984a99912118aefa0dd835d1e679cc1446b363 /src
parent9f65896316418d257670bc65e83aa87c5683579d (diff)
Fixed bug when searching tail of list
When searching tail of list, you cannot use directly input parameter (*l) , you should use a local variable instead (here new_elt). Change-Id: I13fc042f85580342ecf33cca2067a37ee55f4799 Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/utils/list.c129
1 files changed, 62 insertions, 67 deletions
diff --git a/src/utils/list.c b/src/utils/list.c
index d47f761..c5152a3 100644
--- a/src/utils/list.c
+++ b/src/utils/list.c
@@ -17,96 +17,91 @@
#include "list.h"
#include "string.h"
+#include <stdio.h>
+#include <stdlib.h>
-void destroy_list(struct list *l)
+void destroy_list(struct list* l)
{
- struct list *save;
- while(l != NULL) {
- save = l->next;
- free(l);
- l = save;
- }
+ struct list* save;
+ while (l != NULL) {
+ save = l->next;
+ free(l);
+ l = save;
+ }
}
-void add_elt(struct list **l, const char *key, json_object *value)
+void add_elt(struct list** l, const char* key, json_object* value)
{
- struct list *new_elt = malloc(sizeof(struct list));
- new_elt->key = key;
- new_elt->value = value;
- new_elt->next = NULL;
+ struct list* new_elt;
- if(*l) {
- while((*l)->next != NULL) {
- *l = (*l)->next;
- }
- (*l)->next = new_elt;
- }
- else {
- *l = new_elt;
- }
+ // search tail
+ new_elt = *l;
+ while (new_elt != NULL) {
+ l = &new_elt->next;
+ new_elt = new_elt->next;
+ }
+
+ // alloc new elem
+ new_elt = malloc(sizeof(struct list));
+ if (!new_elt) {
+ fprintf(stderr, "OUT of memory");
+ exit(1);
+ }
+
+ *l = new_elt;
+ new_elt->key = key;
+ new_elt->value = value;
+ new_elt->next = NULL;
}
-void add_key(struct list **l, const char *key)
+void add_key(struct list** l, const char* key)
{
- struct list *new_elt = malloc(sizeof(struct list));
- new_elt->key = key;
- new_elt->value = NULL;
- new_elt->next = NULL;
-
- if(*l) {
- while((*l)->next != NULL) {
- *l = (*l)->next;
- }
- (*l)->next = new_elt;
- }
- else {
- *l = new_elt;
- }
+ add_elt(l, key, NULL);
}
-int set_value(struct list *l, json_object *val, int index)
+int set_value(struct list* l, json_object* val, int index)
{
- int i;
+ int i;
- for (i = 0; i < index; i++) {
- l = l->next;
- if ( l == NULL )
- return -1;
- }
+ for (i = 0; i < index; i++) {
+ l = l->next;
+ if (l == NULL)
+ return -1;
+ }
- l->value = val;
- return 0;
+ l->value = val;
+ return 0;
}
-struct list *get_elt(struct list *l, int index)
+struct list* get_elt(struct list* l, int index)
{
- int i;
+ int i;
- for (i = 0; i < index; i++) {
- l = l->next;
- if ( l == NULL )
- return NULL;
- }
+ for (i = 0; i < index; i++) {
+ l = l->next;
+ if (l == NULL)
+ return NULL;
+ }
- return l;
+ return l;
}
-struct list *find_elt_from_key(struct list *l, const char *key)
+struct list* find_elt_from_key(struct list* l, const char* key)
{
- while(l != NULL) {
- if(strcasecmp(l->key, key) == 0)
- return l;
- l = l->next;
- }
- return NULL;
+ while (l != NULL) {
+ if (strcasecmp(l->key, key) == 0)
+ return l;
+ l = l->next;
+ }
+ return NULL;
}
-json_object *find_key_value(struct list *l, const char *key)
+json_object* find_key_value(struct list* l, const char* key)
{
- while(l != NULL) {
- if(strcasecmp(l->key, key) == 0)
- return l->value;
- l = l->next;
- }
- return NULL;
+ while (l != NULL) {
+ if (strcasecmp(l->key, key) == 0)
+ return l->value;
+ l = l->next;
+ }
+ return NULL;
}