Blame json_object_iterator.c

Packit ea8578
/**
Packit ea8578
*******************************************************************************
Packit ea8578
* @file json_object_iterator.c
Packit ea8578
*
Packit ea8578
* Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
Packit ea8578
*
Packit ea8578
* This library is free software; you can redistribute it and/or modify
Packit ea8578
* it under the terms of the MIT license. See COPYING for details.
Packit ea8578
*
Packit ea8578
*******************************************************************************
Packit ea8578
*/
Packit ea8578
Packit ea8578
#include <stddef.h>
Packit ea8578
Packit ea8578
#include "json.h"
Packit ea8578
#include "json_object_private.h"
Packit ea8578
Packit ea8578
#include "json_object_iterator.h"
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * How It Works
Packit ea8578
 *
Packit ea8578
 * For each JSON Object, json-c maintains a linked list of zero
Packit ea8578
 * or more lh_entry (link-hash entry) structures inside the
Packit ea8578
 * Object's link-hash table (lh_table).
Packit ea8578
 *
Packit ea8578
 * Each lh_entry structure on the JSON Object's linked list
Packit ea8578
 * represents a single name/value pair.  The "next" field of the
Packit ea8578
 * last lh_entry in the list is set to NULL, which terminates
Packit ea8578
 * the list.
Packit ea8578
 *
Packit ea8578
 * We represent a valid iterator that refers to an actual
Packit ea8578
 * name/value pair via a pointer to the pair's lh_entry
Packit ea8578
 * structure set as the iterator's opaque_ field.
Packit ea8578
 *
Packit ea8578
 * We follow json-c's current pair list representation by
Packit ea8578
 * representing a valid "end" iterator (one that refers past the
Packit ea8578
 * last pair) with a NULL value in the iterator's opaque_ field.
Packit ea8578
 *
Packit ea8578
 * A JSON Object without any pairs in it will have the "head"
Packit ea8578
 * field of its lh_table structure set to NULL.  For such an
Packit ea8578
 * object, json_object_iter_begin will return an iterator with
Packit ea8578
 * the opaque_ field set to NULL, which is equivalent to the
Packit ea8578
 * "end" iterator.
Packit ea8578
 *
Packit ea8578
 * When iterating, we simply update the iterator's opaque_ field
Packit ea8578
 * to point to the next lh_entry structure in the linked list.
Packit ea8578
 * opaque_ will become NULL once we iterate past the last pair
Packit ea8578
 * in the list, which makes the iterator equivalent to the "end"
Packit ea8578
 * iterator.
Packit ea8578
 */
Packit ea8578
Packit ea8578
/// Our current representation of the "end" iterator;
Packit ea8578
///
Packit ea8578
/// @note May not always be NULL
Packit ea8578
static const void* kObjectEndIterValue = NULL;
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_begin(struct json_object* obj)
Packit ea8578
{
Packit ea8578
    struct json_object_iterator iter;
Packit ea8578
    struct lh_table* pTable;
Packit ea8578
Packit ea8578
    /// @note json_object_get_object will return NULL if passed NULL
Packit ea8578
    ///       or a non-json_type_object instance
Packit ea8578
    pTable = json_object_get_object(obj);
Packit ea8578
    JASSERT(NULL != pTable);
Packit ea8578
Packit ea8578
    /// @note For a pair-less Object, head is NULL, which matches our
Packit ea8578
    ///       definition of the "end" iterator
Packit ea8578
    iter.opaque_ = pTable->head;
Packit ea8578
    return iter;
Packit ea8578
}
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_end(const struct json_object* obj)
Packit ea8578
{
Packit ea8578
    struct json_object_iterator iter;
Packit ea8578
Packit ea8578
    JASSERT(NULL != obj);
Packit ea8578
    JASSERT(json_object_is_type(obj, json_type_object));
Packit ea8578
Packit ea8578
    iter.opaque_ = kObjectEndIterValue;
Packit ea8578
Packit ea8578
    return iter;
Packit ea8578
}
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
void
Packit ea8578
json_object_iter_next(struct json_object_iterator* iter)
Packit ea8578
{
Packit ea8578
    JASSERT(NULL != iter);
Packit ea8578
    JASSERT(kObjectEndIterValue != iter->opaque_);
Packit ea8578
Packit ea8578
    iter->opaque_ = ((const struct lh_entry *)iter->opaque_)->next;
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
const char*
Packit ea8578
json_object_iter_peek_name(const struct json_object_iterator* iter)
Packit ea8578
{
Packit ea8578
    JASSERT(NULL != iter);
Packit ea8578
    JASSERT(kObjectEndIterValue != iter->opaque_);
Packit ea8578
Packit ea8578
    return (const char*)(((const struct lh_entry *)iter->opaque_)->k);
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
struct json_object*
Packit ea8578
json_object_iter_peek_value(const struct json_object_iterator* iter)
Packit ea8578
{
Packit ea8578
    JASSERT(NULL != iter);
Packit ea8578
    JASSERT(kObjectEndIterValue != iter->opaque_);
Packit ea8578
Packit ea8578
    return (struct json_object*)lh_entry_v((const struct lh_entry *)iter->opaque_);
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
json_bool
Packit ea8578
json_object_iter_equal(const struct json_object_iterator* iter1,
Packit ea8578
                       const struct json_object_iterator* iter2)
Packit ea8578
{
Packit ea8578
    JASSERT(NULL != iter1);
Packit ea8578
    JASSERT(NULL != iter2);
Packit ea8578
Packit ea8578
    return (iter1->opaque_ == iter2->opaque_);
Packit ea8578
}
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * ****************************************************************************
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_init_default(void)
Packit ea8578
{
Packit ea8578
    struct json_object_iterator iter;
Packit ea8578
Packit ea8578
    /**
Packit ea8578
     * @note Make this a negative, invalid value, such that
Packit ea8578
     *       accidental access to it would likely be trapped by the
Packit ea8578
     *       hardware as an invalid address.
Packit ea8578
     */
Packit ea8578
    iter.opaque_ = NULL;
Packit ea8578
Packit ea8578
    return iter;
Packit ea8578
}