]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/strutl.h
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $
4 /* ######################################################################
6 String Util - These are some useful string functions
8 _strstrip is a function to remove whitespace from the front and end
11 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
14 ##################################################################### */
30 #ifndef APT_10_CLEANER_HEADERS
33 #ifndef APT_8_CLEANER_HEADERS
41 std::string
Strip(const std::string
&s
);
42 bool Endswith(const std::string
&s
, const std::string
&ending
);
47 bool UTF8ToCodeset(const char *codeset
, const std::string
&orig
, std::string
*dest
);
48 char *_strstrip(char *String
);
49 char *_strrstrip(char *String
); // right strip only
50 char *_strtabexpand(char *String
,size_t Len
);
51 bool ParseQuoteWord(const char *&String
,std::string
&Res
);
52 bool ParseCWord(const char *&String
,std::string
&Res
);
53 std::string
QuoteString(const std::string
&Str
,const char *Bad
);
54 std::string
DeQuoteString(const std::string
&Str
);
55 std::string
DeQuoteString(std::string::const_iterator
const &begin
, std::string::const_iterator
const &end
);
57 // unescape (\0XX and \xXX) from a string
58 std::string
DeEscapeString(const std::string
&input
);
60 std::string
SizeToStr(double Bytes
);
61 std::string
TimeToStr(unsigned long Sec
);
62 std::string
Base64Encode(const std::string
&Str
);
63 std::string
OutputInDepth(const unsigned long Depth
, const char* Separator
=" ");
64 std::string
URItoFileName(const std::string
&URI
);
65 std::string
TimeRFC1123(time_t Date
);
66 bool RFC1123StrToTime(const char* const str
,time_t &time
) APT_MUSTCHECK
;
67 bool FTPMDTMStrToTime(const char* const str
,time_t &time
) APT_MUSTCHECK
;
68 APT_DEPRECATED
bool StrToTime(const std::string
&Val
,time_t &Result
);
69 std::string
LookupTag(const std::string
&Message
,const char *Tag
,const char *Default
= 0);
70 int StringToBool(const std::string
&Text
,int Default
= -1);
71 bool ReadMessages(int Fd
, std::vector
<std::string
> &List
);
72 bool StrToNum(const char *Str
,unsigned long &Res
,unsigned Len
,unsigned Base
= 0);
73 bool StrToNum(const char *Str
,unsigned long long &Res
,unsigned Len
,unsigned Base
= 0);
74 bool Base256ToNum(const char *Str
,unsigned long &Res
,unsigned int Len
);
75 bool Hex2Num(const std::string
&Str
,unsigned char *Num
,unsigned int Length
);
77 // input changing string split
78 bool TokSplitString(char Tok
,char *Input
,char **List
,
79 unsigned long ListMax
);
81 // split a given string by a char
82 std::vector
<std::string
> VectorizeString(std::string
const &haystack
, char const &split
) APT_CONST
;
84 /* \brief Return a vector of strings from string "input" where "sep"
85 * is used as the delimiter string.
87 * \param input The input string.
89 * \param sep The seperator to use.
91 * \param maxsplit (optional) The maximum amount of splitting that
94 * The optional "maxsplit" argument can be used to limit the splitting,
95 * if used the string is only split on maxsplit places and the last
96 * item in the vector contains the remainder string.
98 std::vector
<std::string
> StringSplit(std::string
const &input
,
99 std::string
const &sep
,
100 unsigned int maxsplit
=std::numeric_limits
<unsigned int>::max()) APT_CONST
;
102 void ioprintf(std::ostream
&out
,const char *format
,...) APT_PRINTF(2);
103 void strprintf(std::string
&out
,const char *format
,...) APT_PRINTF(2);
104 char *safe_snprintf(char *Buffer
,char *End
,const char *Format
,...) APT_PRINTF(3);
105 bool CheckDomainList(const std::string
&Host
, const std::string
&List
);
106 int tolower_ascii(int const c
) APT_CONST APT_HOT
;
107 std::string
StripEpoch(const std::string
&VerStr
);
109 #define APT_MKSTRCMP(name,func) \
110 inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
111 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
112 inline APT_PURE int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));} \
113 inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());} \
114 inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);}
116 #define APT_MKSTRCMP2(name,func) \
117 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
118 inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
119 inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
120 inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
122 int APT_PURE
stringcmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
);
123 int APT_PURE
stringcasecmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
);
125 /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
126 case the definition of string::const_iterator is not the same as
127 const char * and we need these extra functions */
129 int APT_PURE
stringcmp(std::string::const_iterator A
,std::string::const_iterator AEnd
,
130 const char *B
,const char *BEnd
);
131 int APT_PURE
stringcmp(std::string::const_iterator A
,std::string::const_iterator AEnd
,
132 std::string::const_iterator B
,std::string::const_iterator BEnd
);
133 int APT_PURE
stringcasecmp(std::string::const_iterator A
,std::string::const_iterator AEnd
,
134 const char *B
,const char *BEnd
);
135 int APT_PURE
stringcasecmp(std::string::const_iterator A
,std::string::const_iterator AEnd
,
136 std::string::const_iterator B
,std::string::const_iterator BEnd
);
138 inline APT_PURE
int stringcmp(std::string::const_iterator A
,std::string::const_iterator Aend
,const char *B
) {return stringcmp(A
,Aend
,B
,B
+strlen(B
));}
139 inline APT_PURE
int stringcasecmp(std::string::const_iterator A
,std::string::const_iterator Aend
,const char *B
) {return stringcasecmp(A
,Aend
,B
,B
+strlen(B
));}
142 APT_MKSTRCMP2(stringcmp
,stringcmp
)
143 APT_MKSTRCMP2(stringcasecmp
,stringcasecmp
)
145 // Return the length of a NULL-terminated string array
146 size_t APT_PURE
strv_length(const char **str_array
);
149 inline const char *DeNull(const char *s
) {return (s
== 0?"(null)":s
);}
153 void CopyFrom(const std::string
&From
);
159 std::string Password
;
164 operator std::string();
165 inline void operator =(const std::string
&From
) {CopyFrom(From
);}
166 inline bool empty() {return Access
.empty();};
167 static std::string
SiteOnly(const std::string
&URI
);
168 static std::string
NoUserPassword(const std::string
&URI
);
170 URI(std::string Path
) {CopyFrom(Path
);}
177 const std::string
*Contents
;
179 std::string
SubstVar(std::string Str
,const struct SubstVar
*Vars
);
180 std::string
SubstVar(const std::string
&Str
,const std::string
&Subst
,const std::string
&Contents
);
188 unsigned long RegexChoice(RxChoiceList
*Rxs
,const char **ListBegin
,
189 const char **ListEnd
);