From 7133a6f43cd4e7075fddf35c4bd929bc5aaca7a7 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 18 Jul 2018 13:15:11 +0200 Subject: [PATCH] GENERIC_2D_ARRAY: Use new serialization API Signed-off-by: Stefan Weil --- src/ccstruct/matrix.h | 50 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/ccstruct/matrix.h b/src/ccstruct/matrix.h index 03a31c12e6..953c3e37c0 100644 --- a/src/ccstruct/matrix.h +++ b/src/ccstruct/matrix.h @@ -30,7 +30,7 @@ #include // for max, min #include // for sqrt, fabs, isfinite #include // for int32_t -#include // for fread, fwrite, FILE +#include // for FILE #include // for memcpy #include "errcode.h" // for ASSERT_HOST #include "helpers.h" // for ReverseN, ClipToRange @@ -143,17 +143,16 @@ 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. @@ -161,22 +160,21 @@ class GENERIC_2D_ARRAY { // 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. @@ -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)); @@ -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;