X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDeserializer.cc;h=c92ef47ef6fb31081c406576d9967980dc0d38ef;hp=8bf230fe68627054e225fd54aa8b48ad9f15a1aa;hb=fdfba4553433b9b2804c2772c7645211b828c2ea;hpb=5fa5f117f28922a7e539a432367960c1a61f837d diff --git a/src/Moof/Deserializer.cc b/src/Moof/Deserializer.cc index 8bf230f..c92ef47 100644 --- a/src/Moof/Deserializer.cc +++ b/src/Moof/Deserializer.cc @@ -32,29 +32,30 @@ #include #include "Deserializer.hh" +#include "Log.hh" #include "Serializable.hh" namespace Mf { -class Deserializer::DeserializerImpl +class Deserializer::Impl { public: - DeserializerImpl(const std::string& filePath, bool comments = false, + Impl(const std::string& filePath, bool comments = false, bool check = false) { std::ifstream* input = new std::ifstream(filePath.c_str()); init(*input, true, comments, check); } - DeserializerImpl(std::istream& input, bool comments = false, + Impl(std::istream& input, bool comments = false, bool check = false) { init(input, false, comments, check); } - ~DeserializerImpl() + ~Impl() { while (!parsed.empty()) { @@ -69,51 +70,50 @@ public: yajl_free(hand); } - void throwError() + void raise() { unsigned char* errorStr = yajl_get_error(hand, 0, 0, 0); - Deserializer::Exception exception((char*)errorStr); + logError("parser error: %s", errorStr); yajl_free_error(hand, errorStr); - throw exception; + + throw Exception(Exception::PARSING_FAILED); } static int parsedNull(void* ctx) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableNull); + ((Impl*)ctx)->parsed.push(new SerializableNull); return 1; } static int parsedBoolean(void* ctx, int value) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableBoolean(value)); + ((Impl*)ctx)->parsed.push(new SerializableBasic(value)); return 1; } static int parsedInteger(void* ctx, long value) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableInteger(value)); + ((Impl*)ctx)->parsed.push(new SerializableBasic(value)); return 1; } static int parsedFloat(void* ctx, double value) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableReal(value)); + ((Impl*)ctx)->parsed.push(new SerializableBasic(value)); return 1; } static int parsedString(void* ctx, const unsigned char* value, unsigned length) { - SerializableString* parsed = - new SerializableString(std::string((char*)value, length)); - ((DeserializerImpl*)ctx)->parsed.push(parsed); + ((Impl*)ctx)->parsed.push(new SerializableBasic(std::string((char*)value, length))); return 1; } static int parsedBeginMap(void* ctx) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableMap); + ((Impl*)ctx)->parsed.push(new SerializableBasic); return 1; } @@ -126,19 +126,21 @@ public: static int parsedEndMap(void* ctx) { - ((DeserializerImpl*)ctx)->parsed.push(0); + // null means the end of a structure + ((Impl*)ctx)->parsed.push(0); return 1; } static int parsedBeginArray(void* ctx) { - ((DeserializerImpl*)ctx)->parsed.push(new SerializableArray); + ((Impl*)ctx)->parsed.push(new SerializableBasic); return 1; } static int parsedEndArray(void* ctx) { - ((DeserializerImpl*)ctx)->parsed.push(0); + // null means the end of a structure + ((Impl*)ctx)->parsed.push(0); return 1; } @@ -166,7 +168,7 @@ public: if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { - throwError(); + raise(); } } } @@ -186,17 +188,17 @@ private: // internal data structures but rather keeps a pointer to this static const yajl_callbacks callbacks = { - DeserializerImpl::parsedNull, - DeserializerImpl::parsedBoolean, - DeserializerImpl::parsedInteger, - DeserializerImpl::parsedFloat, + Impl::parsedNull, + Impl::parsedBoolean, + Impl::parsedInteger, + Impl::parsedFloat, 0, - DeserializerImpl::parsedString, - DeserializerImpl::parsedBeginMap, - DeserializerImpl::parsedMapKey, - DeserializerImpl::parsedEndMap, - DeserializerImpl::parsedBeginArray, - DeserializerImpl::parsedEndArray + Impl::parsedString, + Impl::parsedBeginMap, + Impl::parsedMapKey, + Impl::parsedEndMap, + Impl::parsedBeginArray, + Impl::parsedEndArray }; in = &input; @@ -211,21 +213,21 @@ private: Deserializer::Deserializer(const std::string& filePath, bool comments, bool check) : // pass through - impl_(new Deserializer::DeserializerImpl(filePath, comments, check)) {} + impl_(new Deserializer::Impl(filePath, comments, check)) {} Deserializer::Deserializer(std::istream& input, bool comments, bool check) : // pass through - impl_(new Deserializer::DeserializerImpl(input, comments, check)) {} + impl_(new Deserializer::Impl(input, comments, check)) {} -SerializablePtr Deserializer::deserialize() +SerializableP Deserializer::deserialize() { Serializable* ptr = pullNext(); if (ptr) { ptr->deserialize(*this); } - return SerializablePtr(ptr); + return SerializableP(ptr); }