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