blob: cdc84c6229ee327fd43a4b83217c5a5229b85eb0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*
* (C) 2018 Ribose Inc
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/parsing.h>
#include <botan/exceptn.h>
namespace Botan {
std::map<std::string, std::string> read_kv(const std::string& kv)
{
std::map<std::string, std::string> m;
if(kv == "")
return m;
std::vector<std::string> parts;
try
{
parts = split_on(kv, ',');
}
catch(std::exception&)
{
throw Invalid_Argument("Bad KV spec");
}
bool escaped = false;
bool reading_key = true;
std::string cur_key;
std::string cur_val;
for(char c : kv)
{
if(c == '\\' && !escaped)
{
escaped = true;
}
else if(c == ',' && !escaped)
{
if(cur_key.empty())
throw Invalid_Argument("Bad KV spec empty key");
if(m.find(cur_key) != m.end())
throw Invalid_Argument("Bad KV spec duplicated key");
m[cur_key] = cur_val;
cur_key = "";
cur_val = "";
reading_key = true;
}
else if(c == '=' && !escaped)
{
if(reading_key == false)
throw Invalid_Argument("Bad KV spec unexpected equals sign");
reading_key = false;
}
else
{
if(reading_key)
cur_key += c;
else
cur_val += c;
if(escaped)
escaped = false;
}
}
if(!cur_key.empty())
{
if(reading_key == false)
{
if(m.find(cur_key) != m.end())
throw Invalid_Argument("Bad KV spec duplicated key");
m[cur_key] = cur_val;
}
else
throw Invalid_Argument("Bad KV spec incomplete string");
}
return m;
}
}
|