1 // -*- mode: cpp; mode: fold -*-
3 // $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $
4 /* ######################################################################
6 String Util - Some useful string functions.
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 especially in APT methods.
12 This source is placed in the Public Domain, do with it what you will
13 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
15 ##################################################################### */
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/error.h>
40 // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/
41 // ---------------------------------------------------------------------
42 /* This is handy to use before display some information for enduser */
43 bool UTF8ToCodeset(const char *codeset
, const string
&orig
, string
*dest
)
48 size_t insize
, bufsize
;
51 cd
= iconv_open(codeset
, "UTF-8");
52 if (cd
== (iconv_t
)(-1)) {
53 // Something went wrong
55 _error
->Error("conversion from 'UTF-8' to '%s' not available",
63 insize
= bufsize
= orig
.size();
65 inptr
= (char *)inbuf
;
66 outbuf
= new char[bufsize
];
67 size_t lastError
= -1;
71 char *outptr
= outbuf
;
72 size_t outsize
= bufsize
;
73 size_t const err
= iconv(cd
, &inptr
, &insize
, &outptr
, &outsize
);
74 dest
->append(outbuf
, outptr
- outbuf
);
75 if (err
== (size_t)(-1))
82 // replace a series of unknown multibytes with a single "?"
83 if (lastError
!= insize
) {
84 lastError
= insize
- 1;
96 outbuf
= new char[bufsize
];
110 // strstrip - Remove white space from the front and back of a string /*{{{*/
111 // ---------------------------------------------------------------------
112 /* This is handy to use when parsing a file. It also removes \n's left
113 over from fgets and company */
114 char *_strstrip(char *String
)
116 for (;*String
!= 0 && (*String
== ' ' || *String
== '\t'); String
++);
121 char *End
= String
+ strlen(String
) - 1;
122 for (;End
!= String
- 1 && (*End
== ' ' || *End
== '\t' || *End
== '\n' ||
123 *End
== '\r'); End
--);
129 // strtabexpand - Converts tabs into 8 spaces /*{{{*/
130 // ---------------------------------------------------------------------
132 char *_strtabexpand(char *String
,size_t Len
)
134 for (char *I
= String
; I
!= I
+ Len
&& *I
!= 0; I
++)
138 if (I
+ 8 > String
+ Len
)
144 /* Assume the start of the string is 0 and find the next 8 char
150 Len
= 8 - ((String
- I
) % 8);
158 memmove(I
+ Len
,I
+ 1,strlen(I
) + 1);
159 for (char *J
= I
; J
+ Len
!= I
; *I
= ' ', I
++);
164 // ParseQuoteWord - Parse a single word out of a string /*{{{*/
165 // ---------------------------------------------------------------------
166 /* This grabs a single word, converts any % escaped characters to their
167 proper values and advances the pointer. Double quotes are understood
168 and striped out as well. This is for URI/URL parsing. It also can
169 understand [] brackets.*/
170 bool ParseQuoteWord(const char *&String
,string
&Res
)
172 // Skip leading whitespace
173 const char *C
= String
;
174 for (;*C
!= 0 && *C
== ' '; C
++);
178 // Jump to the next word
179 for (;*C
!= 0 && isspace(*C
) == 0; C
++)
183 C
= strchr(C
+ 1, '"');
189 C
= strchr(C
+ 1, ']');
195 // Now de-quote characters
198 const char *Start
= String
;
200 for (I
= Buffer
; I
< Buffer
+ sizeof(Buffer
) && Start
!= C
; I
++)
202 if (*Start
== '%' && Start
+ 2 < C
&&
203 isxdigit(Start
[1]) && isxdigit(Start
[2]))
208 *I
= (char)strtol(Tmp
,0,16);
221 // Skip ending white space
222 for (;*C
!= 0 && isspace(*C
) != 0; C
++);
227 // ParseCWord - Parses a string like a C "" expression /*{{{*/
228 // ---------------------------------------------------------------------
229 /* This expects a series of space separated strings enclosed in ""'s.
230 It concatenates the ""'s into a single string. */
231 bool ParseCWord(const char *&String
,string
&Res
)
233 // Skip leading whitespace
234 const char *C
= String
;
235 for (;*C
!= 0 && *C
== ' '; C
++);
241 if (strlen(String
) >= sizeof(Buffer
))
248 for (C
++; *C
!= 0 && *C
!= '"'; C
++)
257 if (C
!= String
&& isspace(*C
) != 0 && isspace(C
[-1]) != 0)
259 if (isspace(*C
) == 0)
269 // QuoteString - Convert a string into quoted from /*{{{*/
270 // ---------------------------------------------------------------------
272 string
QuoteString(const string
&Str
, const char *Bad
)
275 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
277 if (strchr(Bad
,*I
) != 0 || isprint(*I
) == 0 ||
278 *I
== 0x25 || // percent '%' char
279 *I
<= 0x20 || *I
>= 0x7F) // control chars
282 sprintf(Buf
,"%%%02x",(int)*I
);
291 // DeQuoteString - Convert a string from quoted from /*{{{*/
292 // ---------------------------------------------------------------------
293 /* This undoes QuoteString */
294 string
DeQuoteString(const string
&Str
)
296 return DeQuoteString(Str
.begin(),Str
.end());
298 string
DeQuoteString(string::const_iterator
const &begin
,
299 string::const_iterator
const &end
)
302 for (string::const_iterator I
= begin
; I
!= end
; ++I
)
304 if (*I
== '%' && I
+ 2 < end
&&
305 isxdigit(I
[1]) && isxdigit(I
[2]))
311 Res
+= (char)strtol(Tmp
,0,16);
322 // SizeToStr - Convert a long into a human readable size /*{{{*/
323 // ---------------------------------------------------------------------
324 /* A max of 4 digits are shown before conversion to the next highest unit.
325 The max length of the string will be 5 chars unless the size is > 10
327 string
SizeToStr(double Size
)
336 /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
337 ExaBytes, ZettaBytes, YottaBytes */
338 char Ext
[] = {'\0','k','M','G','T','P','E','Z','Y'};
342 if (ASize
< 100 && I
!= 0)
344 sprintf(S
,"%'.1f %c",ASize
,Ext
[I
]);
350 sprintf(S
,"%'.0f %c",ASize
,Ext
[I
]);
360 // TimeToStr - Convert the time into a string /*{{{*/
361 // ---------------------------------------------------------------------
362 /* Converts a number of seconds to a hms format */
363 string
TimeToStr(unsigned long Sec
)
371 //d means days, h means hours, min means minutes, s means seconds
372 sprintf(S
,_("%lid %lih %limin %lis"),Sec
/60/60/24,(Sec
/60/60) % 24,(Sec
/60) % 60,Sec
% 60);
378 //h means hours, min means minutes, s means seconds
379 sprintf(S
,_("%lih %limin %lis"),Sec
/60/60,(Sec
/60) % 60,Sec
% 60);
385 //min means minutes, s means seconds
386 sprintf(S
,_("%limin %lis"),Sec
/60,Sec
% 60);
391 sprintf(S
,_("%lis"),Sec
);
398 // SubstVar - Substitute a string for another string /*{{{*/
399 // ---------------------------------------------------------------------
400 /* This replaces all occurances of Subst with Contents in Str. */
401 string
SubstVar(const string
&Str
,const string
&Subst
,const string
&Contents
)
403 string::size_type Pos
= 0;
404 string::size_type OldPos
= 0;
407 while (OldPos
< Str
.length() &&
408 (Pos
= Str
.find(Subst
,OldPos
)) != string::npos
)
410 Temp
+= string(Str
,OldPos
,Pos
) + Contents
;
411 OldPos
= Pos
+ Subst
.length();
417 return Temp
+ string(Str
,OldPos
);
420 string
SubstVar(string Str
,const struct SubstVar
*Vars
)
422 for (; Vars
->Subst
!= 0; Vars
++)
423 Str
= SubstVar(Str
,Vars
->Subst
,*Vars
->Contents
);
427 // OutputInDepth - return a string with separator multiplied with depth /*{{{*/
428 // ---------------------------------------------------------------------
429 /* Returns a string with the supplied separator depth + 1 times in it */
430 std::string
OutputInDepth(const unsigned long Depth
, const char* Separator
)
432 std::string output
= "";
433 for(unsigned long d
=Depth
+1; d
> 0; d
--)
434 output
.append(Separator
);
438 // URItoFileName - Convert the uri into a unique file name /*{{{*/
439 // ---------------------------------------------------------------------
440 /* This converts a URI into a safe filename. It quotes all unsafe characters
441 and converts / to _ and removes the scheme identifier. The resulting
442 file name should be unique and never occur again for a different file */
443 string
URItoFileName(const string
&URI
)
445 // Nuke 'sensitive' items
451 // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
452 string NewURI
= QuoteString(U
,"\\|{}[]<>\"^~_=!@#$%^&*");
453 replace(NewURI
.begin(),NewURI
.end(),'/','_');
457 // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This routine performs a base64 transformation on a string. It was ripped
460 from wget and then patched and bug fixed.
462 This spec can be found in rfc2045 */
463 string
Base64Encode(const string
&S
)
466 static char tbl
[64] = {'A','B','C','D','E','F','G','H',
467 'I','J','K','L','M','N','O','P',
468 'Q','R','S','T','U','V','W','X',
469 'Y','Z','a','b','c','d','e','f',
470 'g','h','i','j','k','l','m','n',
471 'o','p','q','r','s','t','u','v',
472 'w','x','y','z','0','1','2','3',
473 '4','5','6','7','8','9','+','/'};
475 // Pre-allocate some space
477 Final
.reserve((4*S
.length() + 2)/3 + 2);
479 /* Transform the 3x8 bits to 4x6 bits, as required by
481 for (string::const_iterator I
= S
.begin(); I
< S
.end(); I
+= 3)
483 char Bits
[3] = {0,0,0};
490 Final
+= tbl
[Bits
[0] >> 2];
491 Final
+= tbl
[((Bits
[0] & 3) << 4) + (Bits
[1] >> 4)];
493 if (I
+ 1 >= S
.end())
496 Final
+= tbl
[((Bits
[1] & 0xf) << 2) + (Bits
[2] >> 6)];
498 if (I
+ 2 >= S
.end())
501 Final
+= tbl
[Bits
[2] & 0x3f];
504 /* Apply the padding elements, this tells how many bytes the remote
505 end should discard */
506 if (S
.length() % 3 == 2)
508 if (S
.length() % 3 == 1)
514 // stringcmp - Arbitrary string compare /*{{{*/
515 // ---------------------------------------------------------------------
516 /* This safely compares two non-null terminated strings of arbitrary
518 int stringcmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
520 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
524 if (A
== AEnd
&& B
== BEnd
)
536 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
537 const char *B
,const char *BEnd
)
539 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
543 if (A
== AEnd
&& B
== BEnd
)
553 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
554 string::const_iterator B
,string::const_iterator BEnd
)
556 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
560 if (A
== AEnd
&& B
== BEnd
)
572 // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/
573 // ---------------------------------------------------------------------
575 int stringcasecmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
577 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
578 if (tolower_ascii(*A
) != tolower_ascii(*B
))
581 if (A
== AEnd
&& B
== BEnd
)
587 if (tolower_ascii(*A
) < tolower_ascii(*B
))
592 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
593 const char *B
,const char *BEnd
)
595 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
596 if (tolower_ascii(*A
) != tolower_ascii(*B
))
599 if (A
== AEnd
&& B
== BEnd
)
605 if (tolower_ascii(*A
) < tolower_ascii(*B
))
609 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
610 string::const_iterator B
,string::const_iterator BEnd
)
612 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
613 if (tolower_ascii(*A
) != tolower_ascii(*B
))
616 if (A
== AEnd
&& B
== BEnd
)
622 if (tolower_ascii(*A
) < tolower_ascii(*B
))
628 // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
629 // ---------------------------------------------------------------------
630 /* The format is like those used in package files and the method
631 communication system */
632 string
LookupTag(const string
&Message
,const char *Tag
,const char *Default
)
634 // Look for a matching tag.
635 int Length
= strlen(Tag
);
636 for (string::const_iterator I
= Message
.begin(); I
+ Length
< Message
.end(); ++I
)
639 if (I
[Length
] == ':' && stringcasecmp(I
,I
+Length
,Tag
) == 0)
641 // Find the end of line and strip the leading/trailing spaces
642 string::const_iterator J
;
644 for (; isspace(*I
) != 0 && I
< Message
.end(); ++I
);
645 for (J
= I
; *J
!= '\n' && J
< Message
.end(); ++J
);
646 for (; J
> I
&& isspace(J
[-1]) != 0; --J
);
651 for (; *I
!= '\n' && I
< Message
.end(); ++I
);
654 // Failed to find a match
660 // StringToBool - Converts a string into a boolean /*{{{*/
661 // ---------------------------------------------------------------------
662 /* This inspects the string to see if it is true or if it is false and
663 then returns the result. Several varients on true/false are checked. */
664 int StringToBool(const string
&Text
,int Default
)
667 int Res
= strtol(Text
.c_str(),&End
,0);
668 if (End
!= Text
.c_str() && Res
>= 0 && Res
<= 1)
671 // Check for positives
672 if (strcasecmp(Text
.c_str(),"no") == 0 ||
673 strcasecmp(Text
.c_str(),"false") == 0 ||
674 strcasecmp(Text
.c_str(),"without") == 0 ||
675 strcasecmp(Text
.c_str(),"off") == 0 ||
676 strcasecmp(Text
.c_str(),"disable") == 0)
679 // Check for negatives
680 if (strcasecmp(Text
.c_str(),"yes") == 0 ||
681 strcasecmp(Text
.c_str(),"true") == 0 ||
682 strcasecmp(Text
.c_str(),"with") == 0 ||
683 strcasecmp(Text
.c_str(),"on") == 0 ||
684 strcasecmp(Text
.c_str(),"enable") == 0)
690 // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
691 // ---------------------------------------------------------------------
692 /* This converts a time_t into a string time representation that is
693 year 2000 complient and timezone neutral */
694 string
TimeRFC1123(time_t Date
)
697 if (gmtime_r(&Date
, &Conv
) == NULL
)
701 const char *Day
[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
702 const char *Month
[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
703 "Aug","Sep","Oct","Nov","Dec"};
705 snprintf(Buf
, sizeof(Buf
), "%s, %02i %s %i %02i:%02i:%02i GMT",Day
[Conv
.tm_wday
],
706 Conv
.tm_mday
,Month
[Conv
.tm_mon
],Conv
.tm_year
+1900,Conv
.tm_hour
,
707 Conv
.tm_min
,Conv
.tm_sec
);
711 // ReadMessages - Read messages from the FD /*{{{*/
712 // ---------------------------------------------------------------------
713 /* This pulls full messages from the input FD into the message buffer.
714 It assumes that messages will not pause during transit so no
715 fancy buffering is used.
717 In particular: this reads blocks from the input until it believes
718 that it's run out of input text. Each block is terminated by a
719 double newline ('\n' followed by '\n'). As noted below, there is a
720 bug in this code: it assumes that all the blocks have been read if
721 it doesn't see additional text in the buffer after the last one is
722 parsed, which will cause it to lose blocks if the last block
723 coincides with the end of the buffer.
725 bool ReadMessages(int Fd
, vector
<string
> &List
)
729 // Represents any left-over from the previous iteration of the
730 // parse loop. (i.e., if a message is split across the end
731 // of the buffer, it goes here)
732 string PartialMessage
;
736 int Res
= read(Fd
,End
,sizeof(Buffer
) - (End
-Buffer
));
737 if (Res
< 0 && errno
== EINTR
)
740 // Process is dead, this is kind of bad..
745 if (Res
< 0 && errno
== EAGAIN
)
752 // Look for the end of the message
753 for (char *I
= Buffer
; I
+ 1 < End
; I
++)
755 if (I
[0] != '\n' || I
[1] != '\n')
758 // Pull the message out
759 string
Message(Buffer
,I
-Buffer
);
760 PartialMessage
+= Message
;
763 for (; I
< End
&& *I
== '\n'; I
++);
765 memmove(Buffer
,I
,End
-Buffer
);
768 List
.push_back(PartialMessage
);
769 PartialMessage
.clear();
773 // If there's text left in the buffer, store it
774 // in PartialMessage and throw the rest of the buffer
775 // away. This allows us to handle messages that
776 // are longer than the static buffer size.
777 PartialMessage
+= string(Buffer
, End
);
782 // BUG ALERT: if a message block happens to end at a
783 // multiple of 64000 characters, this will cause it to
784 // terminate early, leading to a badly formed block and
785 // probably crashing the method. However, this is the only
786 // way we have to find the end of the message block. I have
787 // an idea of how to fix this, but it will require changes
788 // to the protocol (essentially to mark the beginning and
789 // end of the block).
791 // -- dburrows 2008-04-02
795 if (WaitFd(Fd
) == false)
800 // MonthConv - Converts a month string into a number /*{{{*/
801 // ---------------------------------------------------------------------
802 /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
803 Made it a bit more robust with a few tolower_ascii though. */
804 static int MonthConv(char *Month
)
806 switch (tolower_ascii(*Month
))
809 return tolower_ascii(Month
[1]) == 'p'?3:7;
815 if (tolower_ascii(Month
[1]) == 'a')
817 return tolower_ascii(Month
[2]) == 'n'?5:6;
819 return tolower_ascii(Month
[2]) == 'r'?2:4;
827 // Pretend it is January..
833 // timegm - Internal timegm if the gnu version is not available /*{{{*/
834 // ---------------------------------------------------------------------
835 /* Converts struct tm to time_t, assuming the data in tm is UTC rather
836 than local timezone (mktime assumes the latter).
838 This function is a nonstandard GNU extension that is also present on
839 the BSDs and maybe other systems. For others we follow the advice of
840 the manpage of timegm and use his portable replacement. */
842 static time_t timegm(struct tm
*t
)
844 char *tz
= getenv("TZ");
847 time_t ret
= mktime(t
);
857 // FullDateToTime - Converts a HTTP1.1 full date strings into a time_t /*{{{*/
858 // ---------------------------------------------------------------------
859 /* tries to parses a full date as specified in RFC2616 Section 3.3.1
860 with one exception: All timezones (%Z) are accepted but the protocol
861 says that it MUST be GMT, but this one is equal to UTC which we will
862 encounter from time to time (e.g. in Release files) so we accept all
863 here and just assume it is GMT (or UTC) later on */
864 bool RFC1123StrToTime(const char* const str
,time_t &time
)
867 setlocale (LC_ALL
,"C");
869 // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
870 (strptime(str
, "%a, %d %b %Y %H:%M:%S %Z", &Tm
) == NULL
&&
871 // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
872 strptime(str
, "%A, %d-%b-%y %H:%M:%S %Z", &Tm
) == NULL
&&
873 // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
874 strptime(str
, "%a %b %d %H:%M:%S %Y", &Tm
) == NULL
);
875 setlocale (LC_ALL
,"");
883 // FTPMDTMStrToTime - Converts a ftp modification date into a time_t /*{{{*/
884 // ---------------------------------------------------------------------
886 bool FTPMDTMStrToTime(const char* const str
,time_t &time
)
889 // MDTM includes no whitespaces but recommend and ignored by strptime
890 if (strptime(str
, "%Y %m %d %H %M %S", &Tm
) == NULL
)
897 // StrToTime - Converts a string into a time_t /*{{{*/
898 // ---------------------------------------------------------------------
899 /* This handles all 3 populare time formats including RFC 1123, RFC 1036
900 and the C library asctime format. It requires the GNU library function
901 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
902 reason the C library does not provide any such function :< This also
903 handles the weird, but unambiguous FTP time format*/
904 bool StrToTime(const string
&Val
,time_t &Result
)
909 // Skip the day of the week
910 const char *I
= strchr(Val
.c_str(), ' ');
912 // Handle RFC 1123 time
914 if (sscanf(I
," %2d %3s %4d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,&Tm
.tm_year
,
915 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
917 // Handle RFC 1036 time
918 if (sscanf(I
," %2d-%3s-%3d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,
919 &Tm
.tm_year
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) == 6)
924 if (sscanf(I
," %3s %2d %2d:%2d:%2d %4d",Month
,&Tm
.tm_mday
,
925 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
,&Tm
.tm_year
) != 6)
928 if (sscanf(Val
.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm
.tm_year
,&Tm
.tm_mon
,
929 &Tm
.tm_mday
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
938 Tm
.tm_mon
= MonthConv(Month
);
941 // Convert to local time and then to GMT
942 Result
= timegm(&Tm
);
946 // StrToNum - Convert a fixed length string to a number /*{{{*/
947 // ---------------------------------------------------------------------
948 /* This is used in decoding the crazy fixed length string headers in
950 bool StrToNum(const char *Str
,unsigned long &Res
,unsigned Len
,unsigned Base
)
953 if (Len
>= sizeof(S
))
958 // All spaces is a zero
961 for (I
= 0; S
[I
] == ' '; I
++);
966 Res
= strtoul(S
,&End
,Base
);
973 // StrToNum - Convert a fixed length string to a number /*{{{*/
974 // ---------------------------------------------------------------------
975 /* This is used in decoding the crazy fixed length string headers in
977 bool StrToNum(const char *Str
,unsigned long long &Res
,unsigned Len
,unsigned Base
)
980 if (Len
>= sizeof(S
))
985 // All spaces is a zero
988 for (I
= 0; S
[I
] == ' '; I
++);
993 Res
= strtoull(S
,&End
,Base
);
1001 // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
1002 // ---------------------------------------------------------------------
1003 /* This is used in decoding the 256bit encoded fixed length fields in
1005 bool Base256ToNum(const char *Str
,unsigned long &Res
,unsigned int Len
)
1007 if ((Str
[0] & 0x80) == 0)
1011 Res
= Str
[0] & 0x7F;
1012 for(unsigned int i
= 1; i
< Len
; ++i
)
1013 Res
= (Res
<<8) + Str
[i
];
1018 // HexDigit - Convert a hex character into an integer /*{{{*/
1019 // ---------------------------------------------------------------------
1020 /* Helper for Hex2Num */
1021 static int HexDigit(int c
)
1023 if (c
>= '0' && c
<= '9')
1025 if (c
>= 'a' && c
<= 'f')
1026 return c
- 'a' + 10;
1027 if (c
>= 'A' && c
<= 'F')
1028 return c
- 'A' + 10;
1032 // Hex2Num - Convert a long hex number into a buffer /*{{{*/
1033 // ---------------------------------------------------------------------
1034 /* The length of the buffer must be exactly 1/2 the length of the string. */
1035 bool Hex2Num(const string
&Str
,unsigned char *Num
,unsigned int Length
)
1037 if (Str
.length() != Length
*2)
1040 // Convert each digit. We store it in the same order as the string
1042 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end();J
++, I
+= 2)
1044 if (isxdigit(*I
) == 0 || isxdigit(I
[1]) == 0)
1047 Num
[J
] = HexDigit(I
[0]) << 4;
1048 Num
[J
] += HexDigit(I
[1]);
1054 // TokSplitString - Split a string up by a given token /*{{{*/
1055 // ---------------------------------------------------------------------
1056 /* This is intended to be a faster splitter, it does not use dynamic
1057 memories. Input is changed to insert nulls at each token location. */
1058 bool TokSplitString(char Tok
,char *Input
,char **List
,
1059 unsigned long ListMax
)
1061 // Strip any leading spaces
1062 char *Start
= Input
;
1063 char *Stop
= Start
+ strlen(Start
);
1064 for (; *Start
!= 0 && isspace(*Start
) != 0; Start
++);
1066 unsigned long Count
= 0;
1070 // Skip to the next Token
1071 for (; Pos
!= Stop
&& *Pos
!= Tok
; Pos
++);
1073 // Back remove spaces
1075 for (; End
> Start
&& (End
[-1] == Tok
|| isspace(End
[-1]) != 0); End
--);
1078 List
[Count
++] = Start
;
1079 if (Count
>= ListMax
)
1086 for (; Pos
!= Stop
&& (*Pos
== Tok
|| isspace(*Pos
) != 0 || *Pos
== 0); Pos
++);
1094 // VectorizeString - Split a string up into a vector of strings /*{{{*/
1095 // ---------------------------------------------------------------------
1096 /* This can be used to split a given string up into a vector, so the
1097 propose is the same as in the method above and this one is a bit slower
1098 also, but the advantage is that we have an iteratable vector */
1099 vector
<string
> VectorizeString(string
const &haystack
, char const &split
)
1101 string::const_iterator start
= haystack
.begin();
1102 string::const_iterator end
= start
;
1103 vector
<string
> exploded
;
1105 for (; end
!= haystack
.end() && *end
!= split
; ++end
);
1106 exploded
.push_back(string(start
, end
));
1108 } while (end
!= haystack
.end() && (++end
) != haystack
.end());
1112 // RegexChoice - Simple regex list/list matcher /*{{{*/
1113 // ---------------------------------------------------------------------
1115 unsigned long RegexChoice(RxChoiceList
*Rxs
,const char **ListBegin
,
1116 const char **ListEnd
)
1118 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1121 unsigned long Hits
= 0;
1122 for (; ListBegin
!= ListEnd
; ListBegin
++)
1124 // Check if the name is a regex
1127 for (I
= *ListBegin
; *I
!= 0; I
++)
1128 if (*I
== '.' || *I
== '?' || *I
== '*' || *I
== '|')
1133 // Compile the regex pattern
1136 if (regcomp(&Pattern
,*ListBegin
,REG_EXTENDED
| REG_ICASE
|
1142 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1147 if (strcasecmp(R
->Str
,*ListBegin
) != 0)
1151 if (regexec(&Pattern
,R
->Str
,0,0,0) != 0)
1156 if (R
->Hit
== false)
1166 _error
->Warning(_("Selection %s not found"),*ListBegin
);
1172 // {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/
1173 // ---------------------------------------------------------------------
1174 /* This is used to make the internationalization strings easier to translate
1175 and to allow reordering of parameters */
1176 static bool iovprintf(ostream
&out
, const char *format
,
1177 va_list &args
, ssize_t
&size
) {
1178 char *S
= (char*)malloc(size
);
1179 ssize_t
const n
= vsnprintf(S
, size
, format
, args
);
1180 if (n
> -1 && n
< size
) {
1193 void ioprintf(ostream
&out
,const char *format
,...)
1198 va_start(args
,format
);
1199 if (iovprintf(out
, format
, args
, size
) == true)
1204 void strprintf(string
&out
,const char *format
,...)
1208 std::ostringstream outstr
;
1210 va_start(args
,format
);
1211 if (iovprintf(outstr
, format
, args
, size
) == true)
1218 // safe_snprintf - Safer snprintf /*{{{*/
1219 // ---------------------------------------------------------------------
1220 /* This is a snprintf that will never (ever) go past 'End' and returns a
1221 pointer to the end of the new string. The returned string is always null
1222 terminated unless Buffer == end. This is a better alterantive to using
1223 consecutive snprintfs. */
1224 char *safe_snprintf(char *Buffer
,char *End
,const char *Format
,...)
1229 va_start(args
,Format
);
1234 Did
= vsnprintf(Buffer
,End
- Buffer
,Format
,args
);
1235 if (Did
< 0 || Buffer
+ Did
> End
)
1237 return Buffer
+ Did
;
1240 // StripEpoch - Remove the version "epoch" from a version string /*{{{*/
1241 // ---------------------------------------------------------------------
1242 string
StripEpoch(const string
&VerStr
)
1244 size_t i
= VerStr
.find(":");
1245 if (i
== string::npos
)
1247 return VerStr
.substr(i
+1);
1250 // tolower_ascii - tolower() function that ignores the locale /*{{{*/
1251 // ---------------------------------------------------------------------
1252 /* This little function is the most called method we have and tries
1253 therefore to do the absolut minimum - and is noteable faster than
1254 standard tolower/toupper and as a bonus avoids problems with different
1255 locales - we only operate on ascii chars anyway. */
1256 int tolower_ascii(int const c
)
1258 if (c
>= 'A' && c
<= 'Z')
1264 // CheckDomainList - See if Host is in a , seperate list /*{{{*/
1265 // ---------------------------------------------------------------------
1266 /* The domain list is a comma seperate list of domains that are suffix
1267 matched against the argument */
1268 bool CheckDomainList(const string
&Host
,const string
&List
)
1270 string::const_iterator Start
= List
.begin();
1271 for (string::const_iterator Cur
= List
.begin(); Cur
<= List
.end(); ++Cur
)
1273 if (Cur
< List
.end() && *Cur
!= ',')
1276 // Match the end of the string..
1277 if ((Host
.size() >= (unsigned)(Cur
- Start
)) &&
1279 stringcasecmp(Host
.end() - (Cur
- Start
),Host
.end(),Start
,Cur
) == 0)
1287 // DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/
1288 // ---------------------------------------------------------------------
1290 string
DeEscapeString(const string
&input
)
1293 string::const_iterator it
, escape_start
;
1294 string output
, octal
, hex
;
1295 for (it
= input
.begin(); it
!= input
.end(); ++it
)
1297 // just copy non-escape chars
1304 // deal with double escape
1306 (it
+ 1 < input
.end()) && it
[1] == '\\')
1310 // advance iterator one step further
1315 // ensure we have a char to read
1316 if (it
+ 1 == input
.end())
1324 if (it
+ 2 <= input
.end()) {
1328 output
+= (char)strtol(tmp
, 0, 8);
1333 if (it
+ 2 <= input
.end()) {
1337 output
+= (char)strtol(tmp
, 0, 16);
1342 // FIXME: raise exception here?
1349 // URI::CopyFrom - Copy from an object /*{{{*/
1350 // ---------------------------------------------------------------------
1351 /* This parses the URI into all of its components */
1352 void URI::CopyFrom(const string
&U
)
1354 string::const_iterator I
= U
.begin();
1356 // Locate the first colon, this separates the scheme
1357 for (; I
< U
.end() && *I
!= ':' ; ++I
);
1358 string::const_iterator FirstColon
= I
;
1360 /* Determine if this is a host type URI with a leading double //
1361 and then search for the first single / */
1362 string::const_iterator SingleSlash
= I
;
1363 if (I
+ 3 < U
.end() && I
[1] == '/' && I
[2] == '/')
1366 /* Find the / indicating the end of the hostname, ignoring /'s in the
1368 bool InBracket
= false;
1369 for (; SingleSlash
< U
.end() && (*SingleSlash
!= '/' || InBracket
== true); ++SingleSlash
)
1371 if (*SingleSlash
== '[')
1373 if (InBracket
== true && *SingleSlash
== ']')
1377 if (SingleSlash
> U
.end())
1378 SingleSlash
= U
.end();
1380 // We can now write the access and path specifiers
1381 Access
.assign(U
.begin(),FirstColon
);
1382 if (SingleSlash
!= U
.end())
1383 Path
.assign(SingleSlash
,U
.end());
1384 if (Path
.empty() == true)
1387 // Now we attempt to locate a user:pass@host fragment
1388 if (FirstColon
+ 2 <= U
.end() && FirstColon
[1] == '/' && FirstColon
[2] == '/')
1392 if (FirstColon
>= U
.end())
1395 if (FirstColon
> SingleSlash
)
1396 FirstColon
= SingleSlash
;
1398 // Find the colon...
1400 if (I
> SingleSlash
)
1402 for (; I
< SingleSlash
&& *I
!= ':'; ++I
);
1403 string::const_iterator SecondColon
= I
;
1405 // Search for the @ after the colon
1406 for (; I
< SingleSlash
&& *I
!= '@'; ++I
);
1407 string::const_iterator At
= I
;
1409 // Now write the host and user/pass
1410 if (At
== SingleSlash
)
1412 if (FirstColon
< SingleSlash
)
1413 Host
.assign(FirstColon
,SingleSlash
);
1417 Host
.assign(At
+1,SingleSlash
);
1418 // username and password must be encoded (RFC 3986)
1419 User
.assign(DeQuoteString(FirstColon
,SecondColon
));
1420 if (SecondColon
< At
)
1421 Password
.assign(DeQuoteString(SecondColon
+1,At
));
1424 // Now we parse the RFC 2732 [] hostnames.
1425 unsigned long PortEnd
= 0;
1427 for (unsigned I
= 0; I
!= Host
.length();)
1436 if (InBracket
== true && Host
[I
] == ']')
1447 if (InBracket
== true)
1453 // Now we parse off a port number from the hostname
1455 string::size_type Pos
= Host
.rfind(':');
1456 if (Pos
== string::npos
|| Pos
< PortEnd
)
1459 Port
= atoi(string(Host
,Pos
+1).c_str());
1460 Host
.assign(Host
,0,Pos
);
1463 // URI::operator string - Convert the URI to a string /*{{{*/
1464 // ---------------------------------------------------------------------
1466 URI::operator string()
1470 if (Access
.empty() == false)
1473 if (Host
.empty() == false)
1475 if (Access
.empty() == false)
1478 if (User
.empty() == false)
1481 if (Password
.empty() == false)
1482 Res
+= ":" + Password
;
1486 // Add RFC 2732 escaping characters
1487 if (Access
.empty() == false &&
1488 (Host
.find('/') != string::npos
|| Host
.find(':') != string::npos
))
1489 Res
+= '[' + Host
+ ']';
1496 sprintf(S
,":%u",Port
);
1501 if (Path
.empty() == false)
1512 // URI::SiteOnly - Return the schema and site for the URI /*{{{*/
1513 // ---------------------------------------------------------------------
1515 string
URI::SiteOnly(const string
&URI
)
1525 // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/
1526 // ---------------------------------------------------------------------
1528 string
URI::NoUserPassword(const string
&URI
)