Clean up code

This commit is contained in:
Adam Honse 2025-07-16 08:31:48 -05:00
parent b6afcc222a
commit fed03f88c8

View file

@ -11,20 +11,20 @@
| SPDX-License-Identifier: GPL-2.0-only | | SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/ \*---------------------------------------------------------*/
#include "serial_port.h" #include <algorithm>
#include "filesystem.h" #include "filesystem.h"
#include <algorithm> // For std::sort only #include "serial_port.h"
#ifdef __APPLE__ #ifdef __APPLE__
#include <regex> #include <regex>
#endif #endif
/*---------------------------------------------------------*\ /*---------------------------------------------------------*\
| getSerialPorts(): returns the list of available serial | | getSerialPorts |
| ports in the system | | |
| Returns the list of available serial ports in the |
| system |
\*---------------------------------------------------------*/ \*---------------------------------------------------------*/
std::vector<std::string> serial_port::getSerialPorts() std::vector<std::string> serial_port::getSerialPorts()
{ {
/*-----------------------------------------------------------------------------------*\ /*-----------------------------------------------------------------------------------*\
@ -32,15 +32,23 @@ std::vector<std::string> serial_port::getSerialPorts()
\*-----------------------------------------------------------------------------------*/ \*-----------------------------------------------------------------------------------*/
std::vector<std::string> port_list; std::vector<std::string> port_list;
#if defined (_WIN32) || defined( _WIN64) #if defined (_WIN32) || defined( _WIN64)
const uint32_t CHAR_NUM = 1024; const uint32_t CHAR_NUM = 1024;
const uint32_t MAX_PORTS = 255; const uint32_t MAX_PORTS = 255;
const std::string COM_STR = "COM"; const std::string COM_STR = "COM";
char path[CHAR_NUM]; char path[CHAR_NUM];
for (uint32_t k = 0; k < MAX_PORTS; k++)
for(uint32_t k = 0; k < MAX_PORTS; k++)
{ {
std::string port_name = COM_STR + std::to_string(k); std::string port_name = COM_STR + std::to_string(k);
DWORD test = QueryDosDevice(port_name.c_str(), path, CHAR_NUM); DWORD test = QueryDosDevice(port_name.c_str(), path, CHAR_NUM);
if (test == 0) continue;
if(test == 0)
{
continue;
}
port_list.push_back(port_name); port_list.push_back(port_name);
} }
#endif #endif
@ -49,37 +57,61 @@ std::vector<std::string> serial_port::getSerialPorts()
try try
{ {
filesystem::path p(DEV_PATH); filesystem::path p(DEV_PATH);
if (!filesystem::exists(DEV_PATH)) return port_list;
for (filesystem::directory_entry de: filesystem::directory_iterator(p)) if(!filesystem::exists(DEV_PATH))
{ {
if (filesystem::is_symlink(de.symlink_status())) return port_list;
}
for(filesystem::directory_entry de: filesystem::directory_iterator(p))
{
if(filesystem::is_symlink(de.symlink_status()))
{ {
filesystem::path symlink_points_at = filesystem::read_symlink(de); filesystem::path symlink_points_at = filesystem::read_symlink(de);
port_list.push_back(std::string("/dev/")+symlink_points_at.filename().c_str()); port_list.push_back(std::string("/dev/")+symlink_points_at.filename().c_str());
} }
} }
} }
catch (const filesystem::filesystem_error &ex) {} catch(const filesystem::filesystem_error &ex)
{
}
#endif #endif
#if defined(__APPLE__) #if defined(__APPLE__)
const std::string DEV_PATH = "/dev"; const std::string DEV_PATH = "/dev";
const std::regex base_regex(R"(\/dev\/(tty|cu)\..*)"); const std::regex base_regex(R"(\/dev\/(tty|cu)\..*)");
try try
{ {
filesystem::path p(DEV_PATH); filesystem::path p(DEV_PATH);
if (!filesystem::exists(DEV_PATH)) return port_list;
for (filesystem::directory_entry de: filesystem::directory_iterator(p)) { if(!filesystem::exists(DEV_PATH))
filesystem::path canonical_path = filesystem::canonical(de); {
std::string name = canonical_path.generic_string(); return port_list;
std::smatch res; }
for(filesystem::directory_entry de: filesystem::directory_iterator(p))
{
filesystem::path canonical_path = filesystem::canonical(de);
std::string name = canonical_path.generic_string();
std::smatch res;
std::regex_search(name, res, base_regex); std::regex_search(name, res, base_regex);
if (res.empty()) continue;
if(res.empty())
{
continue;
}
port_list.push_back(canonical_path.generic_string()); port_list.push_back(canonical_path.generic_string());
} }
} }
catch (const filesystem::filesystem_error &ex) {} catch(const filesystem::filesystem_error &ex)
{
}
#endif #endif
std::sort(port_list.begin(), port_list.end()); std::sort(port_list.begin(), port_list.end());
return port_list; return port_list;
} }