1 // Copyright (c) 2009 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_map.h"
8
9 #include "base/logging.h"
10
11 namespace {
12 using StringMap = std::map<CefString, CefString>;
13 } // namespace
14
cef_string_map_alloc()15 CEF_EXPORT cef_string_map_t cef_string_map_alloc() {
16 return new StringMap;
17 }
18
cef_string_map_size(cef_string_map_t map)19 CEF_EXPORT size_t cef_string_map_size(cef_string_map_t map) {
20 DCHECK(map);
21 StringMap* impl = reinterpret_cast<StringMap*>(map);
22 return impl->size();
23 }
24
cef_string_map_find(cef_string_map_t map,const cef_string_t * key,cef_string_t * value)25 CEF_EXPORT int cef_string_map_find(cef_string_map_t map,
26 const cef_string_t* key,
27 cef_string_t* value) {
28 DCHECK(map);
29 DCHECK(value);
30 StringMap* impl = reinterpret_cast<StringMap*>(map);
31 StringMap::const_iterator it = impl->find(CefString(key));
32 if (it == impl->end())
33 return 0;
34
35 const CefString& val = it->second;
36 return cef_string_set(val.c_str(), val.length(), value, true);
37 }
38
cef_string_map_key(cef_string_map_t map,size_t index,cef_string_t * key)39 CEF_EXPORT int cef_string_map_key(cef_string_map_t map,
40 size_t index,
41 cef_string_t* key) {
42 DCHECK(map);
43 DCHECK(key);
44 StringMap* impl = reinterpret_cast<StringMap*>(map);
45 DCHECK_LT(index, impl->size());
46 if (index >= impl->size())
47 return 0;
48
49 StringMap::const_iterator it = impl->begin();
50 for (size_t ct = 0; it != impl->end(); ++it, ct++) {
51 if (ct == index)
52 return cef_string_set(it->first.c_str(), it->first.length(), key, true);
53 }
54 return 0;
55 }
56
cef_string_map_value(cef_string_map_t map,size_t index,cef_string_t * value)57 CEF_EXPORT int cef_string_map_value(cef_string_map_t map,
58 size_t index,
59 cef_string_t* value) {
60 DCHECK(map);
61 DCHECK(value);
62 StringMap* impl = reinterpret_cast<StringMap*>(map);
63 DCHECK_LT(index, impl->size());
64 if (index >= impl->size())
65 return 0;
66
67 StringMap::const_iterator it = impl->begin();
68 for (size_t ct = 0; it != impl->end(); ++it, ct++) {
69 if (ct == index) {
70 return cef_string_set(it->second.c_str(), it->second.length(), value,
71 true);
72 }
73 }
74 return 0;
75 }
76
cef_string_map_append(cef_string_map_t map,const cef_string_t * key,const cef_string_t * value)77 CEF_EXPORT int cef_string_map_append(cef_string_map_t map,
78 const cef_string_t* key,
79 const cef_string_t* value) {
80 DCHECK(map);
81 StringMap* impl = reinterpret_cast<StringMap*>(map);
82 impl->insert(std::make_pair(CefString(key), CefString(value)));
83 return 1;
84 }
85
cef_string_map_clear(cef_string_map_t map)86 CEF_EXPORT void cef_string_map_clear(cef_string_map_t map) {
87 DCHECK(map);
88 StringMap* impl = reinterpret_cast<StringMap*>(map);
89 impl->clear();
90 }
91
cef_string_map_free(cef_string_map_t map)92 CEF_EXPORT void cef_string_map_free(cef_string_map_t map) {
93 DCHECK(map);
94 StringMap* impl = reinterpret_cast<StringMap*>(map);
95 delete impl;
96 }
97