X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/0a8a80e58374771acc225fe1e08ed8e0fe0016cc..d64805b9d9bb6df1ca22aff8ad040b5288c996eb:/apt-pkg/contrib/strutl.cc?ds=sidebyside diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 04a3c7bb7..303cb27db 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1,27 +1,86 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.cc,v 1.6 1998/10/22 04:56:48 jgg Exp $ +// $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $ /* ###################################################################### - String Util - Some usefull string functions. + String Util - Some useful string functions. - strstrip - Remove whitespace from the front and end of a line. + These have been collected from here and there to do all sorts of useful + things to strings. They are useful in file parsers, URI handlers and + especially in APT methods. This source is placed in the Public Domain, do with it what you will - It was originally written by Jason Gunthorpe + It was originally written by Jason Gunthorpe ##################################################################### */ /*}}}*/ // Includes /*{{{*/ -#include +#ifdef __GNUG__ +#pragma implementation "apt-pkg/strutl.h" +#endif + +#include #include +#include +#include + #include #include #include -#include +#include +#include +#include +#include +#include + +#include "config.h" + +using namespace std; /*}}}*/ +// UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/ +// --------------------------------------------------------------------- +/* This is handy to use before display some information for enduser */ +bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) +{ + iconv_t cd; + const char *inbuf; + char *inptr, *outbuf, *outptr; + size_t insize, outsize; + + cd = iconv_open(codeset, "UTF-8"); + if (cd == (iconv_t)(-1)) { + // Something went wrong + if (errno == EINVAL) + _error->Error("conversion from 'UTF-8' to '%s' not available", + codeset); + else + perror("iconv_open"); + + // Clean the destination string + *dest = ""; + + return false; + } + + insize = outsize = orig.size(); + inbuf = orig.data(); + inptr = (char *)inbuf; + outbuf = new char[insize+1]; + outptr = outbuf; + + iconv(cd, &inptr, &insize, &outptr, &outsize); + *outptr = '\0'; + + *dest = outbuf; + delete[] outbuf; + + iconv_close(cd); + + return true; +} + /*}}}*/ // strstrip - Remove white space from the front and back of a string /*{{{*/ // --------------------------------------------------------------------- /* This is handy to use when parsing a file. It also removes \n's left @@ -80,7 +139,8 @@ 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. This is for URI/URL parsing. */ + and striped out as well. This is for URI/URL parsing. It also can + understand [] brackets.*/ bool ParseQuoteWord(const char *&String,string &Res) { // Skip leading whitespace @@ -90,11 +150,17 @@ 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 == '"') { - for (C++;*C != 0 && *C != '"'; C++); + for (C++; *C != 0 && *C != '"'; C++); + if (*C == 0) + return false; + } + if (*C == '[') + { + for (C++; *C != 0 && *C != ']'; C++); if (*C == 0) return false; } @@ -111,7 +177,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; @@ -126,16 +192,16 @@ 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; } /*}}}*/ // ParseCWord - Parses a string like a C "" expression /*{{{*/ // --------------------------------------------------------------------- -/* This expects a series of space seperated strings enclosed in ""'s. +/* This expects a series of space separated strings enclosed in ""'s. It concatenates the ""'s into a single string. */ -bool ParseCWord(const char *String,string &Res) +bool ParseCWord(const char *&String,string &Res) { // Skip leading whitespace const char *C = String; @@ -166,9 +232,10 @@ bool ParseCWord(const char *String,string &Res) if (isspace(*C) == 0) return false; *Buf++ = ' '; - } + } *Buf = 0; Res = Buffer; + String = C; return true; } /*}}}*/ @@ -193,10 +260,35 @@ 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::const_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. The - max length of the string will be 5 chars unless the size is > 10 +/* A max of 4 digits are shown before conversion to the next highest unit. + The max length of the string will be 5 chars unless the size is > 10 YottaBytes (E24) */ string SizeToStr(double Size) { @@ -209,7 +301,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) { @@ -286,6 +378,13 @@ string SubstVar(string Str,string Subst,string Contents) return Temp + string(Str,OldPos); } + +string SubstVar(string Str,const struct SubstVar *Vars) +{ + for (; Vars->Subst != 0; Vars++) + Str = SubstVar(Str,Vars->Subst,*Vars->Contents); + return Str; +} /*}}}*/ // URItoFileName - Convert the uri into a unique file name /*{{{*/ // --------------------------------------------------------------------- @@ -294,11 +393,14 @@ string SubstVar(string Str,string Subst,string Contents) 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++); - + // Nuke 'sensitive' items + ::URI U(URI); + U.User = string(); + U.Password = string(); + U.Access = ""; + // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF"; - URI = QuoteString(string(I,URI.end() - I),"\\|{}[]<>\"^~_=!@#$%^&*"); + URI = QuoteString(U,"\\|{}[]<>\"^~=!@#$%^&*"); string::iterator J = URI.begin(); for (; J != URI.end(); J++) if (*J == '/') @@ -306,17 +408,6 @@ string URItoFileName(string URI) 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 @@ -394,6 +485,43 @@ int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd) return -1; return 1; } + +#if __GNUC__ >= 3 +int stringcmp(string::const_iterator A,string::const_iterator AEnd, + const char *B,const char *BEnd) +{ + for (; A != AEnd && B != BEnd; A++, B++) + if (*A != *B) + break; + + if (A == AEnd && B == BEnd) + return 0; + if (A == AEnd) + return 1; + if (B == BEnd) + return -1; + if (*A < *B) + return -1; + return 1; +} +int stringcmp(string::const_iterator A,string::const_iterator AEnd, + string::const_iterator B,string::const_iterator BEnd) +{ + for (; A != AEnd && B != BEnd; A++, B++) + if (*A != *B) + break; + + if (A == AEnd && B == BEnd) + return 0; + if (A == AEnd) + return 1; + if (B == BEnd) + return -1; + if (*A < *B) + return -1; + return 1; +} +#endif /*}}}*/ // stringcasecmp - Arbitary case insensitive string compare /*{{{*/ // --------------------------------------------------------------------- @@ -414,6 +542,42 @@ int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd) return -1; return 1; } +#if __GNUC__ >= 3 +int stringcasecmp(string::const_iterator A,string::const_iterator 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) + return 1; + if (B == BEnd) + return -1; + if (toupper(*A) < toupper(*B)) + return -1; + return 1; +} +int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, + string::const_iterator B,string::const_iterator BEnd) +{ + for (; A != AEnd && B != BEnd; A++, B++) + if (toupper(*A) != toupper(*B)) + break; + + if (A == AEnd && B == BEnd) + return 0; + if (A == AEnd) + return 1; + if (B == BEnd) + return -1; + if (toupper(*A) < toupper(*B)) + return -1; + return 1; +} +#endif /*}}}*/ // LookupTag - Lookup the value of a tag in a taged string /*{{{*/ // --------------------------------------------------------------------- @@ -435,7 +599,7 @@ string LookupTag(string Message,const char *Tag,const char *Default) for (J = I; *J != '\n' && J < Message.end(); J++); for (; J > I && isspace(J[-1]) != 0; J--); - return string(I,J-I); + return string(I,J); } for (; *I != '\n' && I < Message.end(); I++); @@ -451,7 +615,7 @@ string LookupTag(string Message,const char *Tag,const char *Default) // --------------------------------------------------------------------- /* 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) +int StringToBool(string Text,int Default) { char *End; int Res = strtol(Text.c_str(),&End,0); @@ -462,6 +626,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; @@ -469,6 +634,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,31 +667,35 @@ string TimeRFC1123(time_t Date) fancy buffering is used. */ bool ReadMessages(int Fd, vector &List) { - char Buffer[4000]; + char Buffer[64000]; char *End = Buffer; 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) return false; // No data - if (Res <= 0) + if (Res < 0 && errno == EAGAIN) return true; - + if (Res < 0) + return false; + 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; // Pull the message out - string Message(Buffer,0,I-Buffer); + string Message(Buffer,I-Buffer); // Fix up the buffer for (; I < End && *I == '\n'; I++); @@ -543,3 +713,514 @@ bool ReadMessages(int Fd, vector &List) } } /*}}}*/ +// MonthConv - Converts a month string into a number /*{{{*/ +// --------------------------------------------------------------------- +/* This was lifted from the boa webserver which lifted it from 'wn-v1.07' + Made it a bit more robust with a few touppers though. */ +static int MonthConv(char *Month) +{ + switch (toupper(*Month)) + { + case 'A': + return toupper(Month[1]) == 'P'?3:7; + case 'D': + return 11; + case 'F': + return 1; + case 'J': + if (toupper(Month[1]) == 'A') + return 0; + return toupper(Month[2]) == 'N'?5:6; + case 'M': + return toupper(Month[2]) == 'R'?2:4; + case 'N': + return 10; + case 'O': + return 9; + case 'S': + return 8; + + // Pretend it is January.. + default: + return 0; + } +} + /*}}}*/ +// 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 , with the help of + Mark Baushke and the rest of the Gurus at CISCO. */ + +/* Turned it into an autoconf check, because GNU is not the only thing which + can provide timegm. -- 2002-09-22, Joel Baker */ + +#ifndef HAVE_TIMEGM // Now with autoconf! +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 + and the C library asctime format. It requires the GNU library function + 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar + reason the C library does not provide any such function :< This also + handles the weird, but unambiguous FTP time format*/ +bool StrToTime(string Val,time_t &Result) +{ + struct tm Tm; + char Month[10]; + const char *I = Val.c_str(); + + // Skip the day of the week + for (;*I != 0 && *I != ' '; I++); + + // Handle RFC 1123 time + Month[0] = 0; + if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year, + &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) + { + // Handle RFC 1036 time + if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month, + &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6) + Tm.tm_year += 1900; + else + { + // asctime format + if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday, + &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6) + { + // 'ftp' time + if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon, + &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) + return false; + Tm.tm_mon--; + } + } + } + + Tm.tm_isdst = 0; + if (Month[0] != 0) + Tm.tm_mon = MonthConv(Month); + Tm.tm_year -= 1900; + + // Convert to local time and then to GMT + Result = timegm(&Tm); + 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; +} + /*}}}*/ +// HexDigit - Convert a hex character into an integer /*{{{*/ +// --------------------------------------------------------------------- +/* Helper for Hex2Num */ +static int HexDigit(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return 0; +} + /*}}}*/ +// Hex2Num - Convert a long hex number into a buffer /*{{{*/ +// --------------------------------------------------------------------- +/* The length of the buffer must be exactly 1/2 the length of the string. */ +bool Hex2Num(string Str,unsigned char *Num,unsigned int Length) +{ + if (Str.length() != Length*2) + return false; + + // Convert each digit. We store it in the same order as the string + int J = 0; + for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2) + { + if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0) + return false; + + Num[J] = HexDigit(I[0]) << 4; + Num[J] += HexDigit(I[1]); + } + + return true; +} + /*}}}*/ +// TokSplitString - Split a string up by a given token /*{{{*/ +// --------------------------------------------------------------------- +/* This is intended to be a faster splitter, it does not use dynamic + memories. Input is changed to insert nulls at each token location. */ +bool TokSplitString(char Tok,char *Input,char **List, + unsigned long ListMax) +{ + // Strip any leading spaces + char *Start = Input; + char *Stop = Start + strlen(Start); + for (; *Start != 0 && isspace(*Start) != 0; Start++); + + unsigned long Count = 0; + char *Pos = Start; + while (Pos != Stop) + { + // Skip to the next Token + for (; Pos != Stop && *Pos != Tok; Pos++); + + // Back remove spaces + char *End = Pos; + for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--); + *End = 0; + + List[Count++] = Start; + if (Count >= ListMax) + { + List[Count-1] = 0; + return false; + } + + // Advance pos + for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++); + Start = Pos; + } + + List[Count] = 0; + return true; +} + /*}}}*/ +// RegexChoice - Simple regex list/list matcher /*{{{*/ +// --------------------------------------------------------------------- +/* */ +unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, + const char **ListEnd) +{ + for (RxChoiceList *R = Rxs; R->Str != 0; R++) + R->Hit = false; + + unsigned long Hits = 0; + for (; ListBegin != ListEnd; ListBegin++) + { + // Check if the name is a regex + const char *I; + bool Regex = true; + for (I = *ListBegin; *I != 0; I++) + if (*I == '.' || *I == '?' || *I == '*' || *I == '|') + break; + if (*I == 0) + Regex = false; + + // Compile the regex pattern + regex_t Pattern; + if (Regex == true) + if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE | + REG_NOSUB) != 0) + Regex = false; + + // Search the list + bool Done = false; + for (RxChoiceList *R = Rxs; R->Str != 0; R++) + { + if (R->Str[0] == 0) + continue; + + if (strcasecmp(R->Str,*ListBegin) != 0) + { + if (Regex == false) + continue; + if (regexec(&Pattern,R->Str,0,0,0) != 0) + continue; + } + Done = true; + + if (R->Hit == false) + Hits++; + + R->Hit = true; + } + + if (Regex == true) + regfree(&Pattern); + + if (Done == false) + _error->Warning(_("Selection %s not found"),*ListBegin); + } + + return Hits; +} + /*}}}*/ +// ioprintf - C format string outputter to C++ iostreams /*{{{*/ +// --------------------------------------------------------------------- +/* This is used to make the internationalization strings easier to translate + and to allow reordering of parameters */ +void ioprintf(ostream &out,const char *format,...) +{ + va_list args; + va_start(args,format); + + // sprintf the description + char S[400]; + vsnprintf(S,sizeof(S),format,args); + out << S; +} + /*}}}*/ +// safe_snprintf - Safer snprintf /*{{{*/ +// --------------------------------------------------------------------- +/* This is a snprintf that will never (ever) go past 'End' and returns a + pointer to the end of the new string. The returned string is always null + terminated unless Buffer == end. This is a better alterantive to using + consecutive snprintfs. */ +char *safe_snprintf(char *Buffer,char *End,const char *Format,...) +{ + va_list args; + unsigned long Did; + + va_start(args,Format); + + if (End <= Buffer) + return End; + + Did = vsnprintf(Buffer,End - Buffer,Format,args); + if (Did < 0 || Buffer + Did > End) + return End; + return Buffer + Did; +} + /*}}}*/ + +// CheckDomainList - See if Host is in a , seperate list /*{{{*/ +// --------------------------------------------------------------------- +/* The domain list is a comma seperate list of domains that are suffix + matched against the argument */ +bool CheckDomainList(string Host,string List) +{ + string::const_iterator Start = List.begin(); + for (string::const_iterator Cur = List.begin(); Cur <= List.end(); Cur++) + { + if (Cur < List.end() && *Cur != ',') + continue; + + // Match the end of the string.. + if ((Host.size() >= (unsigned)(Cur - Start)) && + Cur - Start != 0 && + stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0) + return true; + + Start = Cur + 1; + } + return false; +} + /*}}}*/ + +// 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 separates 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; + + /* Find the / indicating the end of the hostname, ignoring /'s in the + square brackets */ + bool InBracket = false; + for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); SingleSlash++) + { + if (*SingleSlash == '[') + InBracket = true; + if (InBracket == true && *SingleSlash == ']') + InBracket = false; + } + + 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 (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/') + FirstColon += 3; + else + FirstColon += 1; + if (FirstColon >= U.end()) + return; + + if (FirstColon > SingleSlash) + FirstColon = SingleSlash; + + // 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; + + // 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 the RFC 2732 [] hostnames. + unsigned long PortEnd = 0; + InBracket = false; + for (unsigned I = 0; I != Host.length();) + { + if (Host[I] == '[') + { + InBracket = true; + Host.erase(I,1); + continue; + } + + if (InBracket == true && Host[I] == ']') + { + InBracket = false; + Host.erase(I,1); + PortEnd = I; + continue; + } + I++; + } + + // Tsk, weird. + if (InBracket == true) + { + Host = string(); + return; + } + + // Now we parse off a port number from the hostname + Port = 0; + string::size_type Pos = Host.rfind(':'); + if (Pos == string::npos || Pos < PortEnd) + 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; + + if (Access.empty() == false) + Res = Access + ':'; + + if (Host.empty() == false) + { + if (Access.empty() == false) + Res += "//"; + + if (User.empty() == false) + { + Res += User; + if (Password.empty() == false) + Res += ":" + Password; + Res += "@"; + } + + // Add RFC 2732 escaping characters + if (Access.empty() == false && + (Host.find('/') != string::npos || Host.find(':') != string::npos)) + Res += '[' + Host + ']'; + else + 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; +} + /*}}}*/ +// URI::SiteOnly - Return the schema and site for the URI /*{{{*/ +// --------------------------------------------------------------------- +/* */ +string URI::SiteOnly(string URI) +{ + ::URI U(URI); + U.User = string(); + U.Password = string(); + U.Path = string(); + U.Port = 0; + return U; +} + /*}}}*/