All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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

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 }
#define NULL
Definition: host.h:144
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 }
#define NULL
Definition: host.h:144

Member Function Documentation

bool tesseract::CachedFile::eof ( )

Definition at line 142 of file cached_file.cpp.

142  {
143  if (fp_ == NULL && Open() == false) {
144  return true;
145  }
146 
147  return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
148 }
#define NULL
Definition: host.h:144
int tesseract::CachedFile::Read ( void *  read_buff,
int  bytes 
)

Definition at line 82 of file cached_file.cpp.

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

Definition at line 126 of file cached_file.cpp.

126  {
127  if (fp_ == NULL && Open() == false) {
128  return 0;
129  }
130 
131  return file_size_;
132 }
#define NULL
Definition: host.h:144
long tesseract::CachedFile::Tell ( )

Definition at line 134 of file cached_file.cpp.

134  {
135  if (fp_ == NULL && Open() == false) {
136  return 0;
137  }
138 
139  return file_pos_ - buff_size_ + buff_pos_;
140 }
#define NULL
Definition: host.h:144

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