OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <ostream> | 8 #include <ostream> |
9 | 9 |
10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 void DictionaryValue::SetStringWithoutPathExpansion( | 455 void DictionaryValue::SetStringWithoutPathExpansion( |
456 const std::string& path, const std::string& in_value) { | 456 const std::string& path, const std::string& in_value) { |
457 SetWithoutPathExpansion(path, CreateStringValue(in_value)); | 457 SetWithoutPathExpansion(path, CreateStringValue(in_value)); |
458 } | 458 } |
459 | 459 |
460 void DictionaryValue::SetStringWithoutPathExpansion( | 460 void DictionaryValue::SetStringWithoutPathExpansion( |
461 const std::string& path, const string16& in_value) { | 461 const std::string& path, const string16& in_value) { |
462 SetWithoutPathExpansion(path, CreateStringValue(in_value)); | 462 SetWithoutPathExpansion(path, CreateStringValue(in_value)); |
463 } | 463 } |
464 | 464 |
465 bool DictionaryValue::Get( | 465 bool DictionaryValue::Get(const std::string& path, |
466 const std::string& path, const Value** out_value) const { | 466 const Value** out_value) const { |
467 DCHECK(IsStringUTF8(path)); | 467 DCHECK(IsStringUTF8(path)); |
468 std::string current_path(path); | 468 std::string current_path(path); |
469 const DictionaryValue* current_dictionary = this; | 469 const DictionaryValue* current_dictionary = this; |
470 for (size_t delimiter_position = current_path.find('.'); | 470 for (size_t delimiter_position = current_path.find('.'); |
471 delimiter_position != std::string::npos; | 471 delimiter_position != std::string::npos; |
472 delimiter_position = current_path.find('.')) { | 472 delimiter_position = current_path.find('.')) { |
473 const DictionaryValue* child_dictionary = NULL; | 473 const DictionaryValue* child_dictionary = NULL; |
474 if (!current_dictionary->GetDictionary( | 474 if (!current_dictionary->GetDictionary( |
475 current_path.substr(0, delimiter_position), &child_dictionary)) | 475 current_path.substr(0, delimiter_position), &child_dictionary)) |
476 return false; | 476 return false; |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 | 744 |
745 Value* entry = entry_iterator->second; | 745 Value* entry = entry_iterator->second; |
746 if (out_value) | 746 if (out_value) |
747 *out_value = entry; | 747 *out_value = entry; |
748 else | 748 else |
749 delete entry; | 749 delete entry; |
750 dictionary_.erase(entry_iterator); | 750 dictionary_.erase(entry_iterator); |
751 return true; | 751 return true; |
752 } | 752 } |
753 | 753 |
| 754 bool DictionaryValue::RemovePath(const std::string& path, Value** out_value) { |
| 755 bool result = false; |
| 756 size_t delimiter_position = path.find('.'); |
| 757 if (delimiter_position == std::string::npos) { |
| 758 return RemoveWithoutPathExpansion(path, out_value); |
| 759 } else { |
| 760 const std::string subdict_path = path.substr(0, delimiter_position); |
| 761 DictionaryValue* subdict = NULL; |
| 762 if (!GetDictionary(subdict_path, &subdict)) |
| 763 return false; |
| 764 result = subdict->RemovePath(path.substr(delimiter_position + 1), |
| 765 out_value); |
| 766 if (result && subdict->empty()) |
| 767 Remove(subdict_path, NULL); |
| 768 } |
| 769 return result; |
| 770 } |
| 771 |
754 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { | 772 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { |
755 Value* copy = CopyWithoutEmptyChildren(this); | 773 Value* copy = CopyWithoutEmptyChildren(this); |
756 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 774 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
757 } | 775 } |
758 | 776 |
759 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 777 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
760 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); | 778 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); |
761 key != dictionary->end_keys(); ++key) { | 779 key != dictionary->end_keys(); ++key) { |
762 const Value* merge_value; | 780 const Value* merge_value; |
763 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { | 781 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 | 1141 |
1124 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1142 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1125 std::string json; | 1143 std::string json; |
1126 JSONWriter::WriteWithOptions(&value, | 1144 JSONWriter::WriteWithOptions(&value, |
1127 JSONWriter::OPTIONS_PRETTY_PRINT, | 1145 JSONWriter::OPTIONS_PRETTY_PRINT, |
1128 &json); | 1146 &json); |
1129 return out << json; | 1147 return out << json; |
1130 } | 1148 } |
1131 | 1149 |
1132 } // namespace base | 1150 } // namespace base |
OLD | NEW |