Skip to content

Commit 6ac7b7e

Browse files
committed
add export-formulas command
1 parent f68161d commit 6ac7b7e

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Integrate `libcurl` for HTTP requests, replacing external `curl`/`wget` tools
66
* Integrate `zlib` for gzip decompression, replacing external `gzip`/`gunzip` tools
77
* Report broken b-files to LODA API server for automatic cache invalidation
8+
* Add internal `export-formulas` command to export all program formulas to a file or stdout
89

910
### Bugfixes
1011

src/cmd/commands.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,40 @@ void Commands::compare(const std::string& path1, const std::string& path2) {
11881188
p1, p2, "First", "Second", seq, full_check, num_usages));
11891189
}
11901190

1191+
void Commands::exportFormulas(const std::string& output_file) {
1192+
initLog(true);
1193+
Parser parser;
1194+
MineManager manager(settings);
1195+
manager.load();
1196+
auto& stats = manager.getStats();
1197+
std::ostream* out = &std::cout;
1198+
std::ofstream file_out;
1199+
if (!output_file.empty()) {
1200+
file_out.open(output_file);
1201+
if (!file_out.good()) {
1202+
Log::get().error("Cannot open output file: " + output_file, true);
1203+
}
1204+
out = &file_out;
1205+
}
1206+
for (auto id : stats.all_program_ids) {
1207+
try {
1208+
Program program = parser.parse(ProgramUtil::getProgramPath(id));
1209+
std::string formula =
1210+
Comments::getCommentField(program, Comments::PREFIX_FORMULA);
1211+
1212+
if (!formula.empty()) {
1213+
*out << id.string() << ": " << formula << std::endl;
1214+
}
1215+
} catch (const std::exception& e) {
1216+
Log::get().warn("Error processing " + id.string() + ": " +
1217+
std::string(e.what()));
1218+
}
1219+
}
1220+
if (file_out.is_open()) {
1221+
file_out.close();
1222+
}
1223+
}
1224+
11911225
void Commands::commitAddedPrograms(size_t min_commit_count) {
11921226
initLog(true);
11931227
SequenceProgram::commitAddedPrograms(min_commit_count);

src/cmd/commands.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class Commands {
9191

9292
void addToList(const std::string& seq_id, const std::string& list_filename);
9393

94+
void exportFormulas(const std::string& output_file);
95+
9496
void commitAddedPrograms(size_t min_commit_count = 5);
9597

9698
void commitUpdatedAndDeletedPrograms();

src/cmd/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ int dispatch(Settings settings, const std::vector<std::string>& args) {
293293
commands.compare(args.at(1), args.at(2));
294294
} else if (cmd == "replace") {
295295
commands.replace(args.at(1), args.at(2));
296+
} else if (cmd == "export-formulas") {
297+
std::string output_file;
298+
if (args.size() > 1) {
299+
output_file = args.at(1);
300+
}
301+
commands.exportFormulas(output_file);
296302
} else if (cmd == "auto-fold") {
297303
commands.autoFold();
298304
} else if (cmd == "add-programs") {

0 commit comments

Comments
 (0)