Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 9 | #ifndef LIBCHROMEOS_CHROMEOS_KEY_VALUE_STORE_H_ |
| 10 | #define LIBCHROMEOS_CHROMEOS_KEY_VALUE_STORE_H_ |
| 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | |
| 15 | #include <base/files/file_path.h> |
| 16 | #include <chromeos/chromeos_export.h> |
| 17 | |
| 18 | namespace chromeos { |
| 19 | |
| 20 | class CHROMEOS_EXPORT KeyValueStore { |
| 21 | public: |
| 22 | // Creates an empty KeyValueStore. |
| 23 | KeyValueStore() = default; |
| 24 | |
Daniel Erat | 9ee4e71 | 2015-01-27 10:29:07 -0700 | [diff] [blame^] | 25 | // Loads the key=value pairs from the given |path|. Lines starting with '#' |
| 26 | // and empty lines are ignored, and whitespace around keys is trimmed. |
| 27 | // Trailing backslashes may be used to extend values across multiple lines. |
| 28 | // Adds all the read key=values to the store, overriding those already defined |
| 29 | // but persisting the ones that aren't present on the passed file. Returns |
| 30 | // whether reading the file succeeded. |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 31 | bool Load(const base::FilePath& path); |
| 32 | |
Daniel Erat | 9ee4e71 | 2015-01-27 10:29:07 -0700 | [diff] [blame^] | 33 | // Saves the current store to the given |path| file. Returns whether the file |
| 34 | // creation succeeded. Calling Load() and then Save() may result in different |
| 35 | // data being written if the original file contained backslash-terminated |
| 36 | // lines (i.e. these values will be rewritten on single lines). |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 37 | bool Save(const base::FilePath& path) const; |
| 38 | |
| 39 | // Getter for the given key. Returns whether the key was found on the store. |
| 40 | bool GetString(const std::string& key, std::string* value) const; |
| 41 | |
| 42 | // Setter for the given key. It overrides the key if already exists. |
| 43 | void SetString(const std::string& key, const std::string& value); |
| 44 | |
| 45 | // Boolean getter. Returns whether the key was found on the store and if it |
| 46 | // has a valid value ("true" or "false"). |
| 47 | bool GetBoolean(const std::string& key, bool* value) const; |
| 48 | |
| 49 | // Boolean setter. Sets the value as "true" or "false". |
| 50 | void SetBoolean(const std::string& key, bool value); |
| 51 | |
| 52 | private: |
| 53 | // The map storing all the key-value pairs. |
| 54 | std::map<std::string, std::string> store_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(KeyValueStore); |
| 57 | }; |
| 58 | |
| 59 | } // namespace chromeos |
| 60 | |
| 61 | #endif // LIBCHROMEOS_CHROMEOS_KEY_VALUE_STORE_H_ |