strutl.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $
  4. /* ######################################################################
  5. String Util - These are some useful string functions
  6. _strstrip is a function to remove whitespace from the front and end
  7. of a string.
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
  10. ##################################################################### */
  11. /*}}}*/
  12. #ifndef STRUTL_H
  13. #define STRUTL_H
  14. #include <limits>
  15. #include <string>
  16. #include <cstring>
  17. #include <vector>
  18. #include <iostream>
  19. #include <time.h>
  20. #include <stddef.h>
  21. #include "macros.h"
  22. #ifndef APT_10_CLEANER_HEADERS
  23. #include <stdlib.h>
  24. #endif
  25. #ifndef APT_8_CLEANER_HEADERS
  26. using std::string;
  27. using std::vector;
  28. using std::ostream;
  29. #endif
  30. namespace APT {
  31. namespace String {
  32. std::string Strip(const std::string &s);
  33. bool Endswith(const std::string &s, const std::string &ending);
  34. }
  35. }
  36. bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
  37. char *_strstrip(char *String);
  38. char *_strrstrip(char *String); // right strip only
  39. char *_strtabexpand(char *String,size_t Len);
  40. bool ParseQuoteWord(const char *&String,std::string &Res);
  41. bool ParseCWord(const char *&String,std::string &Res);
  42. std::string QuoteString(const std::string &Str,const char *Bad);
  43. std::string DeQuoteString(const std::string &Str);
  44. std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
  45. // unescape (\0XX and \xXX) from a string
  46. std::string DeEscapeString(const std::string &input);
  47. std::string SizeToStr(double Bytes);
  48. std::string TimeToStr(unsigned long Sec);
  49. std::string Base64Encode(const std::string &Str);
  50. std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
  51. std::string URItoFileName(const std::string &URI);
  52. std::string TimeRFC1123(time_t Date);
  53. bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  54. bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  55. APT_DEPRECATED bool StrToTime(const std::string &Val,time_t &Result);
  56. std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
  57. int StringToBool(const std::string &Text,int Default = -1);
  58. bool ReadMessages(int Fd, std::vector<std::string> &List);
  59. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
  60. bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
  61. bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
  62. bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
  63. // input changing string split
  64. bool TokSplitString(char Tok,char *Input,char **List,
  65. unsigned long ListMax);
  66. // split a given string by a char
  67. std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_CONST;
  68. /* \brief Return a vector of strings from string "input" where "sep"
  69. * is used as the delimiter string.
  70. *
  71. * \param input The input string.
  72. *
  73. * \param sep The seperator to use.
  74. *
  75. * \param maxsplit (optional) The maximum amount of splitting that
  76. * should be done .
  77. *
  78. * The optional "maxsplit" argument can be used to limit the splitting,
  79. * if used the string is only split on maxsplit places and the last
  80. * item in the vector contains the remainder string.
  81. */
  82. std::vector<std::string> StringSplit(std::string const &input,
  83. std::string const &sep,
  84. unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST;
  85. void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
  86. void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
  87. char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
  88. bool CheckDomainList(const std::string &Host, const std::string &List);
  89. int tolower_ascii(int const c) APT_CONST APT_HOT;
  90. std::string StripEpoch(const std::string &VerStr);
  91. #define APT_MKSTRCMP(name,func) \
  92. inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
  93. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  94. inline 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));} \
  95. inline 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());} \
  96. inline 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);}
  97. #define APT_MKSTRCMP2(name,func) \
  98. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  99. inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
  100. inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
  101. inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
  102. int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  103. int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  104. /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
  105. case the definition of string::const_iterator is not the same as
  106. const char * and we need these extra functions */
  107. #if __GNUC__ >= 3
  108. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  109. const char *B,const char *BEnd);
  110. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  111. std::string::const_iterator B,std::string::const_iterator BEnd);
  112. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  113. const char *B,const char *BEnd);
  114. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  115. std::string::const_iterator B,std::string::const_iterator BEnd);
  116. inline 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));}
  117. inline 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));}
  118. #endif
  119. APT_MKSTRCMP2(stringcmp,stringcmp)
  120. APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
  121. // Return the length of a NULL-terminated string array
  122. size_t APT_PURE strv_length(const char **str_array);
  123. inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
  124. class URI
  125. {
  126. void CopyFrom(const std::string &From);
  127. public:
  128. std::string Access;
  129. std::string User;
  130. std::string Password;
  131. std::string Host;
  132. std::string Path;
  133. unsigned int Port;
  134. operator std::string();
  135. inline void operator =(const std::string &From) {CopyFrom(From);}
  136. inline bool empty() {return Access.empty();};
  137. static std::string SiteOnly(const std::string &URI);
  138. static std::string NoUserPassword(const std::string &URI);
  139. URI(std::string Path) {CopyFrom(Path);}
  140. URI() : Port(0) {}
  141. };
  142. struct SubstVar
  143. {
  144. const char *Subst;
  145. const std::string *Contents;
  146. };
  147. std::string SubstVar(std::string Str,const struct SubstVar *Vars);
  148. std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
  149. struct RxChoiceList
  150. {
  151. void *UserData;
  152. const char *Str;
  153. bool Hit;
  154. };
  155. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  156. const char **ListEnd);
  157. #endif