tesseract  3.05.02
tesseract::CachedFile Class Reference

#include <cached_file.h>

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

◆ CachedFile()

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

Definition at line 27 of file cached_file.cpp.

27  {
28  file_name_ = file_name;
29  buff_ = NULL;
30  buff_pos_ = 0;
31  buff_size_ = 0;
32  file_pos_ = 0;
33  file_size_ = 0;
34  fp_ = NULL;
35 }

◆ ~CachedFile()

tesseract::CachedFile::~CachedFile ( )

Definition at line 37 of file cached_file.cpp.

37  {
38  if (fp_ != NULL) {
39  fclose(fp_);
40  fp_ = NULL;
41  }
42 
43  if (buff_ != NULL) {
44  delete []buff_;
45  buff_ = NULL;
46  }
47 }

Member Function Documentation

◆ eof()

bool tesseract::CachedFile::eof ( )

Definition at line 139 of file cached_file.cpp.

139  {
140  if (fp_ == NULL && Open() == false) {
141  return true;
142  }
143 
144  return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
145 }

◆ Read()

int tesseract::CachedFile::Read ( void *  read_buff,
int  bytes 
)

Definition at line 79 of file cached_file.cpp.

79  {
80  int read_bytes = 0;
81  unsigned char *buff = (unsigned char *)read_buff;
82 
83  // do we need to read beyond the buffer
84  if ((buff_pos_ + bytes) > buff_size_) {
85  // copy as much bytes from the current buffer if any
86  int copy_bytes = buff_size_ - buff_pos_;
87 
88  if (copy_bytes > 0) {
89  memcpy(buff, buff_ + buff_pos_, copy_bytes);
90  buff += copy_bytes;
91  bytes -= copy_bytes;
92  read_bytes += copy_bytes;
93  }
94 
95  // determine how much to read
96  buff_size_ = kCacheSize;
97 
98  if ((file_pos_ + buff_size_) > file_size_) {
99  buff_size_ = static_cast<int>(file_size_ - file_pos_);
100  }
101 
102  // EOF ?
103  if (buff_size_ <= 0 || bytes > buff_size_) {
104  return read_bytes;
105  }
106 
107  // read the first chunck
108  if (fread(buff_, 1, buff_size_, fp_) != buff_size_) {
109  return read_bytes;
110  }
111 
112  buff_pos_ = 0;
113  file_pos_ += buff_size_;
114  }
115 
116  memcpy(buff, buff_ + buff_pos_, bytes);
117  read_bytes += bytes;
118  buff_pos_ += bytes;
119 
120  return read_bytes;
121 }

◆ Size()

long tesseract::CachedFile::Size ( )

Definition at line 123 of file cached_file.cpp.

123  {
124  if (fp_ == NULL && Open() == false) {
125  return 0;
126  }
127 
128  return file_size_;
129 }

◆ Tell()

long tesseract::CachedFile::Tell ( )

Definition at line 131 of file cached_file.cpp.

131  {
132  if (fp_ == NULL && Open() == false) {
133  return 0;
134  }
135 
136  return file_pos_ - buff_size_ + buff_pos_;
137 }

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