2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
2017-07-03 05:35:47 +08:00
|
|
|
* File: serialis.cpp (Formerly serialmac.h)
|
2007-03-08 04:03:40 +08:00
|
|
|
* Description: Inline routines and macros for serialisation functions
|
|
|
|
* Author: Phil Cheatle
|
|
|
|
* Created: Tue Oct 08 08:33:12 BST 1991
|
|
|
|
*
|
|
|
|
* (C) Copyright 1990, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "serialis.h"
|
2013-10-10 10:07:26 +08:00
|
|
|
#include <stdio.h>
|
2014-08-12 07:09:25 +08:00
|
|
|
#include "genericvector.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
namespace tesseract {
|
|
|
|
|
2014-08-12 07:09:25 +08:00
|
|
|
TFile::TFile()
|
2017-05-04 07:09:44 +08:00
|
|
|
: offset_(0),
|
|
|
|
data_(NULL),
|
|
|
|
data_is_owned_(false),
|
|
|
|
is_writing_(false),
|
|
|
|
swap_(false) {}
|
2014-08-12 07:09:25 +08:00
|
|
|
|
|
|
|
TFile::~TFile() {
|
|
|
|
if (data_is_owned_)
|
|
|
|
delete data_;
|
2009-06-03 06:11:51 +08:00
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
bool TFile::Open(const STRING& filename, FileReader reader) {
|
2014-08-12 07:09:25 +08:00
|
|
|
if (!data_is_owned_) {
|
|
|
|
data_ = new GenericVector<char>;
|
|
|
|
data_is_owned_ = true;
|
|
|
|
}
|
2013-10-10 10:07:26 +08:00
|
|
|
offset_ = 0;
|
2014-08-12 07:09:25 +08:00
|
|
|
is_writing_ = false;
|
2017-05-04 07:09:44 +08:00
|
|
|
swap_ = false;
|
2013-10-10 10:07:26 +08:00
|
|
|
if (reader == NULL)
|
2014-08-12 07:09:25 +08:00
|
|
|
return LoadDataFromFile(filename, data_);
|
2013-10-10 10:07:26 +08:00
|
|
|
else
|
2014-08-12 07:09:25 +08:00
|
|
|
return (*reader)(filename, data_);
|
2013-10-10 10:07:26 +08:00
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
bool TFile::Open(const char* data, int size) {
|
|
|
|
offset_ = 0;
|
2014-08-12 07:09:25 +08:00
|
|
|
if (!data_is_owned_) {
|
|
|
|
data_ = new GenericVector<char>;
|
|
|
|
data_is_owned_ = true;
|
|
|
|
}
|
|
|
|
is_writing_ = false;
|
2017-05-04 07:09:44 +08:00
|
|
|
swap_ = false;
|
2017-05-12 20:34:55 +08:00
|
|
|
data_->resize_no_init(size);
|
2014-08-12 07:09:25 +08:00
|
|
|
memcpy(&(*data_)[0], data, size);
|
2013-10-10 10:07:26 +08:00
|
|
|
return true;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 02:01:40 +08:00
|
|
|
bool TFile::Open(FILE* fp, int64_t end_offset) {
|
2013-10-10 10:07:26 +08:00
|
|
|
offset_ = 0;
|
2018-03-23 03:00:01 +08:00
|
|
|
long current_pos = ftell(fp);
|
|
|
|
if (current_pos < 0) {
|
|
|
|
// ftell failed.
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-10 10:07:26 +08:00
|
|
|
if (end_offset < 0) {
|
2014-10-08 00:25:53 +08:00
|
|
|
if (fseek(fp, 0, SEEK_END))
|
|
|
|
return false;
|
2013-10-10 10:07:26 +08:00
|
|
|
end_offset = ftell(fp);
|
2014-10-08 00:25:53 +08:00
|
|
|
if (fseek(fp, current_pos, SEEK_SET))
|
|
|
|
return false;
|
2013-10-10 10:07:26 +08:00
|
|
|
}
|
|
|
|
int size = end_offset - current_pos;
|
2014-08-12 07:09:25 +08:00
|
|
|
is_writing_ = false;
|
2017-05-04 07:09:44 +08:00
|
|
|
swap_ = false;
|
2014-08-12 07:09:25 +08:00
|
|
|
if (!data_is_owned_) {
|
|
|
|
data_ = new GenericVector<char>;
|
|
|
|
data_is_owned_ = true;
|
|
|
|
}
|
2017-05-12 20:34:55 +08:00
|
|
|
data_->resize_no_init(size);
|
2014-08-12 07:09:25 +08:00
|
|
|
return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
|
2013-10-10 10:07:26 +08:00
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
char* TFile::FGets(char* buffer, int buffer_size) {
|
2014-08-12 07:09:25 +08:00
|
|
|
ASSERT_HOST(!is_writing_);
|
2013-10-10 10:07:26 +08:00
|
|
|
int size = 0;
|
2014-08-12 07:09:25 +08:00
|
|
|
while (size + 1 < buffer_size && offset_ < data_->size()) {
|
|
|
|
buffer[size++] = (*data_)[offset_++];
|
|
|
|
if ((*data_)[offset_ - 1] == '\n') break;
|
2013-10-10 10:07:26 +08:00
|
|
|
}
|
|
|
|
if (size < buffer_size) buffer[size] = '\0';
|
|
|
|
return size > 0 ? buffer : NULL;
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2017-05-04 07:09:44 +08:00
|
|
|
int TFile::FReadEndian(void* buffer, int size, int count) {
|
2017-04-28 06:48:23 +08:00
|
|
|
int num_read = FRead(buffer, size, count);
|
2017-05-04 07:09:44 +08:00
|
|
|
if (swap_) {
|
2017-05-11 06:40:31 +08:00
|
|
|
char* char_buffer = static_cast<char*>(buffer);
|
2017-04-28 06:48:23 +08:00
|
|
|
for (int i = 0; i < num_read; ++i, char_buffer += size) {
|
|
|
|
ReverseN(char_buffer, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return num_read;
|
|
|
|
}
|
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
int TFile::FRead(void* buffer, int size, int count) {
|
2014-08-12 07:09:25 +08:00
|
|
|
ASSERT_HOST(!is_writing_);
|
2013-10-10 10:07:26 +08:00
|
|
|
int required_size = size * count;
|
2014-08-12 07:09:25 +08:00
|
|
|
if (required_size <= 0) return 0;
|
|
|
|
if (data_->size() - offset_ < required_size)
|
|
|
|
required_size = data_->size() - offset_;
|
2017-05-12 02:10:17 +08:00
|
|
|
if (required_size > 0 && buffer != NULL)
|
|
|
|
memcpy(buffer, &(*data_)[offset_], required_size);
|
2013-10-10 10:07:26 +08:00
|
|
|
offset_ += required_size;
|
|
|
|
return required_size / size;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2013-10-10 10:07:26 +08:00
|
|
|
|
2014-08-12 07:09:25 +08:00
|
|
|
void TFile::Rewind() {
|
|
|
|
ASSERT_HOST(!is_writing_);
|
|
|
|
offset_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TFile::OpenWrite(GenericVector<char>* data) {
|
|
|
|
offset_ = 0;
|
|
|
|
if (data != NULL) {
|
|
|
|
if (data_is_owned_) delete data_;
|
|
|
|
data_ = data;
|
|
|
|
data_is_owned_ = false;
|
|
|
|
} else if (!data_is_owned_) {
|
|
|
|
data_ = new GenericVector<char>;
|
|
|
|
data_is_owned_ = true;
|
|
|
|
}
|
|
|
|
is_writing_ = true;
|
2017-05-04 07:09:44 +08:00
|
|
|
swap_ = false;
|
2014-08-12 07:09:25 +08:00
|
|
|
data_->truncate(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TFile::CloseWrite(const STRING& filename, FileWriter writer) {
|
|
|
|
ASSERT_HOST(is_writing_);
|
|
|
|
if (writer == NULL)
|
|
|
|
return SaveDataToFile(*data_, filename);
|
|
|
|
else
|
|
|
|
return (*writer)(*data_, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TFile::FWrite(const void* buffer, int size, int count) {
|
|
|
|
ASSERT_HOST(is_writing_);
|
|
|
|
int total = size * count;
|
|
|
|
if (total <= 0) return 0;
|
2017-05-11 06:40:31 +08:00
|
|
|
const char* buf = static_cast<const char*>(buffer);
|
2014-08-12 07:09:25 +08:00
|
|
|
// This isn't very efficient, but memory is so fast compared to disk
|
|
|
|
// that it is relatively unimportant, and very simple.
|
|
|
|
for (int i = 0; i < total; ++i)
|
|
|
|
data_->push_back(buf[i]);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
|
|
|
|
} // namespace tesseract.
|
|
|
|
|