提交 ebf8b783 编写于 作者: J John Ferlan

util: Introduce virJSONValueObjectStealArray

Provide the Steal API for any code paths that will desire to grab the
object array and then free it afterwards rather than relying to freeing
the whole chain from the reply.
上级 8546f723
......@@ -1821,6 +1821,7 @@ virJSONValueObjectHasKey;
virJSONValueObjectIsNull;
virJSONValueObjectKeysNumber;
virJSONValueObjectRemoveKey;
virJSONValueObjectStealArray;
virJSONValueToString;
......
......@@ -770,6 +770,30 @@ virJSONValueObjectGet(virJSONValuePtr object,
}
static virJSONValuePtr
virJSONValueObjectSteal(virJSONValuePtr object,
const char *key)
{
size_t i;
virJSONValuePtr obj = NULL;
if (object->type != VIR_JSON_TYPE_OBJECT)
return NULL;
for (i = 0; i < object->data.object.npairs; i++) {
if (STREQ(object->data.object.pairs[i].key, key)) {
VIR_STEAL_PTR(obj, object->data.object.pairs[i].value);
VIR_FREE(object->data.object.pairs[i].key);
VIR_DELETE_ELEMENT(object->data.object.pairs, i,
object->data.object.npairs);
break;
}
}
return obj;
}
/* Return the value associated with KEY within OBJECT, but return NULL
* if the key is missing or if value is not the correct TYPE. */
virJSONValuePtr
......@@ -785,6 +809,21 @@ virJSONValueObjectGetByType(virJSONValuePtr object,
}
/* Steal the value associated with KEY within OBJECT, but return NULL
* if the key is missing or if value is not the correct TYPE. */
static virJSONValuePtr
virJSONValueObjectStealByType(virJSONValuePtr object,
const char *key,
virJSONType type)
{
virJSONValuePtr value = virJSONValueObjectSteal(object, key);
if (value && value->type == type)
return value;
return NULL;
}
int
virJSONValueObjectKeysNumber(virJSONValuePtr object)
{
......@@ -1194,6 +1233,13 @@ virJSONValueObjectGetArray(virJSONValuePtr object, const char *key)
}
virJSONValuePtr
virJSONValueObjectStealArray(virJSONValuePtr object, const char *key)
{
return virJSONValueObjectStealByType(object, key, VIR_JSON_TYPE_ARRAY);
}
int
virJSONValueObjectIsNull(virJSONValuePtr object,
const char *key)
......
......@@ -136,6 +136,8 @@ virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object,
const char *key);
virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object,
const char *key);
virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object,
const char *key);
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册