123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #include <Jolt/Jolt.h>
- #include <Jolt/ObjectStream/ObjectStreamTextIn.h>
- JPH_NAMESPACE_BEGIN
- ObjectStreamTextIn::ObjectStreamTextIn(istream &inStream) :
- ObjectStreamIn(inStream)
- {
- }
- bool ObjectStreamTextIn::ReadDataType(EOSDataType &outType)
- {
- String token;
- if (ReadWord(token))
- {
- transform(token.begin(), token.end(), token.begin(), [](char inValue) { return (char)tolower(inValue); });
- if (token == "declare")
- outType = EOSDataType::Declare;
- else if (token == "object")
- outType = EOSDataType::Object;
- else if (token == "instance")
- outType = EOSDataType::Instance;
- else if (token == "pointer")
- outType = EOSDataType::Pointer;
- else if (token == "array")
- outType = EOSDataType::Array;
- else if (token == "uint8")
- outType = EOSDataType::T_uint8;
- else if (token == "uint16")
- outType = EOSDataType::T_uint16;
- else if (token == "int")
- outType = EOSDataType::T_int;
- else if (token == "uint32")
- outType = EOSDataType::T_uint32;
- else if (token == "uint64")
- outType = EOSDataType::T_uint64;
- else if (token == "float")
- outType = EOSDataType::T_float;
- else if (token == "bool")
- outType = EOSDataType::T_bool;
- else if (token == "string")
- outType = EOSDataType::T_String;
- else if (token == "float3")
- outType = EOSDataType::T_Float3;
- else if (token == "vec3")
- outType = EOSDataType::T_Vec3;
- else if (token == "vec4")
- outType = EOSDataType::T_Vec4;
- else if (token == "quat")
- outType = EOSDataType::T_Quat;
- else if (token == "mat44")
- outType = EOSDataType::T_Mat44;
- else
- {
- Trace("ObjectStreamTextIn: Found unknown data type.");
- return false;
- }
- return true;
- }
- return false;
- }
- bool ObjectStreamTextIn::ReadName(String &outName)
- {
- return ReadWord(outName);
- }
- bool ObjectStreamTextIn::ReadIdentifier(Identifier &outIdentifier)
- {
- String token;
- if (!ReadWord(token))
- return false;
- outIdentifier = (uint32)std::strtoul(token.c_str(), nullptr, 16);
- if (errno == ERANGE)
- {
- outIdentifier = sNullIdentifier;
- return false;
- }
- return true;
- }
- bool ObjectStreamTextIn::ReadCount(uint32 &outCount)
- {
- return ReadPrimitiveData(outCount);
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(uint8 &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- uint32 temporary;
- IStringStream stream(token);
- stream >> temporary;
- if (!stream.fail())
- {
- outPrimitive = (uint8)temporary;
- return true;
- }
- return false;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(uint16 &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- uint32 temporary;
- IStringStream stream(token);
- stream >> temporary;
- if (!stream.fail())
- {
- outPrimitive = (uint16)temporary;
- return true;
- }
- return false;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(int &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- IStringStream stream(token);
- stream >> outPrimitive;
- return !stream.fail();
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(uint32 &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- IStringStream stream(token);
- stream >> outPrimitive;
- return !stream.fail();
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(uint64 &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- IStringStream stream(token);
- stream >> outPrimitive;
- return !stream.fail();
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(float &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- IStringStream stream(token);
- stream >> outPrimitive;
- return !stream.fail();
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(bool &outPrimitive)
- {
- String token;
- if (!ReadWord(token))
- return false;
- transform(token.begin(), token.end(), token.begin(), [](char inValue) { return (char)tolower(inValue); });
- outPrimitive = token == "true";
- return outPrimitive || token == "false";
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(String &outPrimitive)
- {
- outPrimitive.clear();
- char c;
- // Skip whitespace
- for (;;)
- {
- if (!ReadChar(c))
- return false;
- if (!isspace(c))
- break;
- }
- // Check if it is a opening quote
- if (c != '\"')
- return false;
- // Read string and interpret special characters
- String result;
- bool escaped = false;
- for (;;)
- {
- if (!ReadChar(c))
- break;
- switch (c)
- {
- case '\n':
- case '\t':
- break;
- case '\\':
- if (escaped)
- {
- result += '\\';
- escaped = false;
- }
- else
- escaped = true;
- break;
- case 'n':
- if (escaped)
- {
- result += '\n';
- escaped = false;
- }
- else
- result += 'n';
- break;
- case 't':
- if (escaped)
- {
- result += '\t';
- escaped = false;
- }
- else
- result += 't';
- break;
- case '\"':
- if (escaped)
- {
- result += '\"';
- escaped = false;
- }
- else
- {
- // Found closing double quote
- outPrimitive = result;
- return true;
- }
- break;
- default:
- if (escaped)
- escaped = false;
- else
- result += c;
- break;
- }
- }
- return false;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(Float3 &outPrimitive)
- {
- float x, y, z;
- if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
- return false;
- outPrimitive = Float3(x, y, z);
- return true;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(Vec3 &outPrimitive)
- {
- float x, y, z;
- if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
- return false;
- outPrimitive = Vec3(x, y, z);
- return true;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(Vec4 &outPrimitive)
- {
- float x, y, z, w;
- if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
- return false;
- outPrimitive = Vec4(x, y, z, w);
- return true;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(Quat &outPrimitive)
- {
- float x, y, z, w;
- if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
- return false;
- outPrimitive = Quat(x, y, z, w);
- return true;
- }
- bool ObjectStreamTextIn::ReadPrimitiveData(Mat44 &outPrimitive)
- {
- Vec4 c0, c1, c2, c3;
- if (!ReadPrimitiveData(c0) || !ReadPrimitiveData(c1) || !ReadPrimitiveData(c2) || !ReadPrimitiveData(c3))
- return false;
- outPrimitive = Mat44(c0, c1, c2, c3);
- return true;
- }
- bool ObjectStreamTextIn::ReadChar(char &outChar)
- {
- mStream.get(outChar);
- return !mStream.eof();
- }
- bool ObjectStreamTextIn::ReadWord(String &outWord)
- {
- outWord.clear();
- char c;
- // Skip whitespace
- for (;;)
- {
- if (!ReadChar(c))
- return false;
- if (!isspace(c))
- break;
- }
- // Read word
- for (;;)
- {
- outWord += c;
- if (!ReadChar(c))
- break;
- if (isspace(c))
- break;
- }
- return !outWord.empty();
- }
- JPH_NAMESPACE_END
|