/*! \brief Get next attribute stored in the config context. \param[in] it ConfigIterator used to iterate over the attributes. \param[out] name String to store the name of the next attribute. \param[out] value String to store the value of the next attribute. \return Returns dmz::True if the next name and value were stored in \a name and \a value. Returns dmz::False when there are no more name-value pairs to return. */ dmz::Boolean dmz::Config::get_next_attribute ( ConfigIterator &it, String &name, String &value) const { Boolean result (False); if (_state.context) { ConfigAttributeContext *ac = _state.context->attrTable.get_next (it.state.it); if (ac) { ac->lock.lock (); value = ac->value; ac->lock.unlock (); name = it.state.it.get_hash_key (); if ((name == "") || (!value.get_buffer ())) { result = get_next_attribute (it, name, value); } else { result = True; } } } return result; }
/*! \brief Determines if a Config objects have equal attributes. \param[in] Data Config used to compare the attributes. \return Returns dmz::True if \a Data has a equal number of attributes. And that each of those attributes have the same name and value. Will return dmz::False if \a Data has a different number of attributes. Or if any of the attributes have a different name or value. */ dmz::Boolean dmz::Config::are_attributes_equal (const Config &Data) const { Boolean result (False); if (_state.context && Data._state.context) { if (_state.context->attrTable.get_count () == Data._state.context->attrTable.get_count ()) { Boolean areEqual (True); ConfigIterator it; String name, lhs, rhs; while (areEqual && get_next_attribute (it, name, lhs)) { if (Data.lookup_attribute (name, rhs)) { if (lhs != rhs) { areEqual = False; } } else { areEqual = False; } } result = areEqual; } } return result; }
struct _json_attribute *json_next(FILE *json_input) { int ch; struct _json_attribute *attrs = NULL; while (1) { ch = fgetc(json_input); if (ch == EOF) break; switch (ch) { case ' ': case '\t': continue; case '{': attrs = get_next_attribute(json_input, NULL); break; } } return attrs; }
struct _json_attribute *get_next_attribute(FILE *json_input, struct _json_attribute *prev_attr) { struct _json_attribute *attr, *value_attr, *next_attr; int ch, i = 0, inside_quote = 0, got_name = 0, got_object = 0; char name[128], value[128]; attr = malloc(sizeof(*attr)); while (1) { ch = fgetc(json_input); switch (ch) { case EOF: goto end_loop; case '{': if (!got_name) continue; printf("(value of '%s' is an object)\n", name); ungetc(ch, json_input); value_attr = json_next(json_input); got_object = 1; break; case '}': goto end_loop; case '\"': if (!inside_quote) inside_quote = 1; else { inside_quote = 0; } break; case ',': next_attr = get_next_attribute(json_input, prev_attr); //goto end_loop; break; case ':': got_name = 1; name[i] = 0; i = 0; break; case '\n': continue; case ' ': if (!inside_quote) continue; default: if (!got_name) name[i++] = ch; else value[i++] = ch; } } end_loop: value[i] = 0; printf("'%s' : '%s'\n", name, value); attr->name = strdup(name); if (got_object) { attr->value = value_attr; attr->value_type = JSON_OBJECT; } else { attr->value = strdup(value); attr->value_type = JSON_STRING; } if (prev_attr) { // prepend attribute to list. attr->next = prev_attr->children; prev_attr->children = attr; } return attr; }