2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <map>
|
2023-04-13 18:08:48 +02:00
|
|
|
#include <vector>
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/internal/cef_string_multimap.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2023-12-06 21:16:15 +01:00
|
|
|
#include "base/check_op.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
namespace {
|
2023-04-13 18:08:48 +02:00
|
|
|
|
|
|
|
class StringMultimap {
|
|
|
|
using Map = std::multimap<CefString, CefString>;
|
|
|
|
using value_type = Map::value_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using const_iterator = Map::const_iterator;
|
|
|
|
|
|
|
|
size_t size() const { return map_ref_.size(); }
|
|
|
|
size_t count(const CefString& key) const { return map_.count(key); }
|
|
|
|
const value_type operator[](size_t pos) const { return *map_ref_[pos]; }
|
|
|
|
|
|
|
|
std::pair<const_iterator, const_iterator> equal_range(
|
|
|
|
const CefString& key) const {
|
|
|
|
return map_.equal_range(key);
|
|
|
|
}
|
|
|
|
void insert(value_type&& value) {
|
|
|
|
auto it = map_.insert(std::move(value)); // does not invalidate iterators
|
|
|
|
map_ref_.push_back(std::move(it));
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
map_ref_.clear();
|
|
|
|
map_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Map map_;
|
|
|
|
std::vector<Map::const_iterator> map_ref_;
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
} // namespace
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CEF_EXPORT cef_string_multimap_t cef_string_multimap_alloc() {
|
2023-04-13 18:06:02 +02:00
|
|
|
return reinterpret_cast<cef_string_multimap_t>(new StringMultimap);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 19:38:59 +01:00
|
|
|
CEF_EXPORT size_t cef_string_multimap_size(cef_string_multimap_t map) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(map);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
return impl->size();
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:38:59 +01:00
|
|
|
CEF_EXPORT size_t cef_string_multimap_find_count(cef_string_multimap_t map,
|
2017-05-17 11:29:28 +02:00
|
|
|
const cef_string_t* key) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(map);
|
|
|
|
DCHECK(key);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
return impl->count(CefString(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_string_multimap_enumerate(cef_string_multimap_t map,
|
|
|
|
const cef_string_t* key,
|
2016-11-04 19:38:59 +01:00
|
|
|
size_t value_index,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_t* value) {
|
|
|
|
DCHECK(map);
|
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(value);
|
|
|
|
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
CefString key_str(key);
|
|
|
|
|
2016-11-04 19:38:59 +01:00
|
|
|
DCHECK_LT(value_index, impl->count(key_str));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (value_index >= impl->count(key_str)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-04-13 18:08:48 +02:00
|
|
|
std::pair<StringMultimap::const_iterator, StringMultimap::const_iterator>
|
|
|
|
range_it = impl->equal_range(key_str);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2016-11-04 19:38:59 +01:00
|
|
|
size_t count = value_index;
|
2023-01-02 23:59:03 +01:00
|
|
|
while (count-- && range_it.first != range_it.second) {
|
2012-04-03 03:34:16 +02:00
|
|
|
range_it.first++;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (range_it.first == range_it.second) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
const CefString& val = range_it.first->second;
|
|
|
|
return cef_string_set(val.c_str(), val.length(), value, true);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_multimap_key(cef_string_multimap_t map,
|
|
|
|
size_t index,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_t* key) {
|
|
|
|
DCHECK(map);
|
|
|
|
DCHECK(key);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
2016-11-04 19:38:59 +01:00
|
|
|
DCHECK_LT(index, impl->size());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (index >= impl->size()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-04-13 18:08:48 +02:00
|
|
|
const auto& [k, _] = (*impl)[index];
|
|
|
|
return cef_string_set(k.c_str(), k.length(), key, true);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map,
|
|
|
|
size_t index,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_t* value) {
|
|
|
|
DCHECK(map);
|
|
|
|
DCHECK(value);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
2016-11-04 19:38:59 +01:00
|
|
|
DCHECK_LT(index, impl->size());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (index >= impl->size()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-04-13 18:08:48 +02:00
|
|
|
const auto& [_, v] = (*impl)[index];
|
|
|
|
return cef_string_set(v.c_str(), v.length(), value, true);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map,
|
|
|
|
const cef_string_t* key,
|
|
|
|
const cef_string_t* value) {
|
|
|
|
DCHECK(map);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
impl->insert(std::make_pair(CefString(key), CefString(value)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_multimap_clear(cef_string_multimap_t map) {
|
|
|
|
DCHECK(map);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
impl->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_multimap_free(cef_string_multimap_t map) {
|
|
|
|
DCHECK(map);
|
|
|
|
StringMultimap* impl = reinterpret_cast<StringMultimap*>(map);
|
|
|
|
delete impl;
|
|
|
|
}
|