strutl.h 8.5 KB

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