tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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 
00023 namespace tesseract {
00024 
00025 TFile::TFile() : offset_(0) {
00026 }
00027 
00028 bool TFile::Open(const STRING& filename, FileReader reader) {
00029   offset_ = 0;
00030   if (reader == NULL)
00031     return LoadDataFromFile(filename, &data_);
00032   else
00033     return (*reader)(filename, &data_);
00034 }
00035 
00036 bool TFile::Open(const char* data, int size) {
00037   offset_ = 0;
00038   data_.init_to_size(size, 0);
00039   memcpy(&data_[0], data, size);
00040   return true;
00041 }
00042 
00043 bool TFile::Open(FILE* fp, inT64 end_offset) {
00044   offset_ = 0;
00045   inT64 current_pos = ftell(fp);
00046   if (end_offset < 0) {
00047     fseek(fp, 0, SEEK_END);
00048     end_offset = ftell(fp);
00049     fseek(fp, current_pos, SEEK_SET);
00050   }
00051   int size = end_offset - current_pos;
00052   data_.init_to_size(size, 0);
00053   return static_cast<int>(fread(&data_[0], 1, size, fp)) == size;
00054 }
00055 
00056 char* TFile::FGets(char* buffer, int buffer_size) {
00057   int size = 0;
00058   while (size + 1 < buffer_size && offset_ < data_.size()) {
00059     buffer[size++] = data_[offset_++];
00060     if (data_[offset_ - 1] == '\n') break;
00061   }
00062   if (size < buffer_size) buffer[size] = '\0';
00063   return size > 0 ? buffer : NULL;
00064 }
00065 
00066 int TFile::FRead(void* buffer, int size, int count) {
00067   char* char_buffer = reinterpret_cast<char*>(buffer);
00068   int required_size = size * count;
00069   if (data_.size() - offset_ < required_size)
00070     required_size = data_.size() - offset_;
00071   memcpy(char_buffer, &data_[offset_], required_size);
00072   offset_ += required_size;
00073   return required_size / size;
00074 }
00075 
00076 
00077 }  // namespace tesseract.
00078 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines