Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
37 CachedFile::~CachedFile() {
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 if (buff_ == NULL) {
72 return false;
73 }
74 // init counters
75 buff_size_ = 0;
76 buff_pos_ = 0;
77 file_pos_ = 0;
78 return true;
79 }
80
81 // add a new sample
82 int CachedFile::Read(void *read_buff, int bytes) {
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 }
125
126 long CachedFile::Size() {
127 if (fp_ == NULL && Open() == false) {
128 return 0;
129 }
130
131 return file_size_;
132 }
133
134 long CachedFile::Tell() {
135 if (fp_ == NULL && Open() == false) {
136 return 0;
137 }
138
139 return file_pos_ - buff_size_ + buff_pos_;
140 }
141
142 bool CachedFile::eof() {
143 if (fp_ == NULL && Open() == false) {
144 return true;
145 }
146
147 return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
148 }
149
150 } // namespace tesseract