tesseract  4.0.0-1-g2a2b
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 77 of file serialis.h.

Constructor & Destructor Documentation

◆ TFile()

tesseract::TFile::TFile ( )

Definition at line 91 of file serialis.cpp.

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

◆ ~TFile()

tesseract::TFile::~TFile ( )

Definition at line 98 of file serialis.cpp.

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

Member Function Documentation

◆ CloseWrite()

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

Definition at line 310 of file serialis.cpp.

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

◆ DeSerialize() [1/11]

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

Definition at line 103 of file serialis.cpp.

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

◆ DeSerialize() [2/11]

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

Definition at line 107 of file serialis.cpp.

107  {
108  return FReadEndian(buffer, sizeof(*buffer), count) == count;
109 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [3/11]

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

Definition at line 111 of file serialis.cpp.

111  {
112  return FReadEndian(buffer, sizeof(*buffer), count) == count;
113 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [4/11]

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

Definition at line 115 of file serialis.cpp.

115  {
116  return FRead(buffer, sizeof(*buffer), count) == count;
117 }
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ DeSerialize() [5/11]

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

Definition at line 119 of file serialis.cpp.

119  {
120  return FReadEndian(buffer, sizeof(*buffer), count) == count;
121 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [6/11]

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

Definition at line 123 of file serialis.cpp.

123  {
124  return FReadEndian(buffer, sizeof(*buffer), count) == count;
125 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [7/11]

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

Definition at line 127 of file serialis.cpp.

127  {
128  return FReadEndian(buffer, sizeof(*buffer), count) == count;
129 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [8/11]

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

Definition at line 131 of file serialis.cpp.

131  {
132  return FRead(buffer, sizeof(*buffer), count) == count;
133 }
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ DeSerialize() [9/11]

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

Definition at line 135 of file serialis.cpp.

135  {
136  return FReadEndian(buffer, sizeof(*buffer), count) == count;
137 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [10/11]

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

Definition at line 139 of file serialis.cpp.

139  {
140  return FReadEndian(buffer, sizeof(*buffer), count) == count;
141 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerialize() [11/11]

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

Definition at line 143 of file serialis.cpp.

143  {
144  return FReadEndian(buffer, sizeof(*buffer), count) == count;
145 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ FGets()

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

Definition at line 248 of file serialis.cpp.

248  {
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 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ FRead()

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

Definition at line 270 of file serialis.cpp.

270  {
271  ASSERT_HOST(!is_writing_);
272  ASSERT_HOST(size > 0);
273  ASSERT_HOST(count >= 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 }
int size() const
Definition: genericvector.h:71
int count(LIST var_list)
Definition: oldlist.cpp:98
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ FReadEndian()

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

Definition at line 259 of file serialis.cpp.

259  {
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 }
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
int count(LIST var_list)
Definition: oldlist.cpp:98
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178

◆ FWrite()

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

Definition at line 318 of file serialis.cpp.

318  {
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 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int push_back(T object)
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Open() [1/3]

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

Definition at line 196 of file serialis.cpp.

196  {
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, data_);
206  else
207  return (*reader)(filename, data_);
208 }
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)

◆ Open() [2/3]

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

Definition at line 210 of file serialis.cpp.

210  {
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 }
void resize_no_init(int size)
Definition: genericvector.h:65

◆ Open() [3/3]

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

Definition at line 223 of file serialis.cpp.

223  {
224  offset_ = 0;
225  long current_pos = 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 }
void resize_no_init(int size)
Definition: genericvector.h:65

◆ OpenWrite()

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

Definition at line 295 of file serialis.cpp.

295  {
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 }
void truncate(int size)

◆ Rewind()

void tesseract::TFile::Rewind ( )

Definition at line 290 of file serialis.cpp.

290  {
291  ASSERT_HOST(!is_writing_);
292  offset_ = 0;
293 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Serialize() [1/11]

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

Definition at line 147 of file serialis.cpp.

147  {
148  return FWrite(buffer, sizeof(*buffer), count) == count;
149 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [2/11]

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

Definition at line 151 of file serialis.cpp.

151  {
152  return FWrite(buffer, sizeof(*buffer), count) == count;
153 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [3/11]

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

Definition at line 155 of file serialis.cpp.

155  {
156  return FWrite(buffer, sizeof(*buffer), count) == count;
157 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [4/11]

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

Definition at line 159 of file serialis.cpp.

159  {
160  return FWrite(buffer, sizeof(*buffer), count) == count;
161 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [5/11]

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

Definition at line 163 of file serialis.cpp.

163  {
164  return FWrite(buffer, sizeof(*buffer), count) == count;
165 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [6/11]

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

Definition at line 167 of file serialis.cpp.

167  {
168  return FWrite(buffer, sizeof(*buffer), count) == count;
169 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [7/11]

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

Definition at line 171 of file serialis.cpp.

171  {
172  return FWrite(buffer, sizeof(*buffer), count) == count;
173 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [8/11]

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

Definition at line 175 of file serialis.cpp.

175  {
176  return FWrite(buffer, sizeof(*buffer), count) == count;
177 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [9/11]

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

Definition at line 179 of file serialis.cpp.

179  {
180  return FWrite(buffer, sizeof(*buffer), count) == count;
181 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [10/11]

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

Definition at line 183 of file serialis.cpp.

183  {
184  return FWrite(buffer, sizeof(*buffer), count) == count;
185 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ Serialize() [11/11]

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

Definition at line 187 of file serialis.cpp.

187  {
188  return FWrite(buffer, sizeof(*buffer), count) == count;
189 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ set_swap()

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

Definition at line 91 of file serialis.h.

91 { swap_ = value; }

◆ Skip()

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

Definition at line 191 of file serialis.cpp.

191  {
192  offset_ += count;
193  return true;
194 }
int count(LIST var_list)
Definition: oldlist.cpp:98

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