From 4522877b43062bdd6e37ee9ceb5eb1a05dbaa0da Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 11 Jul 2011 01:51:14 -0400 Subject: [PATCH] Bug #683 - Fixed bug #683, in which the 'config' command sometimes edited comments instead of the proper line in .taskrc (thanks to Erlan Sergaziev). --- ChangeLog | 2 + src/commands/CmdConfig.cpp | 94 ++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 772337cf2..49251f806 100644 --- a/ChangeLog +++ b/ChangeLog @@ -94,6 +94,8 @@ certain circumstances (thanks to Steve Rader). + Fixed bug #645 & #660, which prevented logically combining report filters (thanks to Bryce Harrington). + + Fixed bug #683, in which the 'config' command sometimes edited comments + instead of the proper line in .taskrc (thanks to Erlan Sergaziev). + Fixed bug #691, which was a mis-reporting of file lock state even when file locking was turned off (thanks to Tom Duffy). + Fixed bug #696, where the command line parser was confused by a single '-' diff --git a/src/commands/CmdConfig.cpp b/src/commands/CmdConfig.cpp index 1e9af6e7b..05e961b88 100644 --- a/src/commands/CmdConfig.cpp +++ b/src/commands/CmdConfig.cpp @@ -78,67 +78,80 @@ int CmdConfig::execute (std::string& output) bool change = false; // Read .taskrc (or equivalent) - std::string contents; + std::vector contents; File::read (context.config.original_file, contents); // task config name value // task config name "" - if (words.size () || - words.back ()._first == "") + if (words.size () > 1) { - // Find existing entry & overwrite - std::string::size_type pos = contents.find (name + "="); - if (pos != std::string::npos) + bool found = false; + std::vector ::iterator line; + for (line = contents.begin (); line != contents.end (); ++line) { - std::string::size_type eol = contents.find_first_of ("\r\f\n", pos); - if (eol == std::string::npos) - throw std::string ("Cannot find EOL after entry '") + name + "'."; + // 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 (confirm (std::string ("Are you sure you want to change the value of '") - + name - + "' from '" - + context.config.get(name) - + "' to '" - + value + "'?")) + if (pos != std::string::npos && + (comment == std::string::npos || + comment > pos)) { - contents = contents.substr (0, pos) - + name + "=" + value - + contents.substr (eol); - change = true; + found = true; + if (confirm (std::string ("Are you sure you want to change the value of '") + + name + + "' from '" + + context.config.get(name) + + "' to '" + + value + "'?")) + { + if (comment != std::string::npos) + *line = name + "=" + value + " " + line->substr (comment); + else + *line = name + "=" + value; + + change = true; + } } } // Not found, so append instead. - else + if (!found && + confirm (std::string ("Are you sure you want to add '") + name + "' with a value of '" + value + "'?")) { - if (confirm (std::string ("Are you sure you want to add '") + name + "' with a value of '" + value + "'?")) - { - contents = contents - + "\n" - + name + "=" + value - + "\n"; - change = true; - } + contents.push_back (name + "=" + value); + change = true; } } // task config name else { - // Remove name - std::string::size_type pos = contents.find (name + "="); - if (pos == std::string::npos) - throw std::string ("No entry named '") + name + "' found."; - - std::string::size_type eol = contents.find_first_of ("\r\f\n", pos); - if (eol == std::string::npos) - throw std::string ("Cannot find EOL after entry '") + name + "'."; - - if (confirm (std::string ("Are you sure you want to remove '") + name + "'?")) + bool found = false; + std::vector ::iterator line; + for (line = contents.begin (); line != contents.end (); ++line) { - contents = contents.substr (0, pos) + contents.substr (eol + 1); - change = true; + // 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 (confirm (std::string ("Are you sure you want to remove '") + name + "'?")) + { + *line = ""; + change = true; + } + } } + + if (!found) + throw std::string ("No entry named '") + name + "' found."; } // Write .taskrc (or equivalent) @@ -154,6 +167,7 @@ int CmdConfig::execute (std::string& output) } else throw std::string ("Specify the name of a config variable to modify."); + output = out.str (); } else