From 748d1c2ff3afde4efc4ab3057af6767a89ef1182 Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Thu, 1 Feb 2024 03:44:09 +0300 Subject: [PATCH] Update. --- src/script2txt2/script2txt2.cpp | 78 ++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/src/script2txt2/script2txt2.cpp b/src/script2txt2/script2txt2.cpp index 429bbbd..7512f81 100644 --- a/src/script2txt2/script2txt2.cpp +++ b/src/script2txt2/script2txt2.cpp @@ -22,6 +22,8 @@ #include #include +#include + #pragma pack(push, 1) struct script { uint32_t file_size; @@ -36,42 +38,66 @@ int main(int argc, char *argv[]) { cl::ParseCommandLineOptions(argc, argv); - auto func = [](auto &&fn) { - primitives::templates2::mmap_file f{fn}; + auto func = [](auto filename) { + primitives::templates2::mmap_file f{filename}; stream s{f}; script scr = s; - auto text = s.span(scr.raw_text_size); - uint32_t max_nlines = s; - auto rest = s.span(max_nlines); std::vector lines; - lines.reserve(scr.nlines); + int sz{}; for (int i = 0; i < scr.nlines; ++i) { - lines.emplace_back((const char *)f.p + sizeof(script) + rest[i]); + sz += lines.emplace_back((const char *)f.p + sizeof(script) + sz).size() + 1; } - int a = 5; - a++; // write script - /*{ - filename += ".txt"; - std::ofstream ofile(filename); - if (ofile) - ofile << ctx.getText(); - } - - // write function calls { - std::ofstream functions("functions.txt", std::ios::app); - if (functions) - { - for (auto &f : driver.functions) - { - std::string f2(f.size(), 0); - std::transform(f.begin(), f.end(), f2.begin(), tolower); - functions << f2 << "\n"; + filename += ".txt"; + if (std::ofstream ofile(filename); ofile) { + std::string indent, space = " "s; + auto inc = [&]() { + indent += space; + }; + auto dec = [&]() { + if (!indent.empty()) { + indent.resize(indent.size() - space.size()); + return true; + } + return false; + }; + int procs{}; + for (auto &&l : lines) { + auto proc = l.starts_with("PROC"sv); + auto end = l == "END"sv; + auto lbrace = l == "{"sv; + auto rbrace = l == "}"sv; + + if (rbrace) { + if (!dec()) { + ofile << "// script2txt2 comment: unbalanced!\n"; + } + } + if (end && procs) { + if (!dec()) { + ofile << "// script2txt2 comment: unbalanced!\n"; + } + } + + ofile << indent << l << "\n"; + + if (end && indent.empty()) { + //ofile << "\n"; + } + if (end && procs) { + procs = 0; + } + if (lbrace || proc) { + indent += space; + } + if (proc) { + procs = 1; + } } } - }*/ + } }; if (fs::is_regular_file(p)) {