28 file_name_ = file_name;
50 bool CachedFile::Open() {
55 fp_ = fopen(file_name_.c_str(),
"rb");
61 fseek(fp_, 0, SEEK_END);
63 file_size_ = ftell(fp_);
70 buff_ =
new unsigned char[kCacheSize];
84 unsigned char *buff = (
unsigned char *)read_buff;
87 if ((buff_pos_ + bytes) > buff_size_) {
89 int copy_bytes = buff_size_ - buff_pos_;
92 memcpy(buff, buff_ + buff_pos_, copy_bytes);
95 read_bytes += copy_bytes;
99 buff_size_ = kCacheSize;
101 if ((file_pos_ + buff_size_) > file_size_) {
102 buff_size_ =
static_cast<int>(file_size_ - file_pos_);
106 if (buff_size_ <= 0 || bytes > buff_size_) {
111 if (fread(buff_, 1, buff_size_, fp_) != buff_size_) {
116 file_pos_ += buff_size_;
119 memcpy(buff, buff_ + buff_pos_, bytes);
127 if (fp_ ==
NULL && Open() ==
false) {
135 if (fp_ ==
NULL && Open() ==
false) {
139 return file_pos_ - buff_size_ + buff_pos_;
143 if (fp_ ==
NULL && Open() ==
false) {
147 return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
int Read(void *read_buff, int bytes)
CachedFile(string file_name)