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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <ostream> | 10 #include <ostream> |
(...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 // LOG(WARNING) << "\n1\n"; | 468 // LOG(WARNING) << "\n1\n"; |
469 std::string current_path(path); | 469 std::string current_path(path); |
470 const DictionaryValue* current_dictionary = this; | 470 const DictionaryValue* current_dictionary = this; |
471 // LOG(WARNING) << "\n2\n"; | 471 // LOG(WARNING) << "\n2\n"; |
472 for (size_t delimiter_position = current_path.find('.'); | 472 for (size_t delimiter_position = current_path.find('.'); |
473 delimiter_position != std::string::npos; | 473 delimiter_position != std::string::npos; |
474 delimiter_position = current_path.find('.')) { | 474 delimiter_position = current_path.find('.')) { |
475 const DictionaryValue* child_dictionary = NULL; | 475 const DictionaryValue* child_dictionary = NULL; |
476 if (!current_dictionary->GetDictionary( | 476 if (!current_dictionary->GetDictionary( |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 | 748 |
749 Value* entry = entry_iterator->second; | 749 Value* entry = entry_iterator->second; |
750 if (out_value) | 750 if (out_value) |
751 out_value->reset(entry); | 751 out_value->reset(entry); |
752 else | 752 else |
753 delete entry; | 753 delete entry; |
754 dictionary_.erase(entry_iterator); | 754 dictionary_.erase(entry_iterator); |
755 return true; | 755 return true; |
756 } | 756 } |
757 | 757 |
| 758 bool DictionaryValue::RemovePath(const std::string& path, |
| 759 scoped_ptr<Value>* out_value) { |
| 760 bool result = false; |
| 761 size_t delimiter_position = path.find('.'); |
| 762 |
| 763 if (delimiter_position == std::string::npos) |
| 764 return RemoveWithoutPathExpansion(path, out_value); |
| 765 |
| 766 const std::string subdict_path = path.substr(0, delimiter_position); |
| 767 DictionaryValue* subdict = NULL; |
| 768 if (!GetDictionary(subdict_path, &subdict)) |
| 769 return false; |
| 770 result = subdict->RemovePath(path.substr(delimiter_position + 1), |
| 771 out_value); |
| 772 if (result && subdict->empty()) |
| 773 RemoveWithoutPathExpansion(subdict_path, NULL); |
| 774 |
| 775 return result; |
| 776 } |
| 777 |
758 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const { | 778 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const { |
759 Value* copy = CopyWithoutEmptyChildren(this); | 779 Value* copy = CopyWithoutEmptyChildren(this); |
760 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 780 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
761 } | 781 } |
762 | 782 |
763 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 783 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
764 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { | 784 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
765 const Value* merge_value = &it.value(); | 785 const Value* merge_value = &it.value(); |
766 // Check whether we have to merge dictionaries. | 786 // Check whether we have to merge dictionaries. |
767 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 787 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1113 | 1133 |
1114 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1134 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1115 std::string json; | 1135 std::string json; |
1116 JSONWriter::WriteWithOptions(&value, | 1136 JSONWriter::WriteWithOptions(&value, |
1117 JSONWriter::OPTIONS_PRETTY_PRINT, | 1137 JSONWriter::OPTIONS_PRETTY_PRINT, |
1118 &json); | 1138 &json); |
1119 return out << json; | 1139 return out << json; |
1120 } | 1140 } |
1121 | 1141 |
1122 } // namespace base | 1142 } // namespace base |
OLD | NEW |