tesseract  5.0.0-alpha-619-ge9db
serialis.h
Go to the documentation of this file.
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 
19 #ifndef SERIALIS_H
20 #define SERIALIS_H
21 
22 #include <cstdint> // uint8_t
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 
27 template <typename T>
28 class GenericVector;
29 class STRING;
30 
31 /***********************************************************************
32  QUOTE_IT MACRO DEFINITION
33  ===========================
34 Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens
35 ***********************************************************************/
36 
37 #define QUOTE_IT(parm) #parm
38 
39 namespace tesseract {
40 
41 // Return number of elements of an array.
42 template <typename T, size_t N>
43 constexpr size_t countof(T const (&)[N]) noexcept {
44  return N;
45 }
46 
47 // Function to read a GenericVector<char> from a whole file.
48 // Returns false on failure.
49 using FileReader = bool (*)(const char* filename, GenericVector<char>* data);
50 // Function to write a GenericVector<char> to a whole file.
51 // Returns false on failure.
52 using FileWriter = bool (*)(const GenericVector<char>& data,
53  const char* filename);
54 
55 // Deserialize data from file.
56 bool DeSerialize(FILE* fp, char* data, size_t n = 1);
57 bool DeSerialize(FILE* fp, float* data, size_t n = 1);
58 bool DeSerialize(FILE* fp, int8_t* data, size_t n = 1);
59 bool DeSerialize(FILE* fp, int16_t* data, size_t n = 1);
60 bool DeSerialize(FILE* fp, int32_t* data, size_t n = 1);
61 bool DeSerialize(FILE* fp, uint8_t* data, size_t n = 1);
62 bool DeSerialize(FILE* fp, uint16_t* data, size_t n = 1);
63 bool DeSerialize(FILE* fp, uint32_t* data, size_t n = 1);
64 
65 // Serialize data to file.
66 bool Serialize(FILE* fp, const char* data, size_t n = 1);
67 bool Serialize(FILE* fp, const float* data, size_t n = 1);
68 bool Serialize(FILE* fp, const int8_t* data, size_t n = 1);
69 bool Serialize(FILE* fp, const int16_t* data, size_t n = 1);
70 bool Serialize(FILE* fp, const int32_t* data, size_t n = 1);
71 bool Serialize(FILE* fp, const uint8_t* data, size_t n = 1);
72 bool Serialize(FILE* fp, const uint16_t* data, size_t n = 1);
73 bool Serialize(FILE* fp, const uint32_t* data, size_t n = 1);
74 
75 // Simple file class.
76 // Allows for portable file input from memory and from foreign file systems.
77 class TFile {
78  public:
79  TFile();
80  ~TFile();
81 
82  // All the Open methods load the whole file into memory for reading.
83  // Opens a file with a supplied reader, or nullptr to use the default.
84  // Note that mixed read/write is not supported.
85  bool Open(const STRING& filename, FileReader reader);
86  // From an existing memory buffer.
87  bool Open(const char* data, int size);
88  // From an open file and an end offset.
89  bool Open(FILE* fp, int64_t end_offset);
90  // Sets the value of the swap flag, so that FReadEndian does the right thing.
91  void set_swap(bool value) {
92  swap_ = value;
93  }
94 
95  // Deserialize data.
96  bool DeSerialize(char* data, size_t count = 1);
97  bool DeSerialize(double* data, size_t count = 1);
98  bool DeSerialize(float* data, size_t count = 1);
99  bool DeSerialize(int8_t* data, size_t count = 1);
100  bool DeSerialize(int16_t* data, size_t count = 1);
101  bool DeSerialize(int32_t* data, size_t count = 1);
102  bool DeSerialize(int64_t* data, size_t count = 1);
103  bool DeSerialize(uint8_t* data, size_t count = 1);
104  bool DeSerialize(uint16_t* data, size_t count = 1);
105  bool DeSerialize(uint32_t* data, size_t count = 1);
106  bool DeSerialize(uint64_t* data, size_t count = 1);
107 
108  // Serialize data.
109  bool Serialize(const char* data, size_t count = 1);
110  bool Serialize(const double* data, size_t count = 1);
111  bool Serialize(const float* data, size_t count = 1);
112  bool Serialize(const int8_t* data, size_t count = 1);
113  bool Serialize(const int16_t* data, size_t count = 1);
114  bool Serialize(const int32_t* data, size_t count = 1);
115  bool Serialize(const int64_t* data, size_t count = 1);
116  bool Serialize(const uint8_t* data, size_t count = 1);
117  bool Serialize(const uint16_t* data, size_t count = 1);
118  bool Serialize(const uint32_t* data, size_t count = 1);
119  bool Serialize(const uint64_t* data, size_t count = 1);
120 
121  // Skip data.
122  bool Skip(size_t count);
123 
124  // Reads a line like fgets. Returns nullptr on EOF, otherwise buffer.
125  // Reads at most buffer_size bytes, including '\0' terminator, even if
126  // the line is longer. Does nothing if buffer_size <= 0.
127  char* FGets(char* buffer, int buffer_size);
128  // Replicates fread, followed by a swap of the bytes if needed, returning the
129  // number of items read. If swap_ is true then the count items will each have
130  // size bytes reversed.
131  int FReadEndian(void* buffer, size_t size, int count);
132  // Replicates fread, returning the number of items read.
133  int FRead(void* buffer, size_t size, int count);
134  // Resets the TFile as if it has been Opened, but nothing read.
135  // Only allowed while reading!
136  void Rewind();
137 
138  // Open for writing. Either supply a non-nullptr data with OpenWrite before
139  // calling FWrite, (no close required), or supply a nullptr data to OpenWrite
140  // and call CloseWrite to write to a file after the FWrites.
141  void OpenWrite(GenericVector<char>* data);
142  bool CloseWrite(const STRING& filename, FileWriter writer);
143 
144  // Replicates fwrite, returning the number of items written.
145  // To use fprintf, use snprintf and FWrite.
146  int FWrite(const void* buffer, size_t size, int count);
147 
148  private:
149  // The buffered data from the file.
150  GenericVector<char>* data_;
151  // The number of bytes used so far.
152  int offset_;
153  // True if the data_ pointer is owned by *this.
154  bool data_is_owned_;
155  // True if the TFile is open for writing.
156  bool is_writing_;
157  // True if bytes need to be swapped in FReadEndian.
158  bool swap_;
159 };
160 
161 } // namespace tesseract.
162 
163 #endif
tesseract::FileWriter
bool(*)(const GenericVector< char > &data, const char *filename) FileWriter
Definition: serialis.h:51
tesseract::TFile::FReadEndian
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:273
tesseract::countof
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:41
STRING
Definition: strngs.h:45
tesseract::TFile::Rewind
void Rewind()
Definition: serialis.cpp:304
tesseract::FileReader
bool(*)(const char *filename, GenericVector< char > *data) FileReader
Definition: serialis.h:47
tesseract::TFile::Open
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:210
tesseract::TFile::FRead
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:284
tesseract::TFile::CloseWrite
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:324
tesseract::TFile::DeSerialize
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:117
tesseract::TFile::Serialize
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:161
tesseract::TFile
Definition: serialis.h:75
tesseract
Definition: baseapi.h:65
tesseract::TFile::set_swap
void set_swap(bool value)
Definition: serialis.h:89
GenericVector
Definition: baseapi.h:40
tesseract::TFile::TFile
TFile()
Definition: serialis.cpp:105
count
int count(LIST var_list)
Definition: oldlist.cpp:79
tesseract::TFile::Skip
bool Skip(size_t count)
Definition: serialis.cpp:205
tesseract::TFile::FGets
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:262
tesseract::TFile::FWrite
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:332
tesseract::TFile::~TFile
~TFile()
Definition: serialis.cpp:112
tesseract::DeSerialize
bool DeSerialize(FILE *fp, char *data, size_t n=1)
Definition: serialis.cpp:41
tesseract::TFile::OpenWrite
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:309
tesseract::Serialize
bool Serialize(FILE *fp, const char *data, size_t n=1)
Definition: serialis.cpp:73