Tesseract  3.02
tesseract-ocr/cube/cached_file.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        cached_file.h
00003  * Description: Declaration of a Cached File class
00004  * Author:    Ahmad Abdulkader
00005  * Created:   2007
00006  *
00007  * (C) Copyright 2008, Google Inc.
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 #ifndef CACHED_FILE_H
00021 #define CACHED_FILE_H
00022 
00023 // The CachedFile class provides a large-cache read access to a file
00024 // It is mainly designed for loading large word dump files
00025 
00026 #include <stdio.h>
00027 #include <string>
00028 #ifdef USE_STD_NAMESPACE
00029 using std::string;
00030 #endif
00031 
00032 namespace tesseract {
00033 class CachedFile {
00034  public:
00035   explicit CachedFile(string file_name);
00036   ~CachedFile();
00037 
00038   // reads a specified number of bytes to the specified buffer and
00039   // returns the actual number of bytes read
00040   int Read(void *read_buff, int bytes);
00041   // Returns the file size
00042   long Size();
00043   // returns the current position in the file
00044   long Tell();
00045   // End of file flag
00046   bool eof();
00047 
00048  private:
00049   static const unsigned int kCacheSize = 0x8000000;
00050   // file name
00051   string file_name_;
00052   // internal file buffer
00053   unsigned char *buff_;
00054   // file position
00055   long file_pos_;
00056   // file size
00057   long file_size_;
00058   // position of file within buffer
00059   int buff_pos_;
00060   // buffer size
00061   int buff_size_;
00062   // file handle
00063   FILE *fp_;
00064   // Opens the file
00065   bool Open();
00066 };
00067 }
00068 
00069 #endif  // CACHED_FILE_H