tesseract  5.0.0-alpha-619-ge9db
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 75 of file serialis.h.

Constructor & Destructor Documentation

◆ TFile()

tesseract::TFile::TFile ( )

Definition at line 105 of file serialis.cpp.

107  {
108  return FReadEndian(buffer, sizeof(*buffer), count) == count;
109 }
110 

◆ ~TFile()

tesseract::TFile::~TFile ( )

Definition at line 112 of file serialis.cpp.

115  {

Member Function Documentation

◆ CloseWrite()

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

Definition at line 324 of file serialis.cpp.

◆ DeSerialize() [1/11]

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

Definition at line 117 of file serialis.cpp.

119  {

◆ DeSerialize() [2/11]

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

Definition at line 121 of file serialis.cpp.

123  {

◆ DeSerialize() [3/11]

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

Definition at line 125 of file serialis.cpp.

127  {

◆ DeSerialize() [4/11]

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

Definition at line 133 of file serialis.cpp.

135  {

◆ DeSerialize() [5/11]

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

Definition at line 137 of file serialis.cpp.

139  {

◆ DeSerialize() [6/11]

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

Definition at line 141 of file serialis.cpp.

143  {

◆ DeSerialize() [7/11]

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

Definition at line 129 of file serialis.cpp.

131  {

◆ DeSerialize() [8/11]

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

Definition at line 149 of file serialis.cpp.

151  {

◆ DeSerialize() [9/11]

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

Definition at line 153 of file serialis.cpp.

155  {

◆ DeSerialize() [10/11]

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

Definition at line 157 of file serialis.cpp.

159  {

◆ DeSerialize() [11/11]

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

Definition at line 145 of file serialis.cpp.

147  {

◆ FGets()

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

Definition at line 262 of file serialis.cpp.

263  {
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_);

◆ FRead()

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

Definition at line 284 of file serialis.cpp.

290  {
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>;

◆ FReadEndian()

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

Definition at line 273 of file serialis.cpp.

275  {
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  }

◆ FWrite()

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

Definition at line 332 of file serialis.cpp.

◆ Open() [1/3]

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

Definition at line 224 of file serialis.cpp.

226  {
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;

◆ Open() [2/3]

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

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 }
222 

◆ Open() [3/3]

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

Definition at line 237 of file serialis.cpp.

240  {
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);

◆ OpenWrite()

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

Definition at line 309 of file serialis.cpp.

310  {
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);

◆ Rewind()

void tesseract::TFile::Rewind ( )

Definition at line 304 of file serialis.cpp.

◆ Serialize() [1/11]

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

Definition at line 161 of file serialis.cpp.

163  {

◆ Serialize() [2/11]

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

Definition at line 165 of file serialis.cpp.

167  {

◆ Serialize() [3/11]

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

Definition at line 169 of file serialis.cpp.

171  {

◆ Serialize() [4/11]

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

Definition at line 177 of file serialis.cpp.

179  {

◆ Serialize() [5/11]

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

Definition at line 181 of file serialis.cpp.

183  {

◆ Serialize() [6/11]

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

Definition at line 185 of file serialis.cpp.

187  {

◆ Serialize() [7/11]

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

Definition at line 173 of file serialis.cpp.

175  {

◆ Serialize() [8/11]

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

Definition at line 193 of file serialis.cpp.

◆ Serialize() [9/11]

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

Definition at line 197 of file serialis.cpp.

197  {
198  data_ = new GenericVector<char>;
199  data_is_owned_ = true;

◆ Serialize() [10/11]

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

Definition at line 201 of file serialis.cpp.

◆ Serialize() [11/11]

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

Definition at line 189 of file serialis.cpp.

191  {

◆ set_swap()

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

Definition at line 89 of file serialis.h.

91  {

◆ Skip()

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

Definition at line 205 of file serialis.cpp.


The documentation for this class was generated from the following files:
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::SaveDataToFile
bool SaveDataToFile(const GenericVector< char > &data, const char *filename)
Definition: genericvector.h:362
tesseract::TFile::FRead
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:284
GenericVector::resize_no_init
void resize_no_init(int size)
Definition: genericvector.h:65
GenericVector< char >
count
int count(LIST var_list)
Definition: oldlist.cpp:79
tesseract::TFile::FGets
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:262
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
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::TFile::OpenWrite
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:309