plist.cc
Go to the documentation of this file.00001 #include "plist.h"
00002 #include <stdexcept>
00003
00004 using namespace std;
00005
00006 namespace plist {
00007
00008 ObjectBase* loadXML(xmlNode* node) {
00009 ObjectBase* obj=NULL;
00010 if(ObjectBase::xNodeHasName(node,"array")) {
00011 Array * a=new Array;
00012 a->setLoadSavePolicy(Collection::SYNC,Collection::SYNC);
00013 obj=a;
00014 } else if(ObjectBase::xNodeHasName(node,"dict")) {
00015 Dictionary * d=new Dictionary;
00016 d->setLoadSavePolicy(Collection::SYNC,Collection::SYNC);
00017 obj=d;
00018 } else if(ObjectBase::xNodeHasName(node,"real"))
00019 obj=new Primitive<double>;
00020 else if(ObjectBase::xNodeHasName(node,"integer"))
00021 obj=new Primitive<long>;
00022 else if(ObjectBase::xNodeHasName(node,"string"))
00023 obj=new Primitive<std::string>;
00024 else if(ObjectBase::xNodeHasName(node,"true"))
00025 obj=new Primitive<bool>;
00026 else if(ObjectBase::xNodeHasName(node,"false"))
00027 obj=new Primitive<bool>;
00028 else
00029 return NULL;
00030 obj->loadXML(node);
00031 return obj;
00032 }
00033
00034
00035 class PolymorphicLoader : public ObjectBase {
00036 public:
00037 PolymorphicLoader() : ObjectBase(), obj(NULL) {}
00038 virtual ~PolymorphicLoader() {}
00039 PolymorphicLoader(const PolymorphicLoader& p) : ObjectBase(p), obj(NULL) {}
00040 PolymorphicLoader& operator=(const PolymorphicLoader& p) { ObjectBase::operator=(p); return *this; }
00041 virtual void loadXML(xmlNode* node) { obj=plist::loadXML(node); }
00042 virtual void saveXML(xmlNode * ) const { throw std::logic_error("PolymorphicLoader::saveXML called, should not happen (saveXML on the object itself...)"); }
00043
00044 virtual std::string toString() const { return ""; }
00045 virtual long toLong() const { return 0; }
00046 virtual double toDouble() const { return 0; }
00047 virtual void set(const ObjectBase&) {}
00048 PLIST_CLONE_DEF(PolymorphicLoader,new PolymorphicLoader);
00049
00050 ObjectBase * obj;
00051 };
00052 PLIST_CLONE_IMP(PolymorphicLoader,new PolymorphicLoader);
00053
00054 ObjectBase* loadFile(const std::string& file) {
00055 PolymorphicLoader loader;
00056 if(!loader.loadFile(file.c_str()))
00057 return NULL;
00058 return loader.obj;
00059 }
00060
00061 ObjectBase* loadBuffer(const char* buf, unsigned int len) {
00062 PolymorphicLoader loader;
00063 if(!loader.loadBuffer(buf,len))
00064 return NULL;
00065 return loader.obj;
00066 }
00067
00068 }
00069
00070
00071
00072
00073