apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

strutl.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  String Util - These are some useful string functions
6 
7  _strstrip is a function to remove whitespace from the front and end
8  of a string.
9 
10  This source is placed in the Public Domain, do with it what you will
11  It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
12 
13  ##################################################################### */
14  /*}}}*/
15 #ifndef STRUTL_H
16 #define STRUTL_H
17 
18 #include <cstring>
19 #include <iostream>
20 #include <limits>
21 #include <string>
22 #include <vector>
23 #include <apt-pkg/string_view.h>
24 #include <stddef.h>
25 #include <time.h>
26 
27 #include "macros.h"
28 
29 
30 namespace APT {
31  namespace String {
32  APT_PUBLIC std::string Strip(const std::string &s);
33  APT_PUBLIC bool Endswith(const std::string &s, const std::string &ending);
34  APT_PUBLIC bool Startswith(const std::string &s, const std::string &starting);
35  APT_PUBLIC std::string Join(std::vector<std::string> list, const std::string &sep);
36  // Returns string display length honoring multi-byte characters
37  APT_PUBLIC size_t DisplayLength(StringView str);
38  }
39 }
40 
41 
42 APT_PUBLIC bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
43 APT_PUBLIC char *_strstrip(char *String);
44 APT_PUBLIC char *_strrstrip(char *String); // right strip only
45 APT_DEPRECATED_MSG("Use SubstVar to avoid memory headaches") APT_PUBLIC char *_strtabexpand(char *String,size_t Len);
46 APT_PUBLIC bool ParseQuoteWord(const char *&String,std::string &Res);
47 APT_PUBLIC bool ParseCWord(const char *&String,std::string &Res);
48 APT_PUBLIC std::string QuoteString(const std::string &Str,const char *Bad);
49 APT_PUBLIC std::string DeQuoteString(const std::string &Str);
50 APT_PUBLIC std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
51 
52 // unescape (\0XX and \xXX) from a string
53 APT_PUBLIC std::string DeEscapeString(const std::string &input);
54 
55 APT_PUBLIC std::string SizeToStr(double Bytes);
56 APT_PUBLIC std::string TimeToStr(unsigned long Sec);
57 APT_PUBLIC std::string Base64Encode(const std::string &Str);
58 APT_PUBLIC std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
59 APT_PUBLIC std::string URItoFileName(const std::string &URI);
60 /** returns a datetime string as needed by HTTP/1.1 and Debian files.
61  *
62  * Note: The date will always be represented in a UTC timezone
63  *
64  * @param Date to be represented as a string
65  * @param NumericTimezone is preferred in general, but HTTP/1.1 requires the use
66  * of GMT as timezone instead. \b true means that the timezone should be denoted
67  * as "+0000" while \b false uses "GMT".
68  */
69 APT_PUBLIC std::string TimeRFC1123(time_t Date, bool const NumericTimezone);
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  */
85 APT_PUBLIC bool RFC1123StrToTime(const std::string &str,time_t &time) APT_MUSTCHECK;
86 APT_PUBLIC bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
87 APT_PUBLIC std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
88 APT_PUBLIC int StringToBool(const std::string &Text,int Default = -1);
89 APT_PUBLIC bool ReadMessages(int Fd, std::vector<std::string> &List);
90 APT_PUBLIC bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
91 APT_PUBLIC bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
92 APT_PUBLIC bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
93 APT_PUBLIC bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
94 APT_PUBLIC bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length);
95 // input changing string split
96 APT_PUBLIC bool TokSplitString(char Tok,char *Input,char **List,
97  unsigned long ListMax);
98 
99 // split a given string by a char
100 APT_PUBLIC std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
101 
102 /* \brief Return a vector of strings from string "input" where "sep"
103  * is used as the delimiter string.
104  *
105  * \param input The input string.
106  *
107  * \param sep The separator to use.
108  *
109  * \param maxsplit (optional) The maximum amount of splitting that
110  * should be done .
111  *
112  * The optional "maxsplit" argument can be used to limit the splitting,
113  * if used the string is only split on maxsplit places and the last
114  * item in the vector contains the remainder string.
115  */
116 APT_PUBLIC std::vector<std::string> StringSplit(std::string const &input,
117  std::string const &sep,
118  unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_PURE;
119 
120 
121 APT_HIDDEN bool iovprintf(std::ostream &out, const char *format, va_list &args, ssize_t &size);
122 APT_PUBLIC void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
123 APT_PUBLIC void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
124 APT_PUBLIC char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
125 APT_PUBLIC bool CheckDomainList(const std::string &Host, const std::string &List);
126 
127 /* Do some compat mumbo jumbo */
128 #define tolower_ascii tolower_ascii_inline
129 #define isspace_ascii isspace_ascii_inline
130 
132 static inline int tolower_ascii_unsafe(int const c)
133 {
134  return c | 0x20;
135 }
137 static inline int tolower_ascii_inline(int const c)
138 {
139  return (c >= 'A' && c <= 'Z') ? c + 32 : c;
140 }
142 static inline int isspace_ascii_inline(int const c)
143 {
144  // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' '
145  return (c >= 9 && c <= 13) || c == ' ';
146 }
147 
148 APT_PUBLIC std::string StripEpoch(const std::string &VerStr);
149 
150 #define APT_MKSTRCMP(name,func) \
151 inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
152 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
153 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));} \
154 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());} \
155 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);}
156 
157 #define APT_MKSTRCMP2(name,func) \
158 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
159 inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
160 inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
161 inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
162 
163 APT_PUBLIC int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
164 APT_PUBLIC int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
165 
166 /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
167  case the definition of string::const_iterator is not the same as
168  const char * and we need these extra functions */
169 #if __GNUC__ >= 3
170 APT_PUBLIC int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
171  const char *B,const char *BEnd);
172 APT_PUBLIC int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
173  std::string::const_iterator B,std::string::const_iterator BEnd);
174 APT_PUBLIC int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
175  const char *B,const char *BEnd);
176 APT_PUBLIC int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
177  std::string::const_iterator B,std::string::const_iterator BEnd);
178 
179 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));}
180 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));}
181 #endif
182 
185 
186 // Return the length of a NULL-terminated string array
187 APT_PUBLIC size_t APT_PURE strv_length(const char **str_array);
188 
189 
190 inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
191 
193 {
194  void CopyFrom(const std::string &From);
195 
196  public:
197 
198  std::string Access;
199  std::string User;
200  std::string Password;
201  std::string Host;
202  std::string Path;
203  unsigned int Port;
204 
205  operator std::string();
206  inline void operator =(const std::string &From) {CopyFrom(From);}
207  inline bool empty() {return Access.empty();};
208  static std::string SiteOnly(const std::string &URI);
209  static std::string ArchiveOnly(const std::string &URI);
210  static std::string NoUserPassword(const std::string &URI);
211 
212  explicit URI(std::string Path) { CopyFrom(Path); }
213  URI() : Port(0) {}
214 };
215 
216 struct SubstVar
217 {
218  const char *Subst;
219  const std::string *Contents;
220 };
221 APT_PUBLIC std::string SubstVar(std::string Str,const struct SubstVar *Vars);
222 APT_PUBLIC std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
223 
225 {
226  void *UserData;
227  const char *Str;
228  bool Hit;
229 };
230 APT_PUBLIC unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
231  const char **ListEnd);
232 
233 #endif
Definition: strutl.h:193
std::string Access
Definition: strutl.h:198
std::string Path
Definition: strutl.h:202
bool empty()
Definition: strutl.h:207
unsigned int Port
Definition: strutl.h:203
URI()
Definition: strutl.h:213
URI(std::string Path)
Definition: strutl.h:212
std::string User
Definition: strutl.h:199
std::string Host
Definition: strutl.h:201
std::string Password
Definition: strutl.h:200
static std::vector< std::string > split(std::string const &s)
Definition: init.cc:43
#define APT_PRINTF(n)
Definition: macros.h:58
#define APT_MUSTCHECK
Definition: macros.h:68
#define APT_PURE
Definition: macros.h:56
#define APT_PUBLIC
Definition: macros.h:77
#define APT_HOT
Definition: macros.h:87
#define APT_DEPRECATED_MSG
Definition: macros.h:55
#define APT_HIDDEN
Definition: macros.h:78
std::string Strip(const std::string &str)
Definition: strutl.cc:55
bool Endswith(const std::string &s, const std::string &end)
Definition: strutl.cc:77
std::string Join(std::vector< std::string > list, const std::string &sep)
Definition: strutl.cc:91
size_t DisplayLength(StringView str)
Definition: strutl.cc:103
bool Startswith(const std::string &s, const std::string &start)
Definition: strutl.cc:84
const char * Str
Definition: strutl.h:227
bool Hit
Definition: strutl.h:228
void * UserData
Definition: strutl.h:226
const std::string * Contents
Definition: strutl.h:219
const char * Subst
Definition: strutl.h:218
char * _strstrip(char *String)
Definition: strutl.cc:226
int stringcasecmp(const char *A, const char *AEnd, const char *B, const char *BEnd)
Definition: strutl.cc:685
std::string SizeToStr(double Bytes)
Definition: strutl.cc:437
std::string TimeRFC1123(time_t Date, bool const NumericTimezone)
Definition: strutl.cc:853
std::string DeQuoteString(const std::string &Str)
Definition: strutl.cc:404
unsigned long RegexChoice(RxChoiceList *Rxs, const char **ListBegin, const char **ListEnd)
Definition: strutl.cc:1357
std::string StripEpoch(const std::string &VerStr)
Definition: strutl.cc:1486
static int tolower_ascii_inline(int const c)
Definition: strutl.h:137
char * safe_snprintf(char *Buffer, char *End, const char *Format,...)
Definition: strutl.cc:1468
bool StrToNum(const char *Str, unsigned long &Res, unsigned Len, unsigned Base=0)
Definition: strutl.cc:1146
std::string LookupTag(const std::string &Message, const char *Tag, const char *Default=0)
Definition: strutl.cc:742
bool Hex2Num(const APT::StringView Str, unsigned char *Num, unsigned int Length)
Definition: strutl.cc:1239
static int tolower_ascii_unsafe(int const c)
Definition: strutl.h:132
bool ParseQuoteWord(const char *&String, std::string &Res)
Definition: strutl.cc:288
#define APT_MKSTRCMP2(name, func)
Definition: strutl.h:157
bool ReadMessages(int Fd, std::vector< std::string > &List)
Definition: strutl.cc:883
size_t strv_length(const char **str_array)
Definition: strutl.cc:1549
void strprintf(std::string &out, const char *format,...)
Definition: strutl.cc:1446
const char * DeNull(const char *s)
Definition: strutl.h:190
bool CheckDomainList(const std::string &Host, const std::string &List)
Definition: strutl.cc:1527
bool TokSplitString(char Tok, char *Input, char **List, unsigned long ListMax)
Definition: strutl.cc:1267
bool Base256ToNum(const char *Str, unsigned long &Res, unsigned int Len)
Definition: strutl.cc:1208
bool ParseCWord(const char *&String, std::string &Res)
Definition: strutl.cc:347
bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest)
Definition: strutl.cc:155
std::string SubstVar(std::string Str, const struct SubstVar *Vars)
Definition: strutl.cc:530
bool RFC1123StrToTime(const std::string &str, time_t &time)
Definition: strutl.cc:1040
std::string URItoFileName(const std::string &URI)
Definition: strutl.cc:553
void ioprintf(std::ostream &out, const char *format,...)
Definition: strutl.cc:1433
int stringcmp(const char *A, const char *AEnd, const char *B, const char *BEnd)
Definition: strutl.cc:628
std::string Base64Encode(const std::string &Str)
Definition: strutl.cc:573
static int isspace_ascii_inline(int const c)
Definition: strutl.h:142
std::string TimeToStr(unsigned long Sec)
Definition: strutl.cc:473
std::string DeEscapeString(const std::string &input)
Definition: strutl.cc:1561
bool iovprintf(std::ostream &out, const char *format, va_list &args, ssize_t &size)
Definition: strutl.cc:1418
std::vector< std::string > StringSplit(std::string const &input, std::string const &sep, unsigned int maxsplit=std::numeric_limits< unsigned int >::max())
Definition: strutl.cc:1327
std::vector< std::string > VectorizeString(std::string const &haystack, char const &split)
Definition: strutl.cc:1308
char * _strrstrip(char *String)
Definition: strutl.cc:237
bool FTPMDTMStrToTime(const char *const str, time_t &time)
Definition: strutl.cc:1131
Use SubstVar to avoid memory headaches char * _strtabexpand(char *String, size_t Len)
Definition: strutl.cc:250
int StringToBool(const std::string &Text, int Default=-1)
Definition: strutl.cc:820
std::string OutputInDepth(const unsigned long Depth, const char *Separator=" ")
Definition: strutl.cc:540
std::string QuoteString(const std::string &Str, const char *Bad)
Definition: strutl.cc:384