Skip to content

Commit

Permalink
Change size and count arguments in TFile from int to size_t
Browse files Browse the repository at this point in the history
This matches standard functions like fread, fwrite.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 10, 2021
1 parent 85cb667 commit 9315d4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/ccutil/genericvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ bool GenericVector<T>::read(TFile *f, std::function<bool(TFile *, T *)> cb) {
}
}
} else {
if (f->FReadEndian(data_, sizeof(T), size_used_) != size_used_) {
if (f->FReadEndian(data_, sizeof(T), size_used_) != static_cast<unsigned>(size_used_)) {
return false;
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/ccutil/serialis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ bool SaveDataToFile(const std::vector<char> &data, const char *filename) {
if (fp == nullptr) {
return false;
}
bool result = static_cast<int>(fwrite(&data[0], 1, data.size(), fp)) == data.size();
bool result = fwrite(&data[0], 1, data.size(), fp) == data.size();
fclose(fp);
return result;
}

TFile::TFile()
: data_(nullptr), offset_(0), data_is_owned_(false), is_writing_(false), swap_(false) {}
TFile::TFile() {
}

TFile::~TFile() {
if (data_is_owned_) {
Expand Down Expand Up @@ -152,7 +152,7 @@ bool TFile::Open(const char *filename, FileReader reader) {
}
}

bool TFile::Open(const char *data, int size) {
bool TFile::Open(const char *data, size_t size) {
offset_ = 0;
if (!data_is_owned_) {
data_ = new std::vector<char>;
Expand Down Expand Up @@ -181,15 +181,15 @@ bool TFile::Open(FILE *fp, int64_t end_offset) {
return false;
}
}
int size = end_offset - current_pos;
size_t size = end_offset - current_pos;
is_writing_ = false;
swap_ = false;
if (!data_is_owned_) {
data_ = new std::vector<char>;
data_is_owned_ = true;
}
data_->resize(size); // TODO: optimize no init
return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
return fread(&(*data_)[0], 1, size, fp) == size;
}

char *TFile::FGets(char *buffer, int buffer_size) {
Expand All @@ -207,21 +207,20 @@ char *TFile::FGets(char *buffer, int buffer_size) {
return size > 0 ? buffer : nullptr;
}

int TFile::FReadEndian(void *buffer, size_t size, int count) {
int num_read = FRead(buffer, size, count);
size_t TFile::FReadEndian(void *buffer, size_t size, size_t count) {
auto num_read = FRead(buffer, size, count);
if (swap_ && size != 1) {
char *char_buffer = static_cast<char *>(buffer);
for (int i = 0; i < num_read; ++i, char_buffer += size) {
for (size_t i = 0; i < num_read; ++i, char_buffer += size) {
ReverseN(char_buffer, size);
}
}
return num_read;
}

int TFile::FRead(void *buffer, size_t size, int count) {
size_t TFile::FRead(void *buffer, size_t size, size_t count) {
ASSERT_HOST(!is_writing_);
ASSERT_HOST(size > 0);
ASSERT_HOST(count >= 0);
size_t required_size;
if (SIZE_MAX / size <= count) {
// Avoid integer overflow.
Expand Down Expand Up @@ -270,10 +269,9 @@ bool TFile::CloseWrite(const char *filename, FileWriter writer) {
}
}

int TFile::FWrite(const void *buffer, size_t size, int count) {
size_t TFile::FWrite(const void *buffer, size_t size, size_t count) {
ASSERT_HOST(is_writing_);
ASSERT_HOST(size > 0);
ASSERT_HOST(count >= 0);
ASSERT_HOST(SIZE_MAX / size > count);
size_t total = size * count;
const char *buf = static_cast<const char *>(buffer);
Expand Down
22 changes: 11 additions & 11 deletions src/ccutil/serialis.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TESS_API TFile {
// Note that mixed read/write is not supported.
bool Open(const char *filename, FileReader reader);
// From an existing memory buffer.
bool Open(const char *data, int size);
bool Open(const char *data, size_t size);
// From an open file and an end offset.
bool Open(FILE *fp, int64_t end_offset);
// Sets the value of the swap flag, so that FReadEndian does the right thing.
Expand All @@ -92,7 +92,7 @@ class TESS_API TFile {
//bool DeSerialize(std::vector<std::string> &data);
template <typename T>
bool DeSerialize(T *data, size_t count = 1) {
return FReadEndian(data, sizeof(T), count) == static_cast<int>(count);
return FReadEndian(data, sizeof(T), count) == count;
}
template <typename T>
bool DeSerialize(std::vector<T> &data) {
Expand Down Expand Up @@ -155,7 +155,7 @@ class TESS_API TFile {
bool Serialize(const std::vector<char> &data);
template <typename T>
bool Serialize(const T *data, size_t count = 1) {
return FWrite(data, sizeof(T), count) == static_cast<int>(count);
return FWrite(data, sizeof(T), count) == count;
}
template <typename T>
bool Serialize(const std::vector<T> &data) {
Expand Down Expand Up @@ -207,9 +207,9 @@ class TESS_API TFile {
// Replicates fread, followed by a swap of the bytes if needed, returning the
// number of items read. If swap_ is true then the count items will each have
// size bytes reversed.
int FReadEndian(void *buffer, size_t size, int count);
size_t FReadEndian(void *buffer, size_t size, size_t count);
// Replicates fread, returning the number of items read.
int FRead(void *buffer, size_t size, int count);
size_t FRead(void *buffer, size_t size, size_t count);
// Resets the TFile as if it has been Opened, but nothing read.
// Only allowed while reading!
void Rewind();
Expand All @@ -222,19 +222,19 @@ class TESS_API TFile {

// Replicates fwrite, returning the number of items written.
// To use fprintf, use snprintf and FWrite.
int FWrite(const void *buffer, size_t size, int count);
size_t FWrite(const void *buffer, size_t size, size_t count);

private:
// The buffered data from the file.
std::vector<char> *data_;
std::vector<char> *data_ = nullptr;
// The number of bytes used so far.
int offset_;
unsigned offset_ = 0;
// True if the data_ pointer is owned by *this.
bool data_is_owned_;
bool data_is_owned_ = false;
// True if the TFile is open for writing.
bool is_writing_;
bool is_writing_ = false;
// True if bytes need to be swapped in FReadEndian.
bool swap_;
bool swap_ = false;
};

} // namespace tesseract.
Expand Down

0 comments on commit 9315d4c

Please sign in to comment.