#include <libprop/proplib.h>
#include "prop_object_impl.h"
bool
prop_dictionary_get_dict(prop_dictionary_t dict, const char *key, prop_dictionary_t *dp)
{
prop_object_t o;
o = prop_dictionary_get(dict, key);
if (o == NULL || prop_object_type(o) != PROP_TYPE_DICTIONARY)
return false;
*dp = o;
return true;
}
bool
prop_dictionary_get_bool(prop_dictionary_t dict,
const char *key,
bool *valp)
{
prop_bool_t b;
b = prop_dictionary_get(dict, key);
if (prop_object_type(b) != PROP_TYPE_BOOL)
return (false);
*valp = prop_bool_true(b);
return (true);
}
bool
prop_dictionary_set_bool(prop_dictionary_t dict,
const char *key,
bool val)
{
prop_bool_t b;
int rv;
b = prop_bool_create(val);
if (b == NULL)
return (false);
rv = prop_dictionary_set(dict, key, b);
prop_object_release(b);
return (rv);
}
#define TEMPLATE(size) \
bool \
prop_dictionary_get_int ## size (prop_dictionary_t dict, \
const char *key, \
int ## size ## _t *valp) \
{ \
prop_number_t num; \
\
num = prop_dictionary_get(dict, key); \
if (prop_object_type(num) != PROP_TYPE_NUMBER) \
return (false); \
\
if (prop_number_unsigned(num) && \
prop_number_unsigned_integer_value(num) > \
((size) == 8 ? INT8_MAX : \
(size) == 16 ? INT16_MAX : \
(size) == 32 ? INT32_MAX : INT64_MAX)) { \
return (false); \
} \
\
if (prop_number_size(num) > (size)) \
return (false); \
\
*valp = (int ## size ## _t) prop_number_integer_value(num); \
\
return (true); \
} \
\
bool \
prop_dictionary_get_uint ## size (prop_dictionary_t dict, \
const char *key, \
uint ## size ## _t *valp) \
{ \
prop_number_t num; \
\
num = prop_dictionary_get(dict, key); \
if (prop_object_type(num) != PROP_TYPE_NUMBER) \
return (false); \
\
if (prop_number_unsigned(num) == false && \
prop_number_integer_value(num) < 0) { \
return (false); \
} \
\
if (prop_number_size(num) > (size)) \
return (false); \
\
*valp = (uint ## size ## _t) \
prop_number_unsigned_integer_value(num); \
\
return (true); \
} \
\
bool \
prop_dictionary_set_int ## size (prop_dictionary_t dict, \
const char *key, \
int ## size ## _t val) \
{ \
prop_number_t num; \
int rv; \
\
num = prop_number_create_integer((int64_t) val); \
if (num == NULL) \
return (false); \
rv = prop_dictionary_set(dict, key, num); \
prop_object_release(num); \
\
return (rv); \
} \
\
bool \
prop_dictionary_set_uint ## size (prop_dictionary_t dict, \
const char *key, \
uint ## size ## _t val) \
{ \
prop_number_t num; \
int rv; \
\
num = prop_number_create_unsigned_integer((uint64_t) val); \
if (num == NULL) \
return (false); \
rv = prop_dictionary_set(dict, key, num); \
prop_object_release(num); \
\
return (rv); \
}
TEMPLATE(8)
TEMPLATE(16)
TEMPLATE(32)
TEMPLATE(64)
#undef TEMPLATE
#define TEMPLATE(variant, qualifier) \
bool \
prop_dictionary_get_cstring ## variant (prop_dictionary_t dict, \
const char *key, \
qualifier char **cpp) \
{ \
prop_string_t str; \
\
str = prop_dictionary_get(dict, key); \
if (prop_object_type(str) != PROP_TYPE_STRING) \
return (false); \
\
*cpp = prop_string_cstring ## variant (str); \
\
return (*cpp == NULL ? false : true); \
} \
\
bool \
prop_dictionary_set_cstring ## variant (prop_dictionary_t dict, \
const char *key, \
const char *cp) \
{ \
prop_string_t str; \
int rv; \
\
str = prop_string_create_cstring ## variant (cp); \
if (str == NULL) \
return (false); \
rv = prop_dictionary_set(dict, key, str); \
prop_object_release(str); \
\
return (rv); \
}
TEMPLATE(,)
TEMPLATE(_nocopy,const)
#undef TEMPLATE
bool
prop_dictionary_set_and_rel(prop_dictionary_t dict, const char *key,
prop_object_t po)
{
bool ret;
if (po == NULL)
return false;
ret = prop_dictionary_set(dict, key, po);
prop_object_release(po);
return ret;
}