From cc1d058826d164f1bdb88d9b0d8d2acf66e5d49a Mon Sep 17 00:00:00 2001 From: Sergey Dolgov Date: Mon, 2 Dec 2024 22:41:00 -0800 Subject: [PATCH] planner: infrastructure development --- planning/src/RS/rsOpts.cpp | 41 ++++++++++++---- planning/src/RS/rsOpts.h | 1 + planning/src/file_io/pln_Fio.cpp | 82 +++++++++++++++++++++++++------- planning/src/file_io/pln_Fio.h | 36 +++++++------- planning/src/main.cpp | 2 +- planning/src/util/geo/iv.h | 31 ++++-------- planning/src/util/geo/xyz.h | 12 +++-- planning/src/util/pln_log.h | 24 +++++++++- 8 files changed, 160 insertions(+), 69 deletions(-) diff --git a/planning/src/RS/rsOpts.cpp b/planning/src/RS/rsOpts.cpp index 1dbb6e7a..6312e5a6 100644 --- a/planning/src/RS/rsOpts.cpp +++ b/planning/src/RS/rsOpts.cpp @@ -82,6 +82,22 @@ static bool input_file_exists(CStr fn) noexcept { return sz > 1; // require non-empty file } +static inline bool ends_with_dot_cmd(CStr z, size_t len) noexcept { + assert(z); + if (len < 5) return false; + return z[len - 1] == 'd' and z[len - 2] == 'm' and z[len - 3] == 'c' and z[len - 4] == '.'; + // .cmd + // dmc. +} + +static inline bool ends_with_dot_tcl(CStr z, size_t len) noexcept { + assert(z); + if (len < 5) return false; + return z[len - 1] == 'l' and z[len - 2] == 'c' and z[len - 3] == 't' and z[len - 4] == '.'; + // .tcl + // lct. +} + static bool op_match(CStr op, CStr* aliases) noexcept { assert(op and aliases); if (!op || !aliases) return false; @@ -324,6 +340,23 @@ bool rsOpts::is_implicit_check() const noexcept { return true; } +bool rsOpts::isTclInput() const noexcept { + assert(argv_); + assert(argv_[0]); + assert(argc_ > 0); + if (!argv_ or !argv_[0]) + return false; + if (argc_ > 5 or assignOrder_) + return false; + + if (!input_ || !input_[0]) return false; + size_t len = ::strlen(input_); + if (len < 5 || len > UNIX_Path_Max) return false; + if (!ends_with_dot_tcl(input_, len)) return false; + + return input_file_exists(input_); +} + void rsOpts::parse(int argc, const char** argv) noexcept { using namespace ::pln::alias; @@ -568,14 +601,6 @@ bool rsOpts::createVprArgv(vector& W) noexcept { return true; } -static inline bool ends_with_dot_cmd(CStr z, size_t len) noexcept { - assert(z); - if (len < 5) return false; - return z[len - 1] == 'd' and z[len - 2] == 'm' and z[len - 3] == 'c' and z[len - 4] == '.'; - // .cmd - // dmc. -} - bool rsOpts::isCmdInput() const noexcept { if (!input_ || !input_[0]) return false; size_t len = ::strlen(input_); diff --git a/planning/src/RS/rsOpts.h b/planning/src/RS/rsOpts.h index b82e77eb..44ee4b7b 100644 --- a/planning/src/RS/rsOpts.h +++ b/planning/src/RS/rsOpts.h @@ -115,6 +115,7 @@ struct rsOpts { bool hasInputFile() const noexcept; bool hasCsvFile() const noexcept; bool isCmdInput() const noexcept; + bool isTclInput() const noexcept; private: bool set_VPR_TC_args(CStr raw_tc) noexcept; diff --git a/planning/src/file_io/pln_Fio.cpp b/planning/src/file_io/pln_Fio.cpp index f137397e..52fe5e75 100644 --- a/planning/src/file_io/pln_Fio.cpp +++ b/planning/src/file_io/pln_Fio.cpp @@ -60,6 +60,19 @@ void Info::init() noexcept { } } +void Info::inval() noexcept { + absName_.clear(); + type_.clear(); + size_ = 0; + exists_ = false; + accessible_ = false; + absolute_ = false; + numLines_ = -1; + numWords_ = -1; + maxLlen_ = -1; + numTextChars_ = -1; +} + string Info::get_abs_name(const string& nm) noexcept { namespace fs = std::filesystem; if (nm.empty()) return {}; @@ -549,7 +562,9 @@ int64_t MMapReader::countWC(int64_t& numWords) const noexcept { } // unix command 'wc' -int64_t MMapReader::printWC(std::ostream& os) const noexcept { +int64_t MMapReader::printWC(std::ostream& os, Info* infRet) const noexcept { + if (infRet) + infRet->inval(); assert(fsz_ == sz_); if (!fsz_ || !sz_ || !buf_ || fnm_.empty()) return -1; @@ -557,6 +572,10 @@ int64_t MMapReader::printWC(std::ostream& os) const noexcept { numLines = countWC(numWords); os << numLines << ' ' << numWords << ' ' << sz_ << ' ' << fnm_ << endl; + if (infRet) { + infRet->numLines_ = numLines; + infRet->numWords_ = numWords; + } return numLines; } @@ -571,8 +590,9 @@ static void print_byte(std::ostream& os, CStr prefix, int b) noexcept { os.flush(); } -bool MMapReader::is_text_file(string& label) const noexcept { - label.clear(); +bool MMapReader::is_text_file(Info& typeRet, bool scan) const noexcept { + typeRet.inval(); + typeRet.size_ = sz_; if (!buf_ or sz_ < 4) return false; @@ -582,39 +602,65 @@ bool MMapReader::is_text_file(string& label) const noexcept { // lprintf("\t ....... sig= %u hex %x\n", sig, sig); if (sig == 0x464c457f) { - label = "ELF"; + typeRet.type_ = "ELF"; return false; } if (sig == 0x21726152) { - label = "RAR"; + typeRet.type_ = "RAR"; return false; } if (sig == 0x587a37fd) { - label = "XZ"; + typeRet.type_ = "XZ"; return false; } if (sig == 0xfd2fb528) { - label = "ZST"; + typeRet.type_ = "ZST"; return false; } if (sig == 0x425a6839) { - label = "BZ2"; + typeRet.type_ = "BZ2"; return false; } if (sig == 0x1f8b0808) { - label = "GZ"; + typeRet.type_ = "GZ"; return false; } if (sig == 0x46445025) { - label = "PDF"; + typeRet.type_ = "PDF"; return false; } if (sig == 0x213c6172) { - label = "AR"; + typeRet.type_ = "AR"; return false; } - return true; + size_t numLines = 0, numText = 0; + if (scan) { + for (size_t i = 0; i < sz_; i++) { + char c = buf_[i]; + if (!c) { + numText++; + continue; + } + if (c == '\n') { + numLines++; + numText++; + continue; + } + numText += int(str::c_is_text(c)); + } + } + else { + return true; + } + + if (sz_ - numText < 3) { + typeRet.type_ = "TXT"; + return true; + } + + typeRet.type_ = "BIN"; + return false; } bool MMapReader::printMagic(std::ostream& os) const noexcept { @@ -645,12 +691,12 @@ bool MMapReader::printMagic(std::ostream& os) const noexcept { if (i > 8) break; } - string label; - bool is_txt = is_text_file(label); + Info inf; + bool is_txt = is_text_file(inf, true); if (is_txt) { os << "(guessed TEXT)" << endl; - } else if (!label.empty()) { - os << "(guessed BINARY) : " << label << endl; + } else if (!inf.type_.empty()) { + os << "(guessed BINARY) : " << inf.type_ << endl; } os.flush(); return true; @@ -668,8 +714,8 @@ bool MMapReader::isGoodForParsing() const noexcept { return false; if (sz_ < UINT_MAX) { - string label; - bool is_txt = is_text_file(label); + Info inf; + bool is_txt = is_text_file(inf); return is_txt; } diff --git a/planning/src/file_io/pln_Fio.h b/planning/src/file_io/pln_Fio.h index 22af0140..adbef1ca 100644 --- a/planning/src/file_io/pln_Fio.h +++ b/planning/src/file_io/pln_Fio.h @@ -40,17 +40,24 @@ inline char* p_strdup(CStr p) noexcept { struct Info { string name_, absName_; + string type_; size_t size_ = 0; + int64_t numLines_ = 0; + int64_t numWords_ = 0; + int64_t maxLlen_ = 0; + int64_t numTextChars_ = 0; + bool exists_ = false; bool accessible_ = false; bool absolute_ = false; public: Info() noexcept = default; + explicit Info(CStr nm) noexcept; + explicit Info(const string& nm) noexcept; - Info(CStr nm) noexcept; - Info(const string& nm) noexcept; void init() noexcept; + void inval() noexcept; static string get_abs_name(const string& nm) noexcept; static string get_realpath(const string& nm) noexcept; @@ -215,9 +222,9 @@ class MMapReader : public Fio public: MMapReader() noexcept = default; - MMapReader(CStr nm) noexcept : Fio(nm) {} + explicit MMapReader(CStr nm) noexcept : Fio(nm) {} - MMapReader(const string& nm) noexcept : Fio(nm) {} + explicit MMapReader(const string& nm) noexcept : Fio(nm) {} virtual ~MMapReader(); @@ -242,7 +249,7 @@ class MMapReader : public Fio } int64_t countWC(int64_t& numWords) const noexcept; // ~ wc command - int64_t printWC(std::ostream& os) const noexcept; // ~ wc command + int64_t printWC(std::ostream& os, Info* inf = nullptr) const noexcept; size_t printLines(std::ostream& os) noexcept; int64_t writeFile(const string& nm) noexcept; @@ -257,9 +264,9 @@ class MMapReader : public Fio int dprint1() const noexcept; int dprint2() const noexcept; -private: - bool is_text_file(string& label) const noexcept; + bool is_text_file(Info& typeRet, bool scan = false) const noexcept; +private: vector get_backslashed_block(size_t fromLine) noexcept; }; // MMapReader @@ -276,9 +283,9 @@ struct LineReader : public Fio size_t cap_ = lr_DEFAULT_CAP; public: - LineReader(size_t iniCap = 0) noexcept; - LineReader(CStr nm, size_t iniCap = 0) noexcept; - LineReader(const string& nm, size_t iniCap = 0) noexcept; + explicit LineReader(size_t iniCap = 0) noexcept; + explicit LineReader(CStr nm, size_t iniCap = 0) noexcept; + explicit LineReader(const string& nm, size_t iniCap = 0) noexcept; virtual ~LineReader() { p_free(buf_); } @@ -349,7 +356,7 @@ class CSV_Reader : public MMapReader CSV_Reader(CStr nm) noexcept : MMapReader(nm) {} - CSV_Reader(const string& nm) noexcept : MMapReader(nm) {} + explicit CSV_Reader(const string& nm) noexcept : MMapReader(nm) {} virtual ~CSV_Reader(); @@ -437,8 +444,8 @@ class XML_Reader : public MMapReader public: XML_Reader() noexcept; - XML_Reader(CStr nm) noexcept; - XML_Reader(const string& nm) noexcept; + explicit XML_Reader(CStr nm) noexcept; + explicit XML_Reader(const string& nm) noexcept; virtual ~XML_Reader(); @@ -463,9 +470,6 @@ class XML_Reader : public MMapReader bool addIncludeGuards(LineReader& lr) noexcept; -inline bool c_is_digit(char c) noexcept { return uint32_t(c) - '0' < 10u; } -inline bool c_is_dot_digit(char c) noexcept { return uint32_t(c) - '0' < 10u or c == '.'; } - inline bool has_digit(CStr z) noexcept { if (!z || !z[0]) return false; diff --git a/planning/src/main.cpp b/planning/src/main.cpp index ad4dc2f0..a9415e7b 100644 --- a/planning/src/main.cpp +++ b/planning/src/main.cpp @@ -1,4 +1,4 @@ -static const char* _pln_VERSION_STR = "pln0367"; +static const char* _pln_VERSION_STR = "pln0368"; #include "RS/rsEnv.h" #include "RS/rsDeal.h" diff --git a/planning/src/util/geo/iv.h b/planning/src/util/geo/iv.h index b560bc49..c8c3a123 100644 --- a/planning/src/util/geo/iv.h +++ b/planning/src/util/geo/iv.h @@ -3,27 +3,6 @@ #ifndef _PLN_util_geo_IV_h_b6e76e5fa705cfb_ #define _PLN_util_geo_IV_h_b6e76e5fa705cfb_ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "util/pln_log.h" namespace pln { @@ -102,6 +81,7 @@ struct Iv { Iv() noexcept = default; Iv(int a, int b) noexcept : a_(a), b_(b) {} + explicit Iv(ipair p) noexcept : a_(p.first), b_(p.second) {} void set(Iv i) noexcept { *this = i; } void set(int a, int b) noexcept { @@ -147,11 +127,18 @@ struct Iv { a_ = t; b_ = t + l; } - void moveRel(int dt) noexcept { + void translate(int dt) noexcept { assert(valid() and normal()); a_ += dt; b_ += dt; } + Iv translated(int dt) const noexcept { + assert(valid() and normal()); + Iv ret(*this); + ret.a_ += dt; + ret.b_ += dt; + return ret; + } inline void unite(Iv i) noexcept; inline void unite(int t) noexcept; diff --git a/planning/src/util/geo/xyz.h b/planning/src/util/geo/xyz.h index 4d8f5dd5..bfd20dae 100644 --- a/planning/src/util/geo/xyz.h +++ b/planning/src/util/geo/xyz.h @@ -16,7 +16,7 @@ struct XY { XY() noexcept = default; XY(int a, int b) noexcept : x_(a), y_(b) {} - XY(ipair p) noexcept : x_(p.first), y_(p.second) {} + explicit XY(ipair p) noexcept : x_(p.first), y_(p.second) {} bool valid() const noexcept { return x_ != INT_MIN; } bool nonNeg() const noexcept { return x_ >= 0; } @@ -32,11 +32,17 @@ struct XY { y_ = 0; } - void moveRel(int dx, int dy) noexcept { + XY translated(int dx, int dy) const noexcept { + XY ret(*this); + ret.x_ += dx; + ret.y_ += dy; + return ret; + } + void translate(int dx, int dy) noexcept { x_ += dx; y_ += dy; } - void moveRel(XY d) noexcept { moveRel(d.x_, d.y_); } + void translate(XY d) noexcept { translate(d.x_, d.y_); } void negate() noexcept { x_ = -x_; diff --git a/planning/src/util/pln_log.h b/planning/src/util/pln_log.h index 2f67f9f2..71ad9b87 100644 --- a/planning/src/util/pln_log.h +++ b/planning/src/util/pln_log.h @@ -120,6 +120,28 @@ namespace str { using std::string; +inline constexpr bool c_is_digit(char c) noexcept { + return uint32_t(c) - '0' < 10u; +} +inline constexpr bool c_is_dot_digit(char c) noexcept { + return uint32_t(c) - '0' < 10u or c == '.'; +} +inline constexpr bool c_is_space(char c) noexcept { + return c == ' ' or uint32_t(c) - 9u < 5u; +} +inline constexpr bool c_is_7bit(char c) noexcept { + return uint32_t(c) < 128u; +} +inline constexpr bool i_is_7bit(int i) noexcept { + return uint32_t(i) < 128u; +} +inline constexpr bool c_is_text(char c) noexcept { + // for text/binary file classification + if (not c_is_7bit(c)) + return false; + return uint32_t(c) > 31u or c_is_space(c); +} + string s2lower(const string& s) noexcept; string s2upper(const string& s) noexcept; @@ -278,7 +300,7 @@ inline string concat(CStr a) noexcept { inline CStr trimFront(CStr z) noexcept { if (z and z[0]) { - while (std::isspace(*z)) z++; + while (c_is_space(*z)) z++; } return z; }