ObjectStreamTextIn.cpp 8.4 KB

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