]>
Commit | Line | Data |
---|---|---|
6c139d6e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
1168596f | 3 | // $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $ |
6c139d6e AL |
4 | /* ###################################################################### |
5 | ||
b2e465d6 | 6 | String Util - These are some useful string functions |
6c139d6e AL |
7 | |
8 | _strstrip is a function to remove whitespace from the front and end | |
9 | of a string. | |
10 | ||
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> | |
13 | ||
14 | ##################################################################### */ | |
15 | /*}}}*/ | |
6c139d6e AL |
16 | #ifndef STRUTL_H |
17 | #define STRUTL_H | |
18 | ||
13500573 | 19 | |
2ddab3fb | 20 | #include <limits> |
6c139d6e | 21 | #include <string> |
a7955daa | 22 | #include <cstring> |
0a8a80e5 | 23 | #include <vector> |
0db4a45b | 24 | #include <iostream> |
eff0c22e JAK |
25 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
26 | #include <apt-pkg/string_view.h> | |
27 | #endif | |
492f957a | 28 | #include <time.h> |
453b82a3 | 29 | #include <stddef.h> |
6c139d6e | 30 | |
6dc60370 DK |
31 | #include "macros.h" |
32 | ||
453b82a3 DK |
33 | #ifndef APT_10_CLEANER_HEADERS |
34 | #include <stdlib.h> | |
35 | #endif | |
a4f6bdc8 DK |
36 | #ifndef APT_8_CLEANER_HEADERS |
37 | using std::string; | |
38 | using std::vector; | |
39 | using std::ostream; | |
40 | #endif | |
41 | ||
65dbd5a1 MV |
42 | namespace APT { |
43 | namespace String { | |
44 | std::string Strip(const std::string &s); | |
cf993341 | 45 | bool Endswith(const std::string &s, const std::string &ending); |
a5bb5e1e | 46 | bool Startswith(const std::string &s, const std::string &starting); |
d3e8fbb3 DK |
47 | } |
48 | } | |
65dbd5a1 MV |
49 | |
50 | ||
8f3ba4e8 | 51 | bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest); |
6c139d6e | 52 | char *_strstrip(char *String); |
4fb400a6 | 53 | char *_strrstrip(char *String); // right strip only |
6c139d6e | 54 | char *_strtabexpand(char *String,size_t Len); |
8f3ba4e8 DK |
55 | bool ParseQuoteWord(const char *&String,std::string &Res); |
56 | bool ParseCWord(const char *&String,std::string &Res); | |
57 | std::string QuoteString(const std::string &Str,const char *Bad); | |
58 | std::string DeQuoteString(const std::string &Str); | |
59 | std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end); | |
a513ace2 | 60 | |
b9dc4706 | 61 | // unescape (\0XX and \xXX) from a string |
8f3ba4e8 DK |
62 | std::string DeEscapeString(const std::string &input); |
63 | ||
64 | std::string SizeToStr(double Bytes); | |
65 | std::string TimeToStr(unsigned long Sec); | |
66 | std::string Base64Encode(const std::string &Str); | |
67 | std::string OutputInDepth(const unsigned long Depth, const char* Separator=" "); | |
68 | std::string URItoFileName(const std::string &URI); | |
69 | std::string TimeRFC1123(time_t Date); | |
9febc2b2 DK |
70 | /** parses time as needed by HTTP/1.1 and Debian files. |
71 | * | |
72 | * HTTP/1.1 prefers dates in RFC1123 format (but the other two obsolete date formats | |
73 | * are supported to) and e.g. Release files use the same format in Date & Valid-Until | |
74 | * fields. | |
75 | * | |
76 | * Note: datetime strings need to be in UTC timezones (GMT, UTC, Z, +/-0000) to be | |
77 | * parsed. Other timezones will be rejected as invalid. Previous implementations | |
78 | * accepted other timezones, but treated them as UTC. | |
79 | * | |
80 | * @param str is the datetime string to parse | |
81 | * @param[out] time will be the seconds since epoch of the given datetime if | |
82 | * parsing is successful, undefined otherwise. | |
83 | * @return \b true if parsing was successful, otherwise \b false. | |
84 | */ | |
453b82a3 DK |
85 | bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK; |
86 | bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK; | |
5dd00edb | 87 | APT_DEPRECATED_MSG("Use RFC1123StrToTime or FTPMDTMStrToTime as needed instead") bool StrToTime(const std::string &Val,time_t &Result); |
8f3ba4e8 DK |
88 | std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0); |
89 | int StringToBool(const std::string &Text,int Default = -1); | |
90 | bool ReadMessages(int Fd, std::vector<std::string> &List); | |
ddc1d8d0 | 91 | bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0); |
650faab0 | 92 | bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0); |
f688d1d3 | 93 | bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len); |
3c09d634 | 94 | bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len); |
8f3ba4e8 | 95 | bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length); |
eff0c22e JAK |
96 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
97 | APT_HIDDEN bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length); | |
98 | #endif | |
9572a54b | 99 | // input changing string split |
b2e465d6 AL |
100 | bool TokSplitString(char Tok,char *Input,char **List, |
101 | unsigned long ListMax); | |
9572a54b MV |
102 | |
103 | // split a given string by a char | |
7e9b7ea8 | 104 | std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE; |
9572a54b | 105 | |
41053d72 MV |
106 | /* \brief Return a vector of strings from string "input" where "sep" |
107 | * is used as the delimiter string. | |
108 | * | |
109 | * \param input The input string. | |
110 | * | |
8d89cda7 | 111 | * \param sep The separator to use. |
41053d72 MV |
112 | * |
113 | * \param maxsplit (optional) The maximum amount of splitting that | |
114 | * should be done . | |
115 | * | |
116 | * The optional "maxsplit" argument can be used to limit the splitting, | |
117 | * if used the string is only split on maxsplit places and the last | |
118 | * item in the vector contains the remainder string. | |
119 | */ | |
120 | std::vector<std::string> StringSplit(std::string const &input, | |
121 | std::string const &sep, | |
453b82a3 | 122 | unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST; |
9572a54b | 123 | |
453b82a3 DK |
124 | void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2); |
125 | void strprintf(std::string &out,const char *format,...) APT_PRINTF(2); | |
126 | char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3); | |
8f3ba4e8 | 127 | bool CheckDomainList(const std::string &Host, const std::string &List); |
98eb4e9e JAK |
128 | |
129 | /* Do some compat mumbo jumbo */ | |
130 | #define tolower_ascii tolower_ascii_inline | |
131 | #define isspace_ascii isspace_ascii_inline | |
132 | ||
133 | APT_CONST APT_HOT | |
134 | static inline int tolower_ascii_inline(int const c) | |
135 | { | |
136 | return (c >= 'A' && c <= 'Z') ? c + 32 : c; | |
137 | } | |
138 | APT_CONST APT_HOT | |
139 | static inline int isspace_ascii_inline(int const c) | |
140 | { | |
141 | // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' ' | |
142 | return (c >= 9 && c <= 13) || c == ' '; | |
143 | } | |
98b06343 | 144 | |
8f3ba4e8 | 145 | std::string StripEpoch(const std::string &VerStr); |
6c139d6e | 146 | |
c24972cb | 147 | #define APT_MKSTRCMP(name,func) \ |
a02db58f DK |
148 | inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \ |
149 | inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ | |
150 | 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));} \ | |
151 | 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());} \ | |
152 | 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);} | |
0db4a45b | 153 | |
47db8997 | 154 | #define APT_MKSTRCMP2(name,func) \ |
a02db58f DK |
155 | inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ |
156 | inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \ | |
157 | inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \ | |
158 | inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);} | |
47db8997 | 159 | |
a02db58f DK |
160 | int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); |
161 | int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd); | |
ae0b19f5 AL |
162 | |
163 | /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that | |
164 | case the definition of string::const_iterator is not the same as | |
165 | const char * and we need these extra functions */ | |
166 | #if __GNUC__ >= 3 | |
a02db58f | 167 | int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, |
47db8997 | 168 | const char *B,const char *BEnd); |
a02db58f | 169 | int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, |
8f3ba4e8 | 170 | std::string::const_iterator B,std::string::const_iterator BEnd); |
a02db58f | 171 | int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, |
47db8997 | 172 | const char *B,const char *BEnd); |
a02db58f | 173 | int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, |
8f3ba4e8 | 174 | std::string::const_iterator B,std::string::const_iterator BEnd); |
c24972cb | 175 | |
a02db58f DK |
176 | 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));} |
177 | 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));} | |
ae0b19f5 AL |
178 | #endif |
179 | ||
d3e8fbb3 DK |
180 | APT_MKSTRCMP2(stringcmp,stringcmp) |
181 | APT_MKSTRCMP2(stringcasecmp,stringcasecmp) | |
0db4a45b | 182 | |
b9179170 | 183 | // Return the length of a NULL-terminated string array |
a02db58f | 184 | size_t APT_PURE strv_length(const char **str_array); |
b9179170 MV |
185 | |
186 | ||
d3e8fbb3 | 187 | inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);} |
6c139d6e | 188 | |
93bf083d AL |
189 | class URI |
190 | { | |
8f3ba4e8 | 191 | void CopyFrom(const std::string &From); |
3809194b | 192 | |
93bf083d | 193 | public: |
3809194b | 194 | |
8f3ba4e8 DK |
195 | std::string Access; |
196 | std::string User; | |
197 | std::string Password; | |
198 | std::string Host; | |
199 | std::string Path; | |
93bf083d AL |
200 | unsigned int Port; |
201 | ||
8f3ba4e8 | 202 | operator std::string(); |
d3e8fbb3 | 203 | inline void operator =(const std::string &From) {CopyFrom(From);} |
be4401bf | 204 | inline bool empty() {return Access.empty();}; |
8f3ba4e8 | 205 | static std::string SiteOnly(const std::string &URI); |
1da3b7b8 | 206 | static std::string ArchiveOnly(const std::string &URI); |
8f3ba4e8 | 207 | static std::string NoUserPassword(const std::string &URI); |
93bf083d | 208 | |
d3e8fbb3 DK |
209 | URI(std::string Path) {CopyFrom(Path);} |
210 | URI() : Port(0) {} | |
93bf083d AL |
211 | }; |
212 | ||
b2e465d6 AL |
213 | struct SubstVar |
214 | { | |
215 | const char *Subst; | |
8f3ba4e8 | 216 | const std::string *Contents; |
b2e465d6 | 217 | }; |
8f3ba4e8 DK |
218 | std::string SubstVar(std::string Str,const struct SubstVar *Vars); |
219 | std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents); | |
b2e465d6 AL |
220 | |
221 | struct RxChoiceList | |
222 | { | |
223 | void *UserData; | |
224 | const char *Str; | |
225 | bool Hit; | |
226 | }; | |
227 | unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, | |
228 | const char **ListEnd); | |
229 | ||
6c139d6e | 230 | #endif |