forked from lksj/einstein-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
140 lines (116 loc) · 3.99 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "compressor.h"
#include "exceptions.h"
#include "unicode.h"
#include "table.h"
#include <string.h>
static std::string sourceFile, outputFile;
static bool verbose = false;
static bool showDeps = false;
static void printHelp(int terminate)
{
std::cerr << "USAGE:" << std::endl;
std::cerr << " mkres [options]" << std::endl;
std::cerr << "OPTIONS:" << std::endl;
std::cerr << " --verbose print more messages" << std::endl;
std::cerr << " --deps print make(1) dependencies" << std::endl;
std::cerr << " --source <file> read source file" << std::endl;
std::cerr << " --output <file> write result to file" << std::endl;
std::cerr << " --version show version number" << std::endl;
std::cerr << " --help this help screen" << std::endl;
if (terminate >= 0)
exit(terminate);
}
static void printVersion(int terminate)
{
std::cout << "Resource File Compressor" << std::endl;
std::cout << "Version 1.2" << std::endl;
std::cout << "Copyright (c) 2003-2005 Alexander Babichev" << std::endl;
if (terminate >= 0)
exit(terminate);
}
static void parseArgs(int argc, char *argv[])
{
int i;
if (argc == 1)
printHelp(0);
for (i = 1; i < argc; i++) {
if (! argv[i])
continue;
if ((! strcmp(argv[i], "--source")) && (i < argc - 1))
sourceFile = std::string(argv[++i]);
else if ((! strcmp(argv[i], "--output")) && (i < argc - 1))
outputFile = std::string(argv[++i]);
else if (! strcmp(argv[i], "--help"))
printHelp(0);
else if (! strcmp(argv[i], "--version"))
printVersion(0);
else if (! strcmp(argv[i], "--verbose"))
verbose = true;
else if (! strcmp(argv[i], "--deps"))
showDeps = true;
else {
std::cerr << "Invalid option '" << argv[i] << "'" << std::endl;
printHelp(1);
}
}
if (! sourceFile.length()) {
std::cerr << "Source file not specified" << std::endl;
exit(1);
}
if (! outputFile.length()) {
std::cerr << "Output file not specified" << std::endl;
exit(1);
}
}
static ResourceCompressor compressor;
static void parseFile(const std::string &fileName)
{
Table table(fileName);
compressor.setPriority(table.getInt(L"priority", 1000));
Table *res = table.getTable(L"resources");
if (res) {
for (Table::Iterator i = res->begin(); i != res->end(); i++) {
Value *value = (*i).second;
if ((! value) || (! (Value::Table == value->getType())))
throw Exception(L"Invalid resource entry");
Table *t = value->asTable();
const std::wstring &name = t->getString(L"name");
if (0 >= name.length())
throw Exception(L"Unnamed resource entry");
const std::wstring &format = t->getString(L"format");
Formatter *formatter = formatRegistry.get(format);
if ((0 < format.length()) && (! formatter))
throw Exception(L"Unknown format '" + format + L"'");
compressor.add(Entry(name, t->getInt(L"compr", 9),
t->getString(L"group", L""),
t->getString(L"file", name), formatter));
}
}
}
int main(int argc, char *argv[])
{
int res = 0;
//lua_State *lua = NULL;
parseArgs(argc, argv);
try {
parseFile(sourceFile);
if (! showDeps)
compressor.compress(outputFile, verbose);
else
compressor.printDeps(outputFile, sourceFile);
} catch (Exception &e) {
std::cerr << e.getMessage() << std::endl;
res = 1;
} catch (std::string &s) {
std::cerr << s << std::endl;
res = 1;
} catch (...) {
std::cerr << L"Error: unknown exception" << std::endl;
res = 1;
}
/* if (lua) {
lua_setgcthreshold(lua, 0);
lua_close(lua);
}*/
return res;
}