From 16e19b550b6bc3d05e2f4327865d54bed2965076 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 25 Apr 2024 06:53:51 +0200 Subject: [PATCH] uemis-downloader: use std::string for constructing strings This extends work started by Richard Fuchs Signed-off-by: Berthold Stoeger --- core/uemis-downloader.cpp | 69 ++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/core/uemis-downloader.cpp b/core/uemis-downloader.cpp index 3a64176c4..c30a2bd92 100644 --- a/core/uemis-downloader.cpp +++ b/core/uemis-downloader.cpp @@ -250,7 +250,7 @@ static long bytes_available(int file) return result; } -static int number_of_file(const std::string& path) +static int number_of_file(const std::string &path) { int count = 0; #ifdef WIN32 @@ -282,38 +282,28 @@ static int number_of_file(const std::string& path) return count; } -static std::string build_filename(const std::string& path, const std::string& name) +static std::string build_filename(const std::string &path, const std::string &name) { - std::string str; #if WIN32 - str = path + "\\" + name; + return path + "\\" + name; #else - str = path + "/" + name; + return path + "/" + name; #endif - return str; -} -static std::string build_filename(const std::string& path, const char* name) -{ - return build_filename(path, std::string(name)); -} -static std::string build_filename(const char* path, const char* name) -{ - return build_filename(std::string(path), std::string(name)); } /* Check if there's a req.txt file and get the starting filenr from it. * Test for the maximum number of ANS files (I believe this is always * 4000 but in case there are differences depending on firmware, this * code is easy enough */ -static bool uemis_init(const char *path) +static bool uemis_init(const std::string &path) { - std::string ans_path; - int i; + using namespace std::string_literals; + erase_divespot_mapping(); - if (!path) + if (path.empty()) return false; /* let's check if this is indeed a Uemis DC */ - reqtxt_path = build_filename(path, "req.txt"); + reqtxt_path = build_filename(path, "req.txt"s); reqtxt_file = subsurface_open(reqtxt_path.c_str(), O_RDONLY | O_CREAT, 0666); if (reqtxt_file < 0) { #if UEMIS_DEBUG & 1 @@ -341,10 +331,10 @@ static bool uemis_init(const char *path) /* It would be nice if we could simply go back to the first set of * ANS files. But with a FAT filesystem that isn't possible */ - ans_path = build_filename(path, "ANS"); + std::string ans_path = build_filename(path, "ANS"s); number_of_files = number_of_file(ans_path); /* initialize the array in which we collect the answers */ - for (i = 0; i < NUM_PARAM_BUFS; i++) + for (int i = 0; i < NUM_PARAM_BUFS; i++) param_buff[i] = ""; return true; } @@ -523,14 +513,18 @@ static void uemis_increased_timeout(int *timeout) usleep(*timeout); } -static std::string build_ans_path(const std::string& path, int filenumber) +static std::string build_ans_path(const std::string &path, int filenumber) { - std::string intermediate, ans_path; + using namespace std::string_literals; - std::string fl = std::string("ANS") + std::to_string(filenumber) + ".TXT"; - intermediate = build_filename(path, "ANS"); - ans_path = build_filename(intermediate, fl); - return ans_path; + /* Clamp filenumber into the 0..9999 range. This is never necessary, + * as filenumber can never go above UEMIS_MAX_FILES, but gcc doesn't + * recognize that and produces very noisy warnings. */ + filenumber = filenumber < 0 ? 0 : filenumber % 10000; + + std::string fl = "ANS"s + std::to_string(filenumber) + ".TXT"s; + std::string intermediate = build_filename(path, "ANS"s); + return build_filename(intermediate, fl); } /* send a request to the dive computer and collect the answer */ @@ -548,7 +542,6 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ bool found_answer = false; bool more_files = true; bool answer_in_mbuf = false; - std::string ans_path; int ans_file; int timeout = UEMIS_LONG_TIMEOUT; @@ -556,7 +549,7 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ if (reqtxt_file < 0) { *error_text = "can't open req.txt"; #ifdef UEMIS_DEBUG - report_info("open %s failed with errno %d\n", reqtxt_path, errno); + report_info("open %s failed with errno %d\n", reqtxt_path.c_str(), errno); #endif return false; } @@ -600,12 +593,12 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ if (import_thread_cancelled) return false; progress_bar_fraction = filenr / (double)UEMIS_MAX_FILES; - ans_path = build_ans_path(path, filenr - 1); + std::string ans_path = build_ans_path(std::string(path), filenr - 1); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666); if (ans_file < 0) { *error_text = "can't open Uemis response file"; #ifdef UEMIS_DEBUG - report_info("open %s failed with errno %d\n", ans_path, errno); + report_info("open %s failed with errno %d\n", ans_path.c_str(), errno); #endif return false; } @@ -616,14 +609,14 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ close(ans_file); #if UEMIS_DEBUG & 8 tmp[100] = '\0'; - report_info("::t %s \"%s\"\n", ans_path, tmp); + report_info("::t %s \"%s\"\n", ans_path.c_str(), tmp); #elif UEMIS_DEBUG & 4 char pbuf[4]; pbuf[0] = tmp[0]; pbuf[1] = tmp[1]; pbuf[2] = tmp[2]; pbuf[3] = 0; - report_info("::t %s \"%s...\"\n", ans_path, pbuf); + report_info("::t %s \"%s...\"\n", ans_path.c_str(), pbuf); #endif if (tmp[0] == '1') { searching = false; @@ -665,12 +658,12 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ } if (ismulti && more_files && tmp[0] == '1') { int size; - ans_path = build_ans_path(path, assembling_mbuf ? filenr - 2 : filenr - 1); + std::string ans_path = build_ans_path(std::string(path), assembling_mbuf ? filenr - 2 : filenr - 1); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666); if (ans_file < 0) { *error_text = "can't open Uemis response file"; #ifdef UEMIS_DEBUG - report_info("open %s failed with errno %d\n", ans_path, errno); + report_info("open %s failed with errno %d\n", ans_path.c_str(), errno); #endif return false; } @@ -701,12 +694,12 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ char *buf = NULL; if (!ismulti) { - ans_path = build_ans_path(path, filenr - 1); + std::string ans_path = build_ans_path(std::string(path), filenr - 1); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666); if (ans_file < 0) { *error_text = "can't open Uemis response file"; #ifdef UEMIS_DEBUG - report_info("open %s failed with errno %d\n", ans_path, errno); + report_info("open %s failed with errno %d\n", ans_path.c_str(), errno); #endif return false; } @@ -725,7 +718,7 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_ buffer_add(&mbuf, &mbuf_size, buf); show_progress(buf, what); #if UEMIS_DEBUG & 8 - report_info("::r %s \"%s\"\n", ans_path, buf); + report_info("::r %s \"%s\"\n", ans_path.c_str(), buf); #endif } size -= 3;