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
19namespace brillo {
20
21class BRILLO_EXPORT KeyValueStore {
22 public:
23  // Creates an empty KeyValueStore.
24  KeyValueStore() = default;
25  virtual ~KeyValueStore() = default;
26
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.
33  bool Load(const base::FilePath& path);
34
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.
43  bool Save(const base::FilePath& path) const;
44
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
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
64  // Retrieves the keys for all values currently stored in the map.
65  std::vector<std::string> GetKeys() const;
66
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
74}  // namespace brillo
75
76#endif  // LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
77