strutl.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. bool Startswith(const std::string &s, const std::string &starting);
  35. }
  36. }
  37. bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
  38. char *_strstrip(char *String);
  39. char *_strrstrip(char *String); // right strip only
  40. char *_strtabexpand(char *String,size_t Len);
  41. bool ParseQuoteWord(const char *&String,std::string &Res);
  42. bool ParseCWord(const char *&String,std::string &Res);
  43. std::string QuoteString(const std::string &Str,const char *Bad);
  44. std::string DeQuoteString(const std::string &Str);
  45. std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
  46. // unescape (\0XX and \xXX) from a string
  47. std::string DeEscapeString(const std::string &input);
  48. std::string SizeToStr(double Bytes);
  49. std::string TimeToStr(unsigned long Sec);
  50. std::string Base64Encode(const std::string &Str);
  51. std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
  52. std::string URItoFileName(const std::string &URI);
  53. std::string TimeRFC1123(time_t Date);
  54. bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  55. bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  56. APT_DEPRECATED_MSG("Use RFC1123StrToTime or FTPMDTMStrToTime as needed instead") bool StrToTime(const std::string &Val,time_t &Result);
  57. std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
  58. int StringToBool(const std::string &Text,int Default = -1);
  59. bool ReadMessages(int Fd, std::vector<std::string> &List);
  60. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
  61. bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
  62. bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
  63. bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
  64. bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
  65. // input changing string split
  66. bool TokSplitString(char Tok,char *Input,char **List,
  67. unsigned long ListMax);
  68. // split a given string by a char
  69. std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
  70. /* \brief Return a vector of strings from string "input" where "sep"
  71. * is used as the delimiter string.
  72. *
  73. * \param input The input string.
  74. *
  75. * \param sep The seperator to use.
  76. *
  77. * \param maxsplit (optional) The maximum amount of splitting that
  78. * should be done .
  79. *
  80. * The optional "maxsplit" argument can be used to limit the splitting,
  81. * if used the string is only split on maxsplit places and the last
  82. * item in the vector contains the remainder string.
  83. */
  84. std::vector<std::string> StringSplit(std::string const &input,
  85. std::string const &sep,
  86. unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST;
  87. void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
  88. void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
  89. char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
  90. bool CheckDomainList(const std::string &Host, const std::string &List);
  91. /* Do some compat mumbo jumbo */
  92. #define tolower_ascii tolower_ascii_inline
  93. #define isspace_ascii isspace_ascii_inline
  94. APT_CONST APT_HOT
  95. static inline int tolower_ascii_inline(int const c)
  96. {
  97. return (c >= 'A' && c <= 'Z') ? c + 32 : c;
  98. }
  99. APT_CONST APT_HOT
  100. static inline int isspace_ascii_inline(int const c)
  101. {
  102. // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' '
  103. return (c >= 9 && c <= 13) || c == ' ';
  104. }
  105. std::string StripEpoch(const std::string &VerStr);
  106. #define APT_MKSTRCMP(name,func) \
  107. inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
  108. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  109. 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));} \
  110. 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());} \
  111. 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);}
  112. #define APT_MKSTRCMP2(name,func) \
  113. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  114. inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
  115. inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
  116. inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
  117. int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  118. int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  119. /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
  120. case the definition of string::const_iterator is not the same as
  121. const char * and we need these extra functions */
  122. #if __GNUC__ >= 3
  123. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  124. const char *B,const char *BEnd);
  125. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  126. std::string::const_iterator B,std::string::const_iterator BEnd);
  127. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  128. const char *B,const char *BEnd);
  129. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  130. std::string::const_iterator B,std::string::const_iterator BEnd);
  131. 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));}
  132. 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));}
  133. #endif
  134. APT_MKSTRCMP2(stringcmp,stringcmp)
  135. APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
  136. // Return the length of a NULL-terminated string array
  137. size_t APT_PURE strv_length(const char **str_array);
  138. inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
  139. class URI
  140. {
  141. void CopyFrom(const std::string &From);
  142. public:
  143. std::string Access;
  144. std::string User;
  145. std::string Password;
  146. std::string Host;
  147. std::string Path;
  148. unsigned int Port;
  149. operator std::string();
  150. inline void operator =(const std::string &From) {CopyFrom(From);}
  151. inline bool empty() {return Access.empty();};
  152. static std::string SiteOnly(const std::string &URI);
  153. static std::string ArchiveOnly(const std::string &URI);
  154. static std::string NoUserPassword(const std::string &URI);
  155. URI(std::string Path) {CopyFrom(Path);}
  156. URI() : Port(0) {}
  157. };
  158. struct SubstVar
  159. {
  160. const char *Subst;
  161. const std::string *Contents;
  162. };
  163. std::string SubstVar(std::string Str,const struct SubstVar *Vars);
  164. std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
  165. struct RxChoiceList
  166. {
  167. void *UserData;
  168. const char *Str;
  169. bool Hit;
  170. };
  171. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  172. const char **ListEnd);
  173. #endif