173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe/*
273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * you may not use this file except in compliance with the License.
673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * You may obtain a copy of the License at
773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe *
1073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * Unless required by applicable law or agreed to in writing, software
1173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
1273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * See the License for the specific language governing permissions and
1473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe * limitations under the License.
1573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe */
1673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
1773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#ifndef OTAPREOPT_SYSTEM_PROPERTIES_H_
1873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#define OTAPREOPT_SYSTEM_PROPERTIES_H_
1973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <fstream>
2173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <string>
2273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#include <unordered_map>
2373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
241842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe#include <file_parsing.h>
251842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe
2673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampenamespace android {
2773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampenamespace installd {
2873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
2973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe// Helper class to read system properties into and manage as a string->string map.
3073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampeclass SystemProperties {
3173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe public:
3273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    bool Load(const std::string& strFile) {
331842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe        return ParseFile(strFile, [&](const std::string& line) {
3473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            size_t equals_pos = line.find('=');
3573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            if (equals_pos == std::string::npos || equals_pos == 0) {
3673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe                // Did not find equals sign, or it's the first character - isn't a valid line.
371842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe                return true;
3873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            }
3973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
4073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            std::string key = line.substr(0, equals_pos);
4173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            std::string value = line.substr(equals_pos + 1,
4273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe                                            line.length() - equals_pos + 1);
4373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
4473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            properties_.insert(std::make_pair(key, value));
4573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
461842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe            return true;
471842af3a4a8fccf71f5c569071518de14b3698acAndreas Gampe        });
4873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
4973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
5073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    // Look up the key in the map. Returns null if the key isn't mapped.
5173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    const std::string* GetProperty(const std::string& key) const {
5273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        auto it = properties_.find(key);
5373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        if (it != properties_.end()) {
5473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe            return &it->second;
5573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        }
5673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        return nullptr;
5773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
5873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
5973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    void SetProperty(const std::string& key, const std::string& value) {
6073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe        properties_.insert(std::make_pair(key, value));
6173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    }
6273dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
6373dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe private:
6473dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    // The actual map.
6573dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe    std::unordered_map<std::string, std::string> properties_;
6673dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe};
6773dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
6873dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}  // namespace installd
6973dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe}  // namespace android
7073dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe
7173dae11162aa61396c06cbdb05b954764e944e02Andreas Gampe#endif  // OTAPREOPT_SYSTEM_PROPERTIES_H_
72