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