blob: cc5fa40fb8cfce8d21b80a5ee99bb096506da21e [file] [log] [blame]
Alex Deymoedf1a2b2014-09-23 12:05:56 -07001// 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 Vakulenkofed60b02015-10-27 09:53:05 -07009#ifndef LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
10#define LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_
Alex Deymoedf1a2b2014-09-23 12:05:56 -070011
12#include <map>
13#include <string>
Aaron Kemp35babde2015-02-25 11:08:20 -050014#include <vector>
Alex Deymoedf1a2b2014-09-23 12:05:56 -070015
16#include <base/files/file_path.h>
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070017#include <brillo/brillo_export.h>
Alex Deymoedf1a2b2014-09-23 12:05:56 -070018
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070019namespace brillo {
Alex Deymoedf1a2b2014-09-23 12:05:56 -070020
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070021class BRILLO_EXPORT KeyValueStore {
Alex Deymoedf1a2b2014-09-23 12:05:56 -070022 public:
23 // Creates an empty KeyValueStore.
24 KeyValueStore() = default;
Alex Vakulenko683f3d82015-05-22 15:48:53 -070025 virtual ~KeyValueStore() = default;
Alex Deymoedf1a2b2014-09-23 12:05:56 -070026
Daniel Erat9ee4e712015-01-27 10:29:07 -070027 // 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 Deymoedf1a2b2014-09-23 12:05:56 -070033 bool Load(const base::FilePath& path);
34
Alex Deymo665bc532015-06-26 19:34:26 -070035 // 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 Deymoedf1a2b2014-09-23 12:05:56 -070043 bool Save(const base::FilePath& path) const;
44
Alex Deymo665bc532015-06-26 19:34:26 -070045 // 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 Deymoedf1a2b2014-09-23 12:05:56 -070051 // 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 Kemp35babde2015-02-25 11:08:20 -050064 // Retrieves the keys for all values currently stored in the map.
65 std::vector<std::string> GetKeys() const;
66
Alex Deymoedf1a2b2014-09-23 12:05:56 -070067 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 Vakulenko9ed0cab2015-10-12 15:21:28 -070074} // namespace brillo
Alex Deymoedf1a2b2014-09-23 12:05:56 -070075
Alex Vakulenkofed60b02015-10-27 09:53:05 -070076#endif // LIBBRILLO_BRILLO_KEY_VALUE_STORE_H_