apt  1.5
strutl.h
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $
00004 /* ######################################################################
00005 
00006    String Util - These are some useful string functions
00007    
00008    _strstrip is a function to remove whitespace from the front and end
00009    of a string.
00010    
00011    This source is placed in the Public Domain, do with it what you will
00012    It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>   
00013    
00014    ##################################################################### */
00015                                                                         /*}}}*/
00016 #ifndef STRUTL_H
00017 #define STRUTL_H
00018 
00019 
00020 
00021 #include <stdlib.h>
00022 #include <string>
00023 #include <cstring>
00024 #include <vector>
00025 #include <iostream>
00026 #include <time.h>
00027 
00028 #include "macros.h"
00029 
00030 #ifndef APT_8_CLEANER_HEADERS
00031 using std::string;
00032 using std::vector;
00033 using std::ostream;
00034 #endif
00035 
00036 bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
00037 char *_strstrip(char *String);
00038 char *_strtabexpand(char *String,size_t Len);
00039 bool ParseQuoteWord(const char *&String,std::string &Res);
00040 bool ParseCWord(const char *&String,std::string &Res);
00041 std::string QuoteString(const std::string &Str,const char *Bad);
00042 std::string DeQuoteString(const std::string &Str);
00043 std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
00044 
00045 // unescape (\0XX and \xXX) from a string
00046 std::string DeEscapeString(const std::string &input);
00047 
00048 std::string SizeToStr(double Bytes);
00049 std::string TimeToStr(unsigned long Sec);
00050 std::string Base64Encode(const std::string &Str);
00051 std::string OutputInDepth(const unsigned long Depth, const char* Separator="  ");
00052 std::string URItoFileName(const std::string &URI);
00053 std::string TimeRFC1123(time_t Date);
00054 bool RFC1123StrToTime(const char* const str,time_t &time) __must_check;
00055 bool FTPMDTMStrToTime(const char* const str,time_t &time) __must_check;
00056 __deprecated bool StrToTime(const std::string &Val,time_t &Result);
00057 std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
00058 int StringToBool(const std::string &Text,int Default = -1);
00059 bool ReadMessages(int Fd, std::vector<std::string> &List);
00060 bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
00061 bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
00062 bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
00063 bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
00064 bool TokSplitString(char Tok,char *Input,char **List,
00065                     unsigned long ListMax);
00066 std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) __attrib_const;
00067 void ioprintf(std::ostream &out,const char *format,...) __like_printf(2);
00068 void strprintf(std::string &out,const char *format,...) __like_printf(2);
00069 char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3);
00070 bool CheckDomainList(const std::string &Host, const std::string &List);
00071 int tolower_ascii(int const c) __attrib_const __hot;
00072 std::string StripEpoch(const std::string &VerStr);
00073 
00074 #define APT_MKSTRCMP(name,func) \
00075 inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \
00076 inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
00077 inline int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \
00078 inline 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());}; \
00079 inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);};
00080 
00081 #define APT_MKSTRCMP2(name,func) \
00082 inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
00083 inline int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));}; \
00084 inline int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \
00085 inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);};
00086 
00087 int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
00088 int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
00089 
00090 /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
00091    case the definition of string::const_iterator is not the same as
00092    const char * and we need these extra functions */
00093 #if __GNUC__ >= 3
00094 int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00095               const char *B,const char *BEnd);
00096 int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00097               std::string::const_iterator B,std::string::const_iterator BEnd);
00098 int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00099                   const char *B,const char *BEnd);
00100 int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00101                   std::string::const_iterator B,std::string::const_iterator BEnd);
00102 
00103 inline int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));};
00104 inline int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));};
00105 #endif
00106 
00107 APT_MKSTRCMP2(stringcmp,stringcmp);
00108 APT_MKSTRCMP2(stringcasecmp,stringcasecmp);
00109 
00110 inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);};
00111 
00112 class URI
00113 {
00114    void CopyFrom(const std::string &From);
00115                  
00116    public:
00117    
00118    std::string Access;
00119    std::string User;
00120    std::string Password;
00121    std::string Host;
00122    std::string Path;
00123    unsigned int Port;
00124    
00125    operator std::string();
00126    inline void operator =(const std::string &From) {CopyFrom(From);};
00127    inline bool empty() {return Access.empty();};
00128    static std::string SiteOnly(const std::string &URI);
00129    static std::string NoUserPassword(const std::string &URI);
00130    
00131    URI(std::string Path) {CopyFrom(Path);};
00132    URI() : Port(0) {};
00133 };
00134 
00135 struct SubstVar
00136 {
00137    const char *Subst;
00138    const std::string *Contents;
00139 };
00140 std::string SubstVar(std::string Str,const struct SubstVar *Vars);
00141 std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
00142 
00143 struct RxChoiceList
00144 {
00145    void *UserData;
00146    const char *Str;
00147    bool Hit;
00148 };
00149 unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
00150                       const char **ListEnd);
00151 
00152 #endif