-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanza.cpp
54 lines (41 loc) · 893 Bytes
/
stanza.cpp
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
#include <iostream>
#include "stanza.h"
int Stanza::parse_header(const std::string &line)
{
std::string key, val;
size_t pos;
pos = line.find(':');
if (pos == std::string::npos)
return -1;
key = line.substr(0, pos);
val = line.substr(pos + 2);
fields[key] = val;
return 0;
}
std::string Stanza::operator[](const std::string &key)
{
return fields[key.data()];
}
std::map<std::string, std::string>::iterator Stanza::begin() {
return fields.begin();
};
std::map<std::string, std::string>::iterator Stanza::end() {
return fields.end();
};
Stanza::Stanza(std::istream &in)
{
std::string line;
int ret;
if (in.fail()) {
throw std::invalid_argument("Failed to read a stanza.");
}
while (in.good()) {
std::getline(in, line);
if (line.length() == 0)
break;
ret = parse_header(line);
if (ret != 0) {
throw std::invalid_argument("Invalid input.");
}
}
}