From b48bd99f7083f90db05a6f6c679c2e9c30d8ee86 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sat, 27 Jan 2018 21:09:14 +0000 Subject: [PATCH] More C++17 modernization. --- gsmartcontrol/TODO | 6 --- .../src/applib/storage_detector_linux.cpp | 8 ++-- .../src/applib/storage_detector_other.cpp | 4 +- .../src/applib/storage_detector_win32.cpp | 12 +++--- gsmartcontrol/src/applib/storage_device.cpp | 38 +++++++++---------- gsmartcontrol/src/applib/storage_device.h | 18 ++++----- .../src/applib/storage_property_descr.cpp | 2 +- gsmartcontrol/src/hz/fs_dir.h | 12 +++--- gsmartcontrol/src/hz/fs_path.h | 10 ++--- gsmartcontrol/src/libdebug/dout.cpp | 6 +-- gsmartcontrol/src/libdebug/dstate.cpp | 12 +++--- gsmartcontrol/src/libdebug/dstate.h | 6 +-- gsmartcontrol/src/libdebug/dstream.h | 8 ++-- 13 files changed, 65 insertions(+), 77 deletions(-) diff --git a/gsmartcontrol/TODO b/gsmartcontrol/TODO index 75c375f..e91be45 100644 --- a/gsmartcontrol/TODO +++ b/gsmartcontrol/TODO @@ -53,12 +53,6 @@ Use std::from_chars() in string_is_numeric_impl_classic_locale() (gcc 8) Mark with gettext -std::string setters -> no const&, use std::move. - same for std::vector -for iter -> for auto -push_back -> static vector - - Check TODOs diff --git a/gsmartcontrol/src/applib/storage_detector_linux.cpp b/gsmartcontrol/src/applib/storage_detector_linux.cpp index 33e635f..b960ad2 100644 --- a/gsmartcontrol/src/applib/storage_detector_linux.cpp +++ b/gsmartcontrol/src/applib/storage_detector_linux.cpp @@ -94,8 +94,8 @@ inline std::string detect_drives_linux_udev_byid(std::vector& devic // platform blacklist bool blacked = false; - for (std::vector::const_iterator iter = blacklist.begin(); iter != blacklist.end(); ++iter) { - if (app_pcre_match(*iter, entry)) { + for (const auto bl_pattern : blacklist) { + if (app_pcre_match(bl_pattern, entry)) { blacked = true; break; } @@ -414,8 +414,8 @@ inline std::string detect_drives_linux_proc_partitions(std::vector::const_iterator iter = blacklist.begin(); iter != blacklist.end(); ++iter) { - if (app_pcre_match(*iter, dev)) { + for (const auto& bl_pattern : blacklist) { + if (app_pcre_match(bl_pattern, dev)) { blacked = true; break; } diff --git a/gsmartcontrol/src/applib/storage_detector_other.cpp b/gsmartcontrol/src/applib/storage_detector_other.cpp index a325740..2852027 100644 --- a/gsmartcontrol/src/applib/storage_detector_other.cpp +++ b/gsmartcontrol/src/applib/storage_detector_other.cpp @@ -191,8 +191,8 @@ std::string detect_drives_other(std::vector& drives, const Exe continue; bool matched = false; - for (std::vector::const_iterator iter = whitelist.begin(); iter != whitelist.end(); ++iter) { - if (app_pcre_match(*iter, entry)) { + for (const auto& wl_pattern : whitelist) { + if (app_pcre_match(wl_pattern, entry)) { matched = true; break; } diff --git a/gsmartcontrol/src/applib/storage_detector_win32.cpp b/gsmartcontrol/src/applib/storage_detector_win32.cpp index 77d9564..de24600 100644 --- a/gsmartcontrol/src/applib/storage_detector_win32.cpp +++ b/gsmartcontrol/src/applib/storage_detector_win32.cpp @@ -239,9 +239,9 @@ std::string get_scan_open_multiport_devices(std::vector& drive auto drive = std::make_shared(full_dev, type); std::map letters_volnames; - for (std::map::const_iterator iter = drive_letter_map.begin(); iter != drive_letter_map.end(); ++iter) { - if (iter->second.physical_drives.count(drive_num) > 0) { - letters_volnames[iter->first] = iter->second.volume_name; + for (const auto& iter : drive_letter_map) { + if (iter.second.physical_drives.count(drive_num) > 0) { + letters_volnames[iter.first] = iter.second.volume_name; } } drive->set_drive_letters(letters_volnames); @@ -733,9 +733,9 @@ std::string detect_drives_win32(std::vector& drives, const Exe auto drive = std::make_shared(hz::string_sprintf("pd%d", drive_num)); std::map letters_volnames; - for (std::map::const_iterator iter = drive_letter_map.begin(); iter != drive_letter_map.end(); ++iter) { - if (iter->second.physical_drives.count(drive_num) > 0) { - letters_volnames[iter->first] = iter->second.volume_name; + for (const auto& iter : drive_letter_map) { + if (iter.second.physical_drives.count(drive_num) > 0) { + letters_volnames[iter.first] = iter.second.volume_name; } } drive->set_drive_letters(letters_volnames); diff --git a/gsmartcontrol/src/applib/storage_device.cpp b/gsmartcontrol/src/applib/storage_device.cpp index ae091c0..54db53b 100644 --- a/gsmartcontrol/src/applib/storage_device.cpp +++ b/gsmartcontrol/src/applib/storage_device.cpp @@ -58,24 +58,22 @@ std::string StorageDevice::get_status_name(Status status) -StorageDevice::StorageDevice(const std::string& dev_or_vfile, bool is_virtual) +StorageDevice::StorageDevice(std::string dev_or_vfile, bool is_virtual) { is_virtual_ = is_virtual; if (is_virtual) { - virtual_file_ = dev_or_vfile; + virtual_file_ = std::move(dev_or_vfile); } else { - device_ = dev_or_vfile; + device_ = std::move(dev_or_vfile); } } -StorageDevice::StorageDevice(const std::string& dev, const std::string& type_arg) -{ - device_ = dev; - type_arg_ = type_arg; -} +StorageDevice::StorageDevice(std::string dev, std::string type_arg) + : device_(std::move(dev)), type_arg_(std::move(type_arg)) +{ } @@ -570,9 +568,9 @@ StorageDevice::DetectedType StorageDevice::get_detected_type() const -void StorageDevice::set_type_argument(const std::string& arg) +void StorageDevice::set_type_argument(std::string arg) { - type_arg_ = arg; + type_arg_ = std::move(arg); } @@ -584,9 +582,9 @@ std::string StorageDevice::get_type_argument() const -void StorageDevice::set_extra_arguments(const std::string& args) +void StorageDevice::set_extra_arguments(std::string args) { - extra_args_ = args; + extra_args_ = std::move(args); } @@ -598,9 +596,9 @@ std::string StorageDevice::get_extra_arguments() const -void StorageDevice::set_drive_letters(const std::map& letters) +void StorageDevice::set_drive_letters(std::map letters) { - drive_letters_ = letters; + drive_letters_ = std::move(letters); } @@ -698,9 +696,9 @@ bool StorageDevice::get_is_hdd() const -void StorageDevice::set_info_output(const std::string& s) +void StorageDevice::set_info_output(std::string s) { - info_output_ = s; + info_output_ = std::move(s); } @@ -712,9 +710,9 @@ std::string StorageDevice::get_info_output() const -void StorageDevice::set_full_output(const std::string& s) +void StorageDevice::set_full_output(std::string s) { - full_output_ = s; + full_output_ = std::move(s); } @@ -847,9 +845,9 @@ void StorageDevice::set_parse_status(ParseStatus value) -void StorageDevice::set_properties(const std::vector& props) +void StorageDevice::set_properties(std::vector props) { - properties_ = props; + properties_ = std::move(props); } diff --git a/gsmartcontrol/src/applib/storage_device.h b/gsmartcontrol/src/applib/storage_device.h index 1193a16..a76a9c5 100644 --- a/gsmartcontrol/src/applib/storage_device.h +++ b/gsmartcontrol/src/applib/storage_device.h @@ -71,10 +71,10 @@ class StorageDevice { /// Constructor - explicit StorageDevice(const std::string& dev_or_vfile, bool is_virtual = false); + explicit StorageDevice(std::string dev_or_vfile, bool is_virtual = false); /// Constructor - StorageDevice(const std::string& dev, const std::string& type_arg); + StorageDevice(std::string dev, std::string type_arg); // clear everything fetched before. @@ -83,7 +83,7 @@ class StorageDevice { /// Calls "smartctl -i -H -c" (info section, health, capabilities), then parse_basic_data(). /// Called during drive detection. /// Note: this will clear the non-basic properties! - std::string fetch_basic_data_and_parse(const std::shared_ptr& smartctl_ex = 0); + std::string fetch_basic_data_and_parse(const std::shared_ptr& smartctl_ex = nullptr); /// Detects type, smart support, smart status (on / off). /// Note: this will clear the non-basic properties! @@ -141,21 +141,21 @@ class StorageDevice { /// Set argument for "-d" smartctl parameter - void set_type_argument(const std::string& arg); + void set_type_argument(std::string arg); /// Get argument for "-d" smartctl parameter std::string get_type_argument() const; /// Set extra arguments smartctl - void set_extra_arguments(const std::string& args); + void set_extra_arguments(std::string args); /// Get extra arguments smartctl std::string get_extra_arguments() const; /// Set windows drive letters for this drive - void set_drive_letters(const std::map& letters_volnames); + void set_drive_letters(std::map letters_volnames); /// Get windows drive letters for this drive const std::map& get_drive_letters() const; @@ -201,14 +201,14 @@ class StorageDevice { /// Set "info" output to parse - void set_info_output(const std::string& s); + void set_info_output(std::string s); /// Get "info" output to parse std::string get_info_output() const; /// Set "full" output to parse - void set_full_output(const std::string& s); + void set_full_output(std::string s); /// Get "full" output to parse std::string get_full_output() const; @@ -252,7 +252,7 @@ class StorageDevice { void set_parse_status(ParseStatus value); /// Set parsed properties - void set_properties(const std::vector& props); + void set_properties(std::vector props); private: diff --git a/gsmartcontrol/src/applib/storage_property_descr.cpp b/gsmartcontrol/src/applib/storage_property_descr.cpp index 7877506..0ce33fe 100644 --- a/gsmartcontrol/src/applib/storage_property_descr.cpp +++ b/gsmartcontrol/src/applib/storage_property_descr.cpp @@ -1042,7 +1042,7 @@ namespace { /// different smartctl name (fill the other members from the previous attribute). // void add(int32_t id, const std::string& smartctl_name) // { -// std::map >::iterator iter = id_db.find(id); +// auto iter = id_db.find(id); // DBG_ASSERT(iter != id_db.end() && !iter->second.empty()); // if (iter != id_db.end() || iter->second.empty()) { // AttributeDescription attr = iter->second.front(); diff --git a/gsmartcontrol/src/hz/fs_dir.h b/gsmartcontrol/src/hz/fs_dir.h index e92836e..9a482f7 100644 --- a/gsmartcontrol/src/hz/fs_dir.h +++ b/gsmartcontrol/src/hz/fs_dir.h @@ -855,25 +855,25 @@ bool Dir::list(Container& put_here, bool put_with_path, SortFunctor sort_func, F if (sort_using_paths) { std::sort(path_results.begin(), path_results.end(), sort_func); - for (list_path_list_t::const_iterator iter = path_results.begin(); iter != path_results.end(); ++iter) { + for (const auto& path_result : path_results) { if (put_with_path) { - put_here.push_back(iter->str()); + put_here.push_back(path_result.str()); } else { - put_here.push_back(iter->get_basename()); + put_here.push_back(path_result.get_basename()); } } } else { std::sort(string_results.begin(), string_results.end(), sort_func); - for (list_string_list_t::const_iterator iter = string_results.begin(); iter != string_results.end(); ++iter) { + for (const auto& string_result : string_results) { if (put_with_path) { FsPath p(this->get_path()); - p.append(*iter); + p.append(string_result); put_here.push_back(p.str()); } else { - put_here.push_back(*iter); + put_here.push_back(string_result); } } } diff --git a/gsmartcontrol/src/hz/fs_path.h b/gsmartcontrol/src/hz/fs_path.h index 2c1a50b..b51f28b 100644 --- a/gsmartcontrol/src/hz/fs_path.h +++ b/gsmartcontrol/src/hz/fs_path.h @@ -29,10 +29,6 @@ #include // utime() #endif -#if defined __MINGW32__ - #include <_mingw.h> // MINGW_HAS_SECURE_API -#endif - #include "fs_common.h" // separator #include "fs_path_utils.h" // path_* functions #include "fs_error_holder.h" // FsErrorHolder @@ -461,7 +457,7 @@ inline bool FsPath::is_readable() return false; } -#if defined MINGW_HAS_SECURE_API || defined _MSC_VER +#if defined HAVE_WIN_SE_FUNCS && HAVE_WIN_SE_FUNCS if (_waccess_s(this->get_utf16().c_str(), 04)) // msvc uses integers instead (R_OK == 04 anyway). #elif defined _WIN32 if (_waccess(this->get_utf16().c_str(), 04) == -1) // *access*() may not work with < win2k with directories. @@ -508,7 +504,7 @@ inline bool FsPath::is_writable() // pcheck either doesn't exist, or it's a file. try to open it. std::FILE* f = 0; -#if defined MINGW_HAS_SECURE_API || defined _MSC_VER +#if defined HAVE_WIN_SE_FUNCS && HAVE_WIN_SE_FUNCS errno = _wfopen_s(&f, path_to_check.get_utf16().c_str(), L"ab"); #else f = _wfopen(path_to_check.get_utf16().c_str(), L"ab"); // this creates a 0 size file if it doesn't exist! @@ -567,7 +563,7 @@ inline bool FsPath::exists() return false; } -#if defined MINGW_HAS_SECURE_API || defined _MSC_VER +#if defined HAVE_WIN_SE_FUNCS && HAVE_WIN_SE_FUNCS if (_waccess_s(this->get_utf16().c_str(), 00) != 0) // msvc uses integers instead (F_OK == 00 anyway). #elif defined _WIN32 if (_waccess(this->get_utf16().c_str(), 00) != 0) // msvc uses integers instead (F_OK == 00 anyway). diff --git a/gsmartcontrol/src/libdebug/dout.cpp b/gsmartcontrol/src/libdebug/dout.cpp index 7805abc..534fa14 100644 --- a/gsmartcontrol/src/libdebug/dout.cpp +++ b/gsmartcontrol/src/libdebug/dout.cpp @@ -27,9 +27,9 @@ // This may throw for invalid domain or level. std::ostream& debug_out(debug_level::flag level, const std::string& domain) { - debug_internal::DebugState::domain_map_t& dm = debug_internal::get_debug_state().get_domain_map(); + auto& dm = debug_internal::get_debug_state().get_domain_map(); - debug_internal::DebugState::domain_map_t::iterator level_map = dm.find(domain); + auto level_map = dm.find(domain); if (level_map == dm.end()) { // no such domain std::string msg = "debug_out(): Debug state doesn't contain the requested domain: \"" + domain + "\"."; @@ -45,7 +45,7 @@ std::ostream& debug_out(debug_level::flag level, const std::string& domain) throw debug_internal_error(msg.c_str()); } - debug_internal::DebugState::level_map_t::iterator os = level_map->second.find(level); + auto os = level_map->second.find(level); if (level_map == dm.end()) { std::string msg = std::string("debug_out(): Debug state doesn't contain the requested level ") + debug_level::get_name(level) + " in domain: \"" + domain + "\"."; diff --git a/gsmartcontrol/src/libdebug/dstate.cpp b/gsmartcontrol/src/libdebug/dstate.cpp index 80fdb66..af4b565 100644 --- a/gsmartcontrol/src/libdebug/dstate.cpp +++ b/gsmartcontrol/src/libdebug/dstate.cpp @@ -92,7 +92,7 @@ bool debug_register_domain(const std::string& domain) return false; // copy the "default" domain - use it as a template - DebugState::domain_map_t::iterator def_iter = dm.find("default"); + auto def_iter = dm.find("default"); if (def_iter == dm.end()) { throw debug_internal_error(("debug_register_domain(\"" + domain + "\"): Domain \"default\" doesn't exist.").c_str()); @@ -103,8 +103,8 @@ bool debug_register_domain(const std::string& domain) dm[domain] = DebugState::level_map_t(); DebugState::level_map_t& level_map = dm.find(domain)->second; - for (DebugState::level_map_t::const_iterator iter = def_level_map.begin(); iter != def_level_map.end(); ++iter) { - level_map[iter->first] = std::make_shared(*(iter->second), domain); + for (const auto& iter : def_level_map) { + level_map[iter.first] = std::make_shared(*(iter.second), domain); } return true; @@ -117,7 +117,7 @@ bool debug_unregister_domain(const std::string& domain) using namespace debug_internal; DebugState::domain_map_t& dm = get_debug_state().get_domain_map(); - DebugState::domain_map_t::iterator found = dm.find(domain); + auto found = dm.find(domain); if (found == dm.end()) // doesn't exists return false; @@ -134,8 +134,8 @@ std::vector debug_get_registered_domains() std::vector domains; domains.reserve(dm.size()); - for (DebugState::domain_map_t::iterator iter = dm.begin(); iter != dm.end(); ++iter) - domains.push_back(iter->first); + for (const auto& iter : dm) + domains.push_back(iter.first); return domains; } diff --git a/gsmartcontrol/src/libdebug/dstate.h b/gsmartcontrol/src/libdebug/dstate.h index 360dcf2..131302e 100644 --- a/gsmartcontrol/src/libdebug/dstate.h +++ b/gsmartcontrol/src/libdebug/dstate.h @@ -102,9 +102,9 @@ namespace debug_internal { /// Flush all the stream buffers. This will write prefixes too. void force_output() { - for(domain_map_t::iterator iter = domain_map.begin(); iter != domain_map.end(); ++iter) { - for(level_map_t::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) - iter2->second->force_output(); + for (auto& iter : domain_map) { + for (auto& iter2 : iter.second) + iter2.second->force_output(); } } diff --git a/gsmartcontrol/src/libdebug/dstream.h b/gsmartcontrol/src/libdebug/dstream.h index fd14079..fc99cce 100644 --- a/gsmartcontrol/src/libdebug/dstream.h +++ b/gsmartcontrol/src/libdebug/dstream.h @@ -217,9 +217,9 @@ namespace debug_internal { /// Set channel list to send the data to. - void set_channels(const std::vector& channels) + void set_channels(std::vector channels) { - channels_ = channels; + channels_ = std::move(channels); } /// Get channel list @@ -261,9 +261,9 @@ namespace debug_internal { private: - debug_level::flag level_; ///< Debug level of this stream + debug_level::flag level_ = debug_level::none; ///< Debug level of this stream std::string domain_; ///< Domain of this stream - debug_format::type format_; ///< Format flags + debug_format::type format_ = debug_format::none; ///< Format flags bool is_first_line_ = true; ///< Whether it's the first line of output or not