-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·107 lines (87 loc) · 2.54 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <io.h>
#include <fcntl.h>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include "memoryManager.h"
#include "scrollReader.h"
#include "array.h"
#include "context.h"
using namespace std;
using namespace boost;
using namespace boost::filesystem;
void usage(ostream & os);
int main(int argc, const char * argv[])
{
if (argc != 2)
{
usage(cerr);
return 2;
}
if (_setmode(_fileno(stdin), _O_BINARY) == -1)
{
cerr << "Error: _setmode(stdin, BINARY) failed: " << errno << endl;
return 2;
}
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
{
cerr << "Error: _setmode(stdout, BINARY) failed: " << errno << endl;
return 2;
}
try
{
path scrollPath(argv[1]);
if (!exists(scrollPath))
{
cerr << "Error: Scroll '" << scrollPath << "' not found." << endl;
usage(cerr);
return 2;
}
if (!is_regular_file(scrollPath))
{
cerr << "Error: Scroll '" << scrollPath << "' is not a regular "
"\"file\"." << endl;
usage(cerr);
return 2;
}
uintmax_t scrollSize = file_size(scrollPath);
if (scrollSize == static_cast<uintmax_t>(-1))
{
cerr << "Error: Failed to obtains '" << scrollPath << "' size."
<< endl;
usage(cerr);
return 2;
}
filesystem::ifstream scroll;
/*
* Without ios::binary MS Standard Library implementation will treat 1A
* as an EOF byte. And, probably, '\r\n' is transformed into '\n'?
*/
scroll.open(scrollPath, ios::in | ios::binary);
if (!scroll.is_open())
{
cerr << "Error: Failed to open '" << scrollPath << "'." << endl;
usage(cerr);
return 2;
}
memoryManager mm;
::array * zeroArray =
scrollReader::readLegacy(mm, scroll,
static_cast<size_t>(scrollSize));
context ctx(mm, cin, cout, zeroArray);
ctx.run();
}
catch (const std::exception & e)
{
cerr << "Error: " << e.what() << endl;
return 2;
}
return 0;
}
void usage(ostream & os)
{
os << "Usage:" << endl
<< " um <\"program\" scroll file name>" << endl;
}