Tesseract  3.02
tesseract::CachedFile Class Reference

#include <cached_file.h>

List of all members.

Public Member Functions

 CachedFile (string file_name)
 ~CachedFile ()
int Read (void *read_buff, int bytes)
long Size ()
long Tell ()
bool eof ()

Detailed Description

Definition at line 33 of file cached_file.h.


Constructor & Destructor Documentation

tesseract::CachedFile::CachedFile ( string  file_name) [explicit]

Definition at line 27 of file cached_file.cpp.

                                       {
  file_name_ = file_name;
  buff_ = NULL;
  buff_pos_ = 0;
  buff_size_ = 0;
  file_pos_ = 0;
  file_size_ = 0;
  fp_ = NULL;
}
tesseract::CachedFile::~CachedFile ( )

Definition at line 37 of file cached_file.cpp.

                        {
  if (fp_ != NULL) {
    fclose(fp_);
    fp_ = NULL;
  }

  if (buff_ != NULL) {
    delete []buff_;
    buff_ = NULL;
  }
}

Member Function Documentation

bool tesseract::CachedFile::eof ( )

Definition at line 142 of file cached_file.cpp.

                     {
  if (fp_ == NULL && Open() == false) {
    return true;
  }

  return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
}
int tesseract::CachedFile::Read ( void *  read_buff,
int  bytes 
)

Definition at line 82 of file cached_file.cpp.

                                               {
  int read_bytes = 0;
  unsigned char *buff = (unsigned char *)read_buff;

  // do we need to read beyond the buffer
  if ((buff_pos_ + bytes) > buff_size_) {
    // copy as much bytes from the current buffer if any
    int copy_bytes = buff_size_ - buff_pos_;

    if (copy_bytes > 0) {
      memcpy(buff, buff_ + buff_pos_, copy_bytes);
      buff += copy_bytes;
      bytes -= copy_bytes;
      read_bytes += copy_bytes;
    }

    // determine how much to read
    buff_size_ = kCacheSize;

    if ((file_pos_ + buff_size_) > file_size_) {
      buff_size_ = static_cast<int>(file_size_ - file_pos_);
    }

    // EOF ?
    if (buff_size_ <= 0 || bytes > buff_size_) {
      return read_bytes;
    }

    // read the first chunck
    if (fread(buff_, 1, buff_size_, fp_) != buff_size_) {
      return read_bytes;
    }

    buff_pos_ = 0;
    file_pos_ += buff_size_;
  }

  memcpy(buff, buff_ + buff_pos_, bytes);
  read_bytes += bytes;
  buff_pos_ += bytes;

  return read_bytes;
}
long tesseract::CachedFile::Size ( )

Definition at line 126 of file cached_file.cpp.

                      {
  if (fp_ == NULL && Open() == false) {
    return 0;
  }

  return file_size_;
}
long tesseract::CachedFile::Tell ( )

Definition at line 134 of file cached_file.cpp.

                      {
  if (fp_ == NULL && Open() == false) {
    return 0;
  }

  return file_pos_ - buff_size_ + buff_pos_;
}

The documentation for this class was generated from the following files: