Parcourir la source

add "APT::String::Endswith" and automatic adding of ".list" in apt edit-source

Michael Vogt il y a 12 ans
Parent
commit
cf993341c2

+ 8 - 0
apt-pkg/contrib/strutl.cc

@@ -49,6 +49,14 @@ std::string Strip(const std::string &s)
    size_t end = s.find_last_not_of(" \t\n");
    size_t end = s.find_last_not_of(" \t\n");
    return s.substr(start, end-start+1);
    return s.substr(start, end-start+1);
 }
 }
+
+bool Endswith(const std::string &s, const std::string &end)
+{
+   if (end.size() > s.size())
+      return false;
+   return (s.substr(s.size() - end.size(), s.size()) == end);
+}
+
 }
 }
 }
 }
 									/*}}}*/
 									/*}}}*/

+ 1 - 0
apt-pkg/contrib/strutl.h

@@ -36,6 +36,7 @@ using std::ostream;
 namespace APT {
 namespace APT {
    namespace String {
    namespace String {
       std::string Strip(const std::string &s);
       std::string Strip(const std::string &s);
+      bool Endswith(const std::string &s, const std::string &ending);
    };
    };
 };
 };
 
 

+ 7 - 3
apt-private/private-sources.cc

@@ -21,10 +21,13 @@ bool EditSources(CommandLine &CmdL)
 
 
    std::string sourceslist;
    std::string sourceslist;
    if (CmdL.FileList[1] != NULL)
    if (CmdL.FileList[1] != NULL)
-      sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1] + ".list";
-   else
+   {
+      sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1];
+      if (!APT::String::Endswith(sourceslist, ".list"))
+         sourceslist += ".list";
+   } else {
       sourceslist = _config->FindFile("Dir::Etc::sourcelist");
       sourceslist = _config->FindFile("Dir::Etc::sourcelist");
-
+   }
    HashString before;
    HashString before;
    if (FileExists(sourceslist))
    if (FileExists(sourceslist))
        before.FromFile(sourceslist);
        before.FromFile(sourceslist);
@@ -38,6 +41,7 @@ bool EditSources(CommandLine &CmdL)
          strprintf(outs, _("Failed to parse %s. Edit again? "),
          strprintf(outs, _("Failed to parse %s. Edit again? "),
                    sourceslist.c_str());
                    sourceslist.c_str());
          std::cout << outs;
          std::cout << outs;
+         // FIXME: should we add a "restore previous" option here?
          res = !YnPrompt(true);
          res = !YnPrompt(true);
       }
       }
       _error->RevertToStack();
       _error->RevertToStack();

+ 18 - 0
test/libapt/strutil_test.cc

@@ -69,5 +69,23 @@ int main(int argc,char *argv[])
    result = StringSplit(input, "");
    result = StringSplit(input, "");
    equals(result.size(), 0);
    equals(result.size(), 0);
 
 
+   // endswith
+   bool b;
+   input = "abcd";
+   b = APT::String::Endswith(input, "d");
+   equals(b, true);
+
+   b = APT::String::Endswith(input, "cd");
+   equals(b, true);
+
+   b = APT::String::Endswith(input, "abcd");
+   equals(b, true);
+
+   b = APT::String::Endswith(input, "x");
+   equals(b, false);
+
+   b = APT::String::Endswith(input, "abcndefg");
+   equals(b, false);
+
    return 0;
    return 0;
 }
 }