2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: serialis.h (Formerly serialmac.h)
|
|
|
|
* 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>
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
TFile::TFile() : offset_(0) {
|
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) {
|
|
|
|
offset_ = 0;
|
|
|
|
if (reader == NULL)
|
|
|
|
return LoadDataFromFile(filename, &data_);
|
|
|
|
else
|
|
|
|
return (*reader)(filename, &data_);
|
|
|
|
}
|
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;
|
|
|
|
data_.init_to_size(size, 0);
|
|
|
|
memcpy(&data_[0], data, size);
|
|
|
|
return true;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
bool TFile::Open(FILE* fp, inT64 end_offset) {
|
|
|
|
offset_ = 0;
|
|
|
|
inT64 current_pos = ftell(fp);
|
|
|
|
if (end_offset < 0) {
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
end_offset = ftell(fp);
|
|
|
|
fseek(fp, current_pos, SEEK_SET);
|
|
|
|
}
|
|
|
|
int size = end_offset - current_pos;
|
|
|
|
data_.init_to_size(size, 0);
|
2014-01-25 10:28:51 +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) {
|
|
|
|
int size = 0;
|
|
|
|
while (size + 1 < buffer_size && offset_ < data_.size()) {
|
|
|
|
buffer[size++] = data_[offset_++];
|
|
|
|
if (data_[offset_ - 1] == '\n') break;
|
|
|
|
}
|
|
|
|
if (size < buffer_size) buffer[size] = '\0';
|
|
|
|
return size > 0 ? buffer : NULL;
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2013-10-10 10:07:26 +08:00
|
|
|
int TFile::FRead(void* buffer, int size, int count) {
|
|
|
|
char* char_buffer = reinterpret_cast<char*>(buffer);
|
|
|
|
int required_size = size * count;
|
|
|
|
if (data_.size() - offset_ < required_size)
|
|
|
|
required_size = data_.size() - offset_;
|
|
|
|
memcpy(char_buffer, &data_[offset_], required_size);
|
|
|
|
offset_ += required_size;
|
|
|
|
return required_size / size;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2013-10-10 10:07:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace tesseract.
|
|
|
|
|