]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/contrib/strutl.h
review apt(8) manpage
[apt.git] / apt-pkg / contrib / strutl.h
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $
4/* ######################################################################
5
6 String Util - These are some useful string functions
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 /*}}}*/
16#ifndef STRUTL_H
17#define STRUTL_H
18
19
20#include <limits>
21#include <string>
22#include <cstring>
23#include <vector>
24#include <iostream>
25#include <time.h>
26#include <stddef.h>
27
28#include "macros.h"
29
30#ifndef APT_10_CLEANER_HEADERS
31#include <stdlib.h>
32#endif
33#ifndef APT_8_CLEANER_HEADERS
34using std::string;
35using std::vector;
36using std::ostream;
37#endif
38
39namespace APT {
40 namespace String {
41 std::string Strip(const std::string &s);
42 bool Endswith(const std::string &s, const std::string &ending);
43 bool Startswith(const std::string &s, const std::string &starting);
44 }
45}
46
47
48bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
49char *_strstrip(char *String);
50char *_strrstrip(char *String); // right strip only
51char *_strtabexpand(char *String,size_t Len);
52bool ParseQuoteWord(const char *&String,std::string &Res);
53bool ParseCWord(const char *&String,std::string &Res);
54std::string QuoteString(const std::string &Str,const char *Bad);
55std::string DeQuoteString(const std::string &Str);
56std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
57
58// unescape (\0XX and \xXX) from a string
59std::string DeEscapeString(const std::string &input);
60
61std::string SizeToStr(double Bytes);
62std::string TimeToStr(unsigned long Sec);
63std::string Base64Encode(const std::string &Str);
64std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
65std::string URItoFileName(const std::string &URI);
66std::string TimeRFC1123(time_t Date);
67bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
68bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
69APT_DEPRECATED bool StrToTime(const std::string &Val,time_t &Result);
70std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
71int StringToBool(const std::string &Text,int Default = -1);
72bool ReadMessages(int Fd, std::vector<std::string> &List);
73bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
74bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
75bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
76bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
77bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
78
79// input changing string split
80bool TokSplitString(char Tok,char *Input,char **List,
81 unsigned long ListMax);
82
83// split a given string by a char
84std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
85
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 */
100std::vector<std::string> StringSplit(std::string const &input,
101 std::string const &sep,
102 unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST;
103
104void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
105void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
106char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
107bool CheckDomainList(const std::string &Host, const std::string &List);
108int tolower_ascii(int const c) APT_CONST APT_HOT;
109std::string StripEpoch(const std::string &VerStr);
110
111#define APT_MKSTRCMP(name,func) \
112inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
113inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
114inline 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));} \
115inline 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());} \
116inline 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);}
117
118#define APT_MKSTRCMP2(name,func) \
119inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
120inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
121inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
122inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
123
124int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
125int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
126
127/* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
128 case the definition of string::const_iterator is not the same as
129 const char * and we need these extra functions */
130#if __GNUC__ >= 3
131int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
132 const char *B,const char *BEnd);
133int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
134 std::string::const_iterator B,std::string::const_iterator BEnd);
135int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
136 const char *B,const char *BEnd);
137int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
138 std::string::const_iterator B,std::string::const_iterator BEnd);
139
140inline 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));}
141inline 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#endif
143
144APT_MKSTRCMP2(stringcmp,stringcmp)
145APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
146
147// Return the length of a NULL-terminated string array
148size_t APT_PURE strv_length(const char **str_array);
149
150
151inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
152
153class URI
154{
155 void CopyFrom(const std::string &From);
156
157 public:
158
159 std::string Access;
160 std::string User;
161 std::string Password;
162 std::string Host;
163 std::string Path;
164 unsigned int Port;
165
166 operator std::string();
167 inline void operator =(const std::string &From) {CopyFrom(From);}
168 inline bool empty() {return Access.empty();};
169 static std::string SiteOnly(const std::string &URI);
170 static std::string ArchiveOnly(const std::string &URI);
171 static std::string NoUserPassword(const std::string &URI);
172
173 URI(std::string Path) {CopyFrom(Path);}
174 URI() : Port(0) {}
175};
176
177struct SubstVar
178{
179 const char *Subst;
180 const std::string *Contents;
181};
182std::string SubstVar(std::string Str,const struct SubstVar *Vars);
183std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
184
185struct RxChoiceList
186{
187 void *UserData;
188 const char *Str;
189 bool Hit;
190};
191unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
192 const char **ListEnd);
193
194#endif