Skip to content

Commit

Permalink
Update and fix proplist implementation and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juho Hämäläinen committed Jan 22, 2013
1 parent b8f2c9d commit a593e67
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 43 deletions.
119 changes: 90 additions & 29 deletions libngf/proplist.c
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>

#include "list_p.h"
#include "proplist.h"
Expand Down Expand Up @@ -88,26 +89,38 @@ void ngf_proplist_free (NgfProplist *proplist)
free (proplist);
}

void
int
ngf_proplist_sets (NgfProplist *proplist,
const char *key,
const char *value)
{
PropEntry *item = NULL;

if (proplist == NULL || key == NULL || value == NULL)
return;
goto error;

item = (PropEntry*) malloc (sizeof (PropEntry));
if (item == NULL)
return;
goto error;

item->key = strndup (key, (size_t) MAX_KEY_LENGTH);
item->value = strndup (value, (size_t) MAX_VALUE_LENGTH);
if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((item->value = strndup (value, (size_t) MAX_VALUE_LENGTH)) == NULL)
goto error;
item->type = NGF_PROPLIST_VALUE_TYPE_STRING;
item->next = NULL;

LIST_APPEND (proplist->entries, item);
return 1;

error:
if (item && item->key)
free (item->key);
if (item && item->value)
free (item->value);
if (item)
free (item);
return 0;
}

const char*
Expand All @@ -127,7 +140,7 @@ ngf_proplist_gets (NgfProplist *proplist,
return NULL;
}

void
int
ngf_proplist_set_as_integer (NgfProplist *proplist,
const char *key,
int32_t value)
Expand All @@ -136,23 +149,35 @@ ngf_proplist_set_as_integer (NgfProplist *proplist,
int32_t *data;

if (proplist == NULL || key == NULL)
return;
goto error;

item = (PropEntry*) malloc (sizeof (PropEntry));
if (item == NULL)
return;
goto error;

item->key = strndup (key, (size_t) MAX_KEY_LENGTH);
data = malloc (sizeof(int32_t));
if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(int32_t))) == NULL)
goto error;
*data = value;
item->value = data;
item->type = NGF_PROPLIST_VALUE_TYPE_INTEGER;
item->next = NULL;

LIST_APPEND (proplist->entries, item);
return 1;

error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
}

void
int
ngf_proplist_set_as_unsigned (NgfProplist *proplist,
const char *key,
uint32_t value)
Expand All @@ -161,20 +186,32 @@ ngf_proplist_set_as_unsigned (NgfProplist *proplist,
uint32_t *data;

if (proplist == NULL || key == NULL)
return;
goto error;

item = (PropEntry*) malloc (sizeof (PropEntry));
if (item == NULL)
return;
goto error;

item->key = strndup (key, (size_t) MAX_KEY_LENGTH);
data = malloc (sizeof(uint32_t));
if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(uint32_t))) == NULL)
goto error;
*data = value;
item->value = data;
item->type = NGF_PROPLIST_VALUE_TYPE_UNSIGNED;
item->next = NULL;

LIST_APPEND (proplist->entries, item);
return 1;

error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
}

int
Expand Down Expand Up @@ -219,7 +256,7 @@ ngf_proplist_get_as_unsigned (NgfProplist *proplist,
return 0;
}

void
int
ngf_proplist_set_as_boolean (NgfProplist *proplist,
const char *key,
int value)
Expand All @@ -228,20 +265,32 @@ ngf_proplist_set_as_boolean (NgfProplist *proplist,
int *data;

if (proplist == NULL || key == NULL)
return;
goto error;

item = (PropEntry*) malloc (sizeof (PropEntry));
if (item == NULL)
return;
goto error;

item->key = strndup (key, (size_t) MAX_KEY_LENGTH);
data = malloc (sizeof(int));
if ((item->key = strndup (key, (size_t) MAX_KEY_LENGTH)) == NULL)
goto error;
if ((data = malloc (sizeof(int))) == NULL)
goto error;
*data = value > 0 ? 1 : 0;
item->value = data;
item->type = NGF_PROPLIST_VALUE_TYPE_BOOLEAN;
item->next = NULL;

LIST_APPEND (proplist->entries, item);
return 1;

error:
if (item && item->key)
free (item->key);
if (data)
free (data);
if (item)
free (item);
return 0;
}

int
Expand Down Expand Up @@ -285,20 +334,32 @@ ngf_proplist_get_value_type (NgfProplist *proplist,
int
ngf_proplist_parse_integer (const char *value, int32_t *integer_value)
{
if (value == NULL || integer_value == NULL)
char *endptr = NULL;

if (value == NULL || value[0] == '\0' || integer_value == NULL)
return 0;

errno = 0;
*integer_value = strtol (value, &endptr, 10);
if (errno != 0 || endptr[0] != '\0')
return 0;

*integer_value = strtol (value, NULL, 10);
return 1;
}

int
ngf_proplist_parse_unsigned (const char *value, uint32_t *unsigned_value)
{
if (value == NULL || unsigned_value == NULL)
char *endptr = NULL;

if (value == NULL || value[0] == '\0' || unsigned_value == NULL)
return 0;

errno = 0;
*unsigned_value = strtoul (value, &endptr, 10);
if (errno != 0 || endptr[0] != '\0')
return 0;

*unsigned_value = strtoul (value, NULL, 10);
return 1;
}

Expand All @@ -308,16 +369,16 @@ ngf_proplist_parse_boolean (const char *value, int *boolean_value)
if (value == NULL || boolean_value == NULL)
return 0;

if (strncmp (value, "TRUE", 4) == 0 ||
strncmp (value, "true", 4) == 0 ||
strncmp (value, "True", 4) == 0 ||
strncmp (value, "1", 1) == 0) {
if (strncmp (value, "1", 1) == 0 || strncasecmp (value, "true", 4) == 0) {

*boolean_value = 1;
return 1;
} else if (strncmp (value, "0", 1) == 0 || strncasecmp (value, "false", 5) == 0) {

*boolean_value = 0;
return 1;
}

*boolean_value = 0;
return 0;
}

Expand Down
12 changes: 8 additions & 4 deletions libngf/proplist.h
Expand Up @@ -63,9 +63,10 @@ void ngf_proplist_free (NgfProplist *proplist);
* @param proplist NgfProplist
* @param key Key name
* @param value Value for the key
* @return 1 on success, 0 out of memory or other error.
*/

void ngf_proplist_sets (NgfProplist *proplist, const char *key, const char *value);
int ngf_proplist_sets (NgfProplist *proplist, const char *key, const char *value);

/**
* Get a string value from property list.
Expand All @@ -81,9 +82,10 @@ const char* ngf_proplist_gets (NgfProplist *proplist, const char *key);
* @param proplist NgfProplist
* @param key Key name
* @param value Value for the key
* @return 1 on success, 0 out of memory or other error.
*/

void ngf_proplist_set_as_integer (NgfProplist *proplist, const char *key, int32_t value);
int ngf_proplist_set_as_integer (NgfProplist *proplist, const char *key, int32_t value);

/**
* Get integer value from the property list.
Expand All @@ -100,9 +102,10 @@ int ngf_proplist_get_as_integer (NgfProplist *proplist, const char *
* @param proplist NgfProplist
* @param key Key name
* @param value Value for the key
* @return 1 on success, 0 out of memory or other error.
*/

void ngf_proplist_set_as_unsigned (NgfProplist *proplist, const char *key, uint32_t value);
int ngf_proplist_set_as_unsigned (NgfProplist *proplist, const char *key, uint32_t value);

/**
* Get unsigned integer value from the property list.
Expand All @@ -119,9 +122,10 @@ int ngf_proplist_get_as_unsigned (NgfProplist *proplist, const char
* @param proplist NgfProplist
* @param key Key name
* @param value Value for the key
* @return 1 on success, 0 out of memory or other error.
*/

void ngf_proplist_set_as_boolean (NgfProplist *proplist, const char *key, int value);
int ngf_proplist_set_as_boolean (NgfProplist *proplist, const char *key, int value);

/**
* Get a boolean value from the property list.
Expand Down
24 changes: 14 additions & 10 deletions tests/test-proplist.c
Expand Up @@ -21,6 +21,7 @@
#include <values.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <check.h>
#include <libngf/proplist.h>
Expand Down Expand Up @@ -69,7 +70,8 @@ START_TEST (test_parse_values)
int32_t int_value;
fail_unless (ngf_proplist_parse_integer (NULL, NULL) == 0);
fail_unless (ngf_proplist_parse_integer ("", &int_value) == 0);
fail_unless (ngf_proplist_parse_integer ("1" &int_value) == 1 && int_value == 1);
fail_unless (ngf_proplist_parse_integer ("0", &int_value) == 1 && int_value == 0);
fail_unless (ngf_proplist_parse_integer ("1", &int_value) == 1 && int_value == 1);
fail_unless (ngf_proplist_parse_integer ("555", &int_value) == 1 && int_value == 555);
fail_unless (ngf_proplist_parse_integer ("-555", &int_value) == 1 && int_value == -555);
fail_unless (ngf_proplist_parse_integer (max_int, &int_value) == 1 && int_value == INT32_MAX);
Expand All @@ -80,7 +82,7 @@ START_TEST (test_parse_values)
uint32_t uint_value;
fail_unless (ngf_proplist_parse_unsigned (NULL, NULL) == 0);
fail_unless (ngf_proplist_parse_unsigned ("", &uint_value) == 0);
fail_unless (ngf_proplist_parse_unsigned ("1" &uint_value) == 1 && uint_value == 1);
fail_unless (ngf_proplist_parse_unsigned ("1", &uint_value) == 1 && uint_value == 1);
fail_unless (ngf_proplist_parse_unsigned ("555", &uint_value) == 1 && uint_value == 555);
fail_unless (ngf_proplist_parse_unsigned (max_uint, &uint_value) == 1 && uint_value == UINT32_MAX);
fail_unless (ngf_proplist_parse_unsigned (min_uint, &uint_value) == 1 && uint_value == 0);
Expand All @@ -94,9 +96,11 @@ START_TEST (test_parse_values)
fail_unless (ngf_proplist_parse_boolean ("true", &bool_value) == 1 && bool_value == 1);
fail_unless (ngf_proplist_parse_boolean ("True", &bool_value) == 1 && bool_value == 1);
fail_unless (ngf_proplist_parse_boolean ("1", &bool_value) == 1 && bool_value == 1);
fail_unless (ngf_proplist_parse_boolean ("false", &bool_value) == 1 && bool_value == 0);
fail_unless (ngf_proplist_parse_boolean ("FalsE", &bool_value) == 1 && bool_value == 0);
fail_unless (ngf_proplist_parse_boolean ("FALSE", &bool_value) == 1 && bool_value == 0);
fail_unless (ngf_proplist_parse_boolean ("random", &bool_value) == 1 && bool_value == 0);
fail_unless (ngf_proplist_parse_boolean ("5", &bool_value) == 1 && bool_value == 0);
fail_unless (ngf_proplist_parse_boolean ("random", &bool_value) == 0);
fail_unless (ngf_proplist_parse_boolean ("5", &bool_value) == 0);
}
END_TEST

Expand Down Expand Up @@ -148,7 +152,7 @@ _proplist_cb (const char *key, const void *value, void *userdata)
int32_t integer_value;
int boolean_value;

if (strcmp (key, "first")) {
if (strcmp (key, "first") == 0) {
string_value = (const char*) value;
if (strcmp (value, "1") == 0)
(*num_recognized)++;
Expand Down Expand Up @@ -181,10 +185,10 @@ START_TEST (test_foreach)
proplist = ngf_proplist_new ();
fail_unless (proplist != NULL);

ngf_proplist_sets (proplist, "first", "1");
ngf_proplist_set_as_integer (proplist, "second", -5);
ngf_proplist_set_as_unsigned (proplist, "third", 9);
ngf_proplist_set_as_boolean (proplist, "fourth", 1);
fail_unless (ngf_proplist_sets (proplist, "first", "1") == 1);
fail_unless (ngf_proplist_set_as_integer (proplist, "second", -5) == 1);
fail_unless (ngf_proplist_set_as_unsigned (proplist, "third", 9) == 1);
fail_unless (ngf_proplist_set_as_boolean (proplist, "fourth", 1) == 1);

ngf_proplist_foreach (proplist, _proplist_cb, &num_recognized);
fail_unless (num_recognized == 4);
Expand Down Expand Up @@ -248,7 +252,7 @@ START_TEST (test_foreach_extended)
fail_unless (ngf_proplist_get_value_type (proplist, "integer.1") == NGF_PROPLIST_VALUE_TYPE_INTEGER);

ngf_proplist_set_as_unsigned (proplist, "unsigned.1", 555);
fail_unless (ngf_proplist_get_value_type (proplist, "integer.1") == NGF_PROPLIST_VALUE_TYPE_UNSIGNED);
fail_unless (ngf_proplist_get_value_type (proplist, "unsigned.1") == NGF_PROPLIST_VALUE_TYPE_UNSIGNED);

ngf_proplist_set_as_boolean (proplist, "boolean.1", 1);
fail_unless (ngf_proplist_get_value_type (proplist, "boolean.1") == NGF_PROPLIST_VALUE_TYPE_BOOLEAN);
Expand Down

0 comments on commit a593e67

Please sign in to comment.