strutl.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /** parses time as needed by HTTP/1.1 and Debian files.
  58. *
  59. * HTTP/1.1 prefers dates in RFC1123 format (but the other two obsolete date formats
  60. * are supported to) and e.g. Release files use the same format in Date & Valid-Until
  61. * fields.
  62. *
  63. * Note: datetime strings need to be in UTC timezones (GMT, UTC, Z, +/-0000) to be
  64. * parsed. Other timezones will be rejected as invalid. Previous implementations
  65. * accepted other timezones, but treated them as UTC.
  66. *
  67. * @param str is the datetime string to parse
  68. * @param[out] time will be the seconds since epoch of the given datetime if
  69. * parsing is successful, undefined otherwise.
  70. * @return \b true if parsing was successful, otherwise \b false.
  71. */
  72. bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  73. bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
  74. APT_DEPRECATED_MSG("Use RFC1123StrToTime or FTPMDTMStrToTime as needed instead") bool StrToTime(const std::string &Val,time_t &Result);
  75. std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
  76. int StringToBool(const std::string &Text,int Default = -1);
  77. bool ReadMessages(int Fd, std::vector<std::string> &List);
  78. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
  79. bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
  80. bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
  81. bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
  82. bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
  83. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  84. APT_HIDDEN bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length);
  85. #endif
  86. // input changing string split
  87. bool TokSplitString(char Tok,char *Input,char **List,
  88. unsigned long ListMax);
  89. // split a given string by a char
  90. std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
  91. /* \brief Return a vector of strings from string "input" where "sep"
  92. * is used as the delimiter string.
  93. *
  94. * \param input The input string.
  95. *
  96. * \param sep The separator to use.
  97. *
  98. * \param maxsplit (optional) The maximum amount of splitting that
  99. * should be done .
  100. *
  101. * The optional "maxsplit" argument can be used to limit the splitting,
  102. * if used the string is only split on maxsplit places and the last
  103. * item in the vector contains the remainder string.
  104. */
  105. std::vector<std::string> StringSplit(std::string const &input,
  106. std::string const &sep,
  107. unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST;
  108. void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
  109. void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
  110. char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
  111. bool CheckDomainList(const std::string &Host, const std::string &List);
  112. /* Do some compat mumbo jumbo */
  113. #define tolower_ascii tolower_ascii_inline
  114. #define isspace_ascii isspace_ascii_inline
  115. APT_CONST APT_HOT
  116. static inline int tolower_ascii_inline(int const c)
  117. {
  118. return (c >= 'A' && c <= 'Z') ? c + 32 : c;
  119. }
  120. APT_CONST APT_HOT
  121. static inline int isspace_ascii_inline(int const c)
  122. {
  123. // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' '
  124. return (c >= 9 && c <= 13) || c == ' ';
  125. }
  126. std::string StripEpoch(const std::string &VerStr);
  127. #define APT_MKSTRCMP(name,func) \
  128. inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
  129. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  130. 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));} \
  131. 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());} \
  132. 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);}
  133. #define APT_MKSTRCMP2(name,func) \
  134. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  135. inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
  136. inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
  137. inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
  138. int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  139. int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  140. /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
  141. case the definition of string::const_iterator is not the same as
  142. const char * and we need these extra functions */
  143. #if __GNUC__ >= 3
  144. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  145. const char *B,const char *BEnd);
  146. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  147. std::string::const_iterator B,std::string::const_iterator BEnd);
  148. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  149. const char *B,const char *BEnd);
  150. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  151. std::string::const_iterator B,std::string::const_iterator BEnd);
  152. 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));}
  153. 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));}
  154. #endif
  155. APT_MKSTRCMP2(stringcmp,stringcmp)
  156. APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
  157. // Return the length of a NULL-terminated string array
  158. size_t APT_PURE strv_length(const char **str_array);
  159. inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
  160. class URI
  161. {
  162. void CopyFrom(const std::string &From);
  163. public:
  164. std::string Access;
  165. std::string User;
  166. std::string Password;
  167. std::string Host;
  168. std::string Path;
  169. unsigned int Port;
  170. operator std::string();
  171. inline void operator =(const std::string &From) {CopyFrom(From);}
  172. inline bool empty() {return Access.empty();};
  173. static std::string SiteOnly(const std::string &URI);
  174. static std::string ArchiveOnly(const std::string &URI);
  175. static std::string NoUserPassword(const std::string &URI);
  176. URI(std::string Path) {CopyFrom(Path);}
  177. URI() : Port(0) {}
  178. };
  179. struct SubstVar
  180. {
  181. const char *Subst;
  182. const std::string *Contents;
  183. };
  184. std::string SubstVar(std::string Str,const struct SubstVar *Vars);
  185. std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
  186. struct RxChoiceList
  187. {
  188. void *UserData;
  189. const char *Str;
  190. bool Hit;
  191. };
  192. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  193. const char **ListEnd);
  194. #endif