]> git.saurik.com Git - apt-legacy.git/blame - apt-pkg/contrib/strutl.h
Fix compilation of http when embedding into Cydia.
[apt-legacy.git] / apt-pkg / contrib / strutl.h
CommitLineData
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
30using std::string;
31using std::vector;
32using 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
43bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest);
da6ee469
JF
44char *_strstrip(char *String);
45char *_strtabexpand(char *String,size_t Len);
46bool ParseQuoteWord(const char *&String,string &Res);
47bool ParseCWord(const char *&String,string &Res);
48string QuoteString(const string &Str,const char *Bad);
49string DeQuoteString(const string &Str);
50string SizeToStr(double Bytes);
51string TimeToStr(unsigned long Sec);
52string Base64Encode(const string &Str);
0e5943eb 53string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
da6ee469
JF
54string URItoFileName(const string &URI);
55string TimeRFC1123(time_t Date);
56bool StrToTime(const string &Val,time_t &Result);
57string LookupTag(const string &Message,const char *Tag,const char *Default = 0);
58int StringToBool(const string &Text,int Default = -1);
59bool ReadMessages(int Fd, vector<string> &List);
60bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
61bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length);
734f3daf 62bool Hex2Num(const srkString &Str,unsigned char *Num,unsigned int Length);
da6ee469
JF
63bool TokSplitString(char Tok,char *Input,char **List,
64 unsigned long ListMax);
65void ioprintf(ostream &out,const char *format,...) APT_FORMAT2;
0e5943eb 66void strprintf(string &out,const char *format,...) APT_FORMAT2;
da6ee469
JF
67char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_FORMAT3;
68bool CheckDomainList(const string &Host, const string &List);
0e5943eb 69int tolower_ascii(int c);
da6ee469
JF
70
71#define APT_MKSTRCMP(name,func) \
734f3daf 72inline int name(const srkString &A,const char *B) {return func(A.Start,A.Start+A.Size,B,B+strlen(B));}; \
0e5943eb 73inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \
da6ee469 74inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
0e5943eb
JF
75inline int name(const string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \
76inline 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());}; \
77inline 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 80inline int name(const srkString &A,const char *B) {return func(A.Start,A.Start+A.Size,B,B+strlen(B));}; \
da6ee469 81inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
0e5943eb
JF
82inline int name(const string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));}; \
83inline int name(const string& A,const string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \
84inline int name(const string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);};
da6ee469
JF
85
86int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
87int 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
93int stringcmp(string::const_iterator A,string::const_iterator AEnd,
94 const char *B,const char *BEnd);
95int stringcmp(string::const_iterator A,string::const_iterator AEnd,
96 string::const_iterator B,string::const_iterator BEnd);
97int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
98 const char *B,const char *BEnd);
99int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
100 string::const_iterator B,string::const_iterator BEnd);
101
102inline int stringcmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));};
103inline int stringcasecmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));};
104#endif
105
106APT_MKSTRCMP2(stringcmp,stringcmp);
107APT_MKSTRCMP2(stringcasecmp,stringcasecmp);
108
109inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);};
110
111class 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
133struct SubstVar
134{
135 const char *Subst;
136 const string *Contents;
137};
138string SubstVar(string Str,const struct SubstVar *Vars);
139string SubstVar(const string &Str,const string &Subst,const string &Contents);
140
141struct RxChoiceList
142{
143 void *UserData;
144 const char *Str;
145 bool Hit;
146};
147unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
148 const char **ListEnd);
149
150#undef APT_FORMAT2
151
152#endif