tesseract  5.0.0-alpha-619-ge9db
serialis.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: serialis.cpp (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 #include <tesseract/serialis.h>
20 #include <cstdio>
21 #include "errcode.h"
23 #include <tesseract/strngs.h> // for STRING
24 
25 namespace tesseract {
26 
27 bool DeSerialize(FILE* fp, char* data, size_t n) {
28  return fread(data, sizeof(*data), n, fp) == n;
29 }
30 
31 bool DeSerialize(FILE* fp, float* data, size_t n) {
32  return fread(data, sizeof(*data), n, fp) == n;
33 }
34 
35 bool DeSerialize(FILE* fp, int8_t* data, size_t n) {
36  return fread(data, sizeof(*data), n, fp) == n;
37 }
38 
39 bool DeSerialize(FILE* fp, int16_t* data, size_t n) {
40  return fread(data, sizeof(*data), n, fp) == n;
41 }
42 
43 bool DeSerialize(FILE* fp, int32_t* data, size_t n) {
44  return fread(data, sizeof(*data), n, fp) == n;
45 }
46 
47 bool DeSerialize(FILE* fp, uint8_t* data, size_t n) {
48  return fread(data, sizeof(*data), n, fp) == n;
49 }
50 
51 bool DeSerialize(FILE* fp, uint16_t* data, size_t n) {
52  return fread(data, sizeof(*data), n, fp) == n;
53 }
54 
55 bool DeSerialize(FILE* fp, uint32_t* data, size_t n) {
56  return fread(data, sizeof(*data), n, fp) == n;
57 }
58 
59 bool Serialize(FILE* fp, const char* data, size_t n) {
60  return fwrite(data, sizeof(*data), n, fp) == n;
61 }
62 
63 bool Serialize(FILE* fp, const float* data, size_t n) {
64  return fwrite(data, sizeof(*data), n, fp) == n;
65 }
66 
67 bool Serialize(FILE* fp, const int8_t* data, size_t n) {
68  return fwrite(data, sizeof(*data), n, fp) == n;
69 }
70 
71 bool Serialize(FILE* fp, const int16_t* data, size_t n) {
72  return fwrite(data, sizeof(*data), n, fp) == n;
73 }
74 
75 bool Serialize(FILE* fp, const int32_t* data, size_t n) {
76  return fwrite(data, sizeof(*data), n, fp) == n;
77 }
78 
79 bool Serialize(FILE* fp, const uint8_t* data, size_t n) {
80  return fwrite(data, sizeof(*data), n, fp) == n;
81 }
82 
83 bool Serialize(FILE* fp, const uint16_t* data, size_t n) {
84  return fwrite(data, sizeof(*data), n, fp) == n;
85 }
86 
87 bool Serialize(FILE* fp, const uint32_t* data, size_t n) {
88  return fwrite(data, sizeof(*data), n, fp) == n;
89 }
90 
92  : offset_(0),
93  data_(nullptr),
94  data_is_owned_(false),
95  is_writing_(false),
96  swap_(false) {}
97 
98 TFile::~TFile() {
99  if (data_is_owned_)
100  delete data_;
101 }
102 
103 bool TFile::DeSerialize(char* buffer, size_t count) {
104  return FRead(buffer, sizeof(*buffer), count) == count;
105 }
106 
107 bool TFile::DeSerialize(double* buffer, size_t count) {
108  return FReadEndian(buffer, sizeof(*buffer), count) == count;
109 }
110 
111 bool TFile::DeSerialize(float* buffer, size_t count) {
112  return FReadEndian(buffer, sizeof(*buffer), count) == count;
113 }
114 
115 bool TFile::DeSerialize(int8_t* buffer, size_t count) {
116  return FRead(buffer, sizeof(*buffer), count) == count;
117 }
118 
119 bool TFile::DeSerialize(int16_t* buffer, size_t count) {
120  return FReadEndian(buffer, sizeof(*buffer), count) == count;
121 }
122 
123 bool TFile::DeSerialize(int32_t* buffer, size_t count) {
124  return FReadEndian(buffer, sizeof(*buffer), count) == count;
125 }
126 
127 bool TFile::DeSerialize(int64_t* buffer, size_t count) {
128  return FReadEndian(buffer, sizeof(*buffer), count) == count;
129 }
130 
131 bool TFile::DeSerialize(uint8_t* buffer, size_t count) {
132  return FRead(buffer, sizeof(*buffer), count) == count;
133 }
134 
135 bool TFile::DeSerialize(uint16_t* buffer, size_t count) {
136  return FReadEndian(buffer, sizeof(*buffer), count) == count;
137 }
138 
139 bool TFile::DeSerialize(uint32_t* buffer, size_t count) {
140  return FReadEndian(buffer, sizeof(*buffer), count) == count;
141 }
142 
143 bool TFile::DeSerialize(uint64_t* buffer, size_t count) {
144  return FReadEndian(buffer, sizeof(*buffer), count) == count;
145 }
146 
147 bool TFile::Serialize(const char* buffer, size_t count) {
148  return FWrite(buffer, sizeof(*buffer), count) == count;
149 }
150 
151 bool TFile::Serialize(const double* buffer, size_t count) {
152  return FWrite(buffer, sizeof(*buffer), count) == count;
153 }
154 
155 bool TFile::Serialize(const float* buffer, size_t count) {
156  return FWrite(buffer, sizeof(*buffer), count) == count;
157 }
158 
159 bool TFile::Serialize(const int8_t* buffer, size_t count) {
160  return FWrite(buffer, sizeof(*buffer), count) == count;
161 }
162 
163 bool TFile::Serialize(const int16_t* buffer, size_t count) {
164  return FWrite(buffer, sizeof(*buffer), count) == count;
165 }
166 
167 bool TFile::Serialize(const int32_t* buffer, size_t count) {
168  return FWrite(buffer, sizeof(*buffer), count) == count;
169 }
170 
171 bool TFile::Serialize(const int64_t* buffer, size_t count) {
172  return FWrite(buffer, sizeof(*buffer), count) == count;
173 }
174 
175 bool TFile::Serialize(const uint8_t* buffer, size_t count) {
176  return FWrite(buffer, sizeof(*buffer), count) == count;
177 }
178 
179 bool TFile::Serialize(const uint16_t* buffer, size_t count) {
180  return FWrite(buffer, sizeof(*buffer), count) == count;
181 }
182 
183 bool TFile::Serialize(const uint32_t* buffer, size_t count) {
184  return FWrite(buffer, sizeof(*buffer), count) == count;
185 }
186 
187 bool TFile::Serialize(const uint64_t* buffer, size_t count) {
188  return FWrite(buffer, sizeof(*buffer), count) == count;
189 }
190 
191 bool TFile::Skip(size_t count) {
192  offset_ += count;
193  return true;
194 }
195 
196 bool TFile::Open(const STRING& filename, FileReader reader) {
197  if (!data_is_owned_) {
198  data_ = new GenericVector<char>;
199  data_is_owned_ = true;
200  }
201  offset_ = 0;
202  is_writing_ = false;
203  swap_ = false;
204  if (reader == nullptr)
205  return LoadDataFromFile(filename.c_str(), data_);
206  else
207  return (*reader)(filename.c_str(), data_);
208 }
209 
210 bool TFile::Open(const char* data, int size) {
211  offset_ = 0;
212  if (!data_is_owned_) {
213  data_ = new GenericVector<char>;
214  data_is_owned_ = true;
215  }
216  is_writing_ = false;
217  swap_ = false;
218  data_->resize_no_init(size);
219  memcpy(&(*data_)[0], data, size);
220  return true;
221 }
222 
223 bool TFile::Open(FILE* fp, int64_t end_offset) {
224  offset_ = 0;
225  auto current_pos = std::ftell(fp);
226  if (current_pos < 0) {
227  // ftell failed.
228  return false;
229  }
230  if (end_offset < 0) {
231  if (fseek(fp, 0, SEEK_END))
232  return false;
233  end_offset = ftell(fp);
234  if (fseek(fp, current_pos, SEEK_SET))
235  return false;
236  }
237  int size = end_offset - current_pos;
238  is_writing_ = false;
239  swap_ = false;
240  if (!data_is_owned_) {
241  data_ = new GenericVector<char>;
242  data_is_owned_ = true;
243  }
244  data_->resize_no_init(size);
245  return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
246 }
247 
248 char* TFile::FGets(char* buffer, int buffer_size) {
249  ASSERT_HOST(!is_writing_);
250  int size = 0;
251  while (size + 1 < buffer_size && offset_ < data_->size()) {
252  buffer[size++] = (*data_)[offset_++];
253  if ((*data_)[offset_ - 1] == '\n') break;
254  }
255  if (size < buffer_size) buffer[size] = '\0';
256  return size > 0 ? buffer : nullptr;
257 }
258 
259 int TFile::FReadEndian(void* buffer, size_t size, int count) {
260  int num_read = FRead(buffer, size, count);
261  if (swap_) {
262  char* char_buffer = static_cast<char*>(buffer);
263  for (int i = 0; i < num_read; ++i, char_buffer += size) {
264  ReverseN(char_buffer, size);
265  }
266  }
267  return num_read;
268 }
269 
270 int TFile::FRead(void* buffer, size_t size, int count) {
271  ASSERT_HOST(!is_writing_);
272  ASSERT_HOST(size > 0);
274  size_t required_size;
275  if (SIZE_MAX / size <= count) {
276  // Avoid integer overflow.
277  required_size = data_->size() - offset_;
278  } else {
279  required_size = size * count;
280  if (data_->size() - offset_ < required_size) {
281  required_size = data_->size() - offset_;
282  }
283  }
284  if (required_size > 0 && buffer != nullptr)
285  memcpy(buffer, &(*data_)[offset_], required_size);
286  offset_ += required_size;
287  return required_size / size;
288 }
289 
290 void TFile::Rewind() {
291  ASSERT_HOST(!is_writing_);
292  offset_ = 0;
293 }
294 
296  offset_ = 0;
297  if (data != nullptr) {
298  if (data_is_owned_) delete data_;
299  data_ = data;
300  data_is_owned_ = false;
301  } else if (!data_is_owned_) {
302  data_ = new GenericVector<char>;
303  data_is_owned_ = true;
304  }
305  is_writing_ = true;
306  swap_ = false;
307  data_->truncate(0);
308 }
309 
310 bool TFile::CloseWrite(const STRING& filename, FileWriter writer) {
311  ASSERT_HOST(is_writing_);
312  if (writer == nullptr)
313  return SaveDataToFile(*data_, filename.c_str());
314  else
315  return (*writer)(*data_, filename.c_str());
316 }
317 
318 int TFile::FWrite(const void* buffer, size_t size, int count) {
319  ASSERT_HOST(is_writing_);
320  ASSERT_HOST(size > 0);
321  ASSERT_HOST(count >= 0);
322  ASSERT_HOST(SIZE_MAX / size > count);
323  size_t total = size * count;
324  const char* buf = static_cast<const char*>(buffer);
325  // This isn't very efficient, but memory is so fast compared to disk
326  // that it is relatively unimportant, and very simple.
327  for (size_t i = 0; i < total; ++i)
328  data_->push_back(buf[i]);
329  return count;
330 }
331 
332 } // namespace tesseract.
strngs.h
tesseract::FileWriter
bool(*)(const GenericVector< char > &data, const char *filename) FileWriter
Definition: serialis.h:51
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::TFile::FReadEndian
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:273
tesseract::LoadDataFromFile
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
Definition: genericvector.h:341
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::SaveDataToFile
bool SaveDataToFile(const GenericVector< char > &data, const char *filename)
Definition: genericvector.h:362
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
genericvector.h
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::TFile::CloseWrite
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:324
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
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
GenericVector::resize_no_init
void resize_no_init(int size)
Definition: genericvector.h:65
tesseract
Definition: baseapi.h:65
GenericVector< char >
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
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
tesseract::TFile::FGets
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:262
errcode.h
serialis.h
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183
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
GenericVector::size
int size() const
Definition: genericvector.h:71
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