blob: eb5a4e285b28dd62975bee2ee099c56a1ca52f3f [file] [log] [blame]
Chris Masonef8d037f2014-02-19 01:53:00 +00001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes785bc352010-05-26 12:34:53 -07002// 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 Deymo759c2752014-03-17 21:09:36 -07009#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_
10#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_
Andrew de los Reyes785bc352010-05-26 12:34:53 -070011
12#include <map>
13#include <string>
14
15namespace chromeos_update_engine {
Chris Masonef8d037f2014-02-19 01:53:00 +000016
Alex Deymoa7658442014-04-17 15:49:37 -070017class KeyValueStore {
18 public:
19 // Creates an empty KeyValueStore.
20 KeyValueStore() {}
Andrew de los Reyes785bc352010-05-26 12:34:53 -070021
Alex Deymoa7658442014-04-17 15:49:37 -070022 // Loads the key=value pairs from the given filename. Lines starting with
23 // '#' and empty lines are ignored. Adds all the readed key=values to the
24 // store, overriding those already defined but persisting the ones that
25 // aren't present on the passed file.
26 // Returns whether reading the file succeeded.
27 bool Load(const std::string& filename);
Andrew de los Reyes785bc352010-05-26 12:34:53 -070028
Alex Deymoa7658442014-04-17 15:49:37 -070029 // Saves the current store to the given |filename| file. Returns whether the
30 // file creation succeeded.
31 bool Save(const std::string& filename) const;
32
33 // Getter for the given key. Returns whether the key was found on the store.
34 bool GetString(const std::string& key, std::string* value) const;
35
36 // Setter for the given key. It overrides the key if already exists.
37 void SetString(const std::string& key, const std::string& value);
38
39 // Boolean getter. Returns whether the key was found on the store and if it
40 // has a valid value ("true" or "false").
41 bool GetBoolean(const std::string& key, bool* value) const;
42
43 // Boolean setter. Sets the value as "true" or "false".
44 void SetBoolean(const std::string& key, bool value);
45
46 private:
47 // The map storing all the key-value pairs.
48 std::map<std::string, std::string> store_;
49};
50
Andrew de los Reyes785bc352010-05-26 12:34:53 -070051} // namespace chromeos_update_engine
52
Alex Deymo759c2752014-03-17 21:09:36 -070053#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H_