X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/0db4a45bf91faa06616a90865068ac6c07885edf..fc5aece9d8600b68cf8d654b379b6d840f2a5524:/apt-pkg/contrib/strutl.cc diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 5312a3f4f..a75fbdf92 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.cc,v 1.39 2001/05/27 05:19:30 jgg Exp $ +// $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $ /* ###################################################################### String Util - Some useful string functions. @@ -33,6 +33,8 @@ #include #include +#include "config.h" + using namespace std; /*}}}*/ @@ -200,7 +202,7 @@ bool ParseCWord(const char *&String,string &Res) string QuoteString(string Str,const char *Bad) { string Res; - for (const char *I = Str.c_str(); *I != 0; I++) + for (string::iterator I = Str.begin(); I != Str.end(); I++) { if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || *I <= 0x20 || *I >= 0x7F) @@ -221,9 +223,9 @@ string QuoteString(string Str,const char *Bad) string DeQuoteString(string Str) { string Res; - for (const char *I = Str.c_str(); *I != 0; I++) + for (string::const_iterator I = Str.begin(); I != Str.end(); I++) { - if (*I == '%' && I + 2 < Str.c_str() + Str.length()) + if (*I == '%' && I + 2 < Str.end()) { char Tmp[3]; Tmp[0] = I[1]; @@ -356,8 +358,8 @@ string URItoFileName(string URI) // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF"; URI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*"); - char *J = const_cast(URI.c_str()); - for (; *J != 0; J++) + string::iterator J = URI.begin(); + for (; J != URI.end(); J++) if (*J == '/') *J = '_'; return URI; @@ -387,24 +389,24 @@ string Base64Encode(string S) /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ - for (const char *I = S.c_str(); I < (S.c_str() + S.length()); I += 3) + for (string::const_iterator I = S.begin(); I < S.end(); I += 3) { char Bits[3] = {0,0,0}; Bits[0] = I[0]; - if (I + 1 < S.c_str() + S.length()) + if (I + 1 < S.end()) Bits[1] = I[1]; - if (I + 2 < S.c_str() + S.length()) + if (I + 2 < S.end()) Bits[2] = I[2]; Final += tbl[Bits[0] >> 2]; Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)]; - if (I + 1 >= S.c_str() + S.length()) + if (I + 1 >= S.end()) break; Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)]; - if (I + 2 >= S.c_str() + S.length()) + if (I + 2 >= S.end()) break; Final += tbl[Bits[2] & 0x3f]; @@ -440,6 +442,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 /*{{{*/ // --------------------------------------------------------------------- @@ -460,6 +499,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 /*{{{*/ // --------------------------------------------------------------------- @@ -469,22 +544,22 @@ string LookupTag(string Message,const char *Tag,const char *Default) { // Look for a matching tag. int Length = strlen(Tag); - for (const char *I = Message.c_str(); I + Length < Message.c_str() + Message.length(); I++) + 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 - const char *J; + string::iterator J; I += Length + 1; - for (; isspace(*I) != 0 && *I != 0; I++); - for (J = I; *J != '\n' && *J != 0; J++); + 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); } - for (; *I != '\n' && *I != 0; I++); + for (; *I != '\n' && I < Message.end(); I++); } // Failed to find a match @@ -549,7 +624,7 @@ 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) @@ -577,7 +652,7 @@ bool ReadMessages(int Fd, vector &List) 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++); @@ -639,7 +714,11 @@ static int MonthConv(char *Month) Contributed by Roger Beeman , with the help of Mark Baushke and the rest of the Gurus at CISCO. */ -#ifndef __USE_MISC // glib sets this + +/* 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; @@ -867,8 +946,8 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, /*}}}*/ // ioprintf - C format string outputter to C++ iostreams /*{{{*/ // --------------------------------------------------------------------- -/* This is used to make the internationalization strinc easier to translate - and to allow reordering of parameters */ +/* 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; @@ -877,7 +956,29 @@ void ioprintf(ostream &out,const char *format,...) // sprintf the description char S[400]; vsnprintf(S,sizeof(S),format,args); - // std::ostream::operator <<(out, (const char *)S); + 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; } /*}}}*/ @@ -887,16 +988,16 @@ void ioprintf(ostream &out,const char *format,...) matched against the argument */ bool CheckDomainList(string Host,string List) { - const char *Start = List.c_str(); - for (const char *Cur = List.c_str(); *Cur != 0; Cur++) + string::const_iterator Start = List.begin(); + for (string::const_iterator Cur = List.begin(); Cur <= List.end(); Cur++) { - if (*Cur != ',') + if (Cur < List.end() && *Cur != ',') continue; // Match the end of the string.. - if ((Host.size() >= (unsigned)(Cur - List.c_str())) && + if ((Host.size() >= (unsigned)(Cur - Start)) && Cur - Start != 0 && - stringcasecmp(Host.c_str() + Host.length() - (Cur - Start),Host.c_str()+Host.length(),Start,Cur) == 0) + stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0) return true; Start = Cur + 1; @@ -910,22 +1011,22 @@ bool CheckDomainList(string Host,string List) /* This parses the URI into all of its components */ void URI::CopyFrom(string U) { - const char *I = U.c_str(); + string::const_iterator I = U.begin(); // Locate the first colon, this separates the scheme - for (; *I != 0 && *I != ':' ; I++); - const char *FirstColon = I; + 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 / */ - const char *SingleSlash = I; - if (I + 3 < U.c_str() + U.length() && I[1] == '/' && I[2] == '/') + 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.c_str() + U.length() && (*SingleSlash != '/' || InBracket == true); SingleSlash++) + for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); SingleSlash++) { if (*SingleSlash == '[') InBracket = true; @@ -933,22 +1034,22 @@ void URI::CopyFrom(string U) InBracket = false; } - if (SingleSlash > U.c_str() + U.length()) - SingleSlash = U.c_str() + U.length(); + if (SingleSlash > U.end()) + SingleSlash = U.end(); // We can now write the access and path specifiers - Access = string(U,0,FirstColon - U.c_str()); - if (*SingleSlash != 0) - Path = string(U,SingleSlash - U.c_str()); + 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[1] == '/' && FirstColon[2] == '/') + if (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/') FirstColon += 3; else FirstColon += 1; - if (FirstColon >= U.c_str() + U.length()) + if (FirstColon >= U.end()) return; if (FirstColon > SingleSlash) @@ -959,24 +1060,24 @@ void URI::CopyFrom(string U) if (I > SingleSlash) I = SingleSlash; for (; I < SingleSlash && *I != ':'; I++); - const char *SecondColon = I; + string::const_iterator SecondColon = I; // Search for the @ after the colon for (; I < SingleSlash && *I != '@'; I++); - const char *At = I; + string::const_iterator At = I; // Now write the host and user/pass if (At == SingleSlash) { if (FirstColon < SingleSlash) - Host = string(U,FirstColon - U.c_str(),SingleSlash - FirstColon); + Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon); } else { - Host = string(U,At - U.c_str() + 1,SingleSlash - At - 1); - User = string(U,FirstColon - U.c_str(),SecondColon - FirstColon); + 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.c_str() + 1,At - SecondColon - 1); + Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1); } // Now we parse the RFC 2732 [] hostnames.