-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
189 lines (174 loc) · 7.53 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// CGen
// https://cgen.sophisticatedways.net
// Copyright © 2018-2020 Volodymyr Skladanivskyy. All rights reserved.
// Published under terms of MIT license.
//
#include <stdexcept>
#include <iostream>
#include "shared.hpp"
#include "commandline.hpp"
#include "commands.hpp"
void print_version() {
std::cout << APP_TITLE << " version " << APP_VERSION << std::endl;
};
void print_help() {
print_version();
std::cout << APP_DESCRIPTION << std::endl;
std::cout << APP_USAGE_SHORT;
std::cout << APP_USAGE_LONG;
};
void generate_output_file_name_var(CGenCommandInfo& info, const std::string variable_name, std::string& output_file_name) {
auto map_entry = info.variables_map.find(variable_name);
if (map_entry != info.variables_map.end()) {
std::string prefix = variable_name;
transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower);
output_file_name += prefix;
const CGenVariableInfo* p_value = &(map_entry->second);
if (p_value->except_count > 0) {
output_file_name += "v" + std::to_string(p_value->except_count);
} else if (p_value->except_range_size) {
output_file_name += "v" + std::to_string(p_value->except_range_size);
} else {
output_file_name += "c";
};
};
};
void generate_output_file_name(CGenCommandInfo& info) {
if ((info.command == cmdEncode || info.command == cmdProcess) && info.output_file_name.empty()) {
std::string output_file_name;
if (info.command == cmdEncode) {
switch(info.algorithm) {
case algSHA1:
output_file_name = "sha1";
break;
case algSHA256:
output_file_name = "sha256";
break;
default:
assert(false);
};
output_file_name += "r" + std::to_string(info.rounds);
generate_output_file_name_var(info, "M", output_file_name);
generate_output_file_name_var(info, "H", output_file_name);
if (info.mode == bal::fpmUnoptimized) {
output_file_name += "_u";
};
} else if (info.command == cmdProcess) {
// generate only if there is a conversion
if (info.formula_type == ftCnf && (info.output_format == ofCnfVIGGraphML ||
info.output_format == ofCnfWeightedVIGGraphML ||
info.output_format == ofCnfVIGGEXF)) {
output_file_name = info.input_file_name;
};
};
if (!output_file_name.empty()) {
switch(info.output_format) {
case ofAnfPolybori:
output_file_name += ".anf";
break;
case ofCnfDimacs:
output_file_name += ".cnf";
break;
case ofCnfVIGGraphML:
output_file_name += ".graphml";
break;
case ofCnfWeightedVIGGraphML:
output_file_name += "_w.graphml";
break;
case ofCnfVIGGEXF:
output_file_name += ".gexf";
break;
default:
assert(false);
};
info.output_file_name = output_file_name;
};
};
};
int main(int argc, const char * argv[]) {
try {
CGenCommandInfo info;
CGenCommandLineReader command_line_reader(argc, argv);
command_line_reader.parse(info);
switch(info.command) {
case cmdNone:
std::cout << ERROR_COMMAND_NONE << std::endl;
print_help();
case cmdEncode:
if (info.algorithm == algNone) {
throw std::invalid_argument(ERROR_MISSING_ALGORITHM);
};
generate_output_file_name(info);
if (info.output_file_name.empty()) {
throw std::invalid_argument(ERROR_MISSING_OUTPUT_FILE_NAME);
};
_assert_level_1(info.b_formula_type_specified);
std::cout << "Encoding " << (info.algorithm == algSHA1 ? "SHA-1" : "SHA-256");
std::cout << " into " << get_formula_type_title(info.formula_type) << std::endl;
if (info.formula_type == ftCnf) {
encode_cnf(info.algorithm, info.rounds,
info.variables_map,
info.add_max_args, info.xor_max_args,
info.output_file_name.c_str(),
info.output_format, info.trace_format,
info.b_reindex_variables,
info.b_normalize_variables_specified,
info.b_assign_after_encoding,
info.mode);
} else if (info.formula_type == ftAnf) {
encode_anf(info.algorithm, info.rounds,
info.variables_map,
info.add_max_args, info.xor_max_args,
info.output_file_name.c_str(),
info.output_format, info.trace_format,
info.b_reindex_variables,
info.b_normalize_variables_specified,
info.b_assign_after_encoding,
info.mode);
} else {
_assert_level_1(false);
};
break;
case cmdProcess:
_assert_level_1(info.b_formula_type_specified);
generate_output_file_name(info);
std::cout << "Processing " << get_formula_type_title(info.formula_type) << " formula" << std::endl;
if (info.formula_type == ftCnf) {
process_cnf(info.variables_map,
info.input_file_name.data(), info.output_file_name.data(),
info.output_format, info.trace_format,
info.b_reindex_variables, info.b_normalize_variables_specified, info.mode);
} else if (info.formula_type == ftAnf) {
process_anf(info.variables_map,
info.input_file_name.data(), info.output_file_name.data(),
info.output_format, info.trace_format,
info.b_reindex_variables, info.b_normalize_variables_specified, info.mode);
} else {
_assert_level_1(false);
};
break;
case cmdHelp:
print_help();
break;
case cmdVersion:
print_version();
break;
};
}
catch (TextReaderException e) {
std::cerr << ERROR_COMMAND_LINE_PARSE << ": " << e.get_line() << std::endl;
std::cerr << "Position " << e.get_pos() << ": " << e.get_message() << std::endl;
std::cerr << "See --help for reference" << std::endl;
return 1;
}
catch (std::invalid_argument e) {
std::cerr << ERROR_INVALID_ARGUMENT << ": " << e.what() << std::endl;
return 1;
}
catch (std::out_of_range e) {
std::cerr << ERROR_OUT_OF_RANGE << ": " << e.what() << std::endl;
return 1;
};
return 0;
}