From 3939cc8da28e7ab39e0bed22c9ce2298f1a72cbd Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 29 Apr 2023 18:02:13 +0200 Subject: [PATCH] core: use range based for and std::string in enumerate_devices() Just because now we can... Signed-off-by: Berthold Stoeger --- core/macos.cpp | 18 ++++++------------ core/unix.cpp | 22 +++++++--------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/core/macos.cpp b/core/macos.cpp index 08efe8ea8..5a8a46d04 100644 --- a/core/macos.cpp +++ b/core/macos.cpp @@ -90,8 +90,7 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t const char *dirname = "/dev"; const char *patterns[] = { "tty.*", - "usbserial", - NULL + "usbserial" }; dp = opendir(dirname); @@ -100,16 +99,11 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t } while ((ep = readdir(dp)) != NULL) { - for (i = 0; patterns[i] != NULL; ++i) { - if (fnmatch(patterns[i], ep->d_name, 0) == 0) { - char filename[1024]; - int n = snprintf(filename, sizeof(filename), "%s/%s", dirname, ep->d_name); - if (n >= (int)sizeof(filename)) { - closedir(dp); - return -1; - } - callback(filename, userdata); - if (is_default_dive_computer_device(filename)) + for (const char *pattern: patterns) { + if (fnmatch(pattern, ep->d_name, 0) == 0) { + std::string filename = std::string(dirname) + "/" + ep->d_name; + callback(filename.c_str(), userdata); + if (is_default_dive_computer_device(filename.c_str())) index = entries; entries++; break; diff --git a/core/unix.cpp b/core/unix.cpp index c46b2cdd3..c0b288cc2 100644 --- a/core/unix.cpp +++ b/core/unix.cpp @@ -70,7 +70,6 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t int index = -1, entries = 0; DIR *dp = NULL; struct dirent *ep = NULL; - size_t i; FILE *file; char *line = NULL; size_t len; @@ -79,16 +78,14 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t #ifdef __OpenBSD__ const char *patterns[] = { "ttyU*", - "ttyC*", - NULL + "ttyC*" }; #else const char *patterns[] = { "ttyUSB*", "ttyS*", "ttyACM*", - "rfcomm*", - NULL + "rfcomm*" }; #endif @@ -98,16 +95,11 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t } while ((ep = readdir(dp)) != NULL) { - for (i = 0; patterns[i] != NULL; ++i) { - if (fnmatch(patterns[i], ep->d_name, 0) == 0) { - char filename[1024]; - int n = snprintf(filename, sizeof(filename), "%s/%s", dirname, ep->d_name); - if (n >= (int)sizeof(filename)) { - closedir(dp); - return -1; - } - callback(filename, userdata); - if (is_default_dive_computer_device(filename)) + for (const char *pattern: patterns) { + if (fnmatch(pattern, ep->d_name, 0) == 0) { + std::string filename = std::string(dirname) + "/" + ep->d_name; + callback(filename.c_str(), userdata); + if (is_default_dive_computer_device(filename.c_str())) index = entries; entries++; break;