• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
10 #define LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
11 
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 #include <base/files/file_path.h>
17 #include <brillo/brillo_export.h>
18 
19 namespace brillo {
20 
21 class BRILLO_EXPORT KeyValueStore {
22  public:
23   // Creates an empty KeyValueStore.
24   KeyValueStore();
25   virtual ~KeyValueStore();
26   // Copying is expensive; disallow accidental copies.
27   KeyValueStore(const KeyValueStore&) = delete;
28   KeyValueStore& operator=(const KeyValueStore&) = delete;
29   KeyValueStore(KeyValueStore&&);
30   KeyValueStore& operator=(KeyValueStore&&);
31 
32   // Loads the key=value pairs from the given |path|. Lines starting with '#'
33   // and empty lines are ignored, and whitespace around keys is trimmed.
34   // Trailing backslashes may be used to extend values across multiple lines.
35   // Adds all the read key=values to the store, overriding those already defined
36   // but persisting the ones that aren't present on the passed file.
37   //
38   // Returns true, if the entire file is loaded successfully. If an error occurs
39   // while loading, keeps the pairs that were loaded before the error, and
40   // returns false.
41   //
42   // This function does not clear its internal state before loading. To clear
43   // the internal state, call Clear().
44   bool Load(const base::FilePath& path);
45 
46   // Loads the key=value pairs parsing the text passed in |data|. See Load() for
47   // details.
48   // Returns whether the parsing succeeded.
49   bool LoadFromString(const std::string& data);
50 
51   // Saves the current store to the given |path| file. See SaveToString() for
52   // details on the formate of the created file.
53   // Returns whether the file creation succeeded.
54   bool Save(const base::FilePath& path) const;
55 
56   // Returns a string with the contents of the store as key=value lines.
57   // Calling LoadFromString() and then SaveToString() may result in different
58   // result if the original string contained backslash-terminated lines (i.e.
59   // these values will be rewritten on single lines), comments or empty lines.
60   std::string SaveToString() const;
61 
62   // Clears all the key-value pairs currently stored.
63   void Clear();
64 
65   // Getter for the given key. Returns whether the key was found on the store.
66   bool GetString(const std::string& key, std::string* value) const;
67 
68   // Setter for the given key. It overrides the key if already exists.
69   void SetString(const std::string& key, const std::string& value);
70 
71   // Boolean getter. Returns whether the key was found on the store and if it
72   // has a valid value ("true" or "false").
73   bool GetBoolean(const std::string& key, bool* value) const;
74 
75   // Boolean setter. Sets the value as "true" or "false".
76   void SetBoolean(const std::string& key, bool value);
77 
78   // Retrieves the keys for all values currently stored in the map.
79   std::vector<std::string> GetKeys() const;
80 
81  private:
82   // The map storing all the key-value pairs.
83   std::map<std::string, std::string> store_;
84 };
85 
86 }  // namespace brillo
87 
88 #endif  // LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
89