#include "dictobject.h" DictObject::DictObject(void) { //entry = new map; } DictObject::DictObject(DictObjectType type) { if (type == Dict) DictObject(); this->type = type; } DictObject::DictObject(string v) { this->value = v; this->type = Scalar; } DictObject::~DictObject() { map::iterator iter = entry.begin(); for (; iter != entry.end(); iter++) { if (iter->second == 0) continue; delete iter->second; iter->second = 0; } } // XXX need to do type checking when // trying to access a dict when it's a scalar // or vice versa. // is this a Dictionary DictObject? bool DictObject::isDict(void) { if (type == Dict) return true; return false; } // is this a String DictObject? bool DictObject::isScalar(void) { if (type == Scalar) return true; return false; } // checks for key existance. bool DictObject::hasKey(string key) { if (entry.count(key) == 1) return true; return false; } map& DictObject::getDict(void) { return entry; } // enter a key/value pair into the Dictionary. void DictObject::setValue(string key, DictObject *value) { entry[key] = value; } // retrieve the value using the given Dictionary Key DictObject* DictObject::getValue(string key) { return entry[key]; } // set the Scalar value void DictObject::setValue(string value) { this->value = value; } // retrieve the Scalar value. string DictObject::getValue() { return this->value; } vector* DictObject::getKeys(void) { vector *keys = new vector(); map::iterator it = entry.begin(); for (; it != entry.end(); it++) keys->push_back(it->first); return keys; } // Pretty Print the object. // DEPRECATED string DictObject::format(string indent) { string fmt; if (type == Scalar) { fmt = value; } else { map::iterator it = entry.begin(); fmt = "{\n"; while (it != entry.end()) { string fmtValue = (*it).second->format(indent + "\t"); string key = (*it).first; fmt += "\t" + indent + "'" + key + "' : " + fmtValue + ",\n"; ++it; } fmt += indent + "}"; } return fmt; } //#define TEST_DATAOBJECT #ifdef TEST_DATAOBJECT int main(void) { DictObject *root = new DictObject(DictObject::Dict); DictObject *d1 = new DictObject(DictObject::Scalar); DictObject *d2 = new DictObject(DictObject::Scalar); DictObject *d3 = new DictObject(DictObject::Scalar); DictObject *d4 = new DictObject(DictObject::Dict); DictObject *d5 = new DictObject(DictObject::Dict); d1->setValue("D1 Value"); d2->setValue("D2 Value"); d3->setValue("D3 Value"); if (d3->isScalar()) cout << "IS SCALAR" << endl; DictObject *dv4 = new DictObject("v1"); d4->setValue("k1", dv4); d4->setValue("k2", new DictObject("v2")); d4->setValue("k3", new DictObject("v3")); d4->setValue("k4", new DictObject("v4")); d4->setValue("k14", dv4); d5->setValue("k5", new DictObject("v5")); d5->setValue("k6", new DictObject("v6")); d5->setValue("k7", new DictObject("v7")); if (d5->isDict()) cout << "IS DICT" << endl; root->setValue("ds1", d1); root->setValue("ds2", d2); root->setValue("ds3", d3); root->setValue("ds4", d4); root->setValue("ds5", d5); string s = root->format(); cout << s << endl; map dict = root->getDict(); map::iterator iter = dict.begin(); cout << "KEYS" << endl; for (; iter != dict.end(); iter++) cout << (*iter).first << endl; delete root; return 0; } #endif