ObjectStreamTextIn.cpp 7.8 KB

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