-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTextStorage.cpp
45 lines (32 loc) · 847 Bytes
/
TextStorage.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
//
// Created by Pavel Akhtyamov on 26/03/2018.
//
#include <fstream>
#include <sstream>
#include "TextStorage.h"
void TextStorage::Read(const std::string &filename) {
std::ifstream stream(filename);
std::string buffer;
while (std::getline(stream, buffer)) {
pairs_.push_back(ParseTokens(buffer));
}
stream.close();
}
void TextStorage::Write(const std::string &filename) {
std::ofstream stream(filename);
for (auto &pair : pairs_) {
stream << pair.key << " " << pair.value << "\n";
}
stream.close();
}
KeyValuePair TextStorage::ParseTokens(const std::string &input_string) {
std::stringstream tokenizer;
tokenizer << input_string;
std::string key;
std::string value;
tokenizer >> key >> value;
return { key, value };
}
std::vector<KeyValuePair> TextStorage::GetPairs() const {
return pairs_;
}