-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFileEncoder.cpp
83 lines (73 loc) · 1.78 KB
/
FileEncoder.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
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
#include "FileEncoder.hpp"
#include <cctype>
#include <fstream>
#include <sstream>
namespace before
{
bool write(const std::string& filePath, const std::string& content)
{
std::ofstream outfile(filePath.c_str(), std::ios::trunc);
if (outfile.good())
{
outfile << content << std::endl;
}
return outfile.good();
}
std::optional<std::string> read(const std::string& filePath)
{
std::ifstream fileToRead(filePath);
std::stringstream buffer;
buffer << fileToRead.rdbuf();
if (buffer.good())
{
return std::make_optional(buffer.str());
}
return std::nullopt;
}
bool FileEncoder::encode(const std::string& filePath) const
{
const auto validFileContents = read(filePath);
if (!validFileContents)
{
return false;
}
auto encodedFileContents = validFileContents.value();
for (auto& c : encodedFileContents)
{
if (std::isalnum(c))
{
c++;
}
}
const auto wroteFileSuccessfully
= write(filePath + ".encoded", encodedFileContents);
return wroteFileSuccessfully;
}
} // namespace before
namespace after
{
FileEncoder::FileEncoder(FileReader& fileReader, FileWriter& fileWriter)
: mFileReader{fileReader}
, mFileWriter{fileWriter}
{
}
bool FileEncoder::encode(const std::string& filePath) const
{
const auto validFileContents = mFileReader.read(filePath);
if (!validFileContents)
{
return false;
}
auto encodedFileContents = validFileContents.value();
for (auto& c : encodedFileContents)
{
if (std::isalnum(c))
{
c++;
}
}
const auto wroteFileSuccessfully
= mFileWriter.write(filePath + ".encoded", encodedFileContents);
return wroteFileSuccessfully;
}
} // namespace after