2
0

ObjectStreamTextIn.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/ObjectStream/ObjectStreamTextIn.h>
  5. JPH_NAMESPACE_BEGIN
  6. ObjectStreamTextIn::ObjectStreamTextIn(istream &inStream) :
  7. ObjectStreamIn(inStream)
  8. {
  9. }
  10. bool ObjectStreamTextIn::ReadDataType(EOSDataType &outType)
  11. {
  12. String token;
  13. if (ReadWord(token))
  14. {
  15. transform(token.begin(), token.end(), token.begin(), [](char inValue) { return (char)tolower(inValue); });
  16. if (token == "declare")
  17. outType = EOSDataType::Declare;
  18. else if (token == "object")
  19. outType = EOSDataType::Object;
  20. else if (token == "instance")
  21. outType = EOSDataType::Instance;
  22. else if (token == "pointer")
  23. outType = EOSDataType::Pointer;
  24. else if (token == "array")
  25. outType = EOSDataType::Array;
  26. else if (token == "uint8")
  27. outType = EOSDataType::T_uint8;
  28. else if (token == "uint16")
  29. outType = EOSDataType::T_uint16;
  30. else if (token == "int")
  31. outType = EOSDataType::T_int;
  32. else if (token == "uint32")
  33. outType = EOSDataType::T_uint32;
  34. else if (token == "uint64")
  35. outType = EOSDataType::T_uint64;
  36. else if (token == "float")
  37. outType = EOSDataType::T_float;
  38. else if (token == "bool")
  39. outType = EOSDataType::T_bool;
  40. else if (token == "string")
  41. outType = EOSDataType::T_String;
  42. else if (token == "float3")
  43. outType = EOSDataType::T_Float3;
  44. else if (token == "vec3")
  45. outType = EOSDataType::T_Vec3;
  46. else if (token == "vec4")
  47. outType = EOSDataType::T_Vec4;
  48. else if (token == "quat")
  49. outType = EOSDataType::T_Quat;
  50. else if (token == "mat44")
  51. outType = EOSDataType::T_Mat44;
  52. else
  53. {
  54. Trace("ObjectStreamTextIn: Found unknown data type.");
  55. return false;
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool ObjectStreamTextIn::ReadName(String &outName)
  62. {
  63. return ReadWord(outName);
  64. }
  65. bool ObjectStreamTextIn::ReadIdentifier(Identifier &outIdentifier)
  66. {
  67. String token;
  68. if (!ReadWord(token))
  69. return false;
  70. outIdentifier = (uint32)std::strtoul(token.c_str(), nullptr, 16);
  71. if (errno == ERANGE)
  72. {
  73. outIdentifier = sNullIdentifier;
  74. return false;
  75. }
  76. return true;
  77. }
  78. bool ObjectStreamTextIn::ReadCount(uint32 &outCount)
  79. {
  80. return ReadPrimitiveData(outCount);
  81. }
  82. bool ObjectStreamTextIn::ReadPrimitiveData(uint8 &outPrimitive)
  83. {
  84. String token;
  85. if (!ReadWord(token))
  86. return false;
  87. uint32 temporary;
  88. IStringStream stream(token);
  89. stream >> temporary;
  90. if (!stream.fail())
  91. {
  92. outPrimitive = (uint8)temporary;
  93. return true;
  94. }
  95. return false;
  96. }
  97. bool ObjectStreamTextIn::ReadPrimitiveData(uint16 &outPrimitive)
  98. {
  99. String token;
  100. if (!ReadWord(token))
  101. return false;
  102. uint32 temporary;
  103. IStringStream stream(token);
  104. stream >> temporary;
  105. if (!stream.fail())
  106. {
  107. outPrimitive = (uint16)temporary;
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool ObjectStreamTextIn::ReadPrimitiveData(int &outPrimitive)
  113. {
  114. String token;
  115. if (!ReadWord(token))
  116. return false;
  117. IStringStream stream(token);
  118. stream >> outPrimitive;
  119. return !stream.fail();
  120. }
  121. bool ObjectStreamTextIn::ReadPrimitiveData(uint32 &outPrimitive)
  122. {
  123. String token;
  124. if (!ReadWord(token))
  125. return false;
  126. IStringStream stream(token);
  127. stream >> outPrimitive;
  128. return !stream.fail();
  129. }
  130. bool ObjectStreamTextIn::ReadPrimitiveData(uint64 &outPrimitive)
  131. {
  132. String token;
  133. if (!ReadWord(token))
  134. return false;
  135. IStringStream stream(token);
  136. stream >> outPrimitive;
  137. return !stream.fail();
  138. }
  139. bool ObjectStreamTextIn::ReadPrimitiveData(float &outPrimitive)
  140. {
  141. String token;
  142. if (!ReadWord(token))
  143. return false;
  144. IStringStream stream(token);
  145. stream >> outPrimitive;
  146. return !stream.fail();
  147. }
  148. bool ObjectStreamTextIn::ReadPrimitiveData(bool &outPrimitive)
  149. {
  150. String token;
  151. if (!ReadWord(token))
  152. return false;
  153. transform(token.begin(), token.end(), token.begin(), [](char inValue) { return (char)tolower(inValue); });
  154. outPrimitive = token == "true";
  155. return outPrimitive || token == "false";
  156. }
  157. bool ObjectStreamTextIn::ReadPrimitiveData(String &outPrimitive)
  158. {
  159. outPrimitive.clear();
  160. char c;
  161. // Skip whitespace
  162. for (;;)
  163. {
  164. if (!ReadChar(c))
  165. return false;
  166. if (!isspace(c))
  167. break;
  168. }
  169. // Check if it is a opening quote
  170. if (c != '\"')
  171. return false;
  172. // Read string and interpret special characters
  173. String result;
  174. bool escaped = false;
  175. for (;;)
  176. {
  177. if (!ReadChar(c))
  178. break;
  179. switch (c)
  180. {
  181. case '\n':
  182. case '\t':
  183. break;
  184. case '\\':
  185. if (escaped)
  186. {
  187. result += '\\';
  188. escaped = false;
  189. }
  190. else
  191. escaped = true;
  192. break;
  193. case 'n':
  194. if (escaped)
  195. {
  196. result += '\n';
  197. escaped = false;
  198. }
  199. else
  200. result += 'n';
  201. break;
  202. case 't':
  203. if (escaped)
  204. {
  205. result += '\t';
  206. escaped = false;
  207. }
  208. else
  209. result += 't';
  210. break;
  211. case '\"':
  212. if (escaped)
  213. {
  214. result += '\"';
  215. escaped = false;
  216. }
  217. else
  218. {
  219. // Found closing double quote
  220. outPrimitive = result;
  221. return true;
  222. }
  223. break;
  224. default:
  225. if (escaped)
  226. escaped = false;
  227. else
  228. result += c;
  229. break;
  230. }
  231. }
  232. return false;
  233. }
  234. bool ObjectStreamTextIn::ReadPrimitiveData(Float3 &outPrimitive)
  235. {
  236. float x, y, z;
  237. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  238. return false;
  239. outPrimitive = Float3(x, y, z);
  240. return true;
  241. }
  242. bool ObjectStreamTextIn::ReadPrimitiveData(Vec3 &outPrimitive)
  243. {
  244. float x, y, z;
  245. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  246. return false;
  247. outPrimitive = Vec3(x, y, z);
  248. return true;
  249. }
  250. bool ObjectStreamTextIn::ReadPrimitiveData(Vec4 &outPrimitive)
  251. {
  252. float x, y, z, w;
  253. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
  254. return false;
  255. outPrimitive = Vec4(x, y, z, w);
  256. return true;
  257. }
  258. bool ObjectStreamTextIn::ReadPrimitiveData(Quat &outPrimitive)
  259. {
  260. float x, y, z, w;
  261. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
  262. return false;
  263. outPrimitive = Quat(x, y, z, w);
  264. return true;
  265. }
  266. bool ObjectStreamTextIn::ReadPrimitiveData(Mat44 &outPrimitive)
  267. {
  268. Vec4 c0, c1, c2, c3;
  269. if (!ReadPrimitiveData(c0) || !ReadPrimitiveData(c1) || !ReadPrimitiveData(c2) || !ReadPrimitiveData(c3))
  270. return false;
  271. outPrimitive = Mat44(c0, c1, c2, c3);
  272. return true;
  273. }
  274. bool ObjectStreamTextIn::ReadChar(char &outChar)
  275. {
  276. mStream.get(outChar);
  277. return !mStream.eof();
  278. }
  279. bool ObjectStreamTextIn::ReadWord(String &outWord)
  280. {
  281. outWord.clear();
  282. char c;
  283. // Skip whitespace
  284. for (;;)
  285. {
  286. if (!ReadChar(c))
  287. return false;
  288. if (!isspace(c))
  289. break;
  290. }
  291. // Read word
  292. for (;;)
  293. {
  294. outWord += c;
  295. if (!ReadChar(c))
  296. break;
  297. if (isspace(c))
  298. break;
  299. }
  300. return !outWord.empty();
  301. }
  302. JPH_NAMESPACE_END