monotone

monotone Mtn Source Tree

Root/botan/datastor.cpp

1/*************************************************
2* Data Store Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/datastor.h>
7#include <botan/filters.h>
8#include <botan/parsing.h>
9#include <botan/stl_util.h>
10
11namespace Botan {
12
13/*************************************************
14* Default Matcher transform operation (identity) *
15*************************************************/
16std::pair<std::string, std::string>
17Data_Store::Matcher::transform(const std::string& key,
18 const std::string& value) const
19 {
20 return std::make_pair(key, value);
21 }
22
23/*************************************************
24* Data_Store Equality Comparison *
25*************************************************/
26bool Data_Store::operator==(const Data_Store& other) const
27 {
28 return (contents == other.contents);
29 }
30
31/*************************************************
32* Check if this key has at least one value *
33*************************************************/
34bool Data_Store::has_value(const std::string& key) const
35 {
36 return (contents.lower_bound(key) != contents.end());
37 }
38
39/*************************************************
40* Search based on an arbitrary predicate *
41*************************************************/
42std::multimap<std::string, std::string>
43Data_Store::search_with(const Matcher& matcher) const
44 {
45 std::multimap<std::string, std::string> out;
46
47 std::multimap<std::string, std::string>::const_iterator i =
48 contents.begin();
49
50 while(i != contents.end())
51 {
52 if(matcher(i->first, i->second))
53 out.insert(matcher.transform(i->first, i->second));
54 ++i;
55 }
56
57 return out;
58 }
59
60/*************************************************
61* Search based on key equality *
62*************************************************/
63std::vector<std::string> Data_Store::get(const std::string& looking_for) const
64 {
65 typedef std::multimap<std::string, std::string>::const_iterator iter;
66
67 std::pair<iter, iter> range = contents.equal_range(looking_for);
68
69 std::vector<std::string> out;
70 for(iter i = range.first; i != range.second; ++i)
71 out.push_back(i->second);
72 return out;
73 }
74
75/*************************************************
76* Get a single atom *
77*************************************************/
78std::string Data_Store::get1(const std::string& key) const
79 {
80 std::vector<std::string> vals = get(key);
81
82 if(vals.empty())
83 throw Invalid_State("Data_Store::get1: Not values for " + key);
84 if(vals.size() > 1)
85 throw Invalid_State("Data_Store::get1: More than one value for " + key);
86
87 return vals[0];
88 }
89
90/*************************************************
91* Get a single MemoryVector atom *
92*************************************************/
93MemoryVector<byte>
94Data_Store::get1_memvec(const std::string& key) const
95 {
96 std::vector<std::string> vals = get(key);
97
98 if(vals.size() > 1)
99 throw Invalid_State("Data_Store::get1_memvec: Multiple values for " +
100 key);
101
102 if(vals.empty())
103 return MemoryVector<byte>();
104
105 Pipe pipe(new Hex_Decoder(FULL_CHECK));
106 pipe.start_msg();
107 if(vals.size())
108 pipe.write(vals[0]);
109 pipe.end_msg();
110 return pipe.read_all();
111 }
112
113/*************************************************
114* Get a single u32bit atom *
115*************************************************/
116u32bit Data_Store::get1_u32bit(const std::string& key,
117 u32bit default_val) const
118 {
119 std::vector<std::string> vals = get(key);
120
121 if(vals.empty())
122 return default_val;
123 else if(vals.size() > 1)
124 throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " +
125 key);
126
127 return to_u32bit(vals[0]);
128 }
129
130/*************************************************
131* Insert a single key and value *
132*************************************************/
133void Data_Store::add(const std::string& key, const std::string& val)
134 {
135 multimap_insert(contents, key, val);
136 }
137
138/*************************************************
139* Insert a single key and value *
140*************************************************/
141void Data_Store::add(const std::string& key, u32bit val)
142 {
143 add(key, to_string(val));
144 }
145
146/*************************************************
147* Insert a single key and value *
148*************************************************/
149void Data_Store::add(const std::string& key, const MemoryRegion<byte>& val)
150 {
151 Pipe pipe(new Hex_Encoder);
152 pipe.process_msg(val);
153 add(key, pipe.read_all_as_string());
154 }
155
156/*************************************************
157* Insert a mapping of key/value pairs *
158*************************************************/
159void Data_Store::add(const std::multimap<std::string, std::string>& in)
160 {
161 std::multimap<std::string, std::string>::const_iterator i = in.begin();
162 while(i != in.end())
163 {
164 contents.insert(*i);
165 ++i;
166 }
167 }
168
169}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status