2
0

ObjectStreamTextIn.cpp 7.8 KB

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