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];
81 unsigned char *buff = (
unsigned char *)read_buff;
84 if ((buff_pos_ + bytes) > buff_size_) {
86 int copy_bytes = buff_size_ - buff_pos_;
89 memcpy(buff, buff_ + buff_pos_, copy_bytes);
92 read_bytes += copy_bytes;
96 buff_size_ = kCacheSize;
98 if ((file_pos_ + buff_size_) > file_size_) {
99 buff_size_ =
static_cast<int>(file_size_ - file_pos_);
103 if (buff_size_ <= 0 || bytes > buff_size_) {
108 if (fread(buff_, 1, buff_size_, fp_) != buff_size_) {
113 file_pos_ += buff_size_;
116 memcpy(buff, buff_ + buff_pos_, bytes);
124 if (fp_ == NULL && Open() ==
false) {
132 if (fp_ == NULL && Open() ==
false) {
136 return file_pos_ - buff_size_ + buff_pos_;
140 if (fp_ == NULL && Open() ==
false) {
144 return (file_pos_ - buff_size_ + buff_pos_) >= file_size_;
int Read(void *read_buff, int bytes)
CachedFile(string file_name)