tesseract  3.05.02
cached_file.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: cached_file.pp
3  * Description: Implementation of an Cached File Class
4  * Author: Ahmad Abdulkader
5  * Created: 2007
6  *
7  * (C) Copyright 2008, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include <string>
21 #include <stdlib.h>
22 #include <cstring>
23 #include "cached_file.h"
24 
25 namespace tesseract {
26 
27 CachedFile::CachedFile(string file_name) {
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 }
36 
38  if (fp_ != NULL) {
39  fclose(fp_);
40  fp_ = NULL;
41  }
42 
43  if (buff_ != NULL) {
44  delete []buff_;
45  buff_ = NULL;
46  }
47 }
48 
49 // free buffers and init vars
50 bool CachedFile::Open() {
51  if (fp_ != NULL) {
52  return true;
53  }
54 
55  fp_ = fopen(file_name_.c_str(), "rb");
56  if (fp_ == NULL) {
57  return false;
58  }
59 
60  // seek to the end
61  fseek(fp_, 0, SEEK_END);
62  // get file size
63  file_size_ = ftell(fp_);
64  if (file_size_ < 1) {
65  return false;
66  }
67  // rewind again
68  rewind(fp_);
69  // alloc memory for buffer
70  buff_ = new unsigned char[kCacheSize];
71  // init counters
72  buff_size_ = 0;
73  buff_pos_ = 0;
74  file_pos_ = 0;
75  return true;
76 }
77 
78 // add a new sample
79 int CachedFile::Read(void *read_buff, int bytes) {
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 }
122 
124  if (fp_ == NULL && Open() == false) {
125  return 0;
126  }
127 
128  return file_size_;
129 }
130 
132  if (fp_ == NULL && Open() == false) {
133  return 0;
134  }
135 
136  return file_pos_ - buff_size_ + buff_pos_;
137 }
138 
140  if (fp_ == NULL && Open() == false) {
141  return true;
142  }
143 
144  return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
145 }
146 
147 } // namespace tesseract
int Read(void *read_buff, int bytes)
Definition: cached_file.cpp:79
CachedFile(string file_name)
Definition: cached_file.cpp:27