diff --git a/libs/ardour/port_engine_shared.cc b/libs/ardour/port_engine_shared.cc index e3d0c1c14e..d459cb48e3 100644 --- a/libs/ardour/port_engine_shared.cc +++ b/libs/ardour/port_engine_shared.cc @@ -18,7 +18,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include +#include #include "pbd/error.h" @@ -289,11 +289,14 @@ PortEngineSharedImpl::get_ports ( std::vector& port_names) const { int rv = 0; - regex_t port_regex; + std::regex port_regex; bool use_regexp = false; - if (port_name_pattern.size () > 0) { - if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) { + if (!port_name_pattern.empty()) { + try { + port_regex.assign (port_name_pattern, std::regex::extended); use_regexp = true; + } catch (const std::regex_error&) { + use_regexp = false; } } @@ -301,15 +304,12 @@ PortEngineSharedImpl::get_ports ( for (auto const& port : *p) { if ((port->type () == type) && flags == (port->flags () & flags)) { - if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) { + if (!use_regexp || std::regex_search (port->name(), port_regex)) { port_names.push_back (port->name ()); ++rv; } } } - if (use_regexp) { - regfree (&port_regex); - } return rv; } diff --git a/libs/ardour/port_manager.cc b/libs/ardour/port_manager.cc index e5a160327e..499fb51688 100644 --- a/libs/ardour/port_manager.cc +++ b/libs/ardour/port_manager.cc @@ -24,8 +24,6 @@ #ifdef COMPILER_MSVC #include #include // Microsoft's nearest equivalent to -#else -#include #endif #include @@ -1493,40 +1491,29 @@ PortManager::port_engine () bool PortManager::port_is_control_only (std::string const& name) { - static regex_t compiled_pattern; - static string pattern; + /* This is a list of substrings that identify ports related + * to physical MIDI devices that we do not want to expose as + * normal physical ports. + */ - if (pattern.empty ()) { - /* This is a list of regular expressions that match ports - * related to physical MIDI devices that we do not want to - * expose as normal physical ports. - */ + static const char* const control_only_ports[] = { + X_("Ableton Push"), + X_("FaderPort "), + X_("FaderPort8 "), + X_("FaderPort16 "), + X_("FaderPort2 "), + X_("US-2400 "), + X_("Mackie "), + X_("MIDI Control "), + X_("Console1 "), + }; - const char* const control_only_ports[] = { - X_(".*Ableton Push.*"), - X_(".*FaderPort .*"), - X_(".*FaderPort8 .*"), - X_(".*FaderPort16 .*"), - X_(".*FaderPort2 .*"), - X_(".*US-2400 .*"), - X_(".*Mackie .*"), - X_(".*MIDI Control .*"), - X_(".*Console1 .*"), - }; - - pattern = "("; - for (size_t n = 0; n < sizeof (control_only_ports) / sizeof (control_only_ports[0]); ++n) { - if (n > 0) { - pattern += '|'; - } - pattern += control_only_ports[n]; + for (size_t n = 0; n < sizeof (control_only_ports) / sizeof (control_only_ports[0]); ++n) { + if (name.find (control_only_ports[n]) != string::npos) { + return true; } - pattern += ')'; - - regcomp (&compiled_pattern, pattern.c_str (), REG_EXTENDED | REG_NOSUB); } - - return regexec (&compiled_pattern, name.c_str (), 0, 0, 0) == 0; + return false; } static bool diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc index ec00656c4b..e173bfb8a7 100644 --- a/libs/ardour/smf_source.cc +++ b/libs/ardour/smf_source.cc @@ -27,9 +27,11 @@ #include #include -#include #include -#include + +#ifdef HAVE_UNISTD_H +#include +#endif #include "pbd/file_utils.h" #include "pbd/stl_delete.h" @@ -688,11 +690,6 @@ SMFSource::valid_midi_file (const string& file) bool SMFSource::safe_midi_file_extension (const string& file) { - static regex_t compiled_pattern; - static bool compile = true; - const int nmatches = 2; - regmatch_t matches[nmatches]; - if (Glib::file_test (file, Glib::FILE_TEST_EXISTS)) { if (!Glib::file_test (file, Glib::FILE_TEST_IS_REGULAR)) { /* exists but is not a regular file */ @@ -700,17 +697,14 @@ SMFSource::safe_midi_file_extension (const string& file) } } - if (compile && regcomp (&compiled_pattern, "\\.[mM][iI][dD][iI]?$", REG_EXTENDED)) { - return false; - } else { - compile = false; - } - - if (regexec (&compiled_pattern, file.c_str(), nmatches, matches, 0)) { + const size_t dot_pos = file.rfind ('.'); + if (dot_pos == string::npos) { return false; } - return true; + std::string const ext = PBD::downcase (file.substr(dot_pos + 1)); + + return ext == "mid" || ext == "midi"; } static bool compare_eventlist ( diff --git a/libs/ardour/utils.cc b/libs/ardour/utils.cc index 437952110b..3a32b3bea0 100644 --- a/libs/ardour/utils.cc +++ b/libs/ardour/utils.cc @@ -37,14 +37,9 @@ #include #include #include -#include #include -#include -#ifndef COMPILER_MSVC -#include -#endif #include -#include +#include #include "pbd/gstdio_compat.h" @@ -673,45 +668,30 @@ bool ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path) { string bws = basename_nosuffix (path); - struct dirent* dentry; - GStatBuf statbuf; - DIR* dead; - bool ret = false; - if ((dead = ::opendir (dir.c_str())) == 0) { - error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl; - return false; - } + try { + Glib::Dir directory (dir); - while ((dentry = ::readdir (dead)) != 0) { + for (const auto& name : directory) { + if (name == "." || name == "..") { + continue; + } - /* avoid '.' and '..' */ + string fullpath = Glib::build_filename (dir, name); + if (!Glib::file_test (fullpath, Glib::FILE_TEST_IS_REGULAR)) { + continue; + } - if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') || - (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) { - continue; - } + if (basename_nosuffix (name) == bws) { + return true; + } + } + } catch (const Glib::Error& e) { + error << string_compose (_("cannot open directory %1 (%2)"), dir, e.what()) << endl; + return false; + } - string fullpath = Glib::build_filename (dir, dentry->d_name); - - if (g_stat (fullpath.c_str(), &statbuf)) { - continue; - } - - if (!S_ISREG (statbuf.st_mode)) { - continue; - } - - string bws2 = basename_nosuffix (dentry->d_name); - - if (bws2 == bws) { - ret = true; - break; - } - } - - ::closedir (dead); - return ret; + return false; } uint32_t diff --git a/libs/backends/alsa/alsa_audiobackend.cc b/libs/backends/alsa/alsa_audiobackend.cc index f5e325a02b..ce61983b3f 100644 --- a/libs/backends/alsa/alsa_audiobackend.cc +++ b/libs/backends/alsa/alsa_audiobackend.cc @@ -18,7 +18,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include #include #include diff --git a/libs/backends/coreaudio/coreaudio_backend.cc b/libs/backends/coreaudio/coreaudio_backend.cc index 05c59220d0..7d4ee3e706 100644 --- a/libs/backends/coreaudio/coreaudio_backend.cc +++ b/libs/backends/coreaudio/coreaudio_backend.cc @@ -17,7 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include #include #include diff --git a/libs/backends/dummy/dummy_audiobackend.cc b/libs/backends/dummy/dummy_audiobackend.cc index a7a1dd033b..d753681870 100644 --- a/libs/backends/dummy/dummy_audiobackend.cc +++ b/libs/backends/dummy/dummy_audiobackend.cc @@ -21,7 +21,6 @@ #include #include -#include #include #include diff --git a/libs/backends/portaudio/portaudio_backend.cc b/libs/backends/portaudio/portaudio_backend.cc index 14ccd9c62c..c656e922c2 100644 --- a/libs/backends/portaudio/portaudio_backend.cc +++ b/libs/backends/portaudio/portaudio_backend.cc @@ -18,8 +18,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include - #ifndef PLATFORM_WINDOWS #include #include diff --git a/libs/backends/pulseaudio/pulseaudio_backend.cc b/libs/backends/pulseaudio/pulseaudio_backend.cc index 889190d4ad..ae8873866b 100644 --- a/libs/backends/pulseaudio/pulseaudio_backend.cc +++ b/libs/backends/pulseaudio/pulseaudio_backend.cc @@ -17,7 +17,6 @@ */ #include -#include #include #include #include diff --git a/libs/surfaces/generic_midi/generic_midi_control_protocol.cc b/libs/surfaces/generic_midi/generic_midi_control_protocol.cc index 08e88716d1..157b4f71fc 100644 --- a/libs/surfaces/generic_midi/generic_midi_control_protocol.cc +++ b/libs/surfaces/generic_midi/generic_midi_control_protocol.cc @@ -25,6 +25,7 @@ #include #include +#include #ifdef COMPILER_MSVC #include // Microsoft's nearest equivalent to @@ -1029,24 +1030,13 @@ GenericMidiControlProtocol::lookup_controllable (const string & str, MIDIControl int id = 1; string name; - static regex_t compiled_pattern; - static bool compiled = false; - - if (!compiled) { - const char * const pattern = "^[BS]?[0-9]+"; - /* this pattern compilation is not going to fail */ - regcomp (&compiled_pattern, pattern, REG_EXTENDED|REG_NOSUB); - /* leak compiled pattern */ - compiled = true; - } - /* Step 3: identify what "rest" looks like - name, or simple nueric, or * banked/selection specifier */ - bool matched = (regexec (&compiled_pattern, rest[0].c_str(), 0, 0, 0) == 0); + static const std::regex pattern ("^[BS]?[0-9]+", std::regex::extended); - if (matched) { + if (std::regex_search (rest[0], pattern)) { bool banked = false; if (rest[0][0] == 'B') {