blob: 3c06817c2a70e52c9bc94249f15fd51885a4c6fe [file] [log] [blame]
Yifan Hong2272bf82017-04-28 14:37:56 -07001/*
2 * Copyright (C) 2017 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#ifndef ANDROID_VINTF_UTILS_H
18#define ANDROID_VINTF_UTILS_H
19
20#include <fstream>
21#include <iostream>
22#include <sstream>
23
24#include <android-base/logging.h>
25#include <utils/Errors.h>
26
27#include "parse_xml.h"
28
29namespace android {
30namespace vintf {
31namespace details {
32
33template<typename T>
34status_t fetchAllInformation(const std::string &path,
35 const XmlConverter<T> &converter, T *outObject) {
36 std::ifstream in;
37 in.open(path);
38 if (!in.is_open()) {
39 LOG(WARNING) << "Cannot open " << path;
40 return INVALID_OPERATION;
41 }
42 std::stringstream ss;
43 ss << in.rdbuf();
44 bool success = converter(outObject, ss.str());
45 if (!success) {
46 LOG(ERROR) << "Illformed file: " << path << ": "
47 << converter.lastError();
48 return BAD_VALUE;
49 }
50 return OK;
51}
52
53} // namespace details
54} // namespace vintf
55} // namespace android
56
57
58
59#endif