// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: strutl.cc,v 1.7 1998/10/23 00:49:59 jgg Exp $
+// $Id: strutl.cc,v 1.14 1998/12/03 07:29:18 jgg Exp $
/* ######################################################################
String Util - Some usefull string functions.
##################################################################### */
/*}}}*/
// Includes /*{{{*/
+#ifdef __GNUG__
+#pragma implementation "strutl.h"
+#endif
+
#include <strutl.h>
#include <apt-pkg/fileutl.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
-#include <time.h>
/*}}}*/
// strstrip - Remove white space from the front and back of a string /*{{{*/
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 URI;
- return string(URI,0,Pos);
-}
- /*}}}*/
// Base64Encode - Base64 Encoding routine for short strings /*{{{*/
// ---------------------------------------------------------------------
/* This routine performs a base64 transformation on a string. It was ripped
End += Res;
// Look for the end of the message
- for (char *I = Buffer; I < End; I++)
+ for (char *I = Buffer; I + 1 < End; I++)
{
if (I[0] != '\n' || I[1] != '\n')
continue;
return true;
}
/*}}}*/
+
+// URI::CopyFrom - Copy from an object /*{{{*/
+// ---------------------------------------------------------------------
+/* This parses the URI into all of its components */
+void URI::CopyFrom(string U)
+{
+ string::const_iterator I = U.begin();
+
+ // Locate the first colon, this seperates the scheme
+ for (; I < U.end() && *I != ':' ; I++);
+ string::const_iterator FirstColon = I;
+
+ /* 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] == '/')
+ 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());
+ if (SingleSlash != U.end())
+ Path = string(U,SingleSlash - U.begin());
+ if (Path.empty() == true)
+ Path = "/";
+
+ // Now we attempt to locate a user:pass@host fragment
+ if (U[1] == '/' && U[2] == '/')
+ FirstColon += 3;
+ else
+ FirstColon += 1;
+ if (FirstColon >= U.end())
+ return;
+
+ if (FirstColon > SingleSlash)
+ FirstColon = SingleSlash;
+
+ // Search for the @
+ I = FirstColon;
+ 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)
+ {
+ if (FirstColon < SingleSlash)
+ Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon);
+ }
+ else
+ {
+ Host = string(U,At - U.begin() + 1,SingleSlash - At - 1);
+ User = string(U,FirstColon - U.begin(),SecondColon - FirstColon);
+ if (SecondColon < At)
+ Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
+ }
+
+ // Now we parse off a pot number from the hostname
+ Port = 0;
+ string::size_type Pos = Host.rfind(':');
+ if (Pos == string::npos)
+ return;
+
+ Port = atoi(string(Host,Pos+1).c_str());
+ Host = string(Host,0,Pos);
+}
+ /*}}}*/
+// URI::operator string - Convert the URI to a string /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+URI::operator string()
+{
+ string Res = Access + ':';
+ if (Host.empty() == false)
+ {
+ Res += "//";
+ if (User.empty() == false)
+ {
+ Res += "//" + User;
+ if (Password.empty() == false)
+ Res += ":" + Password;
+ Res += "@";
+ }
+ Res += Host;
+ if (Port != 0)
+ {
+ char S[30];
+ sprintf(S,":%u",Port);
+ Res += S;
+ }
+ }
+
+ if (Path.empty() == false)
+ {
+ if (Path[0] != '/')
+ Res += "/" + Path;
+ else
+ Res += Path;
+ }
+
+ return Res;
+}
+ /*}}}*/