]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/strutl.cc
Minor cleanups, fix for checksum lowercase bug
[apt.git] / apt-pkg / contrib / strutl.cc
index b5086be04f5960276e74adb0d72e726c0ed83a86..fb5f6683c96e1a817aba5d7f6c37acf84408d6b1 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: strutl.cc,v 1.19 1999/02/01 08:11:57 jgg Exp $
+// $Id: strutl.cc,v 1.27 1999/07/26 17:46:08 jgg Exp $
 /* ######################################################################
 
    String Util - Some usefull string functions.
@@ -25,6 +25,8 @@
 #include <ctype.h>
 #include <string.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
                                                                        /*}}}*/
 
 // strstrip - Remove white space from the front and back of a string   /*{{{*/
@@ -95,7 +97,7 @@ bool ParseQuoteWord(const char *&String,string &Res)
       return false;
    
    // Jump to the next word
-   for (;*C != 0 && *C != ' '; C++)
+   for (;*C != 0 && isspace(*C) == 0; C++)
    {
       if (*C == '"')
       {
@@ -131,7 +133,7 @@ bool ParseQuoteWord(const char *&String,string &Res)
    Res = Buffer;
    
    // Skip ending white space
-   for (;*C != 0 && *C == ' '; C++);
+   for (;*C != 0 && isspace(*C) != 0; C++);
    String = C;
    return true;
 }
@@ -177,23 +179,20 @@ bool ParseCWord(const char *String,string &Res)
    return true;
 }
                                                                        /*}}}*/
-// DeQuoteString - Convert a string from quoted from                   /*{{{*/
+// QuoteString - Convert a string into quoted from                     /*{{{*/
 // ---------------------------------------------------------------------
-/* This undoes QuoteString */
-string DeQuoteString(string Str)
+/* */
+string QuoteString(string Str,const char *Bad)
 {
    string Res;
    for (string::iterator I = Str.begin(); I != Str.end(); I++)
    {
-      if (*I == '%' && I + 2 < Str.end())
+      if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || 
+         *I <= 0x20 || *I >= 0x7F)
       {
-        char Tmp[3];
-        Tmp[0] = I[1];
-        Tmp[1] = I[2];
-        Tmp[2] = 0;
-        Res += (char)strtol(Tmp,0,16);
-        I += 2;
-        continue;
+        char Buf[10];
+        sprintf(Buf,"%%%02x",(int)*I);
+        Res += Buf;
       }
       else
         Res += *I;
@@ -201,27 +200,31 @@ string DeQuoteString(string Str)
    return Res;
 }
                                                                        /*}}}*/
-// QuoteString - Convert a string into quoted from                     /*{{{*/
+// DeQuoteString - Convert a string from quoted from                    /*{{{*/
 // ---------------------------------------------------------------------
-/* */
-string QuoteString(string Str,const char *Bad)
+/* This undoes QuoteString */
+string DeQuoteString(string Str)
 {
    string Res;
    for (string::iterator I = Str.begin(); I != Str.end(); I++)
    {
-      if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || 
-         *I <= 0x20 || *I >= 0x7F)
+      if (*I == '%' && I + 2 < Str.end())
       {
-        char Buf[10];
-        sprintf(Buf,"%%%02x",(int)*I);
-        Res += Buf;
+        char Tmp[3];
+        Tmp[0] = I[1];
+        Tmp[1] = I[2];
+        Tmp[2] = 0;
+        Res += (char)strtol(Tmp,0,16);
+        I += 2;
+        continue;
       }
       else
         Res += *I;
    }
-   return Res;
+   return Res;   
 }
-                                                                       /*}}}*/
+
+                                                                        /*}}}*/
 // SizeToStr - Convert a long into a human readable size               /*{{{*/
 // ---------------------------------------------------------------------
 /* A max of 4 digits are shown before conversion to the next highest unit. 
@@ -527,6 +530,8 @@ bool ReadMessages(int Fd, vector<string> &List)
    while (1)
    {
       int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer));
+      if (Res < 0 && errno == EINTR)
+        continue;
       
       // Process is dead, this is kind of bad..
       if (Res == 0)
@@ -596,6 +601,30 @@ static int MonthConv(char *Month)
    }   
 }
                                                                        /*}}}*/
+// timegm - Internal timegm function if gnu is not available           /*{{{*/
+// ---------------------------------------------------------------------
+/* Ripped this evil little function from wget - I prefer the use of 
+   GNU timegm if possible as this technique will have interesting problems
+   with leap seconds, timezones and other.
+   
+   Converts struct tm to time_t, assuming the data in tm is UTC rather
+   than local timezone (mktime assumes the latter).
+   
+   Contributed by Roger Beeman <beeman@cisco.com>, with the help of
+   Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */
+#ifndef __USE_MISC        // glib sets this
+static time_t timegm(struct tm *t)
+{
+   time_t tl, tb;
+   
+   tl = mktime (t);
+   if (tl == -1)
+      return -1;
+   tb = mktime (gmtime (&tl));
+   return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl)));
+}
+#endif
+                                                                       /*}}}*/
 // StrToTime - Converts a string into a time_t                         /*{{{*/
 // ---------------------------------------------------------------------
 /* This handles all 3 populare time formats including RFC 1123, RFC 1036
@@ -637,6 +666,33 @@ bool StrToTime(string Val,time_t &Result)
    return true;
 }
                                                                        /*}}}*/
+// StrToNum - Convert a fixed length string to a number                        /*{{{*/
+// ---------------------------------------------------------------------
+/* This is used in decoding the crazy fixed length string headers in 
+   tar and ar files. */
+bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base)
+{
+   char S[30];
+   if (Len >= sizeof(S))
+      return false;
+   memcpy(S,Str,Len);
+   S[Len] = 0;
+   
+   // All spaces is a zero
+   Res = 0;
+   unsigned I;
+   for (I = 0; S[I] == ' '; I++);
+   if (S[I] == 0)
+      return true;
+   
+   char *End;
+   Res = strtoul(S,&End,Base);
+   if (End == S)
+      return false;
+   
+   return true;
+}
+                                                                       /*}}}*/
 
 // URI::CopyFrom - Copy from an object                                 /*{{{*/
 // ---------------------------------------------------------------------
@@ -676,16 +732,17 @@ void URI::CopyFrom(string U)
    if (FirstColon > SingleSlash)
       FirstColon = SingleSlash;
    
-   // Search for the @
-   I = FirstColon;
+   // Find the colon...
+   I = FirstColon + 1;
+   if (I > SingleSlash)
+      I = SingleSlash;
+   for (; I < SingleSlash && *I != ':'; I++);
+   string::const_iterator SecondColon = I;
+   
+   // Search for the @ after the colon
    for (; I < SingleSlash && *I != '@'; I++);
    string::const_iterator At = I;
    
-   // Colon in the @ section
-   I = FirstColon + 1;
-   for (; I < At && *I != ':'; I++);
-   string::const_iterator SecondColon = I;
-      
    // Now write the host and user/pass
    if (At == SingleSlash)
    {
@@ -700,7 +757,7 @@ void URI::CopyFrom(string U)
         Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
    }   
    
-   // Now we parse off a pot number from the hostname
+   // Now we parse off a port number from the hostname
    Port = 0;
    string::size_type Pos = Host.rfind(':');
    if (Pos == string::npos)