Skip to content

Commit

Permalink
GENERIC_2D_ARRAY: Use new serialization API
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 ea660f8 commit 7133a6f
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/ccstruct/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <algorithm> // for max, min
#include <cmath> // for sqrt, fabs, isfinite
#include <cstdint> // for int32_t
#include <cstdio> // for fread, fwrite, FILE
#include <cstdio> // for FILE
#include <cstring> // for memcpy
#include "errcode.h" // for ASSERT_HOST
#include "helpers.h" // for ReverseN, ClipToRange
Expand Down Expand Up @@ -143,40 +143,38 @@ class GENERIC_2D_ARRAY {
// Only works with bitwise-serializeable types!
bool Serialize(FILE* fp) const {
if (!SerializeSize(fp)) return false;
if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
if (!tesseract::Serialize(fp, &empty_)) return false;
int size = num_elements();
if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
return true;
return tesseract::Serialize(fp, &array_[0], size);
}

bool Serialize(tesseract::TFile* fp) const {
if (!SerializeSize(fp)) return false;
if (fp->FWrite(&empty_, sizeof(empty_), 1) != 1) return false;
if (!fp->Serialize(&empty_)) return false;
int size = num_elements();
if (fp->FWrite(array_, sizeof(*array_), size) != size) return false;
return true;
return fp->Serialize(&array_[0], size);
}

// Reads from the given file. Returns false in case of error.
// Only works with bitwise-serializeable types!
// If swap is true, assumes a big/little-endian swap is needed.
bool DeSerialize(bool swap, FILE* fp) {
if (!DeSerializeSize(swap, fp)) return false;
if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
if (!tesseract::DeSerialize(fp, &empty_)) return false;
if (swap) ReverseN(&empty_, sizeof(empty_));
int size = num_elements();
if (fread(array_, sizeof(*array_), size, fp) != size) return false;
if (!tesseract::DeSerialize(fp, &array_[0], size)) return false;
if (swap) {
for (int i = 0; i < size; ++i)
ReverseN(&array_[i], sizeof(array_[i]));
}
return true;
}

bool DeSerialize(tesseract::TFile* fp) {
if (!DeSerializeSize(fp)) return false;
if (fp->FReadEndian(&empty_, sizeof(empty_), 1) != 1) return false;
int size = num_elements();
if (fp->FReadEndian(array_, sizeof(*array_), size) != size) return false;
return true;
return DeSerializeSize(fp) &&
fp->DeSerialize(&empty_) &&
fp->DeSerialize(&array_[0], num_elements());
}

// Writes to the given file. Returns false in case of error.
Expand Down Expand Up @@ -466,25 +464,23 @@ class GENERIC_2D_ARRAY {
protected:
// Factored helper to serialize the size.
bool SerializeSize(FILE* fp) const {
int32_t size = dim1_;
if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
uint32_t size = dim1_;
if (!tesseract::Serialize(fp, &size)) return false;
size = dim2_;
if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
return true;
return tesseract::Serialize(fp, &size);
}
bool SerializeSize(tesseract::TFile* fp) const {
int32_t size = dim1_;
if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
uint32_t size = dim1_;
if (!fp->Serialize(&size)) return false;
size = dim2_;
if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
return true;
return fp->Serialize(&size);
}
// Factored helper to deserialize the size.
// If swap is true, assumes a big/little-endian swap is needed.
bool DeSerializeSize(bool swap, FILE* fp) {
int32_t size1, size2;
if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
uint32_t size1, size2;
if (!tesseract::DeSerialize(fp, &size1)) return false;
if (!tesseract::DeSerialize(fp, &size2)) return false;
if (swap) {
ReverseN(&size1, sizeof(size1));
ReverseN(&size2, sizeof(size2));
Expand All @@ -497,8 +493,8 @@ class GENERIC_2D_ARRAY {
}
bool DeSerializeSize(tesseract::TFile* fp) {
int32_t size1, size2;
if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
if (!fp->DeSerialize(&size1)) return false;
if (!fp->DeSerialize(&size2)) return false;
// Arbitrarily limit the number of elements to protect against bad data.
if (size1 > UINT16_MAX) return false;
if (size2 > UINT16_MAX) return false;
Expand Down

0 comments on commit 7133a6f

Please sign in to comment.