Skip to content

Commit

Permalink
Add helper functions for serialization of simple data types
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Jul 18, 2018
1 parent f47daac commit 16832f9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/ccutil/serialis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,70 @@

namespace tesseract {

bool DeSerialize(FILE* fp, char* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, float* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, int8_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, int16_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, int32_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, uint8_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, uint16_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool DeSerialize(FILE* fp, uint32_t* data, size_t n) {
return fread(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const char* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const float* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const int8_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const int16_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const int32_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const uint8_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const uint16_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

bool Serialize(FILE* fp, const uint32_t* data, size_t n) {
return fwrite(data, sizeof(*data), n, fp) == n;
}

TFile::TFile()
: offset_(0),
data_(nullptr),
Expand Down Expand Up @@ -172,5 +236,4 @@ int TFile::FWrite(const void* buffer, size_t size, int count) {
return count;
}


} // namespace tesseract.
26 changes: 26 additions & 0 deletions src/ccutil/serialis.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens

namespace tesseract {

// Return number of elements of an array.
template <typename T, size_t N>
constexpr size_t countof(T const (&)[N]) noexcept {
return N;
}

// Function to read a GenericVector<char> from a whole file.
// Returns false on failure.
typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data);
Expand All @@ -46,6 +52,26 @@ typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data);
typedef bool (*FileWriter)(const GenericVector<char>& data,
const STRING& filename);

// Deserialize data from file.
bool DeSerialize(FILE* fp, char* data, size_t n = 1);
bool DeSerialize(FILE* fp, float* data, size_t n = 1);
bool DeSerialize(FILE* fp, int8_t* data, size_t n = 1);
bool DeSerialize(FILE* fp, int16_t* data, size_t n = 1);
bool DeSerialize(FILE* fp, int32_t* data, size_t n = 1);
bool DeSerialize(FILE* fp, uint8_t* data, size_t n = 1);
bool DeSerialize(FILE* fp, uint16_t* data, size_t n = 1);
bool DeSerialize(FILE* fp, uint32_t* data, size_t n = 1);

// Serialize data to file.
bool Serialize(FILE* fp, const char* data, size_t n = 1);
bool Serialize(FILE* fp, const float* data, size_t n = 1);
bool Serialize(FILE* fp, const int8_t* data, size_t n = 1);
bool Serialize(FILE* fp, const int16_t* data, size_t n = 1);
bool Serialize(FILE* fp, const int32_t* data, size_t n = 1);
bool Serialize(FILE* fp, const uint8_t* data, size_t n = 1);
bool Serialize(FILE* fp, const uint16_t* data, size_t n = 1);
bool Serialize(FILE* fp, const uint32_t* data, size_t n = 1);

// Simple file class.
// Allows for portable file input from memory and from foreign file systems.
class TFile {
Expand Down

0 comments on commit 16832f9

Please sign in to comment.