Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/simple_key_value_store.h" |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <string> |
| 9 | #include <vector> |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 12 | #include <base/strings/string_split.h> |
| 13 | #include <base/strings/string_util.h> |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 14 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 15 | #include "update_engine/utils.h" |
| 16 | |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 17 | using std::map; |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 22 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 23 | bool KeyValueStore::Load(const string& filename) { |
| 24 | string file_data; |
| 25 | if (!base::ReadFileToString(base::FilePath(filename), &file_data)) |
| 26 | return false; |
| 27 | |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 28 | // Split along '\n', then along '=' |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 29 | vector<string> lines; |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 30 | base::SplitStringDontTrim(file_data, '\n', &lines); |
| 31 | for (auto& it : lines) { |
| 32 | if (it.empty() || it[0] == '#') |
| 33 | continue; |
| 34 | string::size_type pos = it.find('='); |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 35 | if (pos == string::npos) |
| 36 | continue; |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 37 | store_[it.substr(0, pos)] = it.substr(pos + 1); |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 38 | } |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 39 | return true; |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 42 | bool KeyValueStore::Save(const string& filename) const { |
| 43 | string data; |
| 44 | for (auto& it : store_) |
| 45 | data += it.first + "=" + it.second + "\n"; |
| 46 | |
| 47 | return utils::WriteFile(filename.c_str(), data.c_str(), data.size()); |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 50 | bool KeyValueStore::GetString(const string& key, string* value) const { |
| 51 | auto it = store_.find(key); |
| 52 | if (it == store_.end()) |
| 53 | return false; |
| 54 | *value = it->second; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | void KeyValueStore::SetString(const string& key, const string& value) { |
| 59 | store_[key] = value; |
| 60 | } |
| 61 | |
| 62 | bool KeyValueStore::GetBoolean(const string& key, bool* value) const { |
| 63 | auto it = store_.find(key); |
| 64 | if (it == store_.end()) |
| 65 | return false; |
| 66 | if (it->second == "true") { |
| 67 | *value = true; |
| 68 | return true; |
| 69 | } else if (it-> second == "false") { |
| 70 | *value = false; |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | void KeyValueStore::SetBoolean(const string& key, bool value) { |
| 78 | store_[key] = value ? "true" : "false"; |
| 79 | } |
| 80 | |
Andrew de los Reyes | 785bc35 | 2010-05-26 12:34:53 -0700 | [diff] [blame] | 81 | } // namespace chromeos_update_engine |