• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <sys/stat.h>
18 #include <stdlib.h>
19 
20 #include "bcinfo/Wrap/file_wrapper_input.h"
21 
FileWrapperInput(const char * name)22 FileWrapperInput::FileWrapperInput(const char* name) :
23     _name(name), _at_eof(false), _size_found(false), _size(0) {
24   _file = fopen(name, "rb");
25   if (_file == nullptr) {
26     fprintf(stderr, "Unable to open: %s\n", name);
27     exit(1);
28   }
29 }
30 
~FileWrapperInput()31 FileWrapperInput::~FileWrapperInput() {
32   fclose(_file);
33 }
34 
Read(uint8_t * buffer,size_t wanted)35 size_t FileWrapperInput::Read(uint8_t* buffer, size_t wanted) {
36   size_t found = fread((char*) buffer, 1, wanted, _file);
37   if (feof(_file) || ferror(_file)) {
38     _at_eof = true;
39   }
40   return found;
41 }
42 
AtEof()43 bool FileWrapperInput::AtEof() {
44   return _at_eof;
45 }
46 
Size()47 off_t FileWrapperInput::Size() {
48   if (_size_found) return _size;
49   struct stat st;
50   if (stat(_name, &st) == 0) {
51     _size_found = true;
52     _size = st.st_size;
53     return _size;
54   } else {
55     fprintf(stderr, "Unable to compute file size: %s\n", _name);
56     exit(1);
57   }
58   // NOT REACHABLE.
59   return 0;
60 }
61 
Seek(uint32_t pos)62 bool FileWrapperInput::Seek(uint32_t pos) {
63   return fseek(_file, (long) pos, SEEK_SET) == 0;
64 }
65