-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.cc
More file actions
60 lines (50 loc) · 1.99 KB
/
Copy pathexample.cc
File metadata and controls
60 lines (50 loc) · 1.99 KB
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
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include "lua_bytecode_parser.h"
#include "cpptrace/cpptrace.hpp"
#include "cpptrace/from_current.hpp"
int main(int argc, char* argv[]) {
if (argc < 2 || argc > 3) {
std::cerr << "Usage: " << argv[0] << " <input_lua_bytecode_file.luac> [output_lua_bytecode_file.luac]" << std::endl;
return 1;
}
std::string input_filename = argv[1];
std::ifstream input_file(input_filename, std::ios::binary);
if (!input_file.is_open()) {
std::cerr << "Error: Could not open input file " << input_filename << std::endl;
return 1;
}
std::vector<char> bytecode_data(
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>()
);
input_file.close();
CPPTRACE_TRY {
LuaBytecodeParser parser(bytecode_data);
std::shared_ptr<LuaProto> main_proto = parser.parse();
if (argc == 3) {
std::string output_filename = argv[2];
std::ofstream output_file(output_filename, std::ios::binary);
if (!output_file.is_open()) {
std::cerr << "Error: Could not open output file " << output_filename << std::endl;
throw std::runtime_error("Output file open error");
}
LuaBytecodeWriter writer(output_file);
writer.write(*main_proto);
output_file.close();
std::cout << "Successfully parsed bytecode from " << input_filename
<< " and wrote to " << output_filename << std::endl;
} else {
LuaBytecodeFormatter formatter(std::cout);
formatter.format(*main_proto);
std::cout << "Successfully parsed and formatted bytecode from " << input_filename << std::endl;
}
} CPPTRACE_CATCH (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
cpptrace::from_current_exception().print();
return 1;
}
return 0;
}