tesseract 3.04.01

ccutil/serialis.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        serialis.h  (Formerly serialmac.h)
00003  * Description: Inline routines and macros for serialisation functions
00004  * Author:      Phil Cheatle
00005  * Created:     Tue Oct 08 08:33:12 BST 1991
00006  *
00007  * (C) Copyright 1990, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include "serialis.h"
00021 #include <stdio.h>
00022 #include "genericvector.h"
00023 
00024 namespace tesseract {
00025 
00026 TFile::TFile()
00027     : offset_(0), data_(NULL), data_is_owned_(false), is_writing_(false) {
00028 }
00029 
00030 TFile::~TFile() {
00031   if (data_is_owned_)
00032     delete data_;
00033 }
00034 
00035 bool TFile::Open(const STRING& filename, FileReader reader) {
00036   if (!data_is_owned_) {
00037     data_ = new GenericVector<char>;
00038     data_is_owned_ = true;
00039   }
00040   offset_ = 0;
00041   is_writing_ = false;
00042   if (reader == NULL)
00043     return LoadDataFromFile(filename, data_);
00044   else
00045     return (*reader)(filename, data_);
00046 }
00047 
00048 bool TFile::Open(const char* data, int size) {
00049   offset_ = 0;
00050   if (!data_is_owned_) {
00051     data_ = new GenericVector<char>;
00052     data_is_owned_ = true;
00053   }
00054   is_writing_ = false;
00055   data_->init_to_size(size, 0);
00056   memcpy(&(*data_)[0], data, size);
00057   return true;
00058 }
00059 
00060 bool TFile::Open(FILE* fp, inT64 end_offset) {
00061   offset_ = 0;
00062   inT64 current_pos = ftell(fp);
00063   if (end_offset < 0) {
00064     if (fseek(fp, 0, SEEK_END))
00065       return false;
00066     end_offset = ftell(fp);
00067     if (fseek(fp, current_pos, SEEK_SET))
00068       return false;
00069   }
00070   int size = end_offset - current_pos;
00071   is_writing_ = false;
00072   if (!data_is_owned_) {
00073     data_ = new GenericVector<char>;
00074     data_is_owned_ = true;
00075   }
00076   data_->init_to_size(size, 0);
00077   return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
00078 }
00079 
00080 char* TFile::FGets(char* buffer, int buffer_size) {
00081   ASSERT_HOST(!is_writing_);
00082   int size = 0;
00083   while (size + 1 < buffer_size && offset_ < data_->size()) {
00084     buffer[size++] = (*data_)[offset_++];
00085     if ((*data_)[offset_ - 1] == '\n') break;
00086   }
00087   if (size < buffer_size) buffer[size] = '\0';
00088   return size > 0 ? buffer : NULL;
00089 }
00090 
00091 int TFile::FRead(void* buffer, int size, int count) {
00092   ASSERT_HOST(!is_writing_);
00093   int required_size = size * count;
00094   if (required_size <= 0) return 0;
00095   char* char_buffer = reinterpret_cast<char*>(buffer);
00096   if (data_->size() - offset_ < required_size)
00097     required_size = data_->size() - offset_;
00098   if (required_size > 0)
00099     memcpy(char_buffer, &(*data_)[offset_], required_size);
00100   offset_ += required_size;
00101   return required_size / size;
00102 }
00103 
00104 void TFile::Rewind() {
00105   ASSERT_HOST(!is_writing_);
00106   offset_ = 0;
00107 }
00108 
00109 void TFile::OpenWrite(GenericVector<char>* data) {
00110   offset_ = 0;
00111   if (data != NULL) {
00112     if (data_is_owned_) delete data_;
00113     data_ = data;
00114     data_is_owned_ = false;
00115   } else if (!data_is_owned_) {
00116     data_ = new GenericVector<char>;
00117     data_is_owned_ = true;
00118   }
00119   is_writing_ = true;
00120   data_->truncate(0);
00121 }
00122 
00123 bool TFile::CloseWrite(const STRING& filename, FileWriter writer) {
00124   ASSERT_HOST(is_writing_);
00125   if (writer == NULL)
00126     return SaveDataToFile(*data_, filename);
00127   else
00128     return (*writer)(*data_, filename);
00129 }
00130 
00131 int TFile::FWrite(const void* buffer, int size, int count) {
00132   ASSERT_HOST(is_writing_);
00133   int total = size * count;
00134   if (total <= 0) return 0;
00135   const char* buf = reinterpret_cast<const char*>(buffer);
00136   // This isn't very efficient, but memory is so fast compared to disk
00137   // that it is relatively unimportant, and very simple.
00138   for (int i = 0; i < total; ++i)
00139     data_->push_back(buf[i]);
00140   return count;
00141 }
00142 
00143 
00144 }  // namespace tesseract.
00145 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines