]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/strutl.cc
add APT::String::Strip(), start cleanup of ProcessDpkgStatusLine
[apt.git] / apt-pkg / contrib / strutl.cc
index df02c3499c5b681041c27b9a3e9eef4c4fe6b535..9f794927d0e3280f1a3f9e5072b28c0c896d533a 100644 (file)
 
 using namespace std;
                                                                        /*}}}*/
 
 using namespace std;
                                                                        /*}}}*/
-
+// Strip - Remove white space from the front and back of a string       /*{{{*/
+// ---------------------------------------------------------------------
+namespace APT {
+   namespace String {
+std::string Strip(const std::string &s)
+{
+   size_t start = s.find_first_not_of(" \t\n");
+   // only whitespace
+   if (start == string::npos)
+      return "";
+   size_t end = s.find_last_not_of(" \t\n");
+   return s.substr(start, end-start+1);
+}
+}
+}
+                                                                       /*}}}*/
 // UTF8ToCodeset - Convert some UTF-8 string for some codeset          /*{{{*/
 // ---------------------------------------------------------------------
 /* This is handy to use before display some information for enduser  */
 // UTF8ToCodeset - Convert some UTF-8 string for some codeset          /*{{{*/
 // ---------------------------------------------------------------------
 /* This is handy to use before display some information for enduser  */
@@ -943,6 +958,8 @@ bool StrToTime(const string &Val,time_t &Result)
    Tm.tm_isdst = 0;
    if (Month[0] != 0)
       Tm.tm_mon = MonthConv(Month);
    Tm.tm_isdst = 0;
    if (Month[0] != 0)
       Tm.tm_mon = MonthConv(Month);
+   else
+      Tm.tm_mon = 0; // we don't have a month, so pick something
    Tm.tm_year -= 1900;
    
    // Convert to local time and then to GMT
    Tm.tm_year -= 1900;
    
    // Convert to local time and then to GMT
@@ -1116,6 +1133,37 @@ vector<string> VectorizeString(string const &haystack, char const &split)
    return exploded;
 }
                                                                        /*}}}*/
    return exploded;
 }
                                                                        /*}}}*/
+// StringSplit - split a string into a string vector by token          /*{{{*/
+// ---------------------------------------------------------------------
+/* See header for details.
+ */
+vector<string> StringSplit(std::string const &s, std::string const &sep,
+                           unsigned int maxsplit)
+{
+   vector<string> split;
+   size_t start, pos;
+
+   // no seperator given, this is bogus
+   if(sep.size() == 0)
+      return split;
+
+   start = pos = 0;
+   while (pos != string::npos)
+   {
+      pos = s.find(sep, start);
+      split.push_back(s.substr(start, pos-start));
+      
+      // if maxsplit is reached, the remaining string is the last item
+      if(split.size() >= maxsplit)
+      {
+         split[split.size()-1] = s.substr(start);
+         break;
+      }
+      start = pos+sep.size();
+   }
+   return split;
+}
+                                                                       /*}}}*/
 // RegexChoice - Simple regex list/list matcher                                /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // RegexChoice - Simple regex list/list matcher                                /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -1291,6 +1339,18 @@ bool CheckDomainList(const string &Host,const string &List)
    return false;
 }
                                                                        /*}}}*/
    return false;
 }
                                                                        /*}}}*/
+// strv_length - Return the length of a NULL-terminated string array   /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+size_t strv_length(const char **str_array)
+{
+   size_t i;
+   for (i=0; str_array[i] != NULL; i++)
+      /* nothing */
+      ;
+   return i;
+}
+
 // DeEscapeString - unescape (\0XX and \xXX) from a string             /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // DeEscapeString - unescape (\0XX and \xXX) from a string             /*{{{*/
 // ---------------------------------------------------------------------
 /* */