// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: strutl.cc,v 1.41 2001/05/27 23:30:45 jgg Exp $
+// $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $
/* ######################################################################
String Util - Some useful string functions.
#include <regex.h>
#include <errno.h>
#include <stdarg.h>
+#include <iconv.h>
+
+#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
U.Access = "";
// "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
- URI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*");
+ URI = QuoteString(U,"\\|{}[]<>\"^~=!@#$%^&*");
string::iterator J = URI.begin();
for (; J != URI.end(); J++)
if (*J == '/')
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 /*{{{*/
// ---------------------------------------------------------------------
{
// 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
fancy buffering is used. */
bool ReadMessages(int Fd, vector<string> &List)
{
- char Buffer[4000];
+ char Buffer[64000];
char *End = Buffer;
while (1)
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++);
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;
/*}}}*/
// 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;
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 /*{{{*/
// ---------------------------------------------------------------------
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;
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;