uemis-downloader: use std::string for constructing strings

This extends work started by Richard Fuchs <dfx@dfx.at>

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-04-25 06:53:51 +02:00
parent b5f0e18ca8
commit 17f768b69c

View file

@ -250,7 +250,7 @@ static long bytes_available(int file)
return result; return result;
} }
static int number_of_file(const std::string& path) static int number_of_file(const std::string &path)
{ {
int count = 0; int count = 0;
#ifdef WIN32 #ifdef WIN32
@ -282,38 +282,28 @@ static int number_of_file(const std::string& path)
return count; 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 #if WIN32
str = path + "\\" + name; return path + "\\" + name;
#else #else
str = path + "/" + name; return path + "/" + name;
#endif #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. /* 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 * Test for the maximum number of ANS files (I believe this is always
* 4000 but in case there are differences depending on firmware, this * 4000 but in case there are differences depending on firmware, this
* code is easy enough */ * code is easy enough */
static bool uemis_init(const char *path) static bool uemis_init(const std::string &path)
{ {
std::string ans_path; using namespace std::string_literals;
int i;
erase_divespot_mapping(); erase_divespot_mapping();
if (!path) if (path.empty())
return false; return false;
/* let's check if this is indeed a Uemis DC */ /* 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); reqtxt_file = subsurface_open(reqtxt_path.c_str(), O_RDONLY | O_CREAT, 0666);
if (reqtxt_file < 0) { if (reqtxt_file < 0) {
#if UEMIS_DEBUG & 1 #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 /* 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 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); number_of_files = number_of_file(ans_path);
/* initialize the array in which we collect the answers */ /* 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] = ""; param_buff[i] = "";
return true; return true;
} }
@ -523,14 +513,18 @@ static void uemis_increased_timeout(int *timeout)
usleep(*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"; /* Clamp filenumber into the 0..9999 range. This is never necessary,
intermediate = build_filename(path, "ANS"); * as filenumber can never go above UEMIS_MAX_FILES, but gcc doesn't
ans_path = build_filename(intermediate, fl); * recognize that and produces very noisy warnings. */
return ans_path; 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 */ /* 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 found_answer = false;
bool more_files = true; bool more_files = true;
bool answer_in_mbuf = false; bool answer_in_mbuf = false;
std::string ans_path;
int ans_file; int ans_file;
int timeout = UEMIS_LONG_TIMEOUT; 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) { if (reqtxt_file < 0) {
*error_text = "can't open req.txt"; *error_text = "can't open req.txt";
#ifdef UEMIS_DEBUG #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 #endif
return false; return false;
} }
@ -600,12 +593,12 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_
if (import_thread_cancelled) if (import_thread_cancelled)
return false; return false;
progress_bar_fraction = filenr / (double)UEMIS_MAX_FILES; 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); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666);
if (ans_file < 0) { if (ans_file < 0) {
*error_text = "can't open Uemis response file"; *error_text = "can't open Uemis response file";
#ifdef UEMIS_DEBUG #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 #endif
return false; return false;
} }
@ -616,14 +609,14 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_
close(ans_file); close(ans_file);
#if UEMIS_DEBUG & 8 #if UEMIS_DEBUG & 8
tmp[100] = '\0'; 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 #elif UEMIS_DEBUG & 4
char pbuf[4]; char pbuf[4];
pbuf[0] = tmp[0]; pbuf[0] = tmp[0];
pbuf[1] = tmp[1]; pbuf[1] = tmp[1];
pbuf[2] = tmp[2]; pbuf[2] = tmp[2];
pbuf[3] = 0; pbuf[3] = 0;
report_info("::t %s \"%s...\"\n", ans_path, pbuf); report_info("::t %s \"%s...\"\n", ans_path.c_str(), pbuf);
#endif #endif
if (tmp[0] == '1') { if (tmp[0] == '1') {
searching = false; 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') { if (ismulti && more_files && tmp[0] == '1') {
int size; 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); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666);
if (ans_file < 0) { if (ans_file < 0) {
*error_text = "can't open Uemis response file"; *error_text = "can't open Uemis response file";
#ifdef UEMIS_DEBUG #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 #endif
return false; return false;
} }
@ -701,12 +694,12 @@ static bool uemis_get_answer(const char *path, const char *request, int n_param_
char *buf = NULL; char *buf = NULL;
if (!ismulti) { 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); ans_file = subsurface_open(ans_path.c_str(), O_RDONLY, 0666);
if (ans_file < 0) { if (ans_file < 0) {
*error_text = "can't open Uemis response file"; *error_text = "can't open Uemis response file";
#ifdef UEMIS_DEBUG #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 #endif
return false; 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); buffer_add(&mbuf, &mbuf_size, buf);
show_progress(buf, what); show_progress(buf, what);
#if UEMIS_DEBUG & 8 #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 #endif
} }
size -= 3; size -= 3;