-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.h
52 lines (43 loc) · 1.43 KB
/
filesystem.h
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
#pragma once
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <fstream>
#include "common.h"
namespace filesystem {
class StreamFactory {
public:
virtual ~StreamFactory() {}
virtual std::unique_ptr<std::istream> makeInputStream(const std::string& path) = 0;
/*
* Client assumes empty file.
*/
virtual std::unique_ptr<std::ostream> makeOutputStream(const std::string& path) = 0;
};
class FileStreamFactory : public StreamFactory {
public:
std::unique_ptr<std::istream> makeInputStream(const std::string& path);
std::unique_ptr<std::ostream> makeOutputStream(const std::string& path);
};
class FileStore : public common::KeyValueStore {
public:
FileStore(StreamFactory& streamFactory, const std::string& path);
void set(const std::string& key, const std::string& value);
std::string get(const std::string& key);
private:
static bool beginsWith(const std::string& line, const std::string& key);
static std::string extractPartAfterDelimiter(const std::string& line);
static std::pair<std::string, std::string> splitIntoKeyAndValue(
const std::string& line);
std::map<std::string, std::string> readEntireFile();
void writeEntireFile(std::map<std::string, std::string> entireFile);
std::string readFirstOccurenceOf(const std::string& key);
private:
static const size_t MAX_LINE_LEN{500};
StreamFactory& streamFactory;
std::string path;
char line[MAX_LINE_LEN];
};
} // namespace filesystem