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