1 /*
2 * Copyright (C) 2022 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 "chre_host/file_stream.h"
18
19 #include <fstream>
20 #include "chre_host/log.h"
21
22 namespace android {
23 namespace chre {
24
readFileContents(const char * filename,std::vector<uint8_t> & buffer)25 bool readFileContents(const char *filename, std::vector<uint8_t> &buffer) {
26 bool success = false;
27 std::ifstream file(filename, std::ios::binary | std::ios::ate);
28 if (!file) {
29 LOGE("Couldn't open file '%s': %d (%s)", filename, errno, strerror(errno));
30 } else {
31 ssize_t size = file.tellg();
32 file.seekg(0, std::ios::beg);
33
34 buffer.resize(size);
35 if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
36 LOGE("Couldn't read from file '%s': %d (%s)", filename, errno,
37 strerror(errno));
38 } else {
39 success = true;
40 }
41 }
42
43 return success;
44 }
45
46 } // namespace chre
47 } // namespace android
48