forked from babenek/base91x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase91.cpp
77 lines (63 loc) · 1.56 KB
/
base91.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
#include <cstring>
#include <iostream>
#include <fstream>
#include "base91.h"
//------------------------------------------------------------------------------
template<typename Container>
void
readFile(const std::string & fileName, Container &container)
{
std::fstream fs(fileName, std::ios::in | std::ios::binary);
if (fs.is_open() and fs.good())
{
fs.seekg(0, std::ios::end);
auto newSize = fs.tellp();
fs.seekg(0, std::ios::beg);
container.clear();
container.resize(newSize);
fs.read(&container[0], newSize);
fs.close();
}
return;
}
//------------------------------------------------------------------------------
template<typename Container>
void
writeFile(const std::string & fileName, const Container &container)
{
std::fstream fs(fileName, std::ios::trunc | std::ios::out | std::ios::binary);
if (fs.good())
{
fs.write(container.data(), container.size());
fs.close();
}
return;
}
//------------------------------------------------------------------------------
int
main(const int argc, const char *argv[])
{
if (argc!=4 or not(0==strncmp("-e", argv[1], 3) or 0==strncmp("-d", argv[1], 3)))
{
std::cerr << "base91 -e|-d <in-file> <out-file>";
return -1;
}
if (0==strncmp("-e", argv[1], 3))
{
std::vector<char> in;
std::string out;
readFile(argv[2], in);
base91::encode(in, out);
writeFile(argv[3], out);
}
else
{
std::string in;
std::vector<char> out;
readFile(argv[2], in);
base91::decode(in, out);
writeFile(argv[3], out);
}
return 0;
}
//------------------------------------------------------------------------------