// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: strutl.cc,v 1.32 2000/01/10 03:44:54 jgg Exp $
+// $Id: strutl.cc,v 1.46 2002/11/22 07:15:23 doogie Exp $
/* ######################################################################
- String Util - Some usefull string functions.
+ String Util - Some useful string functions.
- These have been collected from here and there to do all sorts of usefull
- things to strings. They are usefull in file parsers, URI handlers and
+ 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
#include <apt-pkg/strutl.h>
#include <apt-pkg/fileutl.h>
+#include <apt-pkg/error.h>
+#include <apti18n.h>
+
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
+#include <regex.h>
#include <errno.h>
+#include <stdarg.h>
+
+#include "config.h"
+
+using namespace std;
/*}}}*/
// strstrip - Remove white space from the front and back of a string /*{{{*/
// ---------------------------------------------------------------------
/* 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
{
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;
}
/*}}}*/
// 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;
if (isspace(*C) == 0)
return false;
*Buf++ = ' ';
- }
+ }
*Buf = 0;
Res = Buffer;
+ String = C;
return true;
}
/*}}}*/
string DeQuoteString(string Str)
{
string Res;
- for (string::iterator I = Str.begin(); I != Str.end(); I++)
+ for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
{
if (*I == '%' && I + 2 < Str.end())
{
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 /*{{{*/
// ---------------------------------------------------------------------
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 /*{{{*/
// ---------------------------------------------------------------------
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 /*{{{*/
// ---------------------------------------------------------------------
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++);
// ---------------------------------------------------------------------
/* 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);
fancy buffering is used. */
bool ReadMessages(int Fd, vector<string> &List)
{
- char Buffer[4000];
+ char Buffer[64000];
char *End = Buffer;
while (1)
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
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
+
+/* 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;
// 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(const char *Start,const char *End,unsigned char *Num,
- unsigned int Length)
+bool Hex2Num(string Str,unsigned char *Num,unsigned int Length)
{
- if (End - Start != (signed)(Length*2))
+ if (Str.length() != Length*2)
return false;
// Convert each digit. We store it in the same order as the string
int J = 0;
- for (const char *I = Start; I < End;J++, I += 2)
+ for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2)
{
if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0)
return false;
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 strinc 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;
+}
+ /*}}}*/
+
+// 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 /*{{{*/
// ---------------------------------------------------------------------
{
string::const_iterator I = U.begin();
- // Locate the first colon, this seperates the scheme
+ // Locate the first colon, this separates the scheme
for (; I < U.end() && *I != ':' ; I++);
string::const_iterator FirstColon = I;
string::const_iterator SingleSlash = I;
if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
SingleSlash += 3;
- for (; SingleSlash < U.end() && *SingleSlash != '/'; SingleSlash++);
+
+ /* 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();
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)
+ if (Pos == string::npos || Pos < PortEnd)
return;
Port = atoi(string(Host,Pos+1).c_str());
Res = Access + ':';
if (Host.empty() == false)
- {
+ {
if (Access.empty() == false)
Res += "//";
-
+
if (User.empty() == false)
{
Res += User;
Res += "@";
}
- Res += Host;
+ // 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];
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;
+}
+ /*}}}*/