1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include <map>
6
7 #include "include/internal/cef_string_multimap.h"
8
9 #include "base/logging.h"
10
11 namespace {
12 using StringMultimap = std::multimap<CefString, CefString>;
13 } // namespace
14
cef_string_multimap_alloc()15 CEF_EXPORT cef_string_multimap_t cef_string_multimap_alloc() {
16 return new StringMultimap;
17 }
18
cef_string_multimap_size(cef_string_multimap_t map)19 CEF_EXPORT size_t cef_string_multimap_size(cef_string_multimap_t map) {
20 DCHECK(map);
21 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
22 return impl->size();
23 }
24
cef_string_multimap_find_count(cef_string_multimap_t map,const cef_string_t * key)25 CEF_EXPORT size_t cef_string_multimap_find_count(cef_string_multimap_t map,
26 const cef_string_t* key) {
27 DCHECK(map);
28 DCHECK(key);
29 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
30 return impl->count(CefString(key));
31 }
32
cef_string_multimap_enumerate(cef_string_multimap_t map,const cef_string_t * key,size_t value_index,cef_string_t * value)33 CEF_EXPORT int cef_string_multimap_enumerate(cef_string_multimap_t map,
34 const cef_string_t* key,
35 size_t value_index,
36 cef_string_t* value) {
37 DCHECK(map);
38 DCHECK(key);
39 DCHECK(value);
40
41 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
42 CefString key_str(key);
43
44 DCHECK_LT(value_index, impl->count(key_str));
45 if (value_index >= impl->count(key_str))
46 return 0;
47
48 std::pair<StringMultimap::iterator, StringMultimap::iterator> range_it =
49 impl->equal_range(key_str);
50
51 size_t count = value_index;
52 while (count-- && range_it.first != range_it.second)
53 range_it.first++;
54
55 if (range_it.first == range_it.second)
56 return 0;
57
58 const CefString& val = range_it.first->second;
59 return cef_string_set(val.c_str(), val.length(), value, true);
60 }
61
cef_string_multimap_key(cef_string_multimap_t map,size_t index,cef_string_t * key)62 CEF_EXPORT int cef_string_multimap_key(cef_string_multimap_t map,
63 size_t index,
64 cef_string_t* key) {
65 DCHECK(map);
66 DCHECK(key);
67 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
68 DCHECK_LT(index, impl->size());
69 if (index >= impl->size())
70 return 0;
71
72 StringMultimap::const_iterator it = impl->begin();
73 for (size_t ct = 0; it != impl->end(); ++it, ct++) {
74 if (ct == index)
75 return cef_string_set(it->first.c_str(), it->first.length(), key, true);
76 }
77 return 0;
78 }
79
cef_string_multimap_value(cef_string_multimap_t map,size_t index,cef_string_t * value)80 CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map,
81 size_t index,
82 cef_string_t* value) {
83 DCHECK(map);
84 DCHECK(value);
85 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
86 DCHECK_LT(index, impl->size());
87 if (index >= impl->size())
88 return 0;
89
90 StringMultimap::const_iterator it = impl->begin();
91 for (size_t ct = 0; it != impl->end(); ++it, ct++) {
92 if (ct == index) {
93 return cef_string_set(it->second.c_str(), it->second.length(), value,
94 true);
95 }
96 }
97 return 0;
98 }
99
cef_string_multimap_append(cef_string_multimap_t map,const cef_string_t * key,const cef_string_t * value)100 CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map,
101 const cef_string_t* key,
102 const cef_string_t* value) {
103 DCHECK(map);
104 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
105 impl->insert(std::make_pair(CefString(key), CefString(value)));
106 return 1;
107 }
108
cef_string_multimap_clear(cef_string_multimap_t map)109 CEF_EXPORT void cef_string_multimap_clear(cef_string_multimap_t map) {
110 DCHECK(map);
111 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
112 impl->clear();
113 }
114
cef_string_multimap_free(cef_string_multimap_t map)115 CEF_EXPORT void cef_string_multimap_free(cef_string_multimap_t map) {
116 DCHECK(map);
117 StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
118 delete impl;
119 }
120