serialis.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**********************************************************************
  2. * File: serialis.h (Formerly serialmac.h)
  3. * Description: Inline routines and macros for serialisation functions
  4. * Author: Phil Cheatle
  5. *
  6. * (C) Copyright 1990, Hewlett-Packard Ltd.
  7. ** Licensed under the Apache License, Version 2.0 (the "License");
  8. ** you may not use this file except in compliance with the License.
  9. ** You may obtain a copy of the License at
  10. ** http://www.apache.org/licenses/LICENSE-2.0
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. *
  17. **********************************************************************/
  18. #ifndef SERIALIS_H
  19. #define SERIALIS_H
  20. #include <cstdint> // uint8_t
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <cstring>
  24. template <typename T>
  25. class GenericVector;
  26. class STRING;
  27. /***********************************************************************
  28. QUOTE_IT MACRO DEFINITION
  29. ===========================
  30. Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens
  31. ***********************************************************************/
  32. #define QUOTE_IT(parm) #parm
  33. namespace tesseract {
  34. // Return number of elements of an array.
  35. template <typename T, size_t N>
  36. constexpr size_t countof(T const (&)[N]) noexcept {
  37. return N;
  38. }
  39. // Function to read a GenericVector<char> from a whole file.
  40. // Returns false on failure.
  41. using FileReader = bool (*)(const STRING&, GenericVector<char>*);
  42. // Function to write a GenericVector<char> to a whole file.
  43. // Returns false on failure.
  44. using FileWriter = bool (*)(const GenericVector<char>&, const STRING&);
  45. // Deserialize data from file.
  46. bool DeSerialize(FILE* fp, char* data, size_t n = 1);
  47. bool DeSerialize(FILE* fp, float* data, size_t n = 1);
  48. bool DeSerialize(FILE* fp, int8_t* data, size_t n = 1);
  49. bool DeSerialize(FILE* fp, int16_t* data, size_t n = 1);
  50. bool DeSerialize(FILE* fp, int32_t* data, size_t n = 1);
  51. bool DeSerialize(FILE* fp, uint8_t* data, size_t n = 1);
  52. bool DeSerialize(FILE* fp, uint16_t* data, size_t n = 1);
  53. bool DeSerialize(FILE* fp, uint32_t* data, size_t n = 1);
  54. // Serialize data to file.
  55. bool Serialize(FILE* fp, const char* data, size_t n = 1);
  56. bool Serialize(FILE* fp, const float* data, size_t n = 1);
  57. bool Serialize(FILE* fp, const int8_t* data, size_t n = 1);
  58. bool Serialize(FILE* fp, const int16_t* data, size_t n = 1);
  59. bool Serialize(FILE* fp, const int32_t* data, size_t n = 1);
  60. bool Serialize(FILE* fp, const uint8_t* data, size_t n = 1);
  61. bool Serialize(FILE* fp, const uint16_t* data, size_t n = 1);
  62. bool Serialize(FILE* fp, const uint32_t* data, size_t n = 1);
  63. // Simple file class.
  64. // Allows for portable file input from memory and from foreign file systems.
  65. class TFile {
  66. public:
  67. TFile();
  68. ~TFile();
  69. // All the Open methods load the whole file into memory for reading.
  70. // Opens a file with a supplied reader, or nullptr to use the default.
  71. // Note that mixed read/write is not supported.
  72. bool Open(const STRING& filename, FileReader reader);
  73. // From an existing memory buffer.
  74. bool Open(const char* data, int size);
  75. // From an open file and an end offset.
  76. bool Open(FILE* fp, int64_t end_offset);
  77. // Sets the value of the swap flag, so that FReadEndian does the right thing.
  78. void set_swap(bool value) {
  79. swap_ = value;
  80. }
  81. // Deserialize data.
  82. bool DeSerialize(char* data, size_t count = 1);
  83. bool DeSerialize(double* data, size_t count = 1);
  84. bool DeSerialize(float* data, size_t count = 1);
  85. bool DeSerialize(int8_t* data, size_t count = 1);
  86. bool DeSerialize(int16_t* data, size_t count = 1);
  87. bool DeSerialize(int32_t* data, size_t count = 1);
  88. bool DeSerialize(int64_t* data, size_t count = 1);
  89. bool DeSerialize(uint8_t* data, size_t count = 1);
  90. bool DeSerialize(uint16_t* data, size_t count = 1);
  91. bool DeSerialize(uint32_t* data, size_t count = 1);
  92. bool DeSerialize(uint64_t* data, size_t count = 1);
  93. // Serialize data.
  94. bool Serialize(const char* data, size_t count = 1);
  95. bool Serialize(const double* data, size_t count = 1);
  96. bool Serialize(const float* data, size_t count = 1);
  97. bool Serialize(const int8_t* data, size_t count = 1);
  98. bool Serialize(const int16_t* data, size_t count = 1);
  99. bool Serialize(const int32_t* data, size_t count = 1);
  100. bool Serialize(const int64_t* data, size_t count = 1);
  101. bool Serialize(const uint8_t* data, size_t count = 1);
  102. bool Serialize(const uint16_t* data, size_t count = 1);
  103. bool Serialize(const uint32_t* data, size_t count = 1);
  104. bool Serialize(const uint64_t* data, size_t count = 1);
  105. // Skip data.
  106. bool Skip(size_t count);
  107. // Reads a line like fgets. Returns nullptr on EOF, otherwise buffer.
  108. // Reads at most buffer_size bytes, including '\0' terminator, even if
  109. // the line is longer. Does nothing if buffer_size <= 0.
  110. char* FGets(char* buffer, int buffer_size);
  111. // Replicates fread, followed by a swap of the bytes if needed, returning the
  112. // number of items read. If swap_ is true then the count items will each have
  113. // size bytes reversed.
  114. int FReadEndian(void* buffer, size_t size, int count);
  115. // Replicates fread, returning the number of items read.
  116. int FRead(void* buffer, size_t size, int count);
  117. // Resets the TFile as if it has been Opened, but nothing read.
  118. // Only allowed while reading!
  119. void Rewind();
  120. // Open for writing. Either supply a non-nullptr data with OpenWrite before
  121. // calling FWrite, (no close required), or supply a nullptr data to OpenWrite
  122. // and call CloseWrite to write to a file after the FWrites.
  123. void OpenWrite(GenericVector<char>* data);
  124. bool CloseWrite(const STRING& filename, FileWriter writer);
  125. // Replicates fwrite, returning the number of items written.
  126. // To use fprintf, use snprintf and FWrite.
  127. int FWrite(const void* buffer, size_t size, int count);
  128. private:
  129. // The number of bytes used so far.
  130. int offset_;
  131. // The buffered data from the file.
  132. GenericVector<char>* data_;
  133. // True if the data_ pointer is owned by *this.
  134. bool data_is_owned_;
  135. // True if the TFile is open for writing.
  136. bool is_writing_;
  137. // True if bytes need to be swapped in FReadEndian.
  138. bool swap_;
  139. };
  140. } // namespace tesseract.
  141. #endif