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 | |
Alex Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 9 | #ifndef LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_ |
| 10 | #define LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_ |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
Aaron Kemp | 35babde | 2015-02-25 11:08:20 -0500 | [diff] [blame] | 14 | #include <vector> |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 15 | |
| 16 | #include <base/files/file_path.h> |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 17 | #include <brillo/brillo_export.h> |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 19 | namespace brillo { |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 21 | class BRILLO_EXPORT KeyValueStore { |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 22 | public: |
| 23 | // Creates an empty KeyValueStore. |
| 24 | KeyValueStore() = default; |
Alex Vakulenko | 683f3d8 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 25 | virtual ~KeyValueStore() = default; |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 26 | |
Daniel Erat | 9ee4e71 | 2015-01-27 10:29:07 -0700 | [diff] [blame] | 27 | // Loads the key=value pairs from the given |path|. Lines starting with '#' |
| 28 | // and empty lines are ignored, and whitespace around keys is trimmed. |
| 29 | // Trailing backslashes may be used to extend values across multiple lines. |
| 30 | // Adds all the read key=values to the store, overriding those already defined |
| 31 | // but persisting the ones that aren't present on the passed file. Returns |
| 32 | // whether reading the file succeeded. |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 33 | bool Load(const base::FilePath& path); |
| 34 | |
Alex Deymo | 665bc53 | 2015-06-26 19:34:26 -0700 | [diff] [blame] | 35 | // Loads the key=value pairs parsing the text passed in |data|. See Load() for |
| 36 | // details. |
| 37 | // Returns whether the parsing succeeded. |
| 38 | bool LoadFromString(const std::string& data); |
| 39 | |
| 40 | // Saves the current store to the given |path| file. See SaveToString() for |
| 41 | // details on the formate of the created file. |
| 42 | // Returns whether the file creation succeeded. |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 43 | bool Save(const base::FilePath& path) const; |
| 44 | |
Alex Deymo | 665bc53 | 2015-06-26 19:34:26 -0700 | [diff] [blame] | 45 | // Returns a string with the contents of the store as key=value lines. |
| 46 | // Calling LoadFromString() and then SaveToString() may result in different |
| 47 | // result if the original string contained backslash-terminated lines (i.e. |
| 48 | // these values will be rewritten on single lines), comments or empty lines. |
| 49 | std::string SaveToString() const; |
| 50 | |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 51 | // Getter for the given key. Returns whether the key was found on the store. |
| 52 | bool GetString(const std::string& key, std::string* value) const; |
| 53 | |
| 54 | // Setter for the given key. It overrides the key if already exists. |
| 55 | void SetString(const std::string& key, const std::string& value); |
| 56 | |
| 57 | // Boolean getter. Returns whether the key was found on the store and if it |
| 58 | // has a valid value ("true" or "false"). |
| 59 | bool GetBoolean(const std::string& key, bool* value) const; |
| 60 | |
| 61 | // Boolean setter. Sets the value as "true" or "false". |
| 62 | void SetBoolean(const std::string& key, bool value); |
| 63 | |
Aaron Kemp | 35babde | 2015-02-25 11:08:20 -0500 | [diff] [blame] | 64 | // Retrieves the keys for all values currently stored in the map. |
| 65 | std::vector<std::string> GetKeys() const; |
| 66 | |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 67 | private: |
| 68 | // The map storing all the key-value pairs. |
| 69 | std::map<std::string, std::string> store_; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(KeyValueStore); |
| 72 | }; |
| 73 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 74 | } // namespace brillo |
Alex Deymo | edf1a2b | 2014-09-23 12:05:56 -0700 | [diff] [blame] | 75 | |
Alex Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 76 | #endif // LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_ |