1 /* 2 * Copyright 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 #pragma once 18 19 #include <optional> 20 #include <string> 21 #include <string_view> 22 #include <unordered_map> 23 24 namespace netsim { 25 26 // A simple class to process init file. Modified from 27 // external/qemu/android/android-emu-base/android/base/files/IniFile.h 28 class IniFile { 29 public: 30 // Note that the constructor _does not_ read data from the backing file. 31 // Call |Read| to read the data. filepath(std::move (filepath))32 explicit IniFile(std::string filepath = "") : filepath(std::move(filepath)) {} 33 34 // Reads data into IniFile from the backing file, overwriting any 35 // existing data. 36 bool Read(); 37 38 // Writes the current IniFile to the backing file. 39 bool Write(); 40 41 // Checks if a certain key exists in the file. 42 bool HasKey(const std::string &key) const; 43 44 // Gets value. 45 std::optional<std::string> Get(const std::string &key) const; 46 47 // Sets value. 48 void Set(const std::string &key, std::string_view value); 49 50 private: 51 std::unordered_map<std::string, std::string> data; 52 std::string filepath; 53 }; 54 55 } // namespace netsim 56