strutl.h 7.5 KB

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