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>
44 // Strip - Remove white space from the front and back of a string /*{{{*/
45 // ---------------------------------------------------------------------
48 std::string
Strip(const std::string
&str
)
50 // ensure we have at least one character
51 if (str
.empty() == true)
54 char const * const s
= str
.c_str();
56 for (; isspace(s
[start
]) != 0; ++start
)
57 ; // find the first not-space
59 // string contains only whitespaces
63 size_t end
= str
.length() - 1;
64 for (; isspace(s
[end
]) != 0; --end
)
65 ; // find the last not-space
67 return str
.substr(start
, end
- start
+ 1);
70 bool Endswith(const std::string
&s
, const std::string
&end
)
72 if (end
.size() > s
.size())
74 return (s
.substr(s
.size() - end
.size(), s
.size()) == end
);
77 bool Startswith(const std::string
&s
, const std::string
&start
)
79 if (start
.size() > s
.size())
81 return (s
.substr(0, start
.size()) == start
);
87 // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/
88 // ---------------------------------------------------------------------
89 /* This is handy to use before display some information for enduser */
90 bool UTF8ToCodeset(const char *codeset
, const string
&orig
, string
*dest
)
95 size_t insize
, bufsize
;
98 cd
= iconv_open(codeset
, "UTF-8");
99 if (cd
== (iconv_t
)(-1)) {
100 // Something went wrong
102 _error
->Error("conversion from 'UTF-8' to '%s' not available",
105 perror("iconv_open");
110 insize
= bufsize
= orig
.size();
112 inptr
= (char *)inbuf
;
113 outbuf
= new char[bufsize
];
114 size_t lastError
= -1;
118 char *outptr
= outbuf
;
119 size_t outsize
= bufsize
;
120 size_t const err
= iconv(cd
, &inptr
, &insize
, &outptr
, &outsize
);
121 dest
->append(outbuf
, outptr
- outbuf
);
122 if (err
== (size_t)(-1))
129 // replace a series of unknown multibytes with a single "?"
130 if (lastError
!= insize
) {
131 lastError
= insize
- 1;
139 if (outptr
== outbuf
)
143 outbuf
= new char[bufsize
];
157 // strstrip - Remove white space from the front and back of a string /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This is handy to use when parsing a file. It also removes \n's left
160 over from fgets and company */
161 char *_strstrip(char *String
)
163 for (;*String
!= 0 && (*String
== ' ' || *String
== '\t'); String
++);
167 return _strrstrip(String
);
170 // strrstrip - Remove white space from the back of a string /*{{{*/
171 // ---------------------------------------------------------------------
172 char *_strrstrip(char *String
)
174 char *End
= String
+ strlen(String
) - 1;
175 for (;End
!= String
- 1 && (*End
== ' ' || *End
== '\t' || *End
== '\n' ||
176 *End
== '\r'); End
--);
182 // strtabexpand - Converts tabs into 8 spaces /*{{{*/
183 // ---------------------------------------------------------------------
185 char *_strtabexpand(char *String
,size_t Len
)
187 for (char *I
= String
; I
!= I
+ Len
&& *I
!= 0; I
++)
191 if (I
+ 8 > String
+ Len
)
197 /* Assume the start of the string is 0 and find the next 8 char
203 Len
= 8 - ((String
- I
) % 8);
211 memmove(I
+ Len
,I
+ 1,strlen(I
) + 1);
212 for (char *J
= I
; J
+ Len
!= I
; *I
= ' ', I
++);
217 // ParseQuoteWord - Parse a single word out of a string /*{{{*/
218 // ---------------------------------------------------------------------
219 /* This grabs a single word, converts any % escaped characters to their
220 proper values and advances the pointer. Double quotes are understood
221 and striped out as well. This is for URI/URL parsing. It also can
222 understand [] brackets.*/
223 bool ParseQuoteWord(const char *&String
,string
&Res
)
225 // Skip leading whitespace
226 const char *C
= String
;
227 for (;*C
!= 0 && *C
== ' '; C
++);
231 // Jump to the next word
232 for (;*C
!= 0 && isspace(*C
) == 0; C
++)
236 C
= strchr(C
+ 1, '"');
242 C
= strchr(C
+ 1, ']');
248 // Now de-quote characters
251 const char *Start
= String
;
253 for (I
= Buffer
; I
< Buffer
+ sizeof(Buffer
) && Start
!= C
; I
++)
255 if (*Start
== '%' && Start
+ 2 < C
&&
256 isxdigit(Start
[1]) && isxdigit(Start
[2]))
261 *I
= (char)strtol(Tmp
,0,16);
274 // Skip ending white space
275 for (;*C
!= 0 && isspace(*C
) != 0; C
++);
280 // ParseCWord - Parses a string like a C "" expression /*{{{*/
281 // ---------------------------------------------------------------------
282 /* This expects a series of space separated strings enclosed in ""'s.
283 It concatenates the ""'s into a single string. */
284 bool ParseCWord(const char *&String
,string
&Res
)
286 // Skip leading whitespace
287 const char *C
= String
;
288 for (;*C
!= 0 && *C
== ' '; C
++);
294 if (strlen(String
) >= sizeof(Buffer
))
301 for (C
++; *C
!= 0 && *C
!= '"'; C
++)
310 if (C
!= String
&& isspace(*C
) != 0 && isspace(C
[-1]) != 0)
312 if (isspace(*C
) == 0)
322 // QuoteString - Convert a string into quoted from /*{{{*/
323 // ---------------------------------------------------------------------
325 string
QuoteString(const string
&Str
, const char *Bad
)
328 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
330 if (strchr(Bad
,*I
) != 0 || isprint(*I
) == 0 ||
331 *I
== 0x25 || // percent '%' char
332 *I
<= 0x20 || *I
>= 0x7F) // control chars
335 sprintf(Buf
,"%%%02x",(int)*I
);
344 // DeQuoteString - Convert a string from quoted from /*{{{*/
345 // ---------------------------------------------------------------------
346 /* This undoes QuoteString */
347 string
DeQuoteString(const string
&Str
)
349 return DeQuoteString(Str
.begin(),Str
.end());
351 string
DeQuoteString(string::const_iterator
const &begin
,
352 string::const_iterator
const &end
)
355 for (string::const_iterator I
= begin
; I
!= end
; ++I
)
357 if (*I
== '%' && I
+ 2 < end
&&
358 isxdigit(I
[1]) && isxdigit(I
[2]))
364 Res
+= (char)strtol(Tmp
,0,16);
375 // SizeToStr - Convert a long into a human readable size /*{{{*/
376 // ---------------------------------------------------------------------
377 /* A max of 4 digits are shown before conversion to the next highest unit.
378 The max length of the string will be 5 chars unless the size is > 10
380 string
SizeToStr(double Size
)
389 /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
390 ExaBytes, ZettaBytes, YottaBytes */
391 char Ext
[] = {'\0','k','M','G','T','P','E','Z','Y'};
395 if (ASize
< 100 && I
!= 0)
397 sprintf(S
,"%'.1f %c",ASize
,Ext
[I
]);
403 sprintf(S
,"%'.0f %c",ASize
,Ext
[I
]);
413 // TimeToStr - Convert the time into a string /*{{{*/
414 // ---------------------------------------------------------------------
415 /* Converts a number of seconds to a hms format */
416 string
TimeToStr(unsigned long Sec
)
424 //d means days, h means hours, min means minutes, s means seconds
425 sprintf(S
,_("%lid %lih %limin %lis"),Sec
/60/60/24,(Sec
/60/60) % 24,(Sec
/60) % 60,Sec
% 60);
431 //h means hours, min means minutes, s means seconds
432 sprintf(S
,_("%lih %limin %lis"),Sec
/60/60,(Sec
/60) % 60,Sec
% 60);
438 //min means minutes, s means seconds
439 sprintf(S
,_("%limin %lis"),Sec
/60,Sec
% 60);
444 sprintf(S
,_("%lis"),Sec
);
451 // SubstVar - Substitute a string for another string /*{{{*/
452 // ---------------------------------------------------------------------
453 /* This replaces all occurrences of Subst with Contents in Str. */
454 string
SubstVar(const string
&Str
,const string
&Subst
,const string
&Contents
)
456 if (Subst
.empty() == true)
459 string::size_type Pos
= 0;
460 string::size_type OldPos
= 0;
463 while (OldPos
< Str
.length() &&
464 (Pos
= Str
.find(Subst
,OldPos
)) != string::npos
)
467 Temp
.append(Str
, OldPos
, Pos
- OldPos
);
468 if (Contents
.empty() == false)
469 Temp
.append(Contents
);
470 OldPos
= Pos
+ Subst
.length();
476 if (OldPos
>= Str
.length())
478 return Temp
+ string(Str
,OldPos
);
480 string
SubstVar(string Str
,const struct SubstVar
*Vars
)
482 for (; Vars
->Subst
!= 0; Vars
++)
483 Str
= SubstVar(Str
,Vars
->Subst
,*Vars
->Contents
);
487 // OutputInDepth - return a string with separator multiplied with depth /*{{{*/
488 // ---------------------------------------------------------------------
489 /* Returns a string with the supplied separator depth + 1 times in it */
490 std::string
OutputInDepth(const unsigned long Depth
, const char* Separator
)
492 std::string output
= "";
493 for(unsigned long d
=Depth
+1; d
> 0; d
--)
494 output
.append(Separator
);
498 // URItoFileName - Convert the uri into a unique file name /*{{{*/
499 // ---------------------------------------------------------------------
500 /* This converts a URI into a safe filename. It quotes all unsafe characters
501 and converts / to _ and removes the scheme identifier. The resulting
502 file name should be unique and never occur again for a different file */
503 string
URItoFileName(const string
&URI
)
505 // Nuke 'sensitive' items
511 // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
512 string NewURI
= QuoteString(U
,"\\|{}[]<>\"^~_=!@#$%^&*");
513 replace(NewURI
.begin(),NewURI
.end(),'/','_');
517 // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
518 // ---------------------------------------------------------------------
519 /* This routine performs a base64 transformation on a string. It was ripped
520 from wget and then patched and bug fixed.
522 This spec can be found in rfc2045 */
523 string
Base64Encode(const string
&S
)
526 static char tbl
[64] = {'A','B','C','D','E','F','G','H',
527 'I','J','K','L','M','N','O','P',
528 'Q','R','S','T','U','V','W','X',
529 'Y','Z','a','b','c','d','e','f',
530 'g','h','i','j','k','l','m','n',
531 'o','p','q','r','s','t','u','v',
532 'w','x','y','z','0','1','2','3',
533 '4','5','6','7','8','9','+','/'};
535 // Pre-allocate some space
537 Final
.reserve((4*S
.length() + 2)/3 + 2);
539 /* Transform the 3x8 bits to 4x6 bits, as required by
541 for (string::const_iterator I
= S
.begin(); I
< S
.end(); I
+= 3)
543 char Bits
[3] = {0,0,0};
550 Final
+= tbl
[Bits
[0] >> 2];
551 Final
+= tbl
[((Bits
[0] & 3) << 4) + (Bits
[1] >> 4)];
553 if (I
+ 1 >= S
.end())
556 Final
+= tbl
[((Bits
[1] & 0xf) << 2) + (Bits
[2] >> 6)];
558 if (I
+ 2 >= S
.end())
561 Final
+= tbl
[Bits
[2] & 0x3f];
564 /* Apply the padding elements, this tells how many bytes the remote
565 end should discard */
566 if (S
.length() % 3 == 2)
568 if (S
.length() % 3 == 1)
574 // stringcmp - Arbitrary string compare /*{{{*/
575 // ---------------------------------------------------------------------
576 /* This safely compares two non-null terminated strings of arbitrary
578 int stringcmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
580 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
584 if (A
== AEnd
&& B
== BEnd
)
596 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
597 const char *B
,const char *BEnd
)
599 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
603 if (A
== AEnd
&& B
== BEnd
)
613 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
614 string::const_iterator B
,string::const_iterator BEnd
)
616 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
620 if (A
== AEnd
&& B
== BEnd
)
632 // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/
633 // ---------------------------------------------------------------------
635 int stringcasecmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
637 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
638 if (tolower_ascii(*A
) != tolower_ascii(*B
))
641 if (A
== AEnd
&& B
== BEnd
)
647 if (tolower_ascii(*A
) < tolower_ascii(*B
))
652 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
653 const char *B
,const char *BEnd
)
655 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
656 if (tolower_ascii(*A
) != tolower_ascii(*B
))
659 if (A
== AEnd
&& B
== BEnd
)
665 if (tolower_ascii(*A
) < tolower_ascii(*B
))
669 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
670 string::const_iterator B
,string::const_iterator BEnd
)
672 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
673 if (tolower_ascii(*A
) != tolower_ascii(*B
))
676 if (A
== AEnd
&& B
== BEnd
)
682 if (tolower_ascii(*A
) < tolower_ascii(*B
))
688 // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
689 // ---------------------------------------------------------------------
690 /* The format is like those used in package files and the method
691 communication system */
692 string
LookupTag(const string
&Message
,const char *Tag
,const char *Default
)
694 // Look for a matching tag.
695 int Length
= strlen(Tag
);
696 for (string::const_iterator I
= Message
.begin(); I
+ Length
< Message
.end(); ++I
)
699 if (I
[Length
] == ':' && stringcasecmp(I
,I
+Length
,Tag
) == 0)
701 // Find the end of line and strip the leading/trailing spaces
702 string::const_iterator J
;
704 for (; isspace(*I
) != 0 && I
< Message
.end(); ++I
);
705 for (J
= I
; *J
!= '\n' && J
< Message
.end(); ++J
);
706 for (; J
> I
&& isspace(J
[-1]) != 0; --J
);
711 for (; *I
!= '\n' && I
< Message
.end(); ++I
);
714 // Failed to find a match
720 // StringToBool - Converts a string into a boolean /*{{{*/
721 // ---------------------------------------------------------------------
722 /* This inspects the string to see if it is true or if it is false and
723 then returns the result. Several varients on true/false are checked. */
724 int StringToBool(const string
&Text
,int Default
)
727 int Res
= strtol(Text
.c_str(),&ParseEnd
,0);
728 // ensure that the entire string was converted by strtol to avoid
729 // failures on "apt-cache show -a 0ad" where the "0" is converted
730 const char *TextEnd
= Text
.c_str()+Text
.size();
731 if (ParseEnd
== TextEnd
&& Res
>= 0 && Res
<= 1)
734 // Check for positives
735 if (strcasecmp(Text
.c_str(),"no") == 0 ||
736 strcasecmp(Text
.c_str(),"false") == 0 ||
737 strcasecmp(Text
.c_str(),"without") == 0 ||
738 strcasecmp(Text
.c_str(),"off") == 0 ||
739 strcasecmp(Text
.c_str(),"disable") == 0)
742 // Check for negatives
743 if (strcasecmp(Text
.c_str(),"yes") == 0 ||
744 strcasecmp(Text
.c_str(),"true") == 0 ||
745 strcasecmp(Text
.c_str(),"with") == 0 ||
746 strcasecmp(Text
.c_str(),"on") == 0 ||
747 strcasecmp(Text
.c_str(),"enable") == 0)
753 // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
754 // ---------------------------------------------------------------------
755 /* This converts a time_t into a string time representation that is
756 year 2000 complient and timezone neutral */
757 string
TimeRFC1123(time_t Date
)
760 if (gmtime_r(&Date
, &Conv
) == NULL
)
764 const char *Day
[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
765 const char *Month
[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
766 "Aug","Sep","Oct","Nov","Dec"};
768 snprintf(Buf
, sizeof(Buf
), "%s, %02i %s %i %02i:%02i:%02i GMT",Day
[Conv
.tm_wday
],
769 Conv
.tm_mday
,Month
[Conv
.tm_mon
],Conv
.tm_year
+1900,Conv
.tm_hour
,
770 Conv
.tm_min
,Conv
.tm_sec
);
774 // ReadMessages - Read messages from the FD /*{{{*/
775 // ---------------------------------------------------------------------
776 /* This pulls full messages from the input FD into the message buffer.
777 It assumes that messages will not pause during transit so no
778 fancy buffering is used.
780 In particular: this reads blocks from the input until it believes
781 that it's run out of input text. Each block is terminated by a
782 double newline ('\n' followed by '\n'). As noted below, there is a
783 bug in this code: it assumes that all the blocks have been read if
784 it doesn't see additional text in the buffer after the last one is
785 parsed, which will cause it to lose blocks if the last block
786 coincides with the end of the buffer.
788 bool ReadMessages(int Fd
, vector
<string
> &List
)
792 // Represents any left-over from the previous iteration of the
793 // parse loop. (i.e., if a message is split across the end
794 // of the buffer, it goes here)
795 string PartialMessage
;
799 int Res
= read(Fd
,End
,sizeof(Buffer
) - (End
-Buffer
));
800 if (Res
< 0 && errno
== EINTR
)
803 // Process is dead, this is kind of bad..
808 if (Res
< 0 && errno
== EAGAIN
)
815 // Look for the end of the message
816 for (char *I
= Buffer
; I
+ 1 < End
; I
++)
819 (I
[0] != '\n' && strncmp(I
, "\r\n\r\n", 4) != 0))
822 // Pull the message out
823 string
Message(Buffer
,I
-Buffer
);
824 PartialMessage
+= Message
;
827 for (; I
< End
&& (*I
== '\n' || *I
== '\r'); ++I
);
829 memmove(Buffer
,I
,End
-Buffer
);
832 List
.push_back(PartialMessage
);
833 PartialMessage
.clear();
837 // If there's text left in the buffer, store it
838 // in PartialMessage and throw the rest of the buffer
839 // away. This allows us to handle messages that
840 // are longer than the static buffer size.
841 PartialMessage
+= string(Buffer
, End
);
846 // BUG ALERT: if a message block happens to end at a
847 // multiple of 64000 characters, this will cause it to
848 // terminate early, leading to a badly formed block and
849 // probably crashing the method. However, this is the only
850 // way we have to find the end of the message block. I have
851 // an idea of how to fix this, but it will require changes
852 // to the protocol (essentially to mark the beginning and
853 // end of the block).
855 // -- dburrows 2008-04-02
859 if (WaitFd(Fd
) == false)
864 // MonthConv - Converts a month string into a number /*{{{*/
865 // ---------------------------------------------------------------------
866 /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
867 Made it a bit more robust with a few tolower_ascii though. */
868 static int MonthConv(char *Month
)
870 switch (tolower_ascii(*Month
))
873 return tolower_ascii(Month
[1]) == 'p'?3:7;
879 if (tolower_ascii(Month
[1]) == 'a')
881 return tolower_ascii(Month
[2]) == 'n'?5:6;
883 return tolower_ascii(Month
[2]) == 'r'?2:4;
891 // Pretend it is January..
897 // timegm - Internal timegm if the gnu version is not available /*{{{*/
898 // ---------------------------------------------------------------------
899 /* Converts struct tm to time_t, assuming the data in tm is UTC rather
900 than local timezone (mktime assumes the latter).
902 This function is a nonstandard GNU extension that is also present on
903 the BSDs and maybe other systems. For others we follow the advice of
904 the manpage of timegm and use his portable replacement. */
906 static time_t timegm(struct tm
*t
)
908 char *tz
= getenv("TZ");
911 time_t ret
= mktime(t
);
921 // FullDateToTime - Converts a HTTP1.1 full date strings into a time_t /*{{{*/
922 // ---------------------------------------------------------------------
923 /* tries to parses a full date as specified in RFC2616 Section 3.3.1
924 with one exception: All timezones (%Z) are accepted but the protocol
925 says that it MUST be GMT, but this one is equal to UTC which we will
926 encounter from time to time (e.g. in Release files) so we accept all
927 here and just assume it is GMT (or UTC) later on */
928 bool RFC1123StrToTime(const char* const str
,time_t &time
)
931 setlocale (LC_ALL
,"C");
933 // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
934 (strptime(str
, "%a, %d %b %Y %H:%M:%S %Z", &Tm
) == NULL
&&
935 // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
936 strptime(str
, "%A, %d-%b-%y %H:%M:%S %Z", &Tm
) == NULL
&&
937 // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
938 strptime(str
, "%a %b %d %H:%M:%S %Y", &Tm
) == NULL
);
939 setlocale (LC_ALL
,"");
947 // FTPMDTMStrToTime - Converts a ftp modification date into a time_t /*{{{*/
948 // ---------------------------------------------------------------------
950 bool FTPMDTMStrToTime(const char* const str
,time_t &time
)
953 // MDTM includes no whitespaces but recommend and ignored by strptime
954 if (strptime(str
, "%Y %m %d %H %M %S", &Tm
) == NULL
)
961 // StrToTime - Converts a string into a time_t /*{{{*/
962 // ---------------------------------------------------------------------
963 /* This handles all 3 popular time formats including RFC 1123, RFC 1036
964 and the C library asctime format. It requires the GNU library function
965 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
966 reason the C library does not provide any such function :< This also
967 handles the weird, but unambiguous FTP time format*/
968 bool StrToTime(const string
&Val
,time_t &Result
)
973 // Skip the day of the week
974 const char *I
= strchr(Val
.c_str(), ' ');
976 // Handle RFC 1123 time
978 if (sscanf(I
," %2d %3s %4d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,&Tm
.tm_year
,
979 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
981 // Handle RFC 1036 time
982 if (sscanf(I
," %2d-%3s-%3d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,
983 &Tm
.tm_year
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) == 6)
988 if (sscanf(I
," %3s %2d %2d:%2d:%2d %4d",Month
,&Tm
.tm_mday
,
989 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
,&Tm
.tm_year
) != 6)
992 if (sscanf(Val
.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm
.tm_year
,&Tm
.tm_mon
,
993 &Tm
.tm_mday
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
1002 Tm
.tm_mon
= MonthConv(Month
);
1004 Tm
.tm_mon
= 0; // we don't have a month, so pick something
1007 // Convert to local time and then to GMT
1008 Result
= timegm(&Tm
);
1012 // StrToNum - Convert a fixed length string to a number /*{{{*/
1013 // ---------------------------------------------------------------------
1014 /* This is used in decoding the crazy fixed length string headers in
1015 tar and ar files. */
1016 bool StrToNum(const char *Str
,unsigned long &Res
,unsigned Len
,unsigned Base
)
1019 if (Len
>= sizeof(S
))
1024 // All spaces is a zero
1027 for (I
= 0; S
[I
] == ' '; I
++);
1032 Res
= strtoul(S
,&End
,Base
);
1039 // StrToNum - Convert a fixed length string to a number /*{{{*/
1040 // ---------------------------------------------------------------------
1041 /* This is used in decoding the crazy fixed length string headers in
1042 tar and ar files. */
1043 bool StrToNum(const char *Str
,unsigned long long &Res
,unsigned Len
,unsigned Base
)
1046 if (Len
>= sizeof(S
))
1051 // All spaces is a zero
1054 for (I
= 0; S
[I
] == ' '; I
++);
1059 Res
= strtoull(S
,&End
,Base
);
1067 // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
1068 // ---------------------------------------------------------------------
1069 /* This is used in decoding the 256bit encoded fixed length fields in
1071 bool Base256ToNum(const char *Str
,unsigned long long &Res
,unsigned int Len
)
1073 if ((Str
[0] & 0x80) == 0)
1077 Res
= Str
[0] & 0x7F;
1078 for(unsigned int i
= 1; i
< Len
; ++i
)
1079 Res
= (Res
<<8) + Str
[i
];
1084 // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
1085 // ---------------------------------------------------------------------
1086 /* This is used in decoding the 256bit encoded fixed length fields in
1088 bool Base256ToNum(const char *Str
,unsigned long &Res
,unsigned int Len
)
1090 unsigned long long Num
;
1093 rc
= Base256ToNum(Str
, Num
, Len
);
1101 // HexDigit - Convert a hex character into an integer /*{{{*/
1102 // ---------------------------------------------------------------------
1103 /* Helper for Hex2Num */
1104 static int HexDigit(int c
)
1106 if (c
>= '0' && c
<= '9')
1108 if (c
>= 'a' && c
<= 'f')
1109 return c
- 'a' + 10;
1110 if (c
>= 'A' && c
<= 'F')
1111 return c
- 'A' + 10;
1115 // Hex2Num - Convert a long hex number into a buffer /*{{{*/
1116 // ---------------------------------------------------------------------
1117 /* The length of the buffer must be exactly 1/2 the length of the string. */
1118 bool Hex2Num(const string
&Str
,unsigned char *Num
,unsigned int Length
)
1120 if (Str
.length() != Length
*2)
1123 // Convert each digit. We store it in the same order as the string
1125 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end();J
++, I
+= 2)
1127 if (isxdigit(*I
) == 0 || isxdigit(I
[1]) == 0)
1130 Num
[J
] = HexDigit(I
[0]) << 4;
1131 Num
[J
] += HexDigit(I
[1]);
1137 // TokSplitString - Split a string up by a given token /*{{{*/
1138 // ---------------------------------------------------------------------
1139 /* This is intended to be a faster splitter, it does not use dynamic
1140 memories. Input is changed to insert nulls at each token location. */
1141 bool TokSplitString(char Tok
,char *Input
,char **List
,
1142 unsigned long ListMax
)
1144 // Strip any leading spaces
1145 char *Start
= Input
;
1146 char *Stop
= Start
+ strlen(Start
);
1147 for (; *Start
!= 0 && isspace(*Start
) != 0; Start
++);
1149 unsigned long Count
= 0;
1153 // Skip to the next Token
1154 for (; Pos
!= Stop
&& *Pos
!= Tok
; Pos
++);
1156 // Back remove spaces
1158 for (; End
> Start
&& (End
[-1] == Tok
|| isspace(End
[-1]) != 0); End
--);
1161 List
[Count
++] = Start
;
1162 if (Count
>= ListMax
)
1169 for (; Pos
!= Stop
&& (*Pos
== Tok
|| isspace(*Pos
) != 0 || *Pos
== 0); Pos
++);
1177 // VectorizeString - Split a string up into a vector of strings /*{{{*/
1178 // ---------------------------------------------------------------------
1179 /* This can be used to split a given string up into a vector, so the
1180 propose is the same as in the method above and this one is a bit slower
1181 also, but the advantage is that we have an iteratable vector */
1182 vector
<string
> VectorizeString(string
const &haystack
, char const &split
)
1184 vector
<string
> exploded
;
1185 if (haystack
.empty() == true)
1187 string::const_iterator start
= haystack
.begin();
1188 string::const_iterator end
= start
;
1190 for (; end
!= haystack
.end() && *end
!= split
; ++end
);
1191 exploded
.push_back(string(start
, end
));
1193 } while (end
!= haystack
.end() && (++end
) != haystack
.end());
1197 // StringSplit - split a string into a string vector by token /*{{{*/
1198 // ---------------------------------------------------------------------
1199 /* See header for details.
1201 vector
<string
> StringSplit(std::string
const &s
, std::string
const &sep
,
1202 unsigned int maxsplit
)
1204 vector
<string
> split
;
1207 // no seperator given, this is bogus
1212 while (pos
!= string::npos
)
1214 pos
= s
.find(sep
, start
);
1215 split
.push_back(s
.substr(start
, pos
-start
));
1217 // if maxsplit is reached, the remaining string is the last item
1218 if(split
.size() >= maxsplit
)
1220 split
[split
.size()-1] = s
.substr(start
);
1223 start
= pos
+sep
.size();
1228 // RegexChoice - Simple regex list/list matcher /*{{{*/
1229 // ---------------------------------------------------------------------
1231 unsigned long RegexChoice(RxChoiceList
*Rxs
,const char **ListBegin
,
1232 const char **ListEnd
)
1234 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1237 unsigned long Hits
= 0;
1238 for (; ListBegin
< ListEnd
; ++ListBegin
)
1240 // Check if the name is a regex
1243 for (I
= *ListBegin
; *I
!= 0; I
++)
1244 if (*I
== '.' || *I
== '?' || *I
== '*' || *I
== '|')
1249 // Compile the regex pattern
1252 if (regcomp(&Pattern
,*ListBegin
,REG_EXTENDED
| REG_ICASE
|
1258 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1263 if (strcasecmp(R
->Str
,*ListBegin
) != 0)
1267 if (regexec(&Pattern
,R
->Str
,0,0,0) != 0)
1272 if (R
->Hit
== false)
1282 _error
->Warning(_("Selection %s not found"),*ListBegin
);
1288 // {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/
1289 // ---------------------------------------------------------------------
1290 /* This is used to make the internationalization strings easier to translate
1291 and to allow reordering of parameters */
1292 static bool iovprintf(ostream
&out
, const char *format
,
1293 va_list &args
, ssize_t
&size
) {
1294 char *S
= (char*)malloc(size
);
1295 ssize_t
const n
= vsnprintf(S
, size
, format
, args
);
1296 if (n
> -1 && n
< size
) {
1309 void ioprintf(ostream
&out
,const char *format
,...)
1314 va_start(args
,format
);
1315 if (iovprintf(out
, format
, args
, size
) == true)
1320 void strprintf(string
&out
,const char *format
,...)
1324 std::ostringstream outstr
;
1326 va_start(args
,format
);
1327 if (iovprintf(outstr
, format
, args
, size
) == true)
1334 // safe_snprintf - Safer snprintf /*{{{*/
1335 // ---------------------------------------------------------------------
1336 /* This is a snprintf that will never (ever) go past 'End' and returns a
1337 pointer to the end of the new string. The returned string is always null
1338 terminated unless Buffer == end. This is a better alterantive to using
1339 consecutive snprintfs. */
1340 char *safe_snprintf(char *Buffer
,char *End
,const char *Format
,...)
1347 va_start(args
,Format
);
1348 Did
= vsnprintf(Buffer
,End
- Buffer
,Format
,args
);
1351 if (Did
< 0 || Buffer
+ Did
> End
)
1353 return Buffer
+ Did
;
1356 // StripEpoch - Remove the version "epoch" from a version string /*{{{*/
1357 // ---------------------------------------------------------------------
1358 string
StripEpoch(const string
&VerStr
)
1360 size_t i
= VerStr
.find(":");
1361 if (i
== string::npos
)
1363 return VerStr
.substr(i
+1);
1366 // tolower_ascii - tolower() function that ignores the locale /*{{{*/
1367 // ---------------------------------------------------------------------
1368 /* This little function is the most called method we have and tries
1369 therefore to do the absolut minimum - and is notable faster than
1370 standard tolower/toupper and as a bonus avoids problems with different
1371 locales - we only operate on ascii chars anyway. */
1372 int tolower_ascii(int const c
)
1374 if (c
>= 'A' && c
<= 'Z')
1380 // CheckDomainList - See if Host is in a , separate list /*{{{*/
1381 // ---------------------------------------------------------------------
1382 /* The domain list is a comma separate list of domains that are suffix
1383 matched against the argument */
1384 bool CheckDomainList(const string
&Host
,const string
&List
)
1386 string::const_iterator Start
= List
.begin();
1387 for (string::const_iterator Cur
= List
.begin(); Cur
<= List
.end(); ++Cur
)
1389 if (Cur
< List
.end() && *Cur
!= ',')
1392 // Match the end of the string..
1393 if ((Host
.size() >= (unsigned)(Cur
- Start
)) &&
1395 stringcasecmp(Host
.end() - (Cur
- Start
),Host
.end(),Start
,Cur
) == 0)
1403 // strv_length - Return the length of a NULL-terminated string array /*{{{*/
1404 // ---------------------------------------------------------------------
1406 size_t strv_length(const char **str_array
)
1409 for (i
=0; str_array
[i
] != NULL
; i
++)
1415 // DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/
1416 // ---------------------------------------------------------------------
1418 string
DeEscapeString(const string
&input
)
1421 string::const_iterator it
;
1423 for (it
= input
.begin(); it
!= input
.end(); ++it
)
1425 // just copy non-escape chars
1432 // deal with double escape
1434 (it
+ 1 < input
.end()) && it
[1] == '\\')
1438 // advance iterator one step further
1443 // ensure we have a char to read
1444 if (it
+ 1 == input
.end())
1452 if (it
+ 2 <= input
.end()) {
1456 output
+= (char)strtol(tmp
, 0, 8);
1461 if (it
+ 2 <= input
.end()) {
1465 output
+= (char)strtol(tmp
, 0, 16);
1470 // FIXME: raise exception here?
1477 // URI::CopyFrom - Copy from an object /*{{{*/
1478 // ---------------------------------------------------------------------
1479 /* This parses the URI into all of its components */
1480 void URI::CopyFrom(const string
&U
)
1482 string::const_iterator I
= U
.begin();
1484 // Locate the first colon, this separates the scheme
1485 for (; I
< U
.end() && *I
!= ':' ; ++I
);
1486 string::const_iterator FirstColon
= I
;
1488 /* Determine if this is a host type URI with a leading double //
1489 and then search for the first single / */
1490 string::const_iterator SingleSlash
= I
;
1491 if (I
+ 3 < U
.end() && I
[1] == '/' && I
[2] == '/')
1494 /* Find the / indicating the end of the hostname, ignoring /'s in the
1496 bool InBracket
= false;
1497 for (; SingleSlash
< U
.end() && (*SingleSlash
!= '/' || InBracket
== true); ++SingleSlash
)
1499 if (*SingleSlash
== '[')
1501 if (InBracket
== true && *SingleSlash
== ']')
1505 if (SingleSlash
> U
.end())
1506 SingleSlash
= U
.end();
1508 // We can now write the access and path specifiers
1509 Access
.assign(U
.begin(),FirstColon
);
1510 if (SingleSlash
!= U
.end())
1511 Path
.assign(SingleSlash
,U
.end());
1512 if (Path
.empty() == true)
1515 // Now we attempt to locate a user:pass@host fragment
1516 if (FirstColon
+ 2 <= U
.end() && FirstColon
[1] == '/' && FirstColon
[2] == '/')
1520 if (FirstColon
>= U
.end())
1523 if (FirstColon
> SingleSlash
)
1524 FirstColon
= SingleSlash
;
1526 // Find the colon...
1528 if (I
> SingleSlash
)
1530 for (; I
< SingleSlash
&& *I
!= ':'; ++I
);
1531 string::const_iterator SecondColon
= I
;
1533 // Search for the @ after the colon
1534 for (; I
< SingleSlash
&& *I
!= '@'; ++I
);
1535 string::const_iterator At
= I
;
1537 // Now write the host and user/pass
1538 if (At
== SingleSlash
)
1540 if (FirstColon
< SingleSlash
)
1541 Host
.assign(FirstColon
,SingleSlash
);
1545 Host
.assign(At
+1,SingleSlash
);
1546 // username and password must be encoded (RFC 3986)
1547 User
.assign(DeQuoteString(FirstColon
,SecondColon
));
1548 if (SecondColon
< At
)
1549 Password
.assign(DeQuoteString(SecondColon
+1,At
));
1552 // Now we parse the RFC 2732 [] hostnames.
1553 unsigned long PortEnd
= 0;
1555 for (unsigned I
= 0; I
!= Host
.length();)
1564 if (InBracket
== true && Host
[I
] == ']')
1575 if (InBracket
== true)
1581 // Now we parse off a port number from the hostname
1583 string::size_type Pos
= Host
.rfind(':');
1584 if (Pos
== string::npos
|| Pos
< PortEnd
)
1587 Port
= atoi(string(Host
,Pos
+1).c_str());
1588 Host
.assign(Host
,0,Pos
);
1591 // URI::operator string - Convert the URI to a string /*{{{*/
1592 // ---------------------------------------------------------------------
1594 URI::operator string()
1598 if (Access
.empty() == false)
1601 if (Host
.empty() == false)
1603 if (Access
.empty() == false)
1606 if (User
.empty() == false)
1608 // FIXME: Technically userinfo is permitted even less
1609 // characters than these, but this is not conveniently
1610 // expressed with a blacklist.
1611 Res
+= QuoteString(User
, ":/?#[]@");
1612 if (Password
.empty() == false)
1613 Res
+= ":" + QuoteString(Password
, ":/?#[]@");
1617 // Add RFC 2732 escaping characters
1618 if (Access
.empty() == false &&
1619 (Host
.find('/') != string::npos
|| Host
.find(':') != string::npos
))
1620 Res
+= '[' + Host
+ ']';
1627 sprintf(S
,":%u",Port
);
1632 if (Path
.empty() == false)
1643 // URI::SiteOnly - Return the schema and site for the URI /*{{{*/
1644 // ---------------------------------------------------------------------
1646 string
URI::SiteOnly(const string
&URI
)
1655 // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/
1656 // ---------------------------------------------------------------------
1658 string
URI::NoUserPassword(const string
&URI
)