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