mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: use range based for and std::string in enumerate_devices()
Just because now we can... Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
81cd91f78b
commit
3939cc8da2
2 changed files with 13 additions and 27 deletions
|
@ -90,8 +90,7 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t
|
||||||
const char *dirname = "/dev";
|
const char *dirname = "/dev";
|
||||||
const char *patterns[] = {
|
const char *patterns[] = {
|
||||||
"tty.*",
|
"tty.*",
|
||||||
"usbserial",
|
"usbserial"
|
||||||
NULL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dp = opendir(dirname);
|
dp = opendir(dirname);
|
||||||
|
@ -100,16 +99,11 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((ep = readdir(dp)) != NULL) {
|
while ((ep = readdir(dp)) != NULL) {
|
||||||
for (i = 0; patterns[i] != NULL; ++i) {
|
for (const char *pattern: patterns) {
|
||||||
if (fnmatch(patterns[i], ep->d_name, 0) == 0) {
|
if (fnmatch(pattern, ep->d_name, 0) == 0) {
|
||||||
char filename[1024];
|
std::string filename = std::string(dirname) + "/" + ep->d_name;
|
||||||
int n = snprintf(filename, sizeof(filename), "%s/%s", dirname, ep->d_name);
|
callback(filename.c_str(), userdata);
|
||||||
if (n >= (int)sizeof(filename)) {
|
if (is_default_dive_computer_device(filename.c_str()))
|
||||||
closedir(dp);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
callback(filename, userdata);
|
|
||||||
if (is_default_dive_computer_device(filename))
|
|
||||||
index = entries;
|
index = entries;
|
||||||
entries++;
|
entries++;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -70,7 +70,6 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t
|
||||||
int index = -1, entries = 0;
|
int index = -1, entries = 0;
|
||||||
DIR *dp = NULL;
|
DIR *dp = NULL;
|
||||||
struct dirent *ep = NULL;
|
struct dirent *ep = NULL;
|
||||||
size_t i;
|
|
||||||
FILE *file;
|
FILE *file;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
@ -79,16 +78,14 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t
|
||||||
#ifdef __OpenBSD__
|
#ifdef __OpenBSD__
|
||||||
const char *patterns[] = {
|
const char *patterns[] = {
|
||||||
"ttyU*",
|
"ttyU*",
|
||||||
"ttyC*",
|
"ttyC*"
|
||||||
NULL
|
|
||||||
};
|
};
|
||||||
#else
|
#else
|
||||||
const char *patterns[] = {
|
const char *patterns[] = {
|
||||||
"ttyUSB*",
|
"ttyUSB*",
|
||||||
"ttyS*",
|
"ttyS*",
|
||||||
"ttyACM*",
|
"ttyACM*",
|
||||||
"rfcomm*",
|
"rfcomm*"
|
||||||
NULL
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -98,16 +95,11 @@ int enumerate_devices(device_callback_t callback, void *userdata, unsigned int t
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((ep = readdir(dp)) != NULL) {
|
while ((ep = readdir(dp)) != NULL) {
|
||||||
for (i = 0; patterns[i] != NULL; ++i) {
|
for (const char *pattern: patterns) {
|
||||||
if (fnmatch(patterns[i], ep->d_name, 0) == 0) {
|
if (fnmatch(pattern, ep->d_name, 0) == 0) {
|
||||||
char filename[1024];
|
std::string filename = std::string(dirname) + "/" + ep->d_name;
|
||||||
int n = snprintf(filename, sizeof(filename), "%s/%s", dirname, ep->d_name);
|
callback(filename.c_str(), userdata);
|
||||||
if (n >= (int)sizeof(filename)) {
|
if (is_default_dive_computer_device(filename.c_str()))
|
||||||
closedir(dp);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
callback(filename, userdata);
|
|
||||||
if (is_default_dive_computer_device(filename))
|
|
||||||
index = entries;
|
index = entries;
|
||||||
entries++;
|
entries++;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Reference in a new issue