]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/strutl.cc
Start on acquire stuff
[apt.git] / apt-pkg / contrib / strutl.cc
index 8c1f7005cf76533a3d7bf414b5a8f2d85292b7cd..c615f622900c59ded4f14f940d89fdb3da1d0af6 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: strutl.cc,v 1.2 1998/07/16 06:08:41 jgg Exp $
+// $Id: strutl.cc,v 1.5 1998/10/20 02:39:30 jgg Exp $
 /* ######################################################################
 
    String Util - Some usefull string functions.
@@ -77,7 +77,7 @@ char *_strtabexpand(char *String,size_t Len)
 // ---------------------------------------------------------------------
 /* This grabs a single word, converts any % escaped characters to their
    proper values and advances the pointer. Double quotes are understood
-   and striped out as well. */
+   and striped out as well. This is for URI/URL parsing. */
 bool ParseQuoteWord(const char *&String,string &Res)
 {
    // Skip leading whitespace
@@ -128,6 +128,47 @@ bool ParseQuoteWord(const char *&String,string &Res)
    return true;
 }
                                                                        /*}}}*/
+// ParseCWord - Parses a string like a C "" expression                 /*{{{*/
+// ---------------------------------------------------------------------
+/* This expects a series of space seperated strings enclosed in ""'s. 
+   It concatenates the ""'s into a single string. */
+bool ParseCWord(const char *String,string &Res)
+{
+   // Skip leading whitespace
+   const char *C = String;
+   for (;*C != 0 && *C == ' '; C++);
+   if (*C == 0)
+      return false;
+   
+   char Buffer[1024];
+   char *Buf = Buffer;
+   if (strlen(String) >= sizeof(Buffer))
+       return false;
+       
+   for (; *C != 0; C++)
+   {
+      if (*C == '"')
+      {
+        for (C++; *C != 0 && *C != '"'; C++)
+           *Buf++ = *C;
+        
+        if (*C == 0)
+           return false;
+        
+        continue;
+      }      
+      
+      if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0)
+        continue;
+      if (isspace(*C) == 0)
+        return false;
+      *Buf++ = ' ';
+   }   
+   *Buf = 0;
+   Res = Buffer;
+   return true;
+}
+                                                                       /*}}}*/
 // QuoteString - Convert a string into quoted from                     /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -243,6 +284,36 @@ string SubstVar(string Str,string Subst,string Contents)
    return Temp + string(Str,OldPos);
 }
                                                                        /*}}}*/
+// URItoFileName - Convert the uri into a unique file name             /*{{{*/
+// ---------------------------------------------------------------------
+/* This converts a URI into a safe filename. It quotes all unsafe characters
+   and converts / to _ and removes the scheme identifier. The resulting
+   file name should be unique and never occur again for a different file */
+string URItoFileName(string URI)
+{
+   string::const_iterator I = URI.begin() + URI.find(':') + 1;
+   for (; I < URI.end() && *I == '/'; I++);
+
+   // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
+   URI = QuoteString(string(I,URI.end() - I),"\\|{}[]<>\"^~_=!@#$%^&*");
+   string::iterator J = URI.begin();
+   for (; J != URI.end(); J++)
+      if (*J == '/') 
+        *J = '_';
+   return URI;
+}
+                                                                       /*}}}*/
+// URIAccess - Return the access method for the URI                    /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+string URIAccess(string URI)
+{
+   string::size_type Pos = URI.find(':');
+   if (Pos == string::npos)
+      return string();
+   return string(URI,0,Pos);
+}
+                                                                       /*}}}*/
 // Base64Encode - Base64 Encoding routine for short strings            /*{{{*/
 // ---------------------------------------------------------------------
 /* This routine performs a base64 transformation on a string. It was ripped
@@ -329,7 +400,7 @@ int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
    for (; A != AEnd && B != BEnd; A++, B++)
       if (toupper(*A) != toupper(*B))
         break;
-   
+
    if (A == AEnd && B == BEnd)
       return 0;
    if (A == AEnd)
@@ -341,3 +412,63 @@ int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
    return 1;
 }
                                                                        /*}}}*/
+// LookupTag - Lookup the value of a tag in a taged string             /*{{{*/
+// ---------------------------------------------------------------------
+/* The format is like those used in package files and the method 
+   communication system */
+string LookupTag(string Message,const char *Tag,const char *Default)
+{
+   // Look for a matching tag.
+   int Length = strlen(Tag);
+   for (string::iterator I = Message.begin(); I + Length < Message.end(); I++)
+   {
+      // Found the tag
+      if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0)
+      {
+        // Find the end of line and strip the leading/trailing spaces
+        string::iterator J;
+        I += Length + 1;
+        for (; isspace(*I) != 0 && I < Message.end(); I++);
+        for (J = I; *J != '\n' && J < Message.end(); J++);
+        for (; J > I && isspace(J[-1]) != 0; J--);
+        
+        return string(I,J-I);
+      }
+      
+      for (; *I != '\n' && I < Message.end(); I++);
+   }   
+   
+   // Failed to find a match
+   if (Default == 0)
+      return string();
+   return Default;
+}
+                                                                       /*}}}*/
+// StringToBool - Converts a string into a boolean                     /*{{{*/
+// ---------------------------------------------------------------------
+/* This inspects the string to see if it is true or if it is false and
+   then returns the result. Several varients on true/false are checked. */
+int StringToBool(string Text,int Default = -1)
+{
+   char *End;
+   int Res = strtol(Text.c_str(),&End,0);   
+   if (End != Text.c_str() && Res >= 0 && Res <= 1)
+      return Res;
+   
+   // Check for positives
+   if (strcasecmp(Text.c_str(),"no") == 0 ||
+       strcasecmp(Text.c_str(),"false") == 0 ||
+       strcasecmp(Text.c_str(),"without") == 0 ||
+       strcasecmp(Text.c_str(),"disable") == 0)
+      return 0;
+   
+   // Check for negatives
+   if (strcasecmp(Text.c_str(),"yes") == 0 ||
+       strcasecmp(Text.c_str(),"true") == 0 ||
+       strcasecmp(Text.c_str(),"with") == 0 ||
+       strcasecmp(Text.c_str(),"enable") == 0)
+      return 1;
+   
+   return Default;
+}
+                                                                       /*}}}*/