apt @VERSION@
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 bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
00031 char *_strstrip(char *String);
00032 char *_strtabexpand(char *String,size_t Len);
00033 bool ParseQuoteWord(const char *&String,std::string &Res);
00034 bool ParseCWord(const char *&String,std::string &Res);
00035 std::string QuoteString(const std::string &Str,const char *Bad);
00036 std::string DeQuoteString(const std::string &Str);
00037 std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
00038 
00039 // unescape (\0XX and \xXX) from a string
00040 std::string DeEscapeString(const std::string &input);
00041 
00042 std::string SizeToStr(double Bytes);
00043 std::string TimeToStr(unsigned long Sec);
00044 std::string Base64Encode(const std::string &Str);
00045 std::string OutputInDepth(const unsigned long Depth, const char* Separator="  ");
00046 std::string URItoFileName(const std::string &URI);
00047 std::string TimeRFC1123(time_t Date);
00048 bool RFC1123StrToTime(const char* const str,time_t &time) __must_check;
00049 bool FTPMDTMStrToTime(const char* const str,time_t &time) __must_check;
00050 __deprecated bool StrToTime(const std::string &Val,time_t &Result);
00051 std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
00052 int StringToBool(const std::string &Text,int Default = -1);
00053 bool ReadMessages(int Fd, std::vector<std::string> &List);
00054 bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
00055 bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
00056 bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
00057 bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
00058 bool TokSplitString(char Tok,char *Input,char **List,
00059                     unsigned long ListMax);
00060 std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) __attrib_const;
00061 void ioprintf(std::ostream &out,const char *format,...) __like_printf(2);
00062 void strprintf(std::string &out,const char *format,...) __like_printf(2);
00063 char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3);
00064 bool CheckDomainList(const std::string &Host, const std::string &List);
00065 int tolower_ascii(int const c) __attrib_const __hot;
00066 std::string StripEpoch(const std::string &VerStr);
00067 
00068 #define APT_MKSTRCMP(name,func) \
00069 inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \
00070 inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
00071 inline int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \
00072 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());}; \
00073 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);};
00074 
00075 #define APT_MKSTRCMP2(name,func) \
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.begin(),A.end(),B,B+strlen(B));}; \
00078 inline int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \
00079 inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);};
00080 
00081 int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
00082 int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
00083 
00084 /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
00085    case the definition of string::const_iterator is not the same as
00086    const char * and we need these extra functions */
00087 #if __GNUC__ >= 3
00088 int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00089               const char *B,const char *BEnd);
00090 int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00091               std::string::const_iterator B,std::string::const_iterator BEnd);
00092 int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00093                   const char *B,const char *BEnd);
00094 int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
00095                   std::string::const_iterator B,std::string::const_iterator BEnd);
00096 
00097 inline int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));};
00098 inline int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));};
00099 #endif
00100 
00101 APT_MKSTRCMP2(stringcmp,stringcmp);
00102 APT_MKSTRCMP2(stringcasecmp,stringcasecmp);
00103 
00104 inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);};
00105 
00106 class URI
00107 {
00108    void CopyFrom(const std::string &From);
00109                  
00110    public:
00111    
00112    std::string Access;
00113    std::string User;
00114    std::string Password;
00115    std::string Host;
00116    std::string Path;
00117    unsigned int Port;
00118    
00119    operator std::string();
00120    inline void operator =(const std::string &From) {CopyFrom(From);};
00121    inline bool empty() {return Access.empty();};
00122    static std::string SiteOnly(const std::string &URI);
00123    static std::string NoUserPassword(const std::string &URI);
00124    
00125    URI(std::string Path) {CopyFrom(Path);};
00126    URI() : Port(0) {};
00127 };
00128 
00129 struct SubstVar
00130 {
00131    const char *Subst;
00132    const std::string *Contents;
00133 };
00134 std::string SubstVar(std::string Str,const struct SubstVar *Vars);
00135 std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
00136 
00137 struct RxChoiceList
00138 {
00139    void *UserData;
00140    const char *Str;
00141    bool Hit;
00142 };
00143 unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
00144                       const char **ListEnd);
00145 
00146 #endif