From a4d5ab07e9b5fdf2d0f904c5e2c1a8c6c20aaa0e Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 21 Feb 2015 00:47:06 +0100 Subject: [PATCH] CmdConfig: Abstract saving and removal of the configuration values into separate methods --- src/commands/CmdConfig.cpp | 166 +++++++++++++++++++++++-------------- src/commands/CmdConfig.h | 2 + 2 files changed, 106 insertions(+), 62 deletions(-) diff --git a/src/commands/CmdConfig.cpp b/src/commands/CmdConfig.cpp index 8c605f527..4dd3e9b3b 100644 --- a/src/commands/CmdConfig.cpp +++ b/src/commands/CmdConfig.cpp @@ -46,6 +46,100 @@ CmdConfig::CmdConfig () _displays_id = false; } +//////////////////////////////////////////////////////////////////////////////// +bool CmdConfig::setConfigVariable (std::string name, std::string value, bool confirmation /* = false */) +{ + // Read .taskrc (or equivalent) + std::vector contents; + File::read (context.config._original_file, contents); + + bool found = false; + bool change = false; + + std::vector ::iterator line; + for (line = contents.begin (); line != contents.end (); ++line) + { + // If there is a comment on the line, it must follow the pattern. + std::string::size_type comment = line->find ("#"); + std::string::size_type pos = line->find (name + "="); + + if (pos != std::string::npos && + (comment == std::string::npos || + comment > pos)) + { + found = true; + if (!confirmation || + confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value))) + { + if (comment != std::string::npos) + *line = name + "=" + json::encode (value) + " " + line->substr (comment); + else + *line = name + "=" + json::encode (value); + + change = true; + } + } + } + + // Not found, so append instead. + if (!found && + (!confirmation || + confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value)))) + { + contents.push_back (name + "=" + json::encode (value)); + change = true; + } + + if (change) + File::write (context.config._original_file, contents); + + return change; +} + +//////////////////////////////////////////////////////////////////////////////// +int CmdConfig::unsetConfigVariable (std::string name, bool confirmation /* = false */) +{ + // Read .taskrc (or equivalent) + std::vector contents; + File::read (context.config._original_file, contents); + + bool found = false; + bool change = false; + + std::vector ::iterator line; + for (line = contents.begin (); line != contents.end (); ++line) + { + // If there is a comment on the line, it must follow the pattern. + std::string::size_type comment = line->find ("#"); + std::string::size_type pos = line->find (name + "="); + + if (pos != std::string::npos && + (comment == std::string::npos || + comment > pos)) + { + found = true; + + // Remove name + if (!confirmation || + confirm (format (STRING_CMD_CONFIG_CONFIRM3, name))) + { + *line = ""; + change = true; + } + } + } + + if (change) + File::write (context.config._original_file, contents); + + if ( change && found ) + return 0; + else if ( found ) + return 1; + else + return 2; +} + //////////////////////////////////////////////////////////////////////////////// int CmdConfig::execute (std::string& output) { @@ -63,6 +157,9 @@ int CmdConfig::execute (std::string& output) { bool confirmation = context.config.getBoolean ("confirmation"); + bool change = false; + bool found = false; + std::string name = words[0]; std::string value = ""; @@ -81,76 +178,22 @@ int CmdConfig::execute (std::string& output) { bool change = false; - // Read .taskrc (or equivalent) - std::vector contents; - File::read (context.config._original_file, contents); - // task config name value // task config name "" if (words.size () > 1) - { - bool found = false; - std::vector ::iterator line; - for (line = contents.begin (); line != contents.end (); ++line) - { - // If there is a comment on the line, it must follow the pattern. - std::string::size_type comment = line->find ("#"); - std::string::size_type pos = line->find (name + "="); - - if (pos != std::string::npos && - (comment == std::string::npos || - comment > pos)) - { - found = true; - if (!confirmation || - confirm (format (STRING_CMD_CONFIG_CONFIRM, name, context.config.get (name), value))) - { - if (comment != std::string::npos) - *line = name + "=" + json::encode (value) + " " + line->substr (comment); - else - *line = name + "=" + json::encode (value); - - change = true; - } - } - } - - // Not found, so append instead. - if (!found && - (!confirmation || - confirm (format (STRING_CMD_CONFIG_CONFIRM2, name, value)))) - { - contents.push_back (name + "=" + json::encode (value)); - change = true; - } - } + change = setConfigVariable(name, value, confirmation); // task config name else { - bool found = false; - std::vector ::iterator line; - for (line = contents.begin (); line != contents.end (); ++line) + rc = unsetConfigVariable(name, confirmation); + if (rc == 0) { - // If there is a comment on the line, it must follow the pattern. - std::string::size_type comment = line->find ("#"); - std::string::size_type pos = line->find (name + "="); - - if (pos != std::string::npos && - (comment == std::string::npos || - comment > pos)) - { - found = true; - - // Remove name - if (!confirmation || - confirm (format (STRING_CMD_CONFIG_CONFIRM3, name))) - { - *line = ""; - change = true; - } - } + change = true; + found = true; } + else if (rc == 1) + found = true; if (!found) throw format (STRING_CMD_CONFIG_NO_ENTRY, name); @@ -159,7 +202,6 @@ int CmdConfig::execute (std::string& output) // Write .taskrc (or equivalent) if (change) { - File::write (context.config._original_file, contents); out << format (STRING_CMD_CONFIG_FILE_MOD, context.config._original_file._data) << "\n"; diff --git a/src/commands/CmdConfig.h b/src/commands/CmdConfig.h index 782cfaca1..f3f7e8263 100644 --- a/src/commands/CmdConfig.h +++ b/src/commands/CmdConfig.h @@ -34,6 +34,8 @@ class CmdConfig : public Command { public: CmdConfig (); + static bool setConfigVariable (std::string name, std::string value, bool confirmation = false); + static int unsetConfigVariable (std::string name, bool confirmation = false); int execute (std::string&); };