Skip to content

Commit

Permalink
Some code modernization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaduri committed Mar 14, 2024
1 parent 5fe2529 commit eb44ed1
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/applib/command_executor_areca.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ArecaCliExecutorGeneric<ExecutorPolicy>::ArecaCliExecutorGeneric()
template<class ExecutorPolicy>
std::string ArecaCliExecutorGeneric<ExecutorPolicy>::translate_exit_status([[maybe_unused]] int status)
{
return std::string();
return {};
}


Expand Down
2 changes: 1 addition & 1 deletion src/applib/examples/example_smartctl_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main(int argc, char** argv)

ex.execute();

std::string out_str = ex.get_stdout_str();
const std::string out_str = ex.get_stdout_str();
// std::cout << "OUT:\n" << out_str << "\n\n";
std::cerr << "OUT SIZE: " << out_str.size() << "\n";

Expand Down
8 changes: 6 additions & 2 deletions src/applib/smartctl_ata_json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ License: GNU General Public License v3.0 only
/// \weakgroup applib
/// @{

#include <format>

#include "smartctl_ata_json_parser.h"
#include "json/json.hpp"
#include "hz/debug.h"
Expand Down Expand Up @@ -93,7 +95,7 @@ T get_node_data(const nlohmann::json& root, const std::string& path)
const std::string& comp_name = components[comp_index];

if (!curr->is_object()) { // we can't have non-object values in the middle of a path
throw std::runtime_error("Cannot get node data \""s + path + "\", component \"" + comp_name + "\" is not an object.");
throw std::runtime_error(std::format("Cannot get node data \"{}\", component \"{}\" is not an object.", path, comp_name));
}
if (auto iter = curr->find(comp_name); iter != curr->end()) { // path component exists
const auto& jval = iter.value();
Expand All @@ -109,7 +111,7 @@ T get_node_data(const nlohmann::json& root, const std::string& path)
curr = &jval;

} else { // path component doesn't exist
throw std::runtime_error("Cannot get node data \""s + path + "\", component \"" + comp_name + "\" does not exist.");
throw std::runtime_error(std::format("Cannot get node data \"{}\", component \"{}\" does not exist.", path, comp_name));
}
}

Expand Down Expand Up @@ -197,6 +199,8 @@ hz::ExpectedVoid<SmartctlParserError> SmartctlAtaJsonParser::parse_full(const st
debug_out_warn("app", DBG_FUNC_MSG << "Error parsing smartctl output as JSON: " << e.what() << "\n");
return hz::Unexpected(SmartctlParserError::SyntaxError, std::string("Invalid JSON data: ") + e.what());
}

return {};
}


Expand Down
6 changes: 3 additions & 3 deletions src/gsc_executor_error_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace {
dialog.set_default_response(Gtk::RESPONSE_OK);


int response = dialog.run(); // blocks until the dialog is closed
const int response = dialog.run(); // blocks until the dialog is closed

return response;
}
Expand All @@ -70,7 +70,7 @@ namespace {
void gsc_executor_error_dialog_show(const std::string& message, const std::string& sec_message,
Gtk::Window* parent, bool sec_msg_markup, bool show_output_button)
{
int response = show_executor_dialog(Gtk::MESSAGE_ERROR, message, sec_message,
const int response = show_executor_dialog(Gtk::MESSAGE_ERROR, message, sec_message,
parent, sec_msg_markup, show_output_button);

if (response == Gtk::RESPONSE_HELP) {
Expand All @@ -87,7 +87,7 @@ void gsc_no_info_dialog_show(const std::string& message, const std::string& sec_
Gtk::Window* parent, bool sec_msg_markup, const std::string& output,
const std::string& output_window_title, const std::string& default_save_filename)
{
int response = show_executor_dialog(Gtk::MESSAGE_WARNING, message, sec_message,
const int response = show_executor_dialog(Gtk::MESSAGE_WARNING, message, sec_message,
parent, sec_msg_markup, !output.empty());

if (response == Gtk::RESPONSE_HELP) {
Expand Down
2 changes: 1 addition & 1 deletion src/hz/bad_cast_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class bad_cast_except : public std::exception { // from <exception>
{
// Note: we do quite a lot of construction here, but since it's not
// an out-of-memory exception, what the heck.
std::string who = (self_name_.empty() ? "[unknown]" : self_name_);
const std::string who = (self_name_.empty() ? "[unknown]" : self_name_);

std::string from = (src_type_ == typeid(void) ? "[unknown]" : hz::type_name_demangle(src_type_.name()));
if (from.empty())
Expand Down
8 changes: 4 additions & 4 deletions src/hz/format_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ inline std::string format_time_length(std::chrono::seconds secs)

if (secs >= 100h) {
day_unit days = std::chrono::round<day_unit>(secs);
std::chrono::seconds sec_diff = secs - days; // difference between days and actual time (may be positive or negative)
const std::chrono::seconds sec_diff = secs - days; // difference between days and actual time (may be positive or negative)

if (days.count() < 10) { // if less than 10 days, display hours too

// if there's more than half an hour missing from complete day, add a day.
// then add half an hour and convert to hours.
int64_t hours = ((sec_diff.count() < (-hour_size / 2) ? sec_diff.count() + day_size : sec_diff.count()) + hour_size / 2) / hour_size;
const int64_t hours = ((sec_diff.count() < (-hour_size / 2) ? sec_diff.count() + day_size : sec_diff.count()) + hour_size / 2) / hour_size;
if (hours > 0 && sec_diff.count() < (-hour_size / 2))
days--;

Expand All @@ -185,10 +185,10 @@ inline std::string format_time_length(std::chrono::seconds secs)

if (secs >= 100min) {
auto hours = std::chrono::round<std::chrono::hours>(secs);
std::chrono::seconds sec_diff = secs - hours;
const std::chrono::seconds sec_diff = secs - hours;

if (hours.count() < 10) { // if less than 10 hours, display minutes too
int64_t minutes = ((sec_diff.count() < (-min_size / 2) ? sec_diff.count() + hour_size : sec_diff.count()) + min_size / 2) / min_size;
const int64_t minutes = ((sec_diff.count() < (-min_size / 2) ? sec_diff.count() + hour_size : sec_diff.count()) + min_size / 2) / min_size;
if (minutes > 0 && sec_diff.count() < (-min_size / 2))
hours--;

Expand Down
12 changes: 6 additions & 6 deletions src/hz/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ inline std::error_code fs_file_get_contents_noalloc(const fs::path& file, unsign
}

// automatically allocate the buffer if buf_size is -1.
bool auto_alloc = (buf_size == static_cast<std::uintmax_t>(-1));
const bool auto_alloc = (buf_size == static_cast<std::uintmax_t>(-1));
if (!auto_alloc && buf_size < size) {
ec = std::make_error_code(std::errc::no_buffer_space);
break; // goto cleanup
Expand All @@ -225,7 +225,7 @@ inline std::error_code fs_file_get_contents_noalloc(const fs::path& file, unsign
// We don't need large file support here because we read into memory,
// which is limited at 31 bits anyway (on 32-bit systems).
// I really hope this is not a byte-by-byte operation
std::size_t read_bytes = std::fread(buf, 1, static_cast<std::size_t>(size), f);
const std::size_t read_bytes = std::fread(buf, 1, static_cast<std::size_t>(size), f);
if (size != static_cast<std::uintmax_t>(read_bytes)) {
// Unexpected number of bytes read. Not sure if this is reflected in errno or not.
ec = std::error_code(errno, std::system_category());
Expand Down Expand Up @@ -274,7 +274,7 @@ inline std::error_code fs_file_get_contents(const fs::path& file, std::string& p
{
std::uintmax_t size = 0;
unsigned char* buf = nullptr;
std::error_code ec = fs_file_get_contents(file, buf, size, max_size);
const std::error_code ec = fs_file_get_contents(file, buf, size, max_size);
if (ec)
return ec;

Expand Down Expand Up @@ -497,13 +497,13 @@ inline bool fs_path_is_writable(const fs::path& path, std::error_code& ec)
}

std::error_code ignored_ec; // ignore this error
bool is_directory = fs::is_directory(path, ignored_ec);
bool path_exists = fs::exists(path, ec);
const bool is_directory = fs::is_directory(path, ignored_ec);
const bool path_exists = fs::exists(path, ec);
if (ec) {
return false;
}

fs::path dirname = (is_directory ? path : path.parent_path());
const fs::path dirname = (is_directory ? path : path.parent_path());

#ifdef _WIN32 // win32 doesn't get access() (it just doesn't work with writing)

Expand Down
6 changes: 3 additions & 3 deletions src/libdebug/dchannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ std::string debug_format_message(debug_level::flag level, const std::string& dom
}

if (format_flags.test(debug_format::level)) { // print level name
bool use_color = format_flags.test(debug_format::color);
const bool use_color = format_flags.test(debug_format::color);
if (use_color)
ret += debug_level::get_color_start(level);

std::string level_name = std::string("<") + debug_level::get_name(level) + ">";
const std::string level_name = std::string("<") + debug_level::get_name(level) + ">";
ret += level_name + std::string(8 - level_name.size(), ' ');

if (use_color)
Expand All @@ -52,7 +52,7 @@ std::string debug_format_message(debug_level::flag level, const std::string& dom


if (format_flags.test(debug_format::indent)) {
std::string spaces(static_cast<std::size_t>(indent_level * 4), ' '); // indentation spaces
const std::string spaces(static_cast<std::size_t>(indent_level * 4), ' '); // indentation spaces

// replace all newlines with \n(indent-spaces) except for the last one.
std::string::size_type oldpos = 0, pos = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/libdebug/dcmdarg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static gboolean debug_internal_parse_levels([[maybe_unused]] const gchar* option
if (!value)
return FALSE;
auto* args = static_cast<debug_internal::DebugCmdArgs*>(data);
std::string levels = value;
const std::string levels = value;
hz::string_split(levels, ',', args->debug_levels, true);
// will filter out invalid ones later
return TRUE;
Expand Down Expand Up @@ -143,7 +143,7 @@ static gboolean debug_internal_post_parse_func([[maybe_unused]] GOptionContext*
if (args->verbosity_level > 4) args->levels_enabled.set(debug_level::dump);
}

bool color_enabled = static_cast<bool>(args->debug_colorize);
const bool color_enabled = static_cast<bool>(args->debug_colorize);


debug_internal::DebugState::DomainMap& domain_map = debug_internal::get_debug_state_ref().get_domain_map_ref();
Expand Down
6 changes: 3 additions & 3 deletions src/libdebug/dout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ std::ostream& debug_out(debug_level::flag level, const std::string& domain)
auto level_map = dm.find(domain);
if (level_map == dm.end()) { // no such domain
// this is an internal error
std::string msg = "debug_out(): Debug state doesn't contain the requested domain: \"" + domain + "\".";
const std::string msg = "debug_out(): Debug state doesn't contain the requested domain: \"" + domain + "\".";
throw debug_internal_error(msg.c_str());
}

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 ") +
const std::string msg = std::string("debug_out(): Debug state doesn't contain the requested level ") +
debug_level::get_name(level) + " in domain: \"" + domain + "\".";

// this is an internal error
Expand Down Expand Up @@ -110,7 +110,7 @@ namespace debug_internal {
// increase indentation level for all debug levels
void debug_indent_inc(int by)
{
int curr = debug_internal::get_debug_state_ref().get_indent_level();
const int curr = debug_internal::get_debug_state_ref().get_indent_level();
debug_internal::get_debug_state_ref().set_indent_level(curr + by);
}

Expand Down
4 changes: 2 additions & 2 deletions src/libdebug/dstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace debug_internal {
format_flags.set(debug_format::color);
#endif

std::map<debug_level::flag, bool> levels = {
const std::map<debug_level::flag, bool> levels = {
{debug_level::dump, levels_enabled.test(debug_level::dump) },
{debug_level::info, levels_enabled.test(debug_level::info) },
{debug_level::warn, levels_enabled.test(debug_level::warn) },
Expand Down Expand Up @@ -102,7 +102,7 @@ bool debug_register_domain(const std::string& domain)
+ "\"): Domain \"default\" doesn't exist.").c_str());
}

DebugState::LevelMap& def_level_map = def_iter->second;
const DebugState::LevelMap& def_level_map = def_iter->second;

dm[domain] = DebugState::LevelMap();
DebugState::LevelMap& level_map = dm.find(domain)->second;
Expand Down
2 changes: 1 addition & 1 deletion src/libdebug/dstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace debug_internal {
{
if (inside_begin_.empty())
throw debug_usage_error("DebugState::pop_inside_begin(): Begin / End stack underflow! Mismatched begin()/end()?");
bool val = inside_begin_.top();
const bool val = inside_begin_.top();
inside_begin_.pop();
return val;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libdebug/examples/example_libdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ int main_impl()
));


std::string something = "some thing";
const std::string something = "some thing";
const char* obj = "obj";
int op = 5;
const int op = 5;


debug_out_dump("dom", "Dumping something: " << something << std::endl);
Expand Down
2 changes: 1 addition & 1 deletion src/rconfig/examples/example_rconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main()

rconfig::set_data("app/int_var", 11); // override default.

int int_var = rconfig::get_data<int>("app/int_value");
const int int_var = rconfig::get_data<int>("app/int_value");
std::cerr << "app/int_value: " << int_var << "\n";

std::cerr << "app/some_string2: " << rconfig::get_data<std::string>("app/some_string2") << "\n";
Expand Down
2 changes: 1 addition & 1 deletion src/test_all/test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char* argv[])
debug_register_domain("hz");
debug_register_domain("rconfig");

int result = Catch::Session().run( argc, argv );
const int result = Catch::Session().run( argc, argv );
return result;
}

Expand Down

0 comments on commit eb44ed1

Please sign in to comment.