strutl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 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_CONST;
  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. int tolower_ascii(int const c) APT_CONST APT_HOT;
  92. std::string StripEpoch(const std::string &VerStr);
  93. #define APT_MKSTRCMP(name,func) \
  94. inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
  95. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  96. 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));} \
  97. 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());} \
  98. 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);}
  99. #define APT_MKSTRCMP2(name,func) \
  100. inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
  101. inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
  102. inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
  103. inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
  104. int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  105. int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  106. /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
  107. case the definition of string::const_iterator is not the same as
  108. const char * and we need these extra functions */
  109. #if __GNUC__ >= 3
  110. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  111. const char *B,const char *BEnd);
  112. int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  113. std::string::const_iterator B,std::string::const_iterator BEnd);
  114. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  115. const char *B,const char *BEnd);
  116. int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
  117. std::string::const_iterator B,std::string::const_iterator BEnd);
  118. 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));}
  119. 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));}
  120. #endif
  121. APT_MKSTRCMP2(stringcmp,stringcmp)
  122. APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
  123. // Return the length of a NULL-terminated string array
  124. size_t APT_PURE strv_length(const char **str_array);
  125. inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
  126. class URI
  127. {
  128. void CopyFrom(const std::string &From);
  129. public:
  130. std::string Access;
  131. std::string User;
  132. std::string Password;
  133. std::string Host;
  134. std::string Path;
  135. unsigned int Port;
  136. operator std::string();
  137. inline void operator =(const std::string &From) {CopyFrom(From);}
  138. inline bool empty() {return Access.empty();};
  139. static std::string SiteOnly(const std::string &URI);
  140. static std::string NoUserPassword(const std::string &URI);
  141. URI(std::string Path) {CopyFrom(Path);}
  142. URI() : Port(0) {}
  143. };
  144. struct SubstVar
  145. {
  146. const char *Subst;
  147. const std::string *Contents;
  148. };
  149. std::string SubstVar(std::string Str,const struct SubstVar *Vars);
  150. std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
  151. struct RxChoiceList
  152. {
  153. void *UserData;
  154. const char *Str;
  155. bool Hit;
  156. };
  157. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  158. const char **ListEnd);
  159. #endif