1
1
// -*- mode: cpp; mode: fold -*-
2
2
// Description /*{{{*/
3
// $Id: strutl.cc,v 1.34 2000/01/16 05:36:17 jgg Exp $
3
// $Id: strutl.cc,v 1.35 2001/02/20 07:03:17 jgg Exp $
4
4
/* ######################################################################
6
String Util - Some usefull string functions.
6
String Util - Some useful string functions.
8
These have been collected from here and there to do all sorts of usefull
9
things to strings. They are usefull in file parsers, URI handlers and
8
These have been collected from here and there to do all sorts of useful
9
things to strings. They are useful in file parsers, URI handlers and
10
10
especially in APT methods.
12
12
This source is placed in the Public Domain, do with it what you will
148
153
// ParseCWord - Parses a string like a C "" expression /*{{{*/
149
154
// ---------------------------------------------------------------------
150
/* This expects a series of space seperated strings enclosed in ""'s.
155
/* This expects a series of space separated strings enclosed in ""'s.
151
156
It concatenates the ""'s into a single string. */
152
bool ParseCWord(const char *String,string &Res)
157
bool ParseCWord(const char *&String,string &Res)
154
159
// Skip leading whitespace
155
160
const char *C = String;
326
332
return Temp + string(Str,OldPos);
335
string SubstVar(string Str,const struct SubstVar *Vars)
337
for (; Vars->Subst != 0; Vars++)
338
Str = SubstVar(Str,Vars->Subst,*Vars->Contents);
329
342
// URItoFileName - Convert the uri into a unique file name /*{{{*/
330
343
// ---------------------------------------------------------------------
767
// TokSplitString - Split a string up by a given token /*{{{*/
768
// ---------------------------------------------------------------------
769
/* This is intended to be a faster splitter, it does not use dynamic
770
memories. Input is changed to insert nulls at each token location. */
771
bool TokSplitString(char Tok,char *Input,char **List,
772
unsigned long ListMax)
774
// Strip any leading spaces
776
char *Stop = Start + strlen(Start);
777
for (; *Start != 0 && isspace(*Start) != 0; Start++);
779
unsigned long Count = 0;
783
// Skip to the next Token
784
for (; Pos != Stop && *Pos != Tok; Pos++);
786
// Back remove spaces
788
for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--);
791
List[Count++] = Start;
792
if (Count >= ListMax)
799
for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++);
807
// RegexChoice - Simple regex list/list matcher /*{{{*/
808
// ---------------------------------------------------------------------
810
unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
811
const char **ListEnd)
813
for (RxChoiceList *R = Rxs; R->Str != 0; R++)
816
unsigned long Hits = 0;
817
for (; ListBegin != ListEnd; ListBegin++)
819
// Check if the name is a regex
822
for (I = *ListBegin; *I != 0; I++)
823
if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
828
// Compile the regex pattern
831
if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE |
837
for (RxChoiceList *R = Rxs; R->Str != 0; R++)
842
if (strcasecmp(R->Str,*ListBegin) != 0)
846
if (regexec(&Pattern,R->Str,0,0,0) != 0)
861
_error->Warning(_("Selection %s not found"),*ListBegin);
867
// ioprintf - C format string outputter to C++ iostreams /*{{{*/
868
// ---------------------------------------------------------------------
869
/* This is used to make the internationalization strinc easier to translate
870
and to allow reordering of parameters */
871
void ioprintf(ostream &out,const char *format,...)
874
va_start(args,format);
876
// sprintf the description
878
vsnprintf(S,sizeof(S),format,args);
753
883
// URI::CopyFrom - Copy from an object /*{{{*/
754
884
// ---------------------------------------------------------------------
758
888
string::const_iterator I = U.begin();
760
// Locate the first colon, this seperates the scheme
890
// Locate the first colon, this separates the scheme
761
891
for (; I < U.end() && *I != ':' ; I++);
762
892
string::const_iterator FirstColon = I;