diff options
Diffstat (limited to 'lib/cpp/src/thrift/protocol/TDebugProtocol.cpp')
-rw-r--r-- | lib/cpp/src/thrift/protocol/TDebugProtocol.cpp | 218 |
1 files changed, 126 insertions, 92 deletions
diff --git a/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp b/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp index 63ea14de2..4687e82f8 100644 --- a/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp +++ b/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp @@ -28,7 +28,6 @@ using std::string; - static string byte_to_hex(const uint8_t byte) { char buf[3]; int ret = std::sprintf(buf, "%02x", (int)byte); @@ -38,28 +37,46 @@ static string byte_to_hex(const uint8_t byte) { return buf; } - -namespace apache { namespace thrift { namespace protocol { +namespace apache { +namespace thrift { +namespace protocol { string TDebugProtocol::fieldTypeName(TType type) { switch (type) { - case T_STOP : return "stop" ; - case T_VOID : return "void" ; - case T_BOOL : return "bool" ; - case T_BYTE : return "byte" ; - case T_I16 : return "i16" ; - case T_I32 : return "i32" ; - case T_U64 : return "u64" ; - case T_I64 : return "i64" ; - case T_DOUBLE : return "double" ; - case T_STRING : return "string" ; - case T_STRUCT : return "struct" ; - case T_MAP : return "map" ; - case T_SET : return "set" ; - case T_LIST : return "list" ; - case T_UTF8 : return "utf8" ; - case T_UTF16 : return "utf16" ; - default: return "unknown"; + case T_STOP: + return "stop"; + case T_VOID: + return "void"; + case T_BOOL: + return "bool"; + case T_BYTE: + return "byte"; + case T_I16: + return "i16"; + case T_I32: + return "i32"; + case T_U64: + return "u64"; + case T_I64: + return "i64"; + case T_DOUBLE: + return "double"; + case T_STRING: + return "string"; + case T_STRUCT: + return "struct"; + case T_MAP: + return "map"; + case T_SET: + return "set"; + case T_LIST: + return "list"; + case T_UTF8: + return "utf8"; + case T_UTF16: + return "utf16"; + default: + return "unknown"; } } @@ -75,19 +92,19 @@ void TDebugProtocol::indentDown() { } uint32_t TDebugProtocol::writePlain(const string& str) { - if(str.length() > (std::numeric_limits<uint32_t>::max)()) + if (str.length() > (std::numeric_limits<uint32_t>::max)()) throw TProtocolException(TProtocolException::SIZE_LIMIT); trans_->write((uint8_t*)str.data(), static_cast<uint32_t>(str.length())); return static_cast<uint32_t>(str.length()); } uint32_t TDebugProtocol::writeIndented(const string& str) { - if(str.length() > (std::numeric_limits<uint32_t>::max)()) + if (str.length() > (std::numeric_limits<uint32_t>::max)()) throw TProtocolException(TProtocolException::SIZE_LIMIT); - if(indent_str_.length() > (std::numeric_limits<uint32_t>::max)()) + if (indent_str_.length() > (std::numeric_limits<uint32_t>::max)()) throw TProtocolException(TProtocolException::SIZE_LIMIT); uint64_t total_len = indent_str_.length() + str.length(); - if(total_len > (std::numeric_limits<uint32_t>::max)()) + if (total_len > (std::numeric_limits<uint32_t>::max)()) throw TProtocolException(TProtocolException::SIZE_LIMIT); trans_->write((uint8_t*)indent_str_.data(), static_cast<uint32_t>(indent_str_.length())); trans_->write((uint8_t*)str.data(), static_cast<uint32_t>(str.length())); @@ -98,52 +115,51 @@ uint32_t TDebugProtocol::startItem() { uint32_t size; switch (write_state_.back()) { - case UNINIT: - // XXX figure out what to do here. - //throw TProtocolException(TProtocolException::INVALID_DATA); - //return writeIndented(str); - return 0; - case STRUCT: - return 0; - case SET: - return writeIndented(""); - case MAP_KEY: - return writeIndented(""); - case MAP_VALUE: - return writePlain(" -> "); - case LIST: - size = writeIndented( - "[" + boost::lexical_cast<string>(list_idx_.back()) + "] = "); - list_idx_.back()++; - return size; - default: - throw std::logic_error("Invalid enum value."); + case UNINIT: + // XXX figure out what to do here. + // throw TProtocolException(TProtocolException::INVALID_DATA); + // return writeIndented(str); + return 0; + case STRUCT: + return 0; + case SET: + return writeIndented(""); + case MAP_KEY: + return writeIndented(""); + case MAP_VALUE: + return writePlain(" -> "); + case LIST: + size = writeIndented("[" + boost::lexical_cast<string>(list_idx_.back()) + "] = "); + list_idx_.back()++; + return size; + default: + throw std::logic_error("Invalid enum value."); } } uint32_t TDebugProtocol::endItem() { - //uint32_t size; + // uint32_t size; switch (write_state_.back()) { - case UNINIT: - // XXX figure out what to do here. - //throw TProtocolException(TProtocolException::INVALID_DATA); - //return writeIndented(str); - return 0; - case STRUCT: - return writePlain(",\n"); - case SET: - return writePlain(",\n"); - case MAP_KEY: - write_state_.back() = MAP_VALUE; - return 0; - case MAP_VALUE: - write_state_.back() = MAP_KEY; - return writePlain(",\n"); - case LIST: - return writePlain(",\n"); - default: - throw std::logic_error("Invalid enum value."); + case UNINIT: + // XXX figure out what to do here. + // throw TProtocolException(TProtocolException::INVALID_DATA); + // return writeIndented(str); + return 0; + case STRUCT: + return writePlain(",\n"); + case SET: + return writePlain(",\n"); + case MAP_KEY: + write_state_.back() = MAP_VALUE; + return 0; + case MAP_VALUE: + write_state_.back() = MAP_KEY; + return writePlain(",\n"); + case LIST: + return writePlain(",\n"); + default: + throw std::logic_error("Invalid enum value."); } } @@ -158,13 +174,21 @@ uint32_t TDebugProtocol::writeItem(const std::string& str) { uint32_t TDebugProtocol::writeMessageBegin(const std::string& name, const TMessageType messageType, const int32_t seqid) { - (void) seqid; + (void)seqid; string mtype; switch (messageType) { - case T_CALL : mtype = "call" ; break; - case T_REPLY : mtype = "reply" ; break; - case T_EXCEPTION : mtype = "exn" ; break; - case T_ONEWAY : mtype = "oneway" ; break; + case T_CALL: + mtype = "call"; + break; + case T_REPLY: + mtype = "reply"; + break; + case T_EXCEPTION: + mtype = "exn"; + break; + case T_ONEWAY: + mtype = "oneway"; + break; } uint32_t size = writeIndented("(" + mtype + ") " + name + "("); @@ -200,12 +224,10 @@ uint32_t TDebugProtocol::writeFieldBegin(const char* name, const int16_t fieldId) { // sprintf(id_str, "%02d", fieldId); string id_str = boost::lexical_cast<string>(fieldId); - if (id_str.length() == 1) id_str = '0' + id_str; + if (id_str.length() == 1) + id_str = '0' + id_str; - return writeIndented( - id_str + ": " + - name + " (" + - fieldTypeName(fieldType) + ") = "); + return writeIndented(id_str + ": " + name + " (" + fieldTypeName(fieldType) + ") = "); } uint32_t TDebugProtocol::writeFieldEnd() { @@ -215,7 +237,7 @@ uint32_t TDebugProtocol::writeFieldEnd() { uint32_t TDebugProtocol::writeFieldStop() { return 0; - //writeIndented("***STOP***\n"); + // writeIndented("***STOP***\n"); } uint32_t TDebugProtocol::writeMapBegin(const TType keyType, @@ -241,8 +263,7 @@ uint32_t TDebugProtocol::writeMapEnd() { return size; } -uint32_t TDebugProtocol::writeListBegin(const TType elemType, - const uint32_t size) { +uint32_t TDebugProtocol::writeListBegin(const TType elemType, const uint32_t size) { // TODO(dreiss): Optimize short arrays. uint32_t bsize = 0; bsize += startItem(); @@ -265,8 +286,7 @@ uint32_t TDebugProtocol::writeListEnd() { return size; } -uint32_t TDebugProtocol::writeSetBegin(const TType elemType, - const uint32_t size) { +uint32_t TDebugProtocol::writeSetBegin(const TType elemType, const uint32_t size) { // TODO(dreiss): Optimize short sets. uint32_t bsize = 0; bsize += startItem(); @@ -311,7 +331,6 @@ uint32_t TDebugProtocol::writeDouble(const double dub) { return writeItem(boost::lexical_cast<string>(dub)); } - uint32_t TDebugProtocol::writeString(const string& str) { // XXX Raw/UTF-8? @@ -332,16 +351,30 @@ uint32_t TDebugProtocol::writeString(const string& str) { output += *it; } else { switch (*it) { - case '\a': output += "\\a"; break; - case '\b': output += "\\b"; break; - case '\f': output += "\\f"; break; - case '\n': output += "\\n"; break; - case '\r': output += "\\r"; break; - case '\t': output += "\\t"; break; - case '\v': output += "\\v"; break; - default: - output += "\\x"; - output += byte_to_hex(*it); + case '\a': + output += "\\a"; + break; + case '\b': + output += "\\b"; + break; + case '\f': + output += "\\f"; + break; + case '\n': + output += "\\n"; + break; + case '\r': + output += "\\r"; + break; + case '\t': + output += "\\t"; + break; + case '\v': + output += "\\v"; + break; + default: + output += "\\x"; + output += byte_to_hex(*it); } } } @@ -354,5 +387,6 @@ uint32_t TDebugProtocol::writeBinary(const string& str) { // XXX Hex? return TDebugProtocol::writeString(str); } - -}}} // apache::thrift::protocol +} +} +} // apache::thrift::protocol |