tesseract 4.1.1
Loading...
Searching...
No Matches
tesseract::TFile Class Reference

#include <serialis.h>

Public Member Functions

 TFile ()
 
 ~TFile ()
 
bool Open (const STRING &filename, FileReader reader)
 
bool Open (const char *data, int size)
 
bool Open (FILE *fp, int64_t end_offset)
 
void set_swap (bool value)
 
bool DeSerialize (char *data, size_t count=1)
 
bool DeSerialize (double *data, size_t count=1)
 
bool DeSerialize (float *data, size_t count=1)
 
bool DeSerialize (int8_t *data, size_t count=1)
 
bool DeSerialize (int16_t *data, size_t count=1)
 
bool DeSerialize (int32_t *data, size_t count=1)
 
bool DeSerialize (int64_t *data, size_t count=1)
 
bool DeSerialize (uint8_t *data, size_t count=1)
 
bool DeSerialize (uint16_t *data, size_t count=1)
 
bool DeSerialize (uint32_t *data, size_t count=1)
 
bool DeSerialize (uint64_t *data, size_t count=1)
 
bool Serialize (const char *data, size_t count=1)
 
bool Serialize (const double *data, size_t count=1)
 
bool Serialize (const float *data, size_t count=1)
 
bool Serialize (const int8_t *data, size_t count=1)
 
bool Serialize (const int16_t *data, size_t count=1)
 
bool Serialize (const int32_t *data, size_t count=1)
 
bool Serialize (const int64_t *data, size_t count=1)
 
bool Serialize (const uint8_t *data, size_t count=1)
 
bool Serialize (const uint16_t *data, size_t count=1)
 
bool Serialize (const uint32_t *data, size_t count=1)
 
bool Serialize (const uint64_t *data, size_t count=1)
 
bool Skip (size_t count)
 
char * FGets (char *buffer, int buffer_size)
 
int FReadEndian (void *buffer, size_t size, int count)
 
int FRead (void *buffer, size_t size, int count)
 
void Rewind ()
 
void OpenWrite (GenericVector< char > *data)
 
bool CloseWrite (const STRING &filename, FileWriter writer)
 
int FWrite (const void *buffer, size_t size, int count)
 

Detailed Description

Definition at line 76 of file serialis.h.

Constructor & Destructor Documentation

◆ TFile()

tesseract::TFile::TFile ( )

Definition at line 92 of file serialis.cpp.

93 : offset_(0),
94 data_(nullptr),
95 data_is_owned_(false),
96 is_writing_(false),
97 swap_(false) {}

◆ ~TFile()

tesseract::TFile::~TFile ( )

Definition at line 99 of file serialis.cpp.

99 {
100 if (data_is_owned_)
101 delete data_;
102}

Member Function Documentation

◆ CloseWrite()

bool tesseract::TFile::CloseWrite ( const STRING filename,
FileWriter  writer 
)

Definition at line 311 of file serialis.cpp.

311 {
312 ASSERT_HOST(is_writing_);
313 if (writer == nullptr)
314 return SaveDataToFile(*data_, filename.c_str());
315 else
316 return (*writer)(*data_, filename);
317}
#define ASSERT_HOST(x)
Definition: errcode.h:88
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
const char * c_str() const
Definition: strngs.cpp:205

◆ DeSerialize() [1/11]

bool tesseract::TFile::DeSerialize ( char *  data,
size_t  count = 1 
)

Definition at line 104 of file serialis.cpp.

104 {
105 return FRead(buffer, sizeof(*buffer), count) == count;
106}
int count(LIST var_list)
Definition: oldlist.cpp:95
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:271

◆ DeSerialize() [2/11]

bool tesseract::TFile::DeSerialize ( double *  data,
size_t  count = 1 
)

Definition at line 108 of file serialis.cpp.

108 {
109 return FReadEndian(buffer, sizeof(*buffer), count) == count;
110}
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:260

◆ DeSerialize() [3/11]

bool tesseract::TFile::DeSerialize ( float *  data,
size_t  count = 1 
)

Definition at line 112 of file serialis.cpp.

112 {
113 return FReadEndian(buffer, sizeof(*buffer), count) == count;
114}

◆ DeSerialize() [4/11]

bool tesseract::TFile::DeSerialize ( int16_t *  data,
size_t  count = 1 
)

Definition at line 120 of file serialis.cpp.

120 {
121 return FReadEndian(buffer, sizeof(*buffer), count) == count;
122}

◆ DeSerialize() [5/11]

bool tesseract::TFile::DeSerialize ( int32_t *  data,
size_t  count = 1 
)

Definition at line 124 of file serialis.cpp.

124 {
125 return FReadEndian(buffer, sizeof(*buffer), count) == count;
126}

◆ DeSerialize() [6/11]

bool tesseract::TFile::DeSerialize ( int64_t *  data,
size_t  count = 1 
)

Definition at line 128 of file serialis.cpp.

128 {
129 return FReadEndian(buffer, sizeof(*buffer), count) == count;
130}

◆ DeSerialize() [7/11]

bool tesseract::TFile::DeSerialize ( int8_t *  data,
size_t  count = 1 
)

Definition at line 116 of file serialis.cpp.

116 {
117 return FRead(buffer, sizeof(*buffer), count) == count;
118}

◆ DeSerialize() [8/11]

bool tesseract::TFile::DeSerialize ( uint16_t *  data,
size_t  count = 1 
)

Definition at line 136 of file serialis.cpp.

136 {
137 return FReadEndian(buffer, sizeof(*buffer), count) == count;
138}

◆ DeSerialize() [9/11]

bool tesseract::TFile::DeSerialize ( uint32_t *  data,
size_t  count = 1 
)

Definition at line 140 of file serialis.cpp.

140 {
141 return FReadEndian(buffer, sizeof(*buffer), count) == count;
142}

◆ DeSerialize() [10/11]

bool tesseract::TFile::DeSerialize ( uint64_t *  data,
size_t  count = 1 
)

Definition at line 144 of file serialis.cpp.

144 {
145 return FReadEndian(buffer, sizeof(*buffer), count) == count;
146}

◆ DeSerialize() [11/11]

bool tesseract::TFile::DeSerialize ( uint8_t *  data,
size_t  count = 1 
)

Definition at line 132 of file serialis.cpp.

132 {
133 return FRead(buffer, sizeof(*buffer), count) == count;
134}

◆ FGets()

char * tesseract::TFile::FGets ( char *  buffer,
int  buffer_size 
)

Definition at line 249 of file serialis.cpp.

249 {
250 ASSERT_HOST(!is_writing_);
251 int size = 0;
252 while (size + 1 < buffer_size && offset_ < data_->size()) {
253 buffer[size++] = (*data_)[offset_++];
254 if ((*data_)[offset_ - 1] == '\n') break;
255 }
256 if (size < buffer_size) buffer[size] = '\0';
257 return size > 0 ? buffer : nullptr;
258}

◆ FRead()

int tesseract::TFile::FRead ( void *  buffer,
size_t  size,
int  count 
)

Definition at line 271 of file serialis.cpp.

271 {
272 ASSERT_HOST(!is_writing_);
273 ASSERT_HOST(size > 0);
274 ASSERT_HOST(count >= 0);
275 size_t required_size;
276 if (SIZE_MAX / size <= count) {
277 // Avoid integer overflow.
278 required_size = data_->size() - offset_;
279 } else {
280 required_size = size * count;
281 if (data_->size() - offset_ < required_size) {
282 required_size = data_->size() - offset_;
283 }
284 }
285 if (required_size > 0 && buffer != nullptr)
286 memcpy(buffer, &(*data_)[offset_], required_size);
287 offset_ += required_size;
288 return required_size / size;
289}
int size() const
Definition: genericvector.h:72

◆ FReadEndian()

int tesseract::TFile::FReadEndian ( void *  buffer,
size_t  size,
int  count 
)

Definition at line 260 of file serialis.cpp.

260 {
261 int num_read = FRead(buffer, size, count);
262 if (swap_) {
263 char* char_buffer = static_cast<char*>(buffer);
264 for (int i = 0; i < num_read; ++i, char_buffer += size) {
265 ReverseN(char_buffer, size);
266 }
267 }
268 return num_read;
269}
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:185

◆ FWrite()

int tesseract::TFile::FWrite ( const void *  buffer,
size_t  size,
int  count 
)

Definition at line 319 of file serialis.cpp.

319 {
320 ASSERT_HOST(is_writing_);
321 ASSERT_HOST(size > 0);
322 ASSERT_HOST(count >= 0);
323 ASSERT_HOST(SIZE_MAX / size > count);
324 size_t total = size * count;
325 const char* buf = static_cast<const char*>(buffer);
326 // This isn't very efficient, but memory is so fast compared to disk
327 // that it is relatively unimportant, and very simple.
328 for (size_t i = 0; i < total; ++i)
329 data_->push_back(buf[i]);
330 return count;
331}
int push_back(T object)

◆ Open() [1/3]

bool tesseract::TFile::Open ( const char *  data,
int  size 
)

Definition at line 211 of file serialis.cpp.

211 {
212 offset_ = 0;
213 if (!data_is_owned_) {
214 data_ = new GenericVector<char>;
215 data_is_owned_ = true;
216 }
217 is_writing_ = false;
218 swap_ = false;
219 data_->resize_no_init(size);
220 memcpy(&(*data_)[0], data, size);
221 return true;
222}
void resize_no_init(int size)
Definition: genericvector.h:66

◆ Open() [2/3]

bool tesseract::TFile::Open ( const STRING filename,
FileReader  reader 
)

Definition at line 197 of file serialis.cpp.

197 {
198 if (!data_is_owned_) {
199 data_ = new GenericVector<char>;
200 data_is_owned_ = true;
201 }
202 offset_ = 0;
203 is_writing_ = false;
204 swap_ = false;
205 if (reader == nullptr)
206 return LoadDataFromFile(filename.c_str(), data_);
207 else
208 return (*reader)(filename, data_);
209}
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)

◆ Open() [3/3]

bool tesseract::TFile::Open ( FILE *  fp,
int64_t  end_offset 
)

Definition at line 224 of file serialis.cpp.

224 {
225 offset_ = 0;
226 auto current_pos = std::ftell(fp);
227 if (current_pos < 0) {
228 // ftell failed.
229 return false;
230 }
231 if (end_offset < 0) {
232 if (fseek(fp, 0, SEEK_END))
233 return false;
234 end_offset = ftell(fp);
235 if (fseek(fp, current_pos, SEEK_SET))
236 return false;
237 }
238 int size = end_offset - current_pos;
239 is_writing_ = false;
240 swap_ = false;
241 if (!data_is_owned_) {
242 data_ = new GenericVector<char>;
243 data_is_owned_ = true;
244 }
245 data_->resize_no_init(size);
246 return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
247}

◆ OpenWrite()

void tesseract::TFile::OpenWrite ( GenericVector< char > *  data)

Definition at line 296 of file serialis.cpp.

296 {
297 offset_ = 0;
298 if (data != nullptr) {
299 if (data_is_owned_) delete data_;
300 data_ = data;
301 data_is_owned_ = false;
302 } else if (!data_is_owned_) {
303 data_ = new GenericVector<char>;
304 data_is_owned_ = true;
305 }
306 is_writing_ = true;
307 swap_ = false;
308 data_->truncate(0);
309}
void truncate(int size)

◆ Rewind()

void tesseract::TFile::Rewind ( )

Definition at line 291 of file serialis.cpp.

291 {
292 ASSERT_HOST(!is_writing_);
293 offset_ = 0;
294}

◆ Serialize() [1/11]

bool tesseract::TFile::Serialize ( const char *  data,
size_t  count = 1 
)

Definition at line 148 of file serialis.cpp.

148 {
149 return FWrite(buffer, sizeof(*buffer), count) == count;
150}
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:319

◆ Serialize() [2/11]

bool tesseract::TFile::Serialize ( const double *  data,
size_t  count = 1 
)

Definition at line 152 of file serialis.cpp.

152 {
153 return FWrite(buffer, sizeof(*buffer), count) == count;
154}

◆ Serialize() [3/11]

bool tesseract::TFile::Serialize ( const float *  data,
size_t  count = 1 
)

Definition at line 156 of file serialis.cpp.

156 {
157 return FWrite(buffer, sizeof(*buffer), count) == count;
158}

◆ Serialize() [4/11]

bool tesseract::TFile::Serialize ( const int16_t *  data,
size_t  count = 1 
)

Definition at line 164 of file serialis.cpp.

164 {
165 return FWrite(buffer, sizeof(*buffer), count) == count;
166}

◆ Serialize() [5/11]

bool tesseract::TFile::Serialize ( const int32_t *  data,
size_t  count = 1 
)

Definition at line 168 of file serialis.cpp.

168 {
169 return FWrite(buffer, sizeof(*buffer), count) == count;
170}

◆ Serialize() [6/11]

bool tesseract::TFile::Serialize ( const int64_t *  data,
size_t  count = 1 
)

Definition at line 172 of file serialis.cpp.

172 {
173 return FWrite(buffer, sizeof(*buffer), count) == count;
174}

◆ Serialize() [7/11]

bool tesseract::TFile::Serialize ( const int8_t *  data,
size_t  count = 1 
)

Definition at line 160 of file serialis.cpp.

160 {
161 return FWrite(buffer, sizeof(*buffer), count) == count;
162}

◆ Serialize() [8/11]

bool tesseract::TFile::Serialize ( const uint16_t *  data,
size_t  count = 1 
)

Definition at line 180 of file serialis.cpp.

180 {
181 return FWrite(buffer, sizeof(*buffer), count) == count;
182}

◆ Serialize() [9/11]

bool tesseract::TFile::Serialize ( const uint32_t *  data,
size_t  count = 1 
)

Definition at line 184 of file serialis.cpp.

184 {
185 return FWrite(buffer, sizeof(*buffer), count) == count;
186}

◆ Serialize() [10/11]

bool tesseract::TFile::Serialize ( const uint64_t *  data,
size_t  count = 1 
)

Definition at line 188 of file serialis.cpp.

188 {
189 return FWrite(buffer, sizeof(*buffer), count) == count;
190}

◆ Serialize() [11/11]

bool tesseract::TFile::Serialize ( const uint8_t *  data,
size_t  count = 1 
)

Definition at line 176 of file serialis.cpp.

176 {
177 return FWrite(buffer, sizeof(*buffer), count) == count;
178}

◆ set_swap()

void tesseract::TFile::set_swap ( bool  value)
inline

Definition at line 90 of file serialis.h.

90 {
91 swap_ = value;
92 }

◆ Skip()

bool tesseract::TFile::Skip ( size_t  count)

Definition at line 192 of file serialis.cpp.

192 {
193 offset_ += count;
194 return true;
195}

The documentation for this class was generated from the following files: