From 1563ae5be6bc130a9b3a23464f7e28fdb1e87da3 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 13 May 2017 19:01:56 +0500 Subject: 2017-05-13 --- code/FieldParser.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 code/FieldParser.cpp (limited to 'code/FieldParser.cpp') diff --git a/code/FieldParser.cpp b/code/FieldParser.cpp new file mode 100644 index 0000000..500a973 --- /dev/null +++ b/code/FieldParser.cpp @@ -0,0 +1,106 @@ +#include "FieldParser.hpp" + +Field FieldParser::Parse(FieldType type, byte *data, size_t len) { + switch (type) { + case VarInt: + return ParseVarInt(data, len); + case Boolean: + return ParseBool(data, len); + case String: + return ParseString(data, len); + case Long: + return ParseLong(data, len); + case Int: + return ParseInt(data, len); + case UnsignedByte: + return ParseUByte(data, len); + case Byte8_t: + return ParseByte(data, len); + case Float: + return ParseFloat(data, len); + case Position: + return ParsePosition(data, len); + case Double: + return ParseDouble(data, len); + case ByteArray: + return ParseByteArray(data, len); + default: + throw 105; + } +} + +Field FieldParser::ParseString(byte *data, size_t len) { + Field fLen = ParseVarInt(data, 0); + Field f; + f.SetRaw(data, fLen.GetLength() + fLen.GetVarInt(), String); + return f; +} + +Field FieldParser::ParseBool(byte *data, size_t len) { + Field f; + f.SetRaw(data,1,Boolean); + return f; +} + +Field FieldParser::ParseVarInt(byte *data, size_t len) { + if (len != 0) { + Field f; + f.SetRaw(data, len, VarInt); + return f; + } + int val = VarIntRead(data, len); + Field f; + f.SetVarInt(val); + return f; +} + +Field FieldParser::ParseLong(byte *data, size_t len) { + Field f; + f.SetRaw(data, 8, Long); + return f; +} + +Field FieldParser::ParseInt(byte *data, size_t len) { + Field f; + f.SetRaw(data, 4, Int); + return f; +} + +Field FieldParser::ParseUByte(byte *data, size_t len) { + Field f; + f.SetRaw(data, 1, UnsignedByte); + return f; +} + +Field FieldParser::ParseByte(byte *data, size_t len) { + Field f; + f.SetRaw(data, 1, Byte8_t); + return f; +} + +Field FieldParser::ParseFloat(byte *data, size_t len) { + Field f; + f.SetRaw(data, 4, Float); + return f; +} + +Field FieldParser::ParsePosition(byte *data, size_t len) { + Field f; + f.SetRaw(data, 8, Position); + return f; +} + +Field FieldParser::ParseDouble(byte *data, size_t len) { + Field f; + f.SetRaw(data, 8, Double); + return f; +} + +Field FieldParser::ParseByteArray(byte *data, size_t len) { + if (len == 0) + throw 119; + Field f; + f.SetRaw(data, len, Byte8_t); + //f.SetRaw(data, len, ByteArray); + return f; +} -- cgit v1.2.3