mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-09-06 09:04:55 +00:00
2006-06-25 Dan Williams <dcbw@redhat.com>
* libnm-util/dbus-dict-helpers.[ch] test/libnm-util/test-dbus-dict-helpers.c - Add string array support git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@1852 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
parent
d285ae516f
commit
f77dff7643
|
@ -1,3 +1,9 @@
|
|||
2006-06-25 Dan Williams <dcbw@redhat.com>
|
||||
|
||||
* libnm-util/dbus-dict-helpers.[ch]
|
||||
test/libnm-util/test-dbus-dict-helpers.c
|
||||
- Add string array support
|
||||
|
||||
2006-06-24 Dan Williams <dcbw@redhat.com>
|
||||
|
||||
* src/dhcp-manager/nm-dhcp-manager.c
|
||||
|
|
|
@ -216,6 +216,48 @@ _nmu_dbus_add_dict_entry_byte_array (DBusMessageIter *iter_dict,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
static dbus_bool_t
|
||||
_nmu_dbus_add_dict_entry_string_array (DBusMessageIter *iter_dict,
|
||||
const char *key,
|
||||
const char **items,
|
||||
const dbus_uint32_t num_items)
|
||||
{
|
||||
DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
|
||||
dbus_uint32_t i;
|
||||
|
||||
if (!_nmu_dbus_add_dict_entry_start (iter_dict, &iter_dict_entry,
|
||||
key, DBUS_TYPE_ARRAY))
|
||||
return FALSE;
|
||||
|
||||
if (!dbus_message_iter_open_container (&iter_dict_entry,
|
||||
DBUS_TYPE_VARIANT,
|
||||
DBUS_TYPE_ARRAY_AS_STRING
|
||||
DBUS_TYPE_STRING_AS_STRING,
|
||||
&iter_dict_val))
|
||||
return FALSE;
|
||||
|
||||
if (!dbus_message_iter_open_container (&iter_dict_val, DBUS_TYPE_ARRAY,
|
||||
DBUS_TYPE_BYTE_AS_STRING, &iter_array))
|
||||
return FALSE;
|
||||
|
||||
for (i = 0; i < num_items; i++)
|
||||
{
|
||||
if (!dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING,
|
||||
&(items[i])))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!dbus_message_iter_close_container (&iter_dict_val, &iter_array))
|
||||
return FALSE;
|
||||
|
||||
if (!_nmu_dbus_add_dict_entry_end (iter_dict, &iter_dict_entry,
|
||||
&iter_dict_val))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a string entry to the dict.
|
||||
*
|
||||
|
@ -436,6 +478,28 @@ nmu_dbus_dict_append_byte_array (DBusMessageIter *iter_dict,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a string array entry to the dict.
|
||||
*
|
||||
* @param iter_dict A valid DBusMessageIter returned from {@link nmu_dbus_dict_open_write}
|
||||
* @param key The key of the dict item
|
||||
* @param items The array of strings
|
||||
* @param num_items The number of strings in the array
|
||||
* @return TRUE on success, FALSE on failure
|
||||
*
|
||||
*/
|
||||
dbus_bool_t
|
||||
nmu_dbus_dict_append_string_array (DBusMessageIter *iter_dict,
|
||||
const char * key,
|
||||
const char ** items,
|
||||
const dbus_uint32_t num_items)
|
||||
{
|
||||
if (!key) return FALSE;
|
||||
if (!items && (num_items != 0)) return FALSE;
|
||||
return _nmu_dbus_add_dict_entry_string_array (iter_dict, key, items, num_items);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************/
|
||||
/* Stuff for reading dicts */
|
||||
/*****************************************************/
|
||||
|
@ -485,8 +549,8 @@ _nmu_dbus_dict_entry_get_byte_array (DBusMessageIter *iter,
|
|||
entry->bytearray_value = malloc (array_len * sizeof (char));
|
||||
if (!entry->bytearray_value)
|
||||
{
|
||||
fprintf (stderr, "_nmu_dbus_dict_entry_get_byte_array() out of "
|
||||
"memory trying to retrieve a byte array.\n");
|
||||
fprintf (stderr, "%s out of memory trying to retrieve a byte "
|
||||
"array.\n", __func__);
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
@ -505,6 +569,72 @@ done:
|
|||
}
|
||||
|
||||
|
||||
static dbus_bool_t
|
||||
_nmu_dbus_dict_entry_get_string_array (DBusMessageIter *iter,
|
||||
int array_len,
|
||||
int array_type,
|
||||
NMUDictEntry *entry)
|
||||
{
|
||||
dbus_uint32_t count = 0;
|
||||
dbus_bool_t success = FALSE;
|
||||
char ** buffer;
|
||||
|
||||
entry->strarray_value = NULL;
|
||||
entry->array_type = DBUS_TYPE_STRING;
|
||||
|
||||
/* Zero-length arrays are valid. */
|
||||
if (array_len == 0)
|
||||
{
|
||||
success = TRUE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
buffer = (char **)malloc (sizeof (char *) * 8);
|
||||
if (buffer == NULL)
|
||||
{
|
||||
fprintf (stderr, "%s() out of memory trying to retrieve a string"
|
||||
" array.\n", __func__);
|
||||
goto done;
|
||||
}
|
||||
|
||||
entry->strarray_value = buffer;
|
||||
entry->array_len = 0;
|
||||
while (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING)
|
||||
{
|
||||
const char *value;
|
||||
char *str;
|
||||
|
||||
if ((count % 8) == 0 && count != 0)
|
||||
{
|
||||
buffer = realloc (buffer, sizeof (char *) * (count + 8));
|
||||
if (buffer == NULL)
|
||||
{
|
||||
fprintf (stderr, "%s() out of memory trying to retrieve"
|
||||
"the string array.\n", __func__);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
entry->strarray_value = buffer;
|
||||
|
||||
dbus_message_iter_get_basic (iter, &value);
|
||||
str = strdup (value);
|
||||
if (str == NULL)
|
||||
{
|
||||
fprintf (stderr, "%s() out of memory trying to duplicate"
|
||||
"the string array.\n", __func__);
|
||||
goto done;
|
||||
}
|
||||
entry->strarray_value[count] = str;
|
||||
entry->array_len = ++count;
|
||||
dbus_message_iter_next (iter);
|
||||
}
|
||||
success = TRUE;
|
||||
|
||||
done:
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
static dbus_bool_t
|
||||
_nmu_dbus_dict_entry_get_array (DBusMessageIter *iter_dict_val,
|
||||
NMUDictEntry *entry)
|
||||
|
@ -531,6 +661,9 @@ _nmu_dbus_dict_entry_get_array (DBusMessageIter *iter_dict_val,
|
|||
array_len, array_type, entry);
|
||||
break;
|
||||
}
|
||||
case DBUS_TYPE_STRING:
|
||||
success = _nmu_dbus_dict_entry_get_string_array (&iter_array,
|
||||
array_len, array_type, entry);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -703,8 +836,8 @@ nmu_dbus_dict_has_dict_entry (DBusMessageIter *iter_dict)
|
|||
{
|
||||
if (!iter_dict)
|
||||
{
|
||||
fprintf (stderr, "nmu_dbus_dict_has_dict_entry() called with invalid "
|
||||
"arguments; this is an error in the program.\n");
|
||||
fprintf (stderr, "%s called with invalid arguments; this is an "
|
||||
"error in the program.\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
return dbus_message_iter_get_arg_type (iter_dict) == DBUS_TYPE_DICT_ENTRY;
|
||||
|
@ -719,6 +852,7 @@ nmu_dbus_dict_has_dict_entry (DBusMessageIter *iter_dict)
|
|||
void
|
||||
nmu_dbus_dict_entry_clear (NMUDictEntry *entry)
|
||||
{
|
||||
dbus_uint32_t i;
|
||||
if (!entry)
|
||||
return;
|
||||
switch (entry->type)
|
||||
|
@ -733,6 +867,11 @@ nmu_dbus_dict_entry_clear (NMUDictEntry *entry)
|
|||
case DBUS_TYPE_BYTE:
|
||||
free (entry->bytearray_value);
|
||||
break;
|
||||
case DBUS_TYPE_STRING:
|
||||
for (i = 0; i < entry->array_len; i++)
|
||||
free (entry->strarray_value[i]);
|
||||
free (entry->strarray_value);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -99,6 +99,12 @@ nmu_dbus_dict_append_byte_array (DBusMessageIter *iter_dict,
|
|||
const char * value,
|
||||
const dbus_uint32_t value_len);
|
||||
|
||||
dbus_bool_t
|
||||
nmu_dbus_dict_append_string_array (DBusMessageIter *iter_dict,
|
||||
const char * key,
|
||||
const char ** items,
|
||||
const dbus_uint32_t num_items);
|
||||
|
||||
/*
|
||||
* Reading a dict from a DBusMessage
|
||||
*/
|
||||
|
@ -121,6 +127,7 @@ typedef struct NMUDictEntry {
|
|||
dbus_uint64_t uint64_value;
|
||||
double double_value;
|
||||
char * bytearray_value;
|
||||
char ** strarray_value;
|
||||
};
|
||||
dbus_uint32_t array_len;
|
||||
} NMUDictEntry;
|
||||
|
|
|
@ -50,6 +50,7 @@ DECLARE_ENTRY(UInt64Entry, dbus_uint64_t)
|
|||
DECLARE_ENTRY(DoubleEntry, double)
|
||||
DECLARE_ENTRY(OPEntry, const char *)
|
||||
DECLARE_ENTRY(ByteArrayEntry, const char *)
|
||||
DECLARE_ENTRY(StringArrayEntry, char **)
|
||||
|
||||
struct DictEntries {
|
||||
struct StringEntry string;
|
||||
|
@ -65,21 +66,26 @@ struct DictEntries {
|
|||
struct OPEntry op;
|
||||
struct ByteArrayEntry bytearr;
|
||||
struct ByteArrayEntry zlbytearr;
|
||||
struct StringArrayEntry strarr;
|
||||
struct StringArrayEntry zlstrarr;
|
||||
};
|
||||
|
||||
#define TEST_KEY_STRING "String"
|
||||
#define TEST_KEY_BYTE "Byte"
|
||||
#define TEST_KEY_BOOL "Bool"
|
||||
#define TEST_KEY_INT16 "Int16"
|
||||
#define TEST_KEY_UINT16 "UInt16"
|
||||
#define TEST_KEY_INT32 "Int32"
|
||||
#define TEST_KEY_UINT32 "UInt32"
|
||||
#define TEST_KEY_INT64 "Int64"
|
||||
#define TEST_KEY_UINT64 "UInt64"
|
||||
#define TEST_KEY_DOUBLE "Double"
|
||||
#define TEST_KEY_OP "ObjectPath"
|
||||
#define TEST_KEY_BYTEARR "ByteArray"
|
||||
#define TEST_KEY_ZLBYTEARR "ZLByteArray"
|
||||
#define TEST_KEY_STRING "String"
|
||||
#define TEST_KEY_BYTE "Byte"
|
||||
#define TEST_KEY_BOOL "Bool"
|
||||
#define TEST_KEY_INT16 "Int16"
|
||||
#define TEST_KEY_UINT16 "UInt16"
|
||||
#define TEST_KEY_INT32 "Int32"
|
||||
#define TEST_KEY_UINT32 "UInt32"
|
||||
#define TEST_KEY_INT64 "Int64"
|
||||
#define TEST_KEY_UINT64 "UInt64"
|
||||
#define TEST_KEY_DOUBLE "Double"
|
||||
#define TEST_KEY_OP "ObjectPath"
|
||||
#define TEST_KEY_BYTEARR "ByteArray"
|
||||
#define TEST_KEY_ZLBYTEARR "ZLByteArray"
|
||||
#define STRARR_LEN 2
|
||||
#define TEST_KEY_STRINGARR "StringArray"
|
||||
#define TEST_KEY_ZLSTRINGARR "ZLStringArray"
|
||||
|
||||
struct DictEntries entries = {
|
||||
{ TEST_KEY_STRING, "foobar22", FALSE, DBUS_TYPE_STRING },
|
||||
|
@ -94,7 +100,9 @@ struct DictEntries entries = {
|
|||
{ TEST_KEY_DOUBLE, 54.3355632f, FALSE, DBUS_TYPE_DOUBLE },
|
||||
{ TEST_KEY_OP, "/com/it/foobar", FALSE, DBUS_TYPE_OBJECT_PATH },
|
||||
{ TEST_KEY_BYTEARR, "qazwsxedcrfvtgb",FALSE, DBUS_TYPE_BYTE },
|
||||
{ TEST_KEY_ZLBYTEARR,NULL, FALSE, DBUS_TYPE_BYTE }
|
||||
{ TEST_KEY_ZLBYTEARR,NULL, FALSE, DBUS_TYPE_BYTE },
|
||||
{ TEST_KEY_STRINGARR,NULL, FALSE, DBUS_TYPE_STRING },
|
||||
{ TEST_KEY_ZLSTRINGARR,NULL, FALSE, DBUS_TYPE_STRING }
|
||||
};
|
||||
|
||||
|
||||
|
@ -165,6 +173,19 @@ test_write_dict (DBusMessage *message)
|
|||
err_string = "failed to append zero-length byte array entry";
|
||||
goto done;
|
||||
}
|
||||
entries.strarr.val = malloc (sizeof (char *) * STRARR_LEN);
|
||||
entries.strarr.val[0] = "foo";
|
||||
entries.strarr.val[1] = "bar";
|
||||
if (!nmu_dbus_dict_append_string_array (&iter_dict, entries.strarr.key,
|
||||
(const char **)entries.strarr.val, STRARR_LEN)) {
|
||||
err_string = "failed to append string array entry";
|
||||
goto done;
|
||||
}
|
||||
if (!nmu_dbus_dict_append_string_array (&iter_dict, entries.zlstrarr.key,
|
||||
(const char **)entries.zlstrarr.val, 0)) {
|
||||
err_string = "failed to append zero-length string array entry";
|
||||
goto done;
|
||||
}
|
||||
if (!nmu_dbus_dict_close_write (&iter, &iter_dict)) {
|
||||
err_string = "failed to close dictionary";
|
||||
goto done;
|
||||
|
@ -254,6 +275,10 @@ test_read_dict (DBusMessage *message)
|
|||
!memcmp (entry.bytearray_value, entries.bytearr.val, bytearr_len))
|
||||
TEST_CASE_ARRAY (TEST_KEY_ZLBYTEARR, entries.zlbytearr, 0,
|
||||
entry.bytearray_value == entries.zlbytearr.val)
|
||||
TEST_CASE_ARRAY (TEST_KEY_STRINGARR, entries.strarr, STRARR_LEN,
|
||||
(!strcmp (entry.strarray_value[0], "foo") && !strcmp (entry.strarray_value[1], "bar")))
|
||||
TEST_CASE_ARRAY (TEST_KEY_ZLSTRINGARR, entries.zlstrarr, 0,
|
||||
entry.strarray_value == entries.zlstrarr.val)
|
||||
|
||||
err_string = "Unknown dict entry encountered.";
|
||||
goto done;
|
||||
|
@ -265,7 +290,8 @@ test_read_dict (DBusMessage *message)
|
|||
if (!entries.string.found || !entries.byte.found || !entries.bool.found || !entries.int16.found
|
||||
|| !entries.uint16.found || !entries.int32.found || !entries.uint32.found
|
||||
|| !entries.int64.found || !entries.uint64.found || !entries.dbl.found
|
||||
|| !entries.op.found || !entries.bytearr.found || !entries.zlbytearr.found) {
|
||||
|| !entries.op.found || !entries.bytearr.found || !entries.zlbytearr.found
|
||||
|| !entries.strarr.found || !entries.zlstrarr.found) {
|
||||
err_string = "A required entry was not found in the dict.";
|
||||
goto done;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue