ObjectStreamTextIn.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 == "double")
  39. outType = EOSDataType::T_double;
  40. else if (token == "bool")
  41. outType = EOSDataType::T_bool;
  42. else if (token == "string")
  43. outType = EOSDataType::T_String;
  44. else if (token == "float3")
  45. outType = EOSDataType::T_Float3;
  46. else if (token == "double3")
  47. outType = EOSDataType::T_Double3;
  48. else if (token == "vec3")
  49. outType = EOSDataType::T_Vec3;
  50. else if (token == "dvec3")
  51. outType = EOSDataType::T_DVec3;
  52. else if (token == "vec4")
  53. outType = EOSDataType::T_Vec4;
  54. else if (token == "quat")
  55. outType = EOSDataType::T_Quat;
  56. else if (token == "mat44")
  57. outType = EOSDataType::T_Mat44;
  58. else if (token == "dmat44")
  59. outType = EOSDataType::T_DMat44;
  60. else
  61. {
  62. Trace("ObjectStreamTextIn: Found unknown data type.");
  63. return false;
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool ObjectStreamTextIn::ReadName(String &outName)
  70. {
  71. return ReadWord(outName);
  72. }
  73. bool ObjectStreamTextIn::ReadIdentifier(Identifier &outIdentifier)
  74. {
  75. String token;
  76. if (!ReadWord(token))
  77. return false;
  78. outIdentifier = (uint32)std::strtoul(token.c_str(), nullptr, 16);
  79. if (errno == ERANGE)
  80. {
  81. outIdentifier = sNullIdentifier;
  82. return false;
  83. }
  84. return true;
  85. }
  86. bool ObjectStreamTextIn::ReadCount(uint32 &outCount)
  87. {
  88. return ReadPrimitiveData(outCount);
  89. }
  90. bool ObjectStreamTextIn::ReadPrimitiveData(uint8 &outPrimitive)
  91. {
  92. String token;
  93. if (!ReadWord(token))
  94. return false;
  95. uint32 temporary;
  96. IStringStream stream(token);
  97. stream >> temporary;
  98. if (!stream.fail())
  99. {
  100. outPrimitive = (uint8)temporary;
  101. return true;
  102. }
  103. return false;
  104. }
  105. bool ObjectStreamTextIn::ReadPrimitiveData(uint16 &outPrimitive)
  106. {
  107. String token;
  108. if (!ReadWord(token))
  109. return false;
  110. uint32 temporary;
  111. IStringStream stream(token);
  112. stream >> temporary;
  113. if (!stream.fail())
  114. {
  115. outPrimitive = (uint16)temporary;
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool ObjectStreamTextIn::ReadPrimitiveData(int &outPrimitive)
  121. {
  122. String token;
  123. if (!ReadWord(token))
  124. return false;
  125. IStringStream stream(token);
  126. stream >> outPrimitive;
  127. return !stream.fail();
  128. }
  129. bool ObjectStreamTextIn::ReadPrimitiveData(uint32 &outPrimitive)
  130. {
  131. String token;
  132. if (!ReadWord(token))
  133. return false;
  134. IStringStream stream(token);
  135. stream >> outPrimitive;
  136. return !stream.fail();
  137. }
  138. bool ObjectStreamTextIn::ReadPrimitiveData(uint64 &outPrimitive)
  139. {
  140. String token;
  141. if (!ReadWord(token))
  142. return false;
  143. IStringStream stream(token);
  144. stream >> outPrimitive;
  145. return !stream.fail();
  146. }
  147. bool ObjectStreamTextIn::ReadPrimitiveData(float &outPrimitive)
  148. {
  149. String token;
  150. if (!ReadWord(token))
  151. return false;
  152. IStringStream stream(token);
  153. stream >> outPrimitive;
  154. return !stream.fail();
  155. }
  156. bool ObjectStreamTextIn::ReadPrimitiveData(double &outPrimitive)
  157. {
  158. String token;
  159. if (!ReadWord(token))
  160. return false;
  161. IStringStream stream(token);
  162. stream >> outPrimitive;
  163. return !stream.fail();
  164. }
  165. bool ObjectStreamTextIn::ReadPrimitiveData(bool &outPrimitive)
  166. {
  167. String token;
  168. if (!ReadWord(token))
  169. return false;
  170. transform(token.begin(), token.end(), token.begin(), [](char inValue) { return (char)tolower(inValue); });
  171. outPrimitive = token == "true";
  172. return outPrimitive || token == "false";
  173. }
  174. bool ObjectStreamTextIn::ReadPrimitiveData(String &outPrimitive)
  175. {
  176. outPrimitive.clear();
  177. char c;
  178. // Skip whitespace
  179. for (;;)
  180. {
  181. if (!ReadChar(c))
  182. return false;
  183. if (!isspace(c))
  184. break;
  185. }
  186. // Check if it is a opening quote
  187. if (c != '\"')
  188. return false;
  189. // Read string and interpret special characters
  190. String result;
  191. bool escaped = false;
  192. for (;;)
  193. {
  194. if (!ReadChar(c))
  195. break;
  196. switch (c)
  197. {
  198. case '\n':
  199. case '\t':
  200. break;
  201. case '\\':
  202. if (escaped)
  203. {
  204. result += '\\';
  205. escaped = false;
  206. }
  207. else
  208. escaped = true;
  209. break;
  210. case 'n':
  211. if (escaped)
  212. {
  213. result += '\n';
  214. escaped = false;
  215. }
  216. else
  217. result += 'n';
  218. break;
  219. case 't':
  220. if (escaped)
  221. {
  222. result += '\t';
  223. escaped = false;
  224. }
  225. else
  226. result += 't';
  227. break;
  228. case '\"':
  229. if (escaped)
  230. {
  231. result += '\"';
  232. escaped = false;
  233. }
  234. else
  235. {
  236. // Found closing double quote
  237. outPrimitive = result;
  238. return true;
  239. }
  240. break;
  241. default:
  242. if (escaped)
  243. escaped = false;
  244. else
  245. result += c;
  246. break;
  247. }
  248. }
  249. return false;
  250. }
  251. bool ObjectStreamTextIn::ReadPrimitiveData(Float3 &outPrimitive)
  252. {
  253. float x, y, z;
  254. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  255. return false;
  256. outPrimitive = Float3(x, y, z);
  257. return true;
  258. }
  259. bool ObjectStreamTextIn::ReadPrimitiveData(Double3 &outPrimitive)
  260. {
  261. double x, y, z;
  262. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  263. return false;
  264. outPrimitive = Double3(x, y, z);
  265. return true;
  266. }
  267. bool ObjectStreamTextIn::ReadPrimitiveData(Vec3 &outPrimitive)
  268. {
  269. float x, y, z;
  270. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  271. return false;
  272. outPrimitive = Vec3(x, y, z);
  273. return true;
  274. }
  275. bool ObjectStreamTextIn::ReadPrimitiveData(DVec3 &outPrimitive)
  276. {
  277. double x, y, z;
  278. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z))
  279. return false;
  280. outPrimitive = DVec3(x, y, z);
  281. return true;
  282. }
  283. bool ObjectStreamTextIn::ReadPrimitiveData(Vec4 &outPrimitive)
  284. {
  285. float x, y, z, w;
  286. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
  287. return false;
  288. outPrimitive = Vec4(x, y, z, w);
  289. return true;
  290. }
  291. bool ObjectStreamTextIn::ReadPrimitiveData(Quat &outPrimitive)
  292. {
  293. float x, y, z, w;
  294. if (!ReadPrimitiveData(x) || !ReadPrimitiveData(y) || !ReadPrimitiveData(z) || !ReadPrimitiveData(w))
  295. return false;
  296. outPrimitive = Quat(x, y, z, w);
  297. return true;
  298. }
  299. bool ObjectStreamTextIn::ReadPrimitiveData(Mat44 &outPrimitive)
  300. {
  301. Vec4 c0, c1, c2, c3;
  302. if (!ReadPrimitiveData(c0) || !ReadPrimitiveData(c1) || !ReadPrimitiveData(c2) || !ReadPrimitiveData(c3))
  303. return false;
  304. outPrimitive = Mat44(c0, c1, c2, c3);
  305. return true;
  306. }
  307. bool ObjectStreamTextIn::ReadPrimitiveData(DMat44 &outPrimitive)
  308. {
  309. Vec4 c0, c1, c2;
  310. DVec3 c3;
  311. if (!ReadPrimitiveData(c0) || !ReadPrimitiveData(c1) || !ReadPrimitiveData(c2) || !ReadPrimitiveData(c3))
  312. return false;
  313. outPrimitive = DMat44(c0, c1, c2, c3);
  314. return true;
  315. }
  316. bool ObjectStreamTextIn::ReadChar(char &outChar)
  317. {
  318. mStream.get(outChar);
  319. return !mStream.eof();
  320. }
  321. bool ObjectStreamTextIn::ReadWord(String &outWord)
  322. {
  323. outWord.clear();
  324. char c;
  325. // Skip whitespace
  326. for (;;)
  327. {
  328. if (!ReadChar(c))
  329. return false;
  330. if (!isspace(c))
  331. break;
  332. }
  333. // Read word
  334. for (;;)
  335. {
  336. outWord += c;
  337. if (!ReadChar(c))
  338. break;
  339. if (isspace(c))
  340. break;
  341. }
  342. return !outWord.empty();
  343. }
  344. JPH_NAMESPACE_END