From d8442edef014a3cc25c729be495b6b2cda1cc5da Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 22:45:10 +0200 Subject: [PATCH 01/16] Move print trivial print functions out --- uuu/CMakeLists.txt | 1 + uuu/print_helpers.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ uuu/print_helpers.h | 12 +++++++++ uuu/uuu.cpp | 50 +------------------------------------ 4 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 uuu/print_helpers.cpp create mode 100644 uuu/print_helpers.h diff --git a/uuu/CMakeLists.txt b/uuu/CMakeLists.txt index 217f32f5..4557c504 100644 --- a/uuu/CMakeLists.txt +++ b/uuu/CMakeLists.txt @@ -63,6 +63,7 @@ include_directories(${generated_files_dir}) set(SOURCES uuu.cpp + print_helpers.cpp buildincmd.cpp autocomplete.cpp ${CLSTS} diff --git a/uuu/print_helpers.cpp b/uuu/print_helpers.cpp new file mode 100644 index 00000000..4eb6d95b --- /dev/null +++ b/uuu/print_helpers.cpp @@ -0,0 +1,57 @@ +#include "print_helpers.h" + +#include +#include +#include + +using namespace std; + +void print_auto_scroll(string str, size_t len, size_t start) +{ + if (str.size() <= len) + { + str.resize(len, ' '); + cout << str; + return; + } + + if(str.size()) + start = start % str.size(); + else + start = 0; + + string s = str.substr(start, len); + s.resize(len, ' '); + cout << s; +} + +int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, + uint16_t pid, uint16_t vid, uint16_t bcdmin, uint16_t bcdmax, void * /*p*/) +{ + const char *ext; + if (strlen(chip) >= 7) + ext = ""; + else + ext = "\t"; + + if (bcdmin == 0 && bcdmax == 0xFFFF) + printf("\t%s\t %s\t%s 0x%04x\t 0x%04x\n", pro, chip, ext, pid, vid); + else + printf("\t%s\t %s\t%s 0x%04x\t 0x%04x\t [0x%04x..0x%04x]\n", pro, chip, ext, pid, vid, bcdmin, bcdmax); + return 0; +} + +int print_udev_rule(const char * /*pro*/, const char * /*chip*/, const char * /*compatible*/, + uint16_t vid, uint16_t pid, uint16_t /*bcdmin*/, uint16_t /*bcdmax*/, void * /*p*/) +{ + printf("SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", MODE=\"0666\"\n", + vid, pid); + return 0; +} + +int print_usb_device(const char *path, const char *chip, const char *pro, + uint16_t vid, uint16_t pid, uint16_t bcd, void * /*p*/) +{ + printf("\t%s\t %s\t %s\t 0x%04X\t0x%04X\t 0x%04X\n", path, chip, pro, vid, pid, bcd); + return 0; +} diff --git a/uuu/print_helpers.h b/uuu/print_helpers.h new file mode 100644 index 00000000..1727deb6 --- /dev/null +++ b/uuu/print_helpers.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +void print_auto_scroll(std::string str, size_t len, size_t start); +int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, + uint16_t pid, uint16_t vid, uint16_t bcdmin, uint16_t bcdmax, void * /*p*/); +int print_udev_rule(const char * /*pro*/, const char * /*chip*/, const char * /*compatible*/, + uint16_t vid, uint16_t pid, uint16_t /*bcdmin*/, uint16_t /*bcdmax*/, void * /*p*/); +int print_usb_device(const char *path, const char *chip, const char *pro, + uint16_t vid, uint16_t pid, uint16_t bcd, void * /*p*/); diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 4968c011..f326f39d 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -29,6 +29,7 @@ * */ +#include "print_helpers.h" #include #include #include @@ -44,7 +45,6 @@ #include #include #include "buildincmd.h" -#include #include #include "../libuuu/libuuu.h" @@ -186,29 +186,6 @@ void print_version() printf("uuu (Universal Update Utility) for nxp imx chips -- %s\n\n", uuu_get_version_string()); } -int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, uint16_t pid, uint16_t vid, uint16_t bcdmin, uint16_t bcdmax, void * /*p*/) -{ - const char *ext; - if (strlen(chip) >= 7) - ext = ""; - else - ext = "\t"; - - if (bcdmin == 0 && bcdmax == 0xFFFF) - printf("\t%s\t %s\t%s 0x%04x\t 0x%04x\n", pro, chip, ext, pid, vid); - else - printf("\t%s\t %s\t%s 0x%04x\t 0x%04x\t [0x%04x..0x%04x]\n", pro, chip, ext, pid, vid, bcdmin, bcdmax); - return 0; -} - -int print_udev_rule(const char * /*pro*/, const char * /*chip*/, const char * /*compatible*/, - uint16_t vid, uint16_t pid, uint16_t /*bcdmin*/, uint16_t /*bcdmax*/, void * /*p*/) -{ - printf("SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", MODE=\"0666\"\n", - vid, pid); - return 0; -} - int polling_usb(std::atomic& bexit); int g_overall_status; @@ -262,25 +239,6 @@ string build_process_bar(size_t width, size_t pos, size_t total) str.insert(start + per.size() + strlen(g_vt_yellow), g_vt_default); return str; } - -void print_auto_scroll(string str, size_t len, size_t start) -{ - if (str.size() <= len) - { - str.resize(len, ' '); - cout << str; - return; - } - - if(str.size()) - start = start % str.size(); - else - start = 0; - - string s = str.substr(start, len); - s.resize(len, ' '); - cout << s; -} class ShowNotify { public: @@ -766,12 +724,6 @@ void print_udev() fprintf(stderr, "\tsudo udevadm control --reload-rules\n"); } -int print_usb_device(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, void * /*p*/) -{ - printf("\t%s\t %s\t %s\t 0x%04X\t0x%04X\t 0x%04X\n", path, chip, pro, vid, pid, bcd); - return 0; -} - void print_lsusb() { cout << "Connected Known USB Devices\n"; From 8ef60b2fa165ab01b4729418e0c1fae7907d21a9 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:00:01 +0200 Subject: [PATCH 02/16] Move out print functions with external dependncs --- uuu/print_helpers.cpp | 58 +++++++++++++++++++++++++++++++++++++ uuu/print_helpers.h | 6 ++++ uuu/uuu.cpp | 67 +++++-------------------------------------- 3 files changed, 71 insertions(+), 60 deletions(-) diff --git a/uuu/print_helpers.cpp b/uuu/print_helpers.cpp index 4eb6d95b..42ff0a79 100644 --- a/uuu/print_helpers.cpp +++ b/uuu/print_helpers.cpp @@ -1,4 +1,5 @@ #include "print_helpers.h" +#include "../libuuu/libuuu.h" #include #include @@ -41,6 +42,45 @@ int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, return 0; } +void print_lsusb() +{ + cout << "Connected Known USB Devices\n"; + printf("\tPath\t Chip\t Pro\t Vid\t Pid\t BcdVersion\n"); + printf("\t==================================================\n"); + + uuu_for_each_devices(print_usb_device, nullptr); +} + +void print_oneline(string str, int console_width) +{ + size_t w = static_cast(console_width); + if (w <= 3) + return; + + if (str.size() >= w) + { + str.resize(w - 1); + str[str.size() - 1] = '.'; + str[str.size() - 2] = '.'; + str[str.size() - 3] = '.'; + } + else + { + str.resize(w, ' '); + } + cout << str << endl; + +} + +void print_udev() +{ + uuu_for_each_cfg(print_udev_rule, nullptr); + fprintf(stderr, "\n1: put above udev run into /etc/udev/rules.d/99-uuu.rules\n"); + fprintf(stderr, "\tsudo sh -c \"uuu -udev >> /etc/udev/rules.d/99-uuu.rules\"\n"); + fprintf(stderr, "2: update udev rule\n"); + fprintf(stderr, "\tsudo udevadm control --reload-rules\n"); +} + int print_udev_rule(const char * /*pro*/, const char * /*chip*/, const char * /*compatible*/, uint16_t vid, uint16_t pid, uint16_t /*bcdmin*/, uint16_t /*bcdmax*/, void * /*p*/) { @@ -55,3 +95,21 @@ int print_usb_device(const char *path, const char *chip, const char *pro, printf("\t%s\t %s\t %s\t 0x%04X\t0x%04X\t 0x%04X\n", path, chip, pro, vid, pid, bcd); return 0; } + +void print_usb_filter(const vector &usb_path_filters) +{ + if (!usb_path_filters.empty()) + { + cout << " at path "; + for (const auto &usb_path_filter : usb_path_filters) + { + cout << usb_path_filter << " "; + } + } +} + +void print_version() +{ + printf("uuu (Universal Update Utility) for nxp imx chips -- %s\n\n", + uuu_get_version_string()); +} diff --git a/uuu/print_helpers.h b/uuu/print_helpers.h index 1727deb6..6923cda1 100644 --- a/uuu/print_helpers.h +++ b/uuu/print_helpers.h @@ -2,11 +2,17 @@ #include #include +#include void print_auto_scroll(std::string str, size_t len, size_t start); int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, uint16_t pid, uint16_t vid, uint16_t bcdmin, uint16_t bcdmax, void * /*p*/); +void print_lsusb(); +void print_oneline(std::string str, int console_width); +void print_udev(); int print_udev_rule(const char * /*pro*/, const char * /*chip*/, const char * /*compatible*/, uint16_t vid, uint16_t pid, uint16_t /*bcdmin*/, uint16_t /*bcdmax*/, void * /*p*/); int print_usb_device(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, void * /*p*/); +void print_usb_filter(const std::vector &usb_path_filters); +void print_version(); diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index f326f39d..9ed59578 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -37,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -69,7 +68,6 @@ void clean_vt_color() noexcept using namespace std; int get_console_width(); -void print_oneline(string str); int auto_complete(int argc, char**argv); void print_autocomplete_help(); @@ -181,10 +179,6 @@ void print_help(bool detail = false) start = pos; } } -void print_version() -{ - printf("uuu (Universal Update Utility) for nxp imx chips -- %s\n\n", uuu_get_version_string()); -} int polling_usb(std::atomic& bexit); @@ -431,7 +425,7 @@ class ShowNotify str += g_wait[(g_wait_index++) & 0x3]; - print_oneline(str); + print_oneline(str, get_console_width()); return ; } else @@ -485,27 +479,6 @@ return; static map g_map_path_nt; mutex g_callback_mutex; -void print_oneline(string str) -{ - size_t w = get_console_width(); - if (w <= 3) - return; - - if (str.size() >= w) - { - str.resize(w - 1); - str[str.size() - 1] = '.'; - str[str.size() - 2] = '.'; - str[str.size() - 3] = '.'; - } - else - { - str.resize(w, ' '); - } - cout << str << endl; - -} - ShowNotify Summary(map *np) { ShowNotify sn; @@ -566,13 +539,14 @@ int progress(uuu_notify nt, void *p) str += g_usb_path_filter[i] + " "; } - print_oneline(str); - print_oneline(""); + const int console_width = get_console_width(); + print_oneline(str, console_width); + print_oneline("", console_width); if ((*np)[nt.id].m_dev == "Prep" && !g_start_usb_transfer) { Summary(np).print(); }else - print_oneline(""); + print_oneline("", console_width); for (it = g_map_path_nt.begin(); it != g_map_path_nt.end(); it++) it->second.print(); @@ -641,15 +615,6 @@ int get_console_width() return w.ws_col; } #endif -void print_usb_filter() -{ - if (!g_usb_path_filter.empty()) - { - cout << " at path "; - for (size_t i = 0; i < g_usb_path_filter.size(); i++) - cout << g_usb_path_filter[i] << " "; - } -} int runshell(int shell) { @@ -715,24 +680,6 @@ int runshell(int shell) return -1; } -void print_udev() -{ - uuu_for_each_cfg(print_udev_rule, NULL); - fprintf(stderr, "\n1: put above udev run into /etc/udev/rules.d/99-uuu.rules\n"); - fprintf(stderr, "\tsudo sh -c \"uuu -udev >> /etc/udev/rules.d/99-uuu.rules\"\n"); - fprintf(stderr, "2: update udev rule\n"); - fprintf(stderr, "\tsudo udevadm control --reload-rules\n"); -} - -void print_lsusb() -{ - cout << "Connected Known USB Devices\n"; - printf("\tPath\t Chip\t Pro\t Vid\t Pid\t BcdVersion\n"); - printf("\t==================================================\n"); - - uuu_for_each_devices(print_usb_device, NULL); -} - int main(int argc, char **argv) { if (auto_complete(argc, argv) == 0) @@ -969,13 +916,13 @@ int main(int argc, char **argv) if (!shell) cout << "Wait for Known USB Device Appear..."; - print_usb_filter(); + print_usb_filter(g_usb_path_filter); printf("\n"); } else { cout << "Wait for Known USB Device Appear..."; - print_usb_filter(); + print_usb_filter(g_usb_path_filter); cout << "\r"; cout << "\x1b[?25l"; cout.flush(); From e2a3537f280f1694ff1ce181eb019e32acf738ba Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:14:15 +0200 Subject: [PATCH 03/16] Get rid of a few compiler warnings --- uuu/uuu.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 9ed59578..e5a42b0b 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -71,13 +71,13 @@ int get_console_width(); int auto_complete(int argc, char**argv); void print_autocomplete_help(); -char g_sample_cmd_list[] = { +static char g_sample_cmd_list[] = { #include "uuu.clst" }; -vector g_usb_path_filter; +static vector g_usb_path_filter; -int g_verbose = 0; +static int g_verbose = 0; static bool g_start_usb_transfer; class AutoCursor @@ -105,7 +105,7 @@ class string_ex : public std::string { va_list args; va_start(args, fmt); - size_t len = std::vsnprintf(NULL, 0, fmt, args); + size_t len = std::vsnprintf(nullptr, 0, fmt, args); va_end(args); this->resize(len); @@ -182,11 +182,11 @@ void print_help(bool detail = false) int polling_usb(std::atomic& bexit); -int g_overall_status; -int g_overall_okay; -int g_overall_failure; -char g_wait[] = "|/-\\"; -int g_wait_index; +static int g_overall_status; +static int g_overall_okay; +static int g_overall_failure; +static char g_wait[] = "|/-\\"; +static int g_wait_index; string build_process_bar(size_t width, size_t pos, size_t total) @@ -388,7 +388,7 @@ class ShowNotify cout << "Download file:" << nt->str << endl; } - void print(int verbose = 0, uuu_notify*nt=NULL) + void print(int verbose = 0, uuu_notify*nt = nullptr) { verbose ? print_verbose(nt) : print_simple(); } @@ -477,7 +477,7 @@ return; }; static map g_map_path_nt; -mutex g_callback_mutex; +static mutex g_callback_mutex; ShowNotify Summary(map *np) { @@ -506,7 +506,7 @@ ShowNotify Summary(map *np) int progress(uuu_notify nt, void *p) { - map *np = (map*)p; + map *np = reinterpret_cast*>(p); map::iterator it; std::lock_guard lock(g_callback_mutex); @@ -908,7 +908,7 @@ int main(int argc, char **argv) printf("%sBuild in config:%s\n", g_vt_boldwhite, g_vt_default); printf("\tPctl\t Chip\t\t Vid\t Pid\t BcdVersion\n"); printf("\t==================================================\n"); - uuu_for_each_cfg(print_cfg, NULL); + uuu_for_each_cfg(print_cfg, nullptr); if (!cmd_script.empty()) printf("\n%sRun built-in script:%s\n %s\n\n", g_vt_boldwhite, g_vt_default, cmd_script.c_str()); From 353a524f3230ef720aceebeabe39d35fb6001462 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:20:00 +0200 Subject: [PATCH 04/16] Make libuuu.h header accessible by clients --- libuuu/CMakeLists.txt | 1 + libuuu/{ => include}/libuuu.h | 0 msvc/libuuu.vcxproj | 2 +- msvc/libuuu.vcxproj.filters | 2 +- uuu/autocomplete.cpp | 2 +- uuu/print_helpers.cpp | 3 ++- uuu/uuu.cpp | 2 +- 7 files changed, 7 insertions(+), 5 deletions(-) rename libuuu/{ => include}/libuuu.h (100%) diff --git a/libuuu/CMakeLists.txt b/libuuu/CMakeLists.txt index 0ff96952..fa9d8a9a 100644 --- a/libuuu/CMakeLists.txt +++ b/libuuu/CMakeLists.txt @@ -62,3 +62,4 @@ include_directories(${generated_files_dir}) #add_library( uuc SHARED ${SOURCES} )) add_library( uuc_s STATIC ${SOURCES} ) +target_include_directories(uuc_s PUBLIC include) diff --git a/libuuu/libuuu.h b/libuuu/include/libuuu.h similarity index 100% rename from libuuu/libuuu.h rename to libuuu/include/libuuu.h diff --git a/msvc/libuuu.vcxproj b/msvc/libuuu.vcxproj index 6a84e20f..112e3bdb 100644 --- a/msvc/libuuu.vcxproj +++ b/msvc/libuuu.vcxproj @@ -48,7 +48,7 @@ - + diff --git a/msvc/libuuu.vcxproj.filters b/msvc/libuuu.vcxproj.filters index 9970701f..30ef42a9 100644 --- a/msvc/libuuu.vcxproj.filters +++ b/msvc/libuuu.vcxproj.filters @@ -15,7 +15,7 @@ Header Files - + Header Files diff --git a/uuu/autocomplete.cpp b/uuu/autocomplete.cpp index 57f0ac2b..6b45a14b 100644 --- a/uuu/autocomplete.cpp +++ b/uuu/autocomplete.cpp @@ -45,7 +45,7 @@ #include #include "buildincmd.h" -#include "../libuuu/libuuu.h" +#include "libuuu.h" #ifndef _MSC_VER #include diff --git a/uuu/print_helpers.cpp b/uuu/print_helpers.cpp index 42ff0a79..273eeaef 100644 --- a/uuu/print_helpers.cpp +++ b/uuu/print_helpers.cpp @@ -1,5 +1,6 @@ #include "print_helpers.h" -#include "../libuuu/libuuu.h" + +#include "libuuu.h" #include #include diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index e5a42b0b..1deac77b 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -46,7 +46,7 @@ #include "buildincmd.h" #include -#include "../libuuu/libuuu.h" +#include "libuuu.h" const char * g_vt_yellow = "\x1B[93m"; const char * g_vt_default = "\x1B[0m"; From 70a9be378a10584e572cd936ac7026e283b85147 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:22:37 +0200 Subject: [PATCH 05/16] Use previous step to deduplicate string_ex def --- libuuu/{ => include}/libcomm.h | 2 +- msvc/libuuu.vcxproj | 2 +- msvc/libuuu.vcxproj.filters | 2 +- uuu/uuu.cpp | 21 +-------------------- 4 files changed, 4 insertions(+), 23 deletions(-) rename libuuu/{ => include}/libcomm.h (98%) diff --git a/libuuu/libcomm.h b/libuuu/include/libcomm.h similarity index 98% rename from libuuu/libcomm.h rename to libuuu/include/libcomm.h index 81191fda..30205f99 100644 --- a/libuuu/libcomm.h +++ b/libuuu/include/libcomm.h @@ -57,7 +57,7 @@ class string_ex : public std::string this->resize(len); va_start(args, fmt); - std::vsnprintf((char*)c_str(), len+1, fmt, args); + std::vsnprintf((char*)c_str(), len + 1, fmt, args); va_end(args); return 0; diff --git a/msvc/libuuu.vcxproj b/msvc/libuuu.vcxproj index 112e3bdb..05ae3eb4 100644 --- a/msvc/libuuu.vcxproj +++ b/msvc/libuuu.vcxproj @@ -46,7 +46,7 @@ - + diff --git a/msvc/libuuu.vcxproj.filters b/msvc/libuuu.vcxproj.filters index 30ef42a9..dcdc17ec 100644 --- a/msvc/libuuu.vcxproj.filters +++ b/msvc/libuuu.vcxproj.filters @@ -18,7 +18,7 @@ Header Files - + Header Files diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 1deac77b..12ce17a2 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -46,6 +46,7 @@ #include "buildincmd.h" #include +#include "libcomm.h" #include "libuuu.h" const char * g_vt_yellow = "\x1B[93m"; @@ -98,26 +99,6 @@ void ctrl_c_handle(int) exit(1); } -class string_ex : public std::string -{ -public: - int format(const char *fmt, ...) - { - va_list args; - va_start(args, fmt); - size_t len = std::vsnprintf(nullptr, 0, fmt, args); - va_end(args); - - this->resize(len); - - va_start(args, fmt); - std::vsnprintf((char*)c_str(), len + 1, fmt, args); - va_end(args); - - return 0; - } -}; - void print_help(bool detail = false) { const char help[] = From d2e0eaeebee2014f1d6444d6eef22c2af7fb26ac Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:27:09 +0200 Subject: [PATCH 06/16] Move out progress bar creation --- uuu/print_helpers.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ uuu/print_helpers.h | 2 ++ uuu/uuu.cpp | 48 +------------------------------------------ 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/uuu/print_helpers.cpp b/uuu/print_helpers.cpp index 273eeaef..28b5ae0f 100644 --- a/uuu/print_helpers.cpp +++ b/uuu/print_helpers.cpp @@ -1,5 +1,6 @@ #include "print_helpers.h" +#include "libcomm.h" #include "libuuu.h" #include @@ -8,6 +9,52 @@ using namespace std; +string build_progress_bar(size_t width, size_t pos, size_t total, + const char * vt_default, const char * vt_yellow) +{ + string str; + str.resize(width, ' '); + str[0] = '['; + str[width - 1] = ']'; + + if (total == 0) + { + if (pos == 0) + return str; + + string_ex loc; + size_t s = pos / (1024 * 1024); + loc.format("%dM", s); + str.replace(1, loc.size(), loc); + return str; + } + + size_t i; + + if (pos > total) + pos = total; + + for (i = 1; i < (width-2) * pos / total; i++) + { + str[i] = '='; + } + + if (i > 1) + str[i] = '>'; + + if (pos == total) + str[str.size() - 2] = '='; + + string_ex per; + per.format("%d%%", pos * 100 / total); + + size_t start = (width - per.size()) / 2; + str.replace(start, per.size(), per); + str.insert(start, vt_yellow); + str.insert(start + per.size() + strlen(vt_yellow), vt_default); + return str; +} + void print_auto_scroll(string str, size_t len, size_t start) { if (str.size() <= len) diff --git a/uuu/print_helpers.h b/uuu/print_helpers.h index 6923cda1..68602373 100644 --- a/uuu/print_helpers.h +++ b/uuu/print_helpers.h @@ -4,6 +4,8 @@ #include #include +std::string build_progress_bar(size_t width, size_t pos, size_t total, + const char * vt_default, const char * vt_yellow); void print_auto_scroll(std::string str, size_t len, size_t start); int print_cfg(const char *pro, const char * chip, const char * /*compatible*/, uint16_t pid, uint16_t vid, uint16_t bcdmin, uint16_t bcdmax, void * /*p*/); diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 12ce17a2..5aa43517 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -168,52 +168,6 @@ static int g_overall_okay; static int g_overall_failure; static char g_wait[] = "|/-\\"; static int g_wait_index; - - -string build_process_bar(size_t width, size_t pos, size_t total) -{ - string str; - str.resize(width, ' '); - str[0] = '['; - str[width - 1] = ']'; - - if (total == 0) - { - if (pos == 0) - return str; - - string_ex loc; - size_t s = pos / (1024 * 1024); - loc.format("%dM", s); - str.replace(1, loc.size(), loc); - return str; - } - - size_t i; - - if (pos > total) - pos = total; - - for (i = 1; i < (width-2) * pos / total; i++) - { - str[i] = '='; - } - - if (i > 1) - str[i] = '>'; - - if (pos == total) - str[str.size() - 2] = '='; - - string_ex per; - per.format("%d%%", pos * 100 / total); - - size_t start = (width - per.size()) / 2; - str.replace(start, per.size(), per); - str.insert(start, g_vt_yellow); - str.insert(start + per.size() + strlen(g_vt_yellow), g_vt_default); - return str; -} class ShowNotify { public: @@ -440,7 +394,7 @@ class ShowNotify } cout << str; } else { - cout << build_process_bar(bar, m_trans_pos, m_trans_size); + cout << build_progress_bar(bar, m_trans_pos, m_trans_size, g_vt_yellow, g_vt_default); } cout << " "; print_auto_scroll(m_cmd, width - bar - info-1, m_start_pos); From 766bd31cb5e04cf331d5e117192c1753c63c2d4a Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 4 Jul 2020 23:44:31 +0200 Subject: [PATCH 07/16] Move and rename AutoCursor class The class' purpose fits to the new location and a new more descriptive name was given. --- uuu/print_helpers.cpp | 5 +++++ uuu/print_helpers.h | 6 ++++++ uuu/uuu.cpp | 13 ++----------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/uuu/print_helpers.cpp b/uuu/print_helpers.cpp index 28b5ae0f..6f6e3bdd 100644 --- a/uuu/print_helpers.cpp +++ b/uuu/print_helpers.cpp @@ -9,6 +9,11 @@ using namespace std; +AutoReactivateCursor::~AutoReactivateCursor() +{ + printf("\x1b[?25h\n\n\n"); +} + string build_progress_bar(size_t width, size_t pos, size_t total, const char * vt_default, const char * vt_yellow) { diff --git a/uuu/print_helpers.h b/uuu/print_helpers.h index 68602373..4affbd51 100644 --- a/uuu/print_helpers.h +++ b/uuu/print_helpers.h @@ -4,6 +4,12 @@ #include #include +class AutoReactivateCursor +{ +public: + ~AutoReactivateCursor(); +}; + std::string build_progress_bar(size_t width, size_t pos, size_t total, const char * vt_default, const char * vt_yellow); void print_auto_scroll(std::string str, size_t len, size_t start); diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 5aa43517..96579caa 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -81,19 +81,10 @@ static vector g_usb_path_filter; static int g_verbose = 0; static bool g_start_usb_transfer; -class AutoCursor -{ -public: - ~AutoCursor() - { - printf("\x1b[?25h\n\n\n"); - } -}; - void ctrl_c_handle(int) { do { - AutoCursor a; + AutoReactivateCursor a; } while(0); exit(1); @@ -630,7 +621,7 @@ int main(int argc, char **argv) } } - AutoCursor a; + AutoReactivateCursor a; print_version(); From 941eb7bdd3011222fc6636557ad429340208a8ce Mon Sep 17 00:00:00 2001 From: markuspg Date: Fri, 10 Jul 2020 15:06:09 +0200 Subject: [PATCH 08/16] Drop using namespace std directive from buildincmd --- uuu/autocomplete.cpp | 2 ++ uuu/buildincmd.h | 57 ++++++++++++++++++++++---------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/uuu/autocomplete.cpp b/uuu/autocomplete.cpp index 6b45a14b..36dc45ef 100644 --- a/uuu/autocomplete.cpp +++ b/uuu/autocomplete.cpp @@ -54,6 +54,8 @@ #include #endif +using namespace std; + void linux_auto_arg(const char *space = " ", const char * filter = "") { string str = filter; diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index d01edaea..a57703aa 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -28,14 +28,13 @@ * POSSIBILITY OF SUCH DAMAGE. * */ + #pragma once +#include +#include #include #include -#include -#include - -using namespace std; extern const char * g_vt_yellow ; extern const char * g_vt_default ; @@ -54,10 +53,10 @@ struct BuildCmd class Arg { public: - string m_arg; - string m_desc; + std::string m_arg; + std::string m_desc; uint32_t m_flags; - string m_options; + std::string m_options; enum { ARG_MUST = 0x1, @@ -65,11 +64,11 @@ class Arg ARG_OPTION_KEY = 0x4, }; Arg() { m_flags = ARG_MUST; } - int parser(string option) + int parser(std::string option) { size_t pos; pos = option.find('['); - if (pos == string::npos) + if (pos == std::string::npos) return 0; m_options = option.substr(pos + 1, option.find(']') - pos - 1); m_flags = ARG_OPTION | ARG_OPTION_KEY; @@ -80,11 +79,11 @@ class Arg class BuildInScript { public: - string m_script; - string m_desc; - string m_cmd; - vector m_args; - bool find_args(string arg) + std::string m_script; + std::string m_desc; + std::string m_cmd; + std::vector m_args; + bool find_args(std::string arg) { for (size_t i = 0; i < m_args.size(); i++) { @@ -106,7 +105,7 @@ class BuildInScript { size_t off; size_t off_tab; - string param; + std::string param; if (m_script[i] == '_' && (m_script[i - 1] == '@' || m_script[i - 1] == ' ')) { @@ -118,7 +117,7 @@ class BuildInScript if (ofn < off) off = ofn; - if (off == string::npos) + if (off == std::string::npos) off = m_script.size() + 1; param = m_script.substr(i, off - i); @@ -135,15 +134,15 @@ class BuildInScript for (size_t i = 0; i < m_args.size(); i++) { size_t pos = 0; - string str; + std::string str; str += "@"; str += m_args[i].m_arg; pos = m_script.find(str); - if (pos != string::npos) { - string def; + if (pos != std::string::npos) { + std::string def; size_t start_descript; start_descript = m_script.find('|', pos); - if (start_descript != string::npos) + if (start_descript != std::string::npos) { m_args[i].m_desc = m_script.substr(start_descript + 1, m_script.find('\n', start_descript) - start_descript - 1); @@ -164,7 +163,7 @@ class BuildInScript printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str()); for (size_t i=0; i < m_args.size(); i++) { - string desc; + std::string desc; desc += m_args[i].m_arg; if (m_args[i].m_flags & Arg::ARG_OPTION) { @@ -178,10 +177,10 @@ class BuildInScript } } - inline string str_to_upper(string str) + inline std::string str_to_upper(std::string str) { std::locale loc; - string s; + std::string s; for (size_t i = 0; i < str.size(); i++) s.push_back(std::toupper(str[i], loc)); @@ -189,7 +188,7 @@ class BuildInScript return s; } - string replace_str(string str, string key, string replace) + std::string replace_str(std::string str, std::string key, std::string replace) { if (replace.size() > 4) { @@ -199,7 +198,7 @@ class BuildInScript } } - for (size_t j = 0; (j = str.find(key, j)) != string::npos;) + for (size_t j = 0; (j = str.find(key, j)) != std::string::npos;) { str.replace(j, key.size(), replace); j += key.size(); @@ -207,9 +206,9 @@ class BuildInScript return str; } - string replace_script_args(vector args) + std::string replace_script_args(std::vector args) { - string script = m_script; + std::string script = m_script; for (size_t i = 0; i < args.size() && i < m_args.size(); i++) { script = replace_str(script, m_args[i].m_arg, args[i]); @@ -234,7 +233,7 @@ class BuildInScript } }; -class BuildInScriptVector : public map +class BuildInScriptVector : public std::map { public: BuildInScriptVector(BuildCmd*p) @@ -270,7 +269,7 @@ class BuildInScriptVector : public map printf(">"); } - void PrintAutoComplete(string match, const char *space=" " ) + void PrintAutoComplete(std::string match, const char *space=" " ) { for (auto iCol = begin(); iCol != end(); ++iCol) { From af146a3d551f07c360cba6bd1897caba5953c729 Mon Sep 17 00:00:00 2001 From: markuspg Date: Fri, 10 Jul 2020 15:24:09 +0200 Subject: [PATCH 09/16] Make array of built in commands constexpr --- uuu/buildincmd.cpp | 31 +++++++++++++++++-------------- uuu/buildincmd.h | 24 +++++++++++++----------- uuu/uuu.cpp | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index 7d632d2b..573c9566 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -31,52 +31,55 @@ #include "buildincmd.h" -BuildCmd g_buildin_cmd[] = +constexpr BuildCmd::BuildCmd(const char * const cmd, const char * const buildcmd, + const char * const desc) : + m_cmd{cmd}, + m_buildcmd{buildcmd}, + m_desc{desc} { - { +} + +static constexpr std::array g_buildin_cmd +{ + BuildCmd{ "emmc", #include "emmc_burn_loader.clst" ,"burn boot loader to eMMC boot partition" }, - { + BuildCmd{ "emmc_all", #include "emmc_burn_all.clst" ,"burn whole image to eMMC" }, - { + BuildCmd{ "fat_write", #include "fat_write.clst" ,"update one file in fat partition, require uboot fastboot running in board" }, - { + BuildCmd{ "nand", #include "nand_burn_loader.clst" ,"burn boot loader to NAND flash" }, - { + BuildCmd{ "qspi", #include "qspi_burn_loader.clst" ,"burn boot loader to qspi nor flash" }, - { + BuildCmd{ "sd", #include "sd_burn_loader.clst" ,"burn boot loader to sd card" }, - { + BuildCmd{ "sd_all", #include "sd_burn_all.clst" ,"burn whole image to sd card" }, - { + BuildCmd{ "spl", #include "spl_boot.clst" ,"boot spl and uboot" - }, - { - NULL, - NULL, - NULL, } }; diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index a57703aa..ef32d216 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -45,6 +45,10 @@ extern const char * g_vt_boldwhite ; struct BuildCmd { + BuildCmd() = default; + constexpr BuildCmd(const char * const cmd, const char * const buildcmd, + const char * const desc); + const char *m_cmd; const char *m_buildcmd; const char *m_desc; @@ -93,13 +97,13 @@ class BuildInScript return false; } BuildInScript() {}; - BuildInScript(BuildCmd*p) + BuildInScript(const BuildCmd &p) { - m_script = p->m_buildcmd; - if(p->m_desc) - m_desc = p->m_desc; - if(p->m_cmd) - m_cmd = p->m_cmd; + m_script = p.m_buildcmd; + if(p.m_desc) + m_desc = p.m_desc; + if(p.m_cmd) + m_cmd = p.m_cmd; for (size_t i = 1; i < m_script.size(); i++) { @@ -236,13 +240,11 @@ class BuildInScript class BuildInScriptVector : public std::map { public: - BuildInScriptVector(BuildCmd*p) + BuildInScriptVector(const std::array &build_cmds) { - while (p->m_cmd) - { - BuildInScript one(p); + for (const auto &build_cmd : build_cmds) { + BuildInScript one{build_cmd}; (*this)[one.m_cmd] = one; - p++; } } diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 96579caa..ebd2c204 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -756,7 +756,7 @@ int main(int argc, char **argv) tmpCmd.m_desc = "Script loaded from file"; - BuildInScript tmpBuildInScript(&tmpCmd); + BuildInScript tmpBuildInScript(tmpCmd); g_BuildScripts[tmpCmdFileName] = tmpBuildInScript; cmd_script = g_BuildScripts[tmpCmdFileName].replace_script_args(args); From 180604356818b1e50b179fa5fc71ff9f905a5880 Mon Sep 17 00:00:00 2001 From: markuspg Date: Fri, 10 Jul 2020 15:40:18 +0200 Subject: [PATCH 10/16] Better encapsulate data in BuildInScript --- uuu/buildincmd.cpp | 45 +++++++++++++++++++++++++++++++++++++ uuu/buildincmd.h | 56 +++++++++++----------------------------------- uuu/uuu.cpp | 2 +- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index 573c9566..180bf156 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -31,6 +31,7 @@ #include "buildincmd.h" +using namespace std; constexpr BuildCmd::BuildCmd(const char * const cmd, const char * const buildcmd, const char * const desc) : m_cmd{cmd}, @@ -84,3 +85,47 @@ static constexpr std::array g_buildin_cmd }; BuildInScriptVector g_BuildScripts(g_buildin_cmd); + +BuildInScriptVector::BuildInScriptVector(const array &build_cmds) +{ + for (const auto &build_cmd : build_cmds) { + BuildInScript one{build_cmd}; + (*this)[one.get_cmd()] = one; + } +} + +void BuildInScriptVector::PrintAutoComplete(string match, const char *space) +{ + for (auto iCol = begin(); iCol != end(); ++iCol) + { + if(iCol->first.substr(0, match.size()) == match) + { + printf("%s%s\n", iCol->first.c_str(), space); + } + } +} + +void BuildInScriptVector::ShowAll() +{ + for (auto iCol = begin(); iCol != end(); ++iCol) + { + iCol->second.show_cmd(); + } +} + +void BuildInScriptVector::ShowCmds() +{ + printf("<"); + for (auto iCol = begin(); iCol != end(); ++iCol) + { + printf("%s%s%s", g_vt_boldwhite, iCol->first.c_str(), g_vt_default); + + auto i = iCol; + i++; + if(i != end()) + { + printf("|"); + } + } + printf(">"); +} diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index ef32d216..2c9dcafa 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -83,10 +83,6 @@ class Arg class BuildInScript { public: - std::string m_script; - std::string m_desc; - std::string m_cmd; - std::vector m_args; bool find_args(std::string arg) { for (size_t i = 0; i < m_args.size(); i++) @@ -157,6 +153,9 @@ class BuildInScript } } + inline const std::string &get_cmd() const noexcept { return m_cmd; } + inline const std::string &get_script() const noexcept { return m_script; } + void show() { printf("%s\n", m_script.c_str()); @@ -235,51 +234,22 @@ class BuildInScript } return script; } + +private: + std::string m_script; + std::string m_desc; + std::string m_cmd; + std::vector m_args; }; class BuildInScriptVector : public std::map { public: - BuildInScriptVector(const std::array &build_cmds) - { - for (const auto &build_cmd : build_cmds) { - BuildInScript one{build_cmd}; - (*this)[one.m_cmd] = one; - } - } - - void ShowAll() - { - for (auto iCol = begin(); iCol != end(); ++iCol) - { - iCol->second.show_cmd(); - } - } - - void ShowCmds() - { - printf("<"); - for (auto iCol = begin(); iCol != end(); ++iCol) - { - printf("%s%s%s", g_vt_boldwhite, iCol->first.c_str(), g_vt_default); - - auto i = iCol; - i++; - if(i != end()) - printf("|"); - } - printf(">"); - } - - void PrintAutoComplete(std::string match, const char *space=" " ) - { - for (auto iCol = begin(); iCol != end(); ++iCol) - { - if(iCol->first.substr(0, match.size()) == match) - printf("%s%s\n", iCol->first.c_str(), space); - } - } + BuildInScriptVector(const std::array &build_cmds); + void PrintAutoComplete(std::string match, const char *space=" " ); + void ShowAll(); + void ShowCmds(); }; extern BuildInScriptVector g_BuildScripts; diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index ebd2c204..bc8c3f35 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -777,7 +777,7 @@ int main(int argc, char **argv) } else { - printf("%s", g_BuildScripts[argv[i + 1]].m_script.c_str()); + printf("%s", g_BuildScripts[argv[i + 1]].get_script().c_str()); return 0; } } From f5f128070abd520ca2216d0ec858c76a9e34af72 Mon Sep 17 00:00:00 2001 From: markuspg Date: Fri, 10 Jul 2020 16:05:52 +0200 Subject: [PATCH 11/16] Move definitions out of buildincmd.h --- uuu/buildincmd.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++ uuu/buildincmd.h | 165 +++------------------------------------------ 2 files changed, 168 insertions(+), 156 deletions(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index 180bf156..a3b30050 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -86,6 +86,165 @@ static constexpr std::array g_buildin_cmd BuildInScriptVector g_BuildScripts(g_buildin_cmd); +int Arg::parser(string option) +{ + size_t pos; + pos = option.find('['); + if (pos == std::string::npos) + return 0; + m_options = option.substr(pos + 1, option.find(']') - pos - 1); + m_flags = ARG_OPTION | ARG_OPTION_KEY; + return 0; +} + +BuildInScript::BuildInScript(const BuildCmd &p) +{ + m_script = p.m_buildcmd; + if(p.m_desc) + m_desc = p.m_desc; + if(p.m_cmd) + m_cmd = p.m_cmd; + + for (size_t i = 1; i < m_script.size(); i++) + { + size_t off; + size_t off_tab; + std::string param; + if (m_script[i] == '_' + && (m_script[i - 1] == '@' || m_script[i - 1] == ' ')) + { + off = m_script.find(' ', i); + off_tab = m_script.find('\t', i); + size_t ofn = m_script.find('\n', i); + if (off_tab < off) + off = off_tab; + if (ofn < off) + off = ofn; + + if (off == std::string::npos) + off = m_script.size() + 1; + + param = m_script.substr(i, off - i); + if (!find_args(param)) + { + Arg a; + a.m_arg = param; + a.m_flags = Arg::ARG_MUST; + m_args.push_back(a); + } + } + } + + for (size_t i = 0; i < m_args.size(); i++) + { + size_t pos = 0; + std::string str; + str += "@"; + str += m_args[i].m_arg; + pos = m_script.find(str); + if (pos != std::string::npos) { + std::string def; + size_t start_descript; + start_descript = m_script.find('|', pos); + if (start_descript != std::string::npos) + { + m_args[i].m_desc = m_script.substr(start_descript + 1, + m_script.find('\n', start_descript) - start_descript - 1); + def = m_script.substr(pos, start_descript - pos); + m_args[i].parser(def); + } + } + } +} + +bool BuildInScript::find_args(string arg) +{ + for (size_t i = 0; i < m_args.size(); i++) + { + if (m_args[i].m_arg == arg) + { + return true; + } + } + return false; +} + +string BuildInScript::replace_script_args(vector args) +{ + std::string script = m_script; + for (size_t i = 0; i < args.size() && i < m_args.size(); i++) + { + script = replace_str(script, m_args[i].m_arg, args[i]); + } + + //handle option args; + for (size_t i = args.size(); i < m_args.size(); i++) + { + if (m_args[i].m_flags & Arg::ARG_OPTION_KEY) + { + for (size_t j = 0; j < args.size(); j++) + { + if (m_args[j].m_arg == m_args[i].m_options) + { + script = replace_str(script, m_args[i].m_arg, args[j]); + break; + } + } + } + } + return script; +} + +string BuildInScript::replace_str(string str, string key, string replace) +{ + if (replace.size() > 4) + { + if (str_to_upper(replace.substr(replace.size() - 4)) == ".BZ2") + { + replace += "/*"; + } + } + + for (size_t j = 0; (j = str.find(key, j)) != std::string::npos;) + { + str.replace(j, key.size(), replace); + j += key.size(); + } + return str; +} + +void BuildInScript::show_cmd() +{ + printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str()); + for (size_t i=0; i < m_args.size(); i++) + { + std::string desc; + desc += m_args[i].m_arg; + if (m_args[i].m_flags & Arg::ARG_OPTION) + { + desc += g_vt_boldwhite; + desc += "[Optional]"; + desc += g_vt_default; + } + desc += " "; + desc += m_args[i].m_desc; + printf("\t\targ%d: %s\n", (int)i, desc.c_str()); + } +} + +string BuildInScript::str_to_upper(string str) +{ + std::locale loc; + std::string s; + + for (size_t i = 0; i < str.size(); i++) + { + s.push_back(std::toupper(str[i], loc)); + } + + return s; +} + BuildInScriptVector::BuildInScriptVector(const array &build_cmds) { for (const auto &build_cmd : build_cmds) { diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index 2c9dcafa..18d2021c 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -68,172 +68,25 @@ class Arg ARG_OPTION_KEY = 0x4, }; Arg() { m_flags = ARG_MUST; } - int parser(std::string option) - { - size_t pos; - pos = option.find('['); - if (pos == std::string::npos) - return 0; - m_options = option.substr(pos + 1, option.find(']') - pos - 1); - m_flags = ARG_OPTION | ARG_OPTION_KEY; - return 0; - } + + int parser(std::string option); }; class BuildInScript { public: - bool find_args(std::string arg) - { - for (size_t i = 0; i < m_args.size(); i++) - { - if (m_args[i].m_arg == arg) - return true; - } - return false; - } + bool find_args(std::string arg); BuildInScript() {}; - BuildInScript(const BuildCmd &p) - { - m_script = p.m_buildcmd; - if(p.m_desc) - m_desc = p.m_desc; - if(p.m_cmd) - m_cmd = p.m_cmd; - - for (size_t i = 1; i < m_script.size(); i++) - { - size_t off; - size_t off_tab; - std::string param; - if (m_script[i] == '_' - && (m_script[i - 1] == '@' || m_script[i - 1] == ' ')) - { - off = m_script.find(' ', i); - off_tab = m_script.find('\t', i); - size_t ofn = m_script.find('\n', i); - if (off_tab < off) - off = off_tab; - if (ofn < off) - off = ofn; - - if (off == std::string::npos) - off = m_script.size() + 1; - - param = m_script.substr(i, off - i); - if (!find_args(param)) - { - Arg a; - a.m_arg = param; - a.m_flags = Arg::ARG_MUST; - m_args.push_back(a); - } - } - } - - for (size_t i = 0; i < m_args.size(); i++) - { - size_t pos = 0; - std::string str; - str += "@"; - str += m_args[i].m_arg; - pos = m_script.find(str); - if (pos != std::string::npos) { - std::string def; - size_t start_descript; - start_descript = m_script.find('|', pos); - if (start_descript != std::string::npos) - { - m_args[i].m_desc = m_script.substr(start_descript + 1, - m_script.find('\n', start_descript) - start_descript - 1); - def = m_script.substr(pos, start_descript - pos); - m_args[i].parser(def); - } - } - } - } + BuildInScript(const BuildCmd &p); inline const std::string &get_cmd() const noexcept { return m_cmd; } inline const std::string &get_script() const noexcept { return m_script; } + std::string replace_script_args(std::vector args); + std::string replace_str(std::string str, std::string key, std::string replace); + inline void show() { printf("%s\n", m_script.c_str()); } + void show_cmd(); + std::string str_to_upper(std::string str); - void show() - { - printf("%s\n", m_script.c_str()); - } - - void show_cmd() - { - printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str()); - for (size_t i=0; i < m_args.size(); i++) - { - std::string desc; - desc += m_args[i].m_arg; - if (m_args[i].m_flags & Arg::ARG_OPTION) - { - desc += g_vt_boldwhite; - desc += "[Optional]"; - desc += g_vt_default; - } - desc += " "; - desc += m_args[i].m_desc; - printf("\t\targ%d: %s\n", (int)i, desc.c_str()); - } - } - - inline std::string str_to_upper(std::string str) - { - std::locale loc; - std::string s; - - for (size_t i = 0; i < str.size(); i++) - s.push_back(std::toupper(str[i], loc)); - - return s; - } - - std::string replace_str(std::string str, std::string key, std::string replace) - { - if (replace.size() > 4) - { - if (str_to_upper(replace.substr(replace.size() - 4)) == ".BZ2") - { - replace += "/*"; - } - } - - for (size_t j = 0; (j = str.find(key, j)) != std::string::npos;) - { - str.replace(j, key.size(), replace); - j += key.size(); - } - return str; - } - - std::string replace_script_args(std::vector args) - { - std::string script = m_script; - for (size_t i = 0; i < args.size() && i < m_args.size(); i++) - { - script = replace_str(script, m_args[i].m_arg, args[i]); - } - - //handle option args; - for (size_t i = args.size(); i < m_args.size(); i++) - { - if (m_args[i].m_flags & Arg::ARG_OPTION_KEY) - { - for (size_t j = 0; j < args.size(); j++) - { - if (m_args[j].m_arg == m_args[i].m_options) - { - script = replace_str(script, m_args[i].m_arg, args[j]); - break; - } - } - } - } - return script; - } private: std::string m_script; From b4d513629aba07c35122788bbed00240c7b398c5 Mon Sep 17 00:00:00 2001 From: markuspg Date: Fri, 10 Jul 2020 16:38:25 +0200 Subject: [PATCH 12/16] Polishing for function definitions --- uuu/buildincmd.cpp | 68 ++++++++++++++++++++++++---------------------- uuu/buildincmd.h | 17 ++++++------ 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index a3b30050..5e53f6e3 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -97,24 +97,26 @@ int Arg::parser(string option) return 0; } -BuildInScript::BuildInScript(const BuildCmd &p) +BuildInScript::BuildInScript(const BuildCmd &p) : + m_script{p.m_buildcmd} { - m_script = p.m_buildcmd; - if(p.m_desc) + if (p.m_desc) + { m_desc = p.m_desc; - if(p.m_cmd) + } + if (p.m_cmd) + { m_cmd = p.m_cmd; + } for (size_t i = 1; i < m_script.size(); i++) { - size_t off; - size_t off_tab; - std::string param; + string param; if (m_script[i] == '_' && (m_script[i - 1] == '@' || m_script[i - 1] == ' ')) { - off = m_script.find(' ', i); - off_tab = m_script.find('\t', i); + size_t off = m_script.find(' ', i); + size_t off_tab = m_script.find('\t', i); size_t ofn = m_script.find('\n', i); if (off_tab < off) off = off_tab; @@ -157,11 +159,11 @@ BuildInScript::BuildInScript(const BuildCmd &p) } } -bool BuildInScript::find_args(string arg) +bool BuildInScript::find_args(const string &arg) const { - for (size_t i = 0; i < m_args.size(); i++) + for (const auto &arg_ref : m_args) { - if (m_args[i].m_arg == arg) + if (arg_ref.m_arg == arg) { return true; } @@ -171,7 +173,7 @@ bool BuildInScript::find_args(string arg) string BuildInScript::replace_script_args(vector args) { - std::string script = m_script; + string script{m_script}; for (size_t i = 0; i < args.size() && i < m_args.size(); i++) { script = replace_str(script, m_args[i].m_arg, args[i]); @@ -195,7 +197,7 @@ string BuildInScript::replace_script_args(vector args) return script; } -string BuildInScript::replace_str(string str, string key, string replace) +string BuildInScript::replace_str(string str, const string &key, string replace) { if (replace.size() > 4) { @@ -213,13 +215,12 @@ string BuildInScript::replace_str(string str, string key, string replace) return str; } -void BuildInScript::show_cmd() +void BuildInScript::show_cmd() const { printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str()); - for (size_t i=0; i < m_args.size(); i++) + for (size_t i = 0; i < m_args.size(); i++) { - std::string desc; - desc += m_args[i].m_arg; + string desc{m_args[i].m_arg}; if (m_args[i].m_flags & Arg::ARG_OPTION) { desc += g_vt_boldwhite; @@ -228,14 +229,15 @@ void BuildInScript::show_cmd() } desc += " "; desc += m_args[i].m_desc; - printf("\t\targ%d: %s\n", (int)i, desc.c_str()); + printf("\t\targ%d: %s\n", static_cast(i), desc.c_str()); } } -string BuildInScript::str_to_upper(string str) +string BuildInScript::str_to_upper(const string &str) { - std::locale loc; + const std::locale loc; std::string s; + s.reserve(str.size()); for (size_t i = 0; i < str.size(); i++) { @@ -253,35 +255,37 @@ BuildInScriptVector::BuildInScriptVector(const array &build_c } } -void BuildInScriptVector::PrintAutoComplete(string match, const char *space) +void BuildInScriptVector::PrintAutoComplete(const string &match, const char *space) { - for (auto iCol = begin(); iCol != end(); ++iCol) + for (const auto &str_n_script : *this) { - if(iCol->first.substr(0, match.size()) == match) + if(str_n_script.first.substr(0, match.size()) == match) { - printf("%s%s\n", iCol->first.c_str(), space); + printf("%s%s\n", str_n_script.first.c_str(), space); } } } -void BuildInScriptVector::ShowAll() +void BuildInScriptVector::ShowAll() const { - for (auto iCol = begin(); iCol != end(); ++iCol) + for (const auto &str_n_script : *this) { - iCol->second.show_cmd(); + str_n_script.second.show_cmd(); } } -void BuildInScriptVector::ShowCmds() +void BuildInScriptVector::ShowCmds() const { printf("<"); - for (auto iCol = begin(); iCol != end(); ++iCol) + for (auto iCol = cbegin(); iCol != cend(); ++iCol) { + // Print the current command's name printf("%s%s%s", g_vt_boldwhite, iCol->first.c_str(), g_vt_default); + // Check if it's the last command and if not print a separating bar auto i = iCol; - i++; - if(i != end()) + ++i; + if(i != cend()) { printf("|"); } diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index 18d2021c..283a0e64 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -75,20 +75,21 @@ class Arg class BuildInScript { public: - bool find_args(std::string arg); BuildInScript() {}; BuildInScript(const BuildCmd &p); inline const std::string &get_cmd() const noexcept { return m_cmd; } inline const std::string &get_script() const noexcept { return m_script; } std::string replace_script_args(std::vector args); - std::string replace_str(std::string str, std::string key, std::string replace); inline void show() { printf("%s\n", m_script.c_str()); } - void show_cmd(); - std::string str_to_upper(std::string str); - + void show_cmd() const; private: + bool find_args(const std::string &arg) const; + static std::string replace_str(std::string str, const std::string &key, std::string replace); + static std::string str_to_upper(const std::string &str); + + std::string m_script; std::string m_desc; std::string m_cmd; @@ -100,9 +101,9 @@ class BuildInScriptVector : public std::map public: BuildInScriptVector(const std::array &build_cmds); - void PrintAutoComplete(std::string match, const char *space=" " ); - void ShowAll(); - void ShowCmds(); + void PrintAutoComplete(const std::string &match, const char *space=" " ); + void ShowAll() const; + void ShowCmds() const; }; extern BuildInScriptVector g_BuildScripts; From ebb65fc6fe8fd22b4c96cc21bf72b74656cbce77 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 11 Jul 2020 14:48:07 +0200 Subject: [PATCH 13/16] Reduce scope of include directive --- uuu/buildincmd.cpp | 2 ++ uuu/buildincmd.h | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index 5e53f6e3..a90ee24d 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -31,6 +31,8 @@ #include "buildincmd.h" +#include + using namespace std; constexpr BuildCmd::BuildCmd(const char * const cmd, const char * const buildcmd, const char * const desc) : diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index 283a0e64..4eed3f93 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -31,7 +31,6 @@ #pragma once -#include #include #include #include From 1c038ffebac0019b53adb107a8ef00629b793160 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 11 Jul 2020 14:59:08 +0200 Subject: [PATCH 14/16] Reduce visibility of Arg's members --- uuu/buildincmd.cpp | 17 ++++++++--------- uuu/buildincmd.h | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/uuu/buildincmd.cpp b/uuu/buildincmd.cpp index a90ee24d..e809880e 100644 --- a/uuu/buildincmd.cpp +++ b/uuu/buildincmd.cpp @@ -88,7 +88,7 @@ static constexpr std::array g_buildin_cmd BuildInScriptVector g_BuildScripts(g_buildin_cmd); -int Arg::parser(string option) +int Arg::parser(const string &option) { size_t pos; pos = option.find('['); @@ -131,8 +131,7 @@ BuildInScript::BuildInScript(const BuildCmd &p) : param = m_script.substr(i, off - i); if (!find_args(param)) { - Arg a; - a.m_arg = param; + Arg a{param}; a.m_flags = Arg::ARG_MUST; m_args.push_back(a); } @@ -144,7 +143,7 @@ BuildInScript::BuildInScript(const BuildCmd &p) : size_t pos = 0; std::string str; str += "@"; - str += m_args[i].m_arg; + str += m_args[i].get_arg(); pos = m_script.find(str); if (pos != std::string::npos) { std::string def; @@ -165,7 +164,7 @@ bool BuildInScript::find_args(const string &arg) const { for (const auto &arg_ref : m_args) { - if (arg_ref.m_arg == arg) + if (arg_ref.get_arg() == arg) { return true; } @@ -178,7 +177,7 @@ string BuildInScript::replace_script_args(vector args) string script{m_script}; for (size_t i = 0; i < args.size() && i < m_args.size(); i++) { - script = replace_str(script, m_args[i].m_arg, args[i]); + script = replace_str(script, m_args[i].get_arg(), args[i]); } //handle option args; @@ -188,9 +187,9 @@ string BuildInScript::replace_script_args(vector args) { for (size_t j = 0; j < args.size(); j++) { - if (m_args[j].m_arg == m_args[i].m_options) + if (m_args[j].get_arg() == m_args[i].get_options()) { - script = replace_str(script, m_args[i].m_arg, args[j]); + script = replace_str(script, m_args[i].get_arg(), args[j]); break; } } @@ -222,7 +221,7 @@ void BuildInScript::show_cmd() const printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str()); for (size_t i = 0; i < m_args.size(); i++) { - string desc{m_args[i].m_arg}; + string desc{m_args[i].get_arg()}; if (m_args[i].m_flags & Arg::ARG_OPTION) { desc += g_vt_boldwhite; diff --git a/uuu/buildincmd.h b/uuu/buildincmd.h index 4eed3f93..906c8fdd 100644 --- a/uuu/buildincmd.h +++ b/uuu/buildincmd.h @@ -56,19 +56,25 @@ struct BuildCmd class Arg { public: - std::string m_arg; - std::string m_desc; - uint32_t m_flags; - std::string m_options; enum { ARG_MUST = 0x1, ARG_OPTION = 0x2, ARG_OPTION_KEY = 0x4, }; - Arg() { m_flags = ARG_MUST; } - int parser(std::string option); + Arg(const std::string &arg) : m_arg{arg} {} + + const std::string &get_arg() const noexcept { return m_arg; } + const std::string &get_options() const noexcept { return m_options; } + int parser(const std::string &option); + + std::string m_desc; + uint32_t m_flags = ARG_MUST; + +private: + std::string m_arg; + std::string m_options; }; class BuildInScript From 4a7607e8cac9b1b619be966cdc3edf82e1996fb1 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 11 Jul 2020 20:52:06 +0200 Subject: [PATCH 15/16] Reduce variable scope and make static --- uuu/uuu.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index bc8c3f35..8704c0bf 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -72,10 +72,6 @@ int get_console_width(); int auto_complete(int argc, char**argv); void print_autocomplete_help(); -static char g_sample_cmd_list[] = { -#include "uuu.clst" -}; - static vector g_usb_path_filter; static int g_verbose = 0; @@ -92,7 +88,10 @@ void ctrl_c_handle(int) void print_help(bool detail = false) { - const char help[] = + static char g_sample_cmd_list[] = { + #include "uuu.clst" + }; + static constexpr char help[] = "uuu [-d -m -v -V] <" "bootloader|cmdlists|cmd" ">\n\n" " bootloader download bootloader to board by usb\n" " cmdlist run all commands in cmdlist file\n" From b1583ba17dca4634f58cf73e78f31ca49a5e5a67 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 11 Jul 2020 20:52:35 +0200 Subject: [PATCH 16/16] Add own header for autocomplete functions --- uuu/autocomplete.cpp | 2 ++ uuu/autocomplete.h | 35 +++++++++++++++++++++++++++++++++++ uuu/uuu.cpp | 3 +-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 uuu/autocomplete.h diff --git a/uuu/autocomplete.cpp b/uuu/autocomplete.cpp index 36dc45ef..49a2b27b 100644 --- a/uuu/autocomplete.cpp +++ b/uuu/autocomplete.cpp @@ -29,6 +29,8 @@ * */ +#include "autocomplete.h" + #include #include #include diff --git a/uuu/autocomplete.h b/uuu/autocomplete.h new file mode 100644 index 00000000..a6a41896 --- /dev/null +++ b/uuu/autocomplete.h @@ -0,0 +1,35 @@ +/* +* Copyright 2020 NXP. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* Neither the name of the NXP Semiconductor nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#pragma once + +int auto_complete(int argc, char**argv); +void print_autocomplete_help(); diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 8704c0bf..4fdec723 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -29,6 +29,7 @@ * */ +#include "autocomplete.h" #include "print_helpers.h" #include #include @@ -69,8 +70,6 @@ void clean_vt_color() noexcept using namespace std; int get_console_width(); -int auto_complete(int argc, char**argv); -void print_autocomplete_help(); static vector g_usb_path_filter;