fileio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #pragma once
  2. #include "ThirdPartyHeadersBegin.h"
  3. #include <fstream>
  4. #include <map>
  5. #include <sstream>
  6. #include <stdexcept>
  7. #include <vector>
  8. #include <boost/foreach.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. #include "ThirdPartyHeadersEnd.h"
  11. #include "AsciiOutputInfo.h"
  12. #include "FileReaderInterface.h"
  13. #include "FileWriterInterface.h"
  14. #define READ_ENUM(___4336, type, file, isASCII) \
  15. uint32_t tempVar##type; \
  16. ::tecplot::tecioszl::readScalar(file, tempVar##type, isASCII); \
  17. ___4336 = static_cast<type>(tempVar##type)
  18. #define READ_ENUM_VECTOR(vec, type, file, isASCII) \
  19. std::vector<uint32_t> tempVec##type; \
  20. ::tecplot::tecioszl::readVector(file, tempVec##type, isASCII); \
  21. vec.reserve(tempVec##type.size()); \
  22. for (std::vector<uint32_t>::const_iterator it = tempVec##type.begin(); it != tempVec##type.end(); ++it) \
  23. vec.push_back(static_cast<type>(*it))
  24. namespace tecplot
  25. {
  26. namespace tecioszl
  27. {
  28. class FileIOException : public std::runtime_error
  29. {
  30. public:
  31. FileIOException(char const *what) : std::runtime_error(what) {}
  32. virtual ~FileIOException() throw() {}
  33. };
  34. template <typename T>
  35. inline void writeScalar(___3933::FileWriterInterface &outputFile, T ___4298, bool ___4480)
  36. {
  37. if (___4480)
  38. {
  39. char ___416[100];
  40. tecplot::___3933::encodeAsciiValue<T, false, 0>(___416, 100, ___4298);
  41. size_t len = strlen(___416);
  42. ___416[len] = '\r';
  43. ___416[len + 1] = '\n';
  44. if (outputFile.fwrite(___416, 1, len + 2) != len + 2)
  45. throw FileIOException("Error writing scalar to file");
  46. }
  47. else
  48. {
  49. if (outputFile.fwrite(&___4298, sizeof(___4298), 1) != 1)
  50. throw FileIOException("Error writing scalar to file");
  51. }
  52. }
  53. template <typename T>
  54. inline uint64_t scalarSizeInFile(T, bool ___4480)
  55. {
  56. if (___4480)
  57. return ___3933::___199<T, false>::size + 2;
  58. else
  59. return sizeof(T);
  60. }
  61. template <typename T>
  62. inline void readScalar(___3933::___1399 &inputFile, T &___4298, bool readASCII)
  63. {
  64. if (readASCII)
  65. {
  66. char ___416[100];
  67. inputFile.fgets(___416, 100);
  68. std::istringstream inputStream(___416);
  69. inputStream >> ___4298;
  70. if (inputStream.fail())
  71. throw FileIOException("Error reading scalar from file");
  72. }
  73. else
  74. {
  75. if (inputFile.fread((char *)&___4298, sizeof(___4298), 1) != 1)
  76. throw FileIOException("Error reading scalar from file");
  77. }
  78. }
  79. inline void ___4544(___3933::FileWriterInterface &outputFile, std::string const &str, bool ___4480)
  80. {
  81. uint64_t length = str.size();
  82. writeScalar(outputFile, length, ___4480);
  83. if (outputFile.fwrite(str.c_str(), 1, str.size()) != str.size())
  84. throw FileIOException("Error writing string length to file");
  85. if (___4480 && outputFile.fwrite("\r\n", 1, 2) != 2)
  86. throw FileIOException("Error writing string to file");
  87. }
  88. inline uint64_t stringSizeInFile(std::string const &str, bool ___4480)
  89. {
  90. uint64_t length = str.size();
  91. uint64_t sizeInFile = scalarSizeInFile(length, ___4480) + str.size();
  92. if (___4480)
  93. sizeInFile += 2;
  94. return sizeInFile;
  95. }
  96. inline void readString(___3933::___1399 &inputFile, std::string &str, bool readASCII)
  97. {
  98. uint64_t length;
  99. readScalar(inputFile, length, readASCII);
  100. str.resize((size_t)length);
  101. if (readASCII)
  102. {
  103. char ___416[100];
  104. inputFile.fgets(___416, 100);
  105. std::istringstream inputStream(___416);
  106. inputStream >> str;
  107. if (inputStream.fail())
  108. throw FileIOException("Error reading string length from file");
  109. }
  110. else
  111. {
  112. if (inputFile.fread(&str[0], 1, (size_t)length) != (size_t)length)
  113. throw FileIOException("Error reading string from file");
  114. }
  115. }
  116. template <typename T>
  117. inline void writeVector(___3933::FileWriterInterface &outputFile, std::vector<T> const &vec, bool ___4480)
  118. {
  119. uint64_t length = vec.size();
  120. writeScalar(outputFile, length, ___4480);
  121. if (vec.size() > 0)
  122. {
  123. if (___4480)
  124. {
  125. BOOST_FOREACH (T ___4298, vec)
  126. {
  127. writeScalar(outputFile, ___4298, ___4480);
  128. }
  129. }
  130. else
  131. {
  132. if (outputFile.fwrite(&(vec[0]), sizeof(T), vec.size()) != vec.size())
  133. throw FileIOException("Error writing vector to file");
  134. }
  135. }
  136. }
  137. template <typename T>
  138. inline uint64_t vectorSizeInFile(std::vector<T> const &vec, bool ___4480)
  139. {
  140. uint64_t length = vec.size();
  141. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  142. if (vec.size() > 0)
  143. sizeInFile += length * scalarSizeInFile(vec[0], ___4480);
  144. return sizeInFile;
  145. }
  146. template <typename T>
  147. inline void readVector(___3933::___1399 &inputFile, std::vector<T> &vec, bool readASCII)
  148. {
  149. uint64_t length;
  150. readScalar(inputFile, length, readASCII);
  151. vec.resize((size_t)length);
  152. if (length > 0)
  153. {
  154. if (readASCII)
  155. {
  156. for (uint64_t i = 0; i < length; ++i)
  157. readScalar(inputFile, vec[i], readASCII);
  158. }
  159. else
  160. {
  161. if (inputFile.fread((&vec[0]), sizeof(T), (size_t)length) != (size_t)length)
  162. throw FileIOException("Error reading vector from file");
  163. }
  164. }
  165. }
  166. template <>
  167. inline void writeVector<std::string>(___3933::FileWriterInterface &outputFile, std::vector<std::string> const &vec, bool ___4480)
  168. {
  169. uint64_t length = vec.size();
  170. writeScalar(outputFile, length, ___4480);
  171. for (size_t i = 0; i < vec.size(); ++i)
  172. ___4544(outputFile, vec[i], ___4480);
  173. }
  174. template <>
  175. inline uint64_t vectorSizeInFile<std::string>(std::vector<std::string> const &vec, bool ___4480)
  176. {
  177. uint64_t length = vec.size();
  178. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  179. for (size_t i = 0; i < vec.size(); ++i)
  180. sizeInFile += stringSizeInFile(vec[i], ___4480);
  181. return sizeInFile;
  182. }
  183. template <>
  184. inline void readVector<std::string>(___3933::___1399 &inputFile, std::vector<std::string> &vec, bool readASCII)
  185. {
  186. uint64_t length;
  187. readScalar(inputFile, length, readASCII);
  188. vec.resize((size_t)length);
  189. for (size_t i = 0; i < vec.size(); ++i)
  190. readString(inputFile, vec[i], readASCII);
  191. }
  192. template <typename T>
  193. inline void writeVectorOfObjects(___3933::FileWriterInterface &outputFile, std::vector<T> const &vec, bool ___4480)
  194. {
  195. uint64_t length = vec.size();
  196. writeScalar(outputFile, length, ___4480);
  197. for (size_t i = 0; i < vec.size(); ++i)
  198. vec[i].writeToFile(outputFile, ___4480);
  199. }
  200. template <typename T>
  201. inline uint64_t vectorOfObjectsSizeInFile(std::vector<T> const &vec, bool ___4480)
  202. {
  203. uint64_t length = vec.size();
  204. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  205. for (size_t i = 0; i < vec.size(); ++i)
  206. sizeInFile += vec[i].sizeInFile(___4480);
  207. return sizeInFile;
  208. }
  209. template <typename T>
  210. inline void readVectorOfObjects(___3933::___1399 &inputFile, std::vector<T> &vec, bool readASCII)
  211. {
  212. uint64_t length;
  213. readScalar(inputFile, length, readASCII);
  214. vec.reserve((size_t)length);
  215. for (uint64_t i = 0; i < length; ++i)
  216. vec.push_back(T(inputFile, readASCII));
  217. }
  218. template <typename T>
  219. inline void writeVectorOfPtrs(___3933::FileWriterInterface &outputFile, std::vector<boost::shared_ptr<T>> const &vec, bool ___4480)
  220. {
  221. uint64_t length = vec.size();
  222. writeScalar(outputFile, length, ___4480);
  223. for (size_t i = 0; i < vec.size(); ++i)
  224. vec[i]->writeToFile(outputFile, ___4480);
  225. }
  226. template <typename T>
  227. inline uint64_t vectorOfPtrsSizeInFile(std::vector<boost::shared_ptr<T>> const &vec, bool ___4480)
  228. {
  229. uint64_t length = vec.size();
  230. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  231. for (size_t i = 0; i < vec.size(); ++i)
  232. sizeInFile += vec[i]->sizeInFile(___4480);
  233. return sizeInFile;
  234. }
  235. template <typename T>
  236. inline void readVectorOfPtrs(___3933::___1399 &inputFile, std::vector<boost::shared_ptr<T>> &vec, bool readASCII)
  237. {
  238. uint64_t length;
  239. readScalar(inputFile, length, readASCII);
  240. vec.resize(0);
  241. vec.reserve((size_t)length);
  242. for (uint64_t i = 0; i < length; ++i)
  243. vec.push_back(T::makePtr(inputFile, readASCII));
  244. }
  245. template <typename T, typename U>
  246. inline void writeMapOfScalarsToPtrs(___3933::FileWriterInterface &outputFile, std::map<T, boost::shared_ptr<U>> const &m, bool ___4480)
  247. {
  248. uint64_t length = m.size();
  249. writeScalar(outputFile, length, ___4480);
  250. typedef std::pair<T, boost::shared_ptr<U>> ValuePair;
  251. BOOST_FOREACH (ValuePair const &valuePair, m)
  252. {
  253. writeScalar(outputFile, valuePair.first, ___4480);
  254. valuePair.second->writeToFile(outputFile, ___4480);
  255. }
  256. }
  257. template <typename T, typename U>
  258. inline uint64_t mapOfScalarsToPtrsSizeInFile(std::map<T, boost::shared_ptr<U>> const &m, bool ___4480)
  259. {
  260. uint64_t length = m.size();
  261. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  262. typedef std::pair<T, boost::shared_ptr<U>> ValuePair;
  263. BOOST_FOREACH (ValuePair const &valuePair, m)
  264. {
  265. sizeInFile += scalarSizeInFile(valuePair.first, ___4480);
  266. sizeInFile += valuePair.second->sizeInFile(___4480);
  267. }
  268. return sizeInFile;
  269. }
  270. template <typename T, typename U>
  271. inline void readMapOfScalarsToPtrs(___3933::___1399 &inputFile, std::map<T, boost::shared_ptr<U>> &m, bool readASCII)
  272. {
  273. uint64_t length;
  274. readScalar(inputFile, length, readASCII);
  275. for (uint64_t i = 0; i < length; ++i)
  276. {
  277. T key;
  278. readScalar(inputFile, key, readASCII);
  279. m[key] = U::makePtr(inputFile, readASCII);
  280. }
  281. }
  282. template <typename T, typename U>
  283. inline void writeMapOfPairsToObjects(___3933::FileWriterInterface &outputFile, std::map<T, U> const &m, bool ___4480)
  284. {
  285. uint64_t length = m.size();
  286. writeScalar(outputFile, length, ___4480);
  287. typedef std::pair<T, U> ValuePair;
  288. BOOST_FOREACH (ValuePair const &valuePair, m)
  289. {
  290. writeScalar(outputFile, valuePair.first.first, ___4480);
  291. writeScalar(outputFile, valuePair.first.second, ___4480);
  292. valuePair.second.writeToFile(outputFile, ___4480);
  293. }
  294. }
  295. template <typename T, typename U>
  296. inline uint64_t mapOfPairsToObjectsSizeInFile(std::map<T, U> const &m, bool ___4480)
  297. {
  298. uint64_t length = m.size();
  299. uint64_t sizeInFile = scalarSizeInFile(length, ___4480);
  300. typedef std::pair<T, U> ValuePair;
  301. BOOST_FOREACH (ValuePair const &valuePair, m)
  302. {
  303. sizeInFile += scalarSizeInFile(valuePair.first.first, ___4480);
  304. sizeInFile += scalarSizeInFile(valuePair.first.second, ___4480);
  305. sizeInFile += valuePair.second.sizeInFile(___4480);
  306. }
  307. return sizeInFile;
  308. }
  309. template <typename T, typename U>
  310. inline void readMapOfPairsToObjects(___3933::___1399 &inputFile, std::map<T, U> &m, bool readASCII)
  311. {
  312. uint64_t length;
  313. readScalar(inputFile, length, readASCII);
  314. for (uint64_t i = 0; i < length; ++i)
  315. {
  316. T key;
  317. readScalar(inputFile, key.first, readASCII);
  318. readScalar(inputFile, key.second, readASCII);
  319. m[key] = U(inputFile, readASCII);
  320. }
  321. }
  322. }
  323. }