]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/strutl.cc
*** empty log message ***
[apt.git] / apt-pkg / contrib / strutl.cc
index c99f88c3fc930e33dab905024f6125920cb857ae..a6eaeaa79aa9f3980ccfb6db78e9d5f5b6dc1e05 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: strutl.cc,v 1.12 1998/11/04 07:11:13 jgg Exp $
+// $Id: strutl.cc,v 1.23 1999/03/16 07:27:17 jgg Exp $
 /* ######################################################################
 
    String Util - Some usefull string functions.
                                                                        /*}}}*/
 // Includes                                                            /*{{{*/
 #ifdef __GNUG__
-#pragma implementation "strutl.h"
+#pragma implementation "apt-pkg/strutl.h"
 #endif
 
-#include <strutl.h>
+#include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 
 #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   /*{{{*/
@@ -116,7 +118,7 @@ bool ParseQuoteWord(const char *&String,string &Res)
       {
         Tmp[0] = Start[1];
         Tmp[1] = Start[2];
-        Tmp[3] = 0;
+        Tmp[2] = 0;
         *I = (char)strtol(Tmp,0,16);
         Start += 3;
         continue;
@@ -198,6 +200,31 @@ string QuoteString(string Str,const char *Bad)
    return Res;
 }
                                                                        /*}}}*/
+// DeQuoteString - Convert a string from quoted from                    /*{{{*/
+// ---------------------------------------------------------------------
+/* This undoes QuoteString */
+string DeQuoteString(string Str)
+{
+   string Res;
+   for (string::iterator I = Str.begin(); I != Str.end(); I++)
+   {
+      if (*I == '%' && I + 2 < Str.end())
+      {
+        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;   
+}
+
+                                                                        /*}}}*/
 // SizeToStr - Convert a long into a human readable size               /*{{{*/
 // ---------------------------------------------------------------------
 /* A max of 4 digits are shown before conversion to the next highest unit. 
@@ -214,7 +241,7 @@ string SizeToStr(double Size)
    
    /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, 
       ExaBytes, ZettaBytes, YottaBytes */
-   char Ext[] = {'b','k','M','G','T','P','E','Z','Y'};
+   char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
    int I = 0;
    while (I <= 8)
    {
@@ -456,6 +483,7 @@ int StringToBool(string Text,int Default = -1)
    if (strcasecmp(Text.c_str(),"no") == 0 ||
        strcasecmp(Text.c_str(),"false") == 0 ||
        strcasecmp(Text.c_str(),"without") == 0 ||
+       strcasecmp(Text.c_str(),"off") == 0 ||
        strcasecmp(Text.c_str(),"disable") == 0)
       return 0;
    
@@ -463,6 +491,7 @@ int StringToBool(string Text,int Default = -1)
    if (strcasecmp(Text.c_str(),"yes") == 0 ||
        strcasecmp(Text.c_str(),"true") == 0 ||
        strcasecmp(Text.c_str(),"with") == 0 ||
+       strcasecmp(Text.c_str(),"on") == 0 ||
        strcasecmp(Text.c_str(),"enable") == 0)
       return 1;
    
@@ -501,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)
@@ -570,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
@@ -623,23 +678,14 @@ void URI::CopyFrom(string U)
    for (; I < U.end() && *I != ':' ; I++);
    string::const_iterator FirstColon = I;
 
-   // Determine if this is a host type URI with a leading double //
+   /* Determine if this is a host type URI with a leading double //
+      and then search for the first single / */
    string::const_iterator SingleSlash = I;
    if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
-   {
-      // Locate the single / that starts the path
-      for (; I < U.end(); I++)
-      {
-        if (*I == '/' && I+1 < U.end() && I[1] == '/')
-           I += 2;
-        else 
-           if (*I == '/')
-              break;
-      }
-      if (I > U.end())
-        I = U.end();
-      SingleSlash = I;      
-   }
+      SingleSlash += 3;
+   for (; SingleSlash < U.end() && *SingleSlash != '/'; SingleSlash++);
+   if (SingleSlash > U.end())
+      SingleSlash = U.end();
 
    // We can now write the access and path specifiers
    Access = string(U,0,FirstColon - U.begin());
@@ -649,7 +695,10 @@ void URI::CopyFrom(string U)
       Path = "/";
 
    // Now we attempt to locate a user:pass@host fragment
-   FirstColon += 3;
+   if (FirstColon[1] == '/' && FirstColon[2] == '/')
+      FirstColon += 3;
+   else
+      FirstColon += 1;
    if (FirstColon >= U.end())
       return;