]>
Commit | Line | Data |
---|---|---|
da6ee469 JF |
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 | ||
00ec24d0 | 19 | |
da6ee469 JF |
20 | |
21 | #include <stdlib.h> | |
22 | #include <string> | |
00ec24d0 | 23 | #include <cstring> |
da6ee469 JF |
24 | #include <vector> |
25 | #include <iostream> | |
26 | #include <time.h> | |
27 | ||
734f3daf JF |
28 | #include <apt-pkg/srkstring.h> |
29 | ||
da6ee469 JF |
30 | using std::string; |
31 | using std::vector; | |
32 | using std::ostream; | |
33 | ||
34 | #ifdef __GNUG__ | |
35 | // Methods have a hidden this parameter that is visible to this attribute | |
36 | #define APT_FORMAT2 __attribute__ ((format (printf, 2, 3))) | |
37 | #define APT_FORMAT3 __attribute__ ((format (printf, 3, 4))) | |
38 | #else | |
39 | #define APT_FORMAT2 | |
40 | #define APT_FORMAT3 | |
41 | #endif | |
00ec24d0 JF |
42 | |
43 | bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest); | |
da6ee469 JF |
44 | char *_strstrip(char *String); |
45 | char *_strtabexpand(char *String,size_t Len); | |
46 | bool ParseQuoteWord(const char *&String,string &Res); | |
47 | bool ParseCWord(const char *&String,string &Res); | |
48 | string QuoteString(const string &Str,const char *Bad); | |
49 | string DeQuoteString(const string &Str); | |
50 | string SizeToStr(double Bytes); | |
51 | string TimeToStr(unsigned long Sec); | |
52 | string Base64Encode(const string &Str); | |
0e5943eb | 53 | string OutputInDepth(const unsigned long Depth, const char* Separator=" "); |
da6ee469 JF |
54 | string URItoFileName(const string &URI); |
55 | string TimeRFC1123(time_t Date); | |
56 | bool StrToTime(const string &Val,time_t &Result); | |
57 | string LookupTag(const string &Message,const char *Tag,const char *Default = 0); | |
58 | int StringToBool(const string &Text,int Default = -1); | |
59 | bool ReadMessages(int Fd, vector<string> &List); | |
60 | bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0); | |
61 | bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length); | |
734f3daf | 62 | bool Hex2Num(const srkString &Str,unsigned char *Num,unsigned int Length); |
da6ee469 JF |
63 | bool TokSplitString(char Tok,char *Input,char **List, |
64 | unsigned long ListMax); | |
65 | void ioprintf(ostream &out,const char *format,...) APT_FORMAT2; | |
0e5943eb | 66 | void strprintf(string &out,const char *format,...) APT_FORMAT2; |
da6ee469 JF |
67 | char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_FORMAT3; |
68 | bool CheckDomainList(const string &Host, const string &List); | |
0e5943eb | 69 | int tolower_ascii(int c); |
da6ee469 JF |
70 | |
71 | #define APT_MKSTRCMP(name,func) \ | |
734f3daf | 72 | inline int name(const srkString &A,const char *B) {return func(A.Start,A.Start+A.Size,B,B+strlen(B));}; \ |
0e5943eb | 73 | inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \ |
da6ee469 | 74 | inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \ |
0e5943eb JF |
75 | inline int name(const string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \ |
76 | inline int name(const string& A,const string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());}; \ | |
77 | inline int name(const string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);}; | |
da6ee469 JF |
78 | |
79 | #define APT_MKSTRCMP2(name,func) \ | |
b6d30aa2 | 80 | inline int name(const srkString &A,const char *B) {return func(A.Start,A.Start+A.Size,B,B+strlen(B));}; \ |
da6ee469 | 81 | inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \ |
0e5943eb JF |
82 | inline int name(const string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));}; \ |
83 | inline int name(const string& A,const string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \ | |
84 | inline int name(const string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}; | |
da6ee469 JF |
85 | |
86 | int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); | |
87 | int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd); | |
88 | ||
89 | /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that | |
90 | case the definition of string::const_iterator is not the same as | |
91 | const char * and we need these extra functions */ | |
92 | #if __GNUC__ >= 3 | |
93 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
94 | const char *B,const char *BEnd); | |
95 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
96 | string::const_iterator B,string::const_iterator BEnd); | |
97 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
98 | const char *B,const char *BEnd); | |
99 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
100 | string::const_iterator B,string::const_iterator BEnd); | |
101 | ||
102 | inline int stringcmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));}; | |
103 | inline int stringcasecmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));}; | |
104 | #endif | |
105 | ||
106 | APT_MKSTRCMP2(stringcmp,stringcmp); | |
107 | APT_MKSTRCMP2(stringcasecmp,stringcasecmp); | |
108 | ||
109 | inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}; | |
110 | ||
111 | class URI | |
112 | { | |
113 | void CopyFrom(const string &From); | |
114 | ||
115 | public: | |
116 | ||
117 | string Access; | |
118 | string User; | |
119 | string Password; | |
120 | string Host; | |
121 | string Path; | |
122 | unsigned int Port; | |
123 | ||
124 | operator string(); | |
125 | inline void operator =(const string &From) {CopyFrom(From);}; | |
126 | inline bool empty() {return Access.empty();}; | |
127 | static string SiteOnly(const string &URI); | |
128 | ||
129 | URI(string Path) {CopyFrom(Path);}; | |
130 | URI() : Port(0) {}; | |
131 | }; | |
132 | ||
133 | struct SubstVar | |
134 | { | |
135 | const char *Subst; | |
136 | const string *Contents; | |
137 | }; | |
138 | string SubstVar(string Str,const struct SubstVar *Vars); | |
139 | string SubstVar(const string &Str,const string &Subst,const string &Contents); | |
140 | ||
141 | struct RxChoiceList | |
142 | { | |
143 | void *UserData; | |
144 | const char *Str; | |
145 | bool Hit; | |
146 | }; | |
147 | unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, | |
148 | const char **ListEnd); | |
149 | ||
150 | #undef APT_FORMAT2 | |
151 | ||
152 | #endif |