Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // These functions can parse a blob of data that's formatted as a simple |
| 6 | // key value store. Each key/value pair is stored on its own line and |
| 7 | // separated by the first '=' on the line. |
| 8 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 9 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_ |
| 10 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_ |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | |
| 15 | namespace chromeos_update_engine { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 16 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 17 | class KeyValueStore { |
| 18 | public: |
| 19 | // Creates an empty KeyValueStore. |
| 20 | KeyValueStore() {} |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 21 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 22 | // Loads the key=value pairs from the given filename. Lines starting with |
| 23 | // '#' and empty lines are ignored. Adds all the readed key=values to the |
| 24 | // store, overriding those already defined but persisting the ones that |
| 25 | // aren't present on the passed file. |
| 26 | // Returns whether reading the file succeeded. |
| 27 | bool Load(const std::string& filename); |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 28 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 29 | // Saves the current store to the given |filename| file. Returns whether the |
| 30 | // file creation succeeded. |
| 31 | bool Save(const std::string& filename) const; |
| 32 | |
| 33 | // Getter for the given key. Returns whether the key was found on the store. |
| 34 | bool GetString(const std::string& key, std::string* value) const; |
| 35 | |
| 36 | // Setter for the given key. It overrides the key if already exists. |
| 37 | void SetString(const std::string& key, const std::string& value); |
| 38 | |
| 39 | // Boolean getter. Returns whether the key was found on the store and if it |
| 40 | // has a valid value ("true" or "false"). |
| 41 | bool GetBoolean(const std::string& key, bool* value) const; |
| 42 | |
| 43 | // Boolean setter. Sets the value as "true" or "false". |
| 44 | void SetBoolean(const std::string& key, bool value); |
| 45 | |
| 46 | private: |
| 47 | // The map storing all the key-value pairs. |
| 48 | std::map<std::string, std::string> store_; |
| 49 | }; |
| 50 | |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 51 | } // namespace chromeos_update_engine |
| 52 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 53 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_ |