You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

144 lines
5.7 KiB

  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 <stdlib.h>
  15. #include <string>
  16. #include <cstring>
  17. #include <vector>
  18. #include <iostream>
  19. #include <time.h>
  20. #include "macros.h"
  21. using std::string;
  22. using std::vector;
  23. using std::ostream;
  24. bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest);
  25. char *_strstrip(char *String);
  26. char *_strtabexpand(char *String,size_t Len);
  27. bool ParseQuoteWord(const char *&String,string &Res);
  28. bool ParseCWord(const char *&String,string &Res);
  29. string QuoteString(const string &Str,const char *Bad);
  30. string DeQuoteString(const string &Str);
  31. string DeQuoteString(string::const_iterator const &begin, string::const_iterator const &end);
  32. string SizeToStr(double Bytes);
  33. string TimeToStr(unsigned long Sec);
  34. string Base64Encode(const string &Str);
  35. string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
  36. string URItoFileName(const string &URI);
  37. string TimeRFC1123(time_t Date);
  38. bool RFC1123StrToTime(const char* const str,time_t &time) __must_check;
  39. bool FTPMDTMStrToTime(const char* const str,time_t &time) __must_check;
  40. __deprecated bool StrToTime(const string &Val,time_t &Result);
  41. string LookupTag(const string &Message,const char *Tag,const char *Default = 0);
  42. int StringToBool(const string &Text,int Default = -1);
  43. bool ReadMessages(int Fd, vector<string> &List);
  44. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
  45. bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length);
  46. bool TokSplitString(char Tok,char *Input,char **List,
  47. unsigned long ListMax);
  48. vector<string> VectorizeString(string const &haystack, char const &split) __attrib_const;
  49. void ioprintf(ostream &out,const char *format,...) __like_printf(2);
  50. void strprintf(string &out,const char *format,...) __like_printf(2);
  51. char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3);
  52. bool CheckDomainList(const string &Host, const string &List);
  53. int tolower_ascii(int const c) __attrib_const __hot;
  54. #define APT_MKSTRCMP(name,func) \
  55. inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \
  56. inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
  57. inline int name(const string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \
  58. inline int name(const string& A,const string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());}; \
  59. inline int name(const string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);};
  60. #define APT_MKSTRCMP2(name,func) \
  61. inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \
  62. inline int name(const string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));}; \
  63. inline int name(const string& A,const string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \
  64. inline int name(const string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);};
  65. int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  66. int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
  67. /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
  68. case the definition of string::const_iterator is not the same as
  69. const char * and we need these extra functions */
  70. #if __GNUC__ >= 3
  71. int stringcmp(string::const_iterator A,string::const_iterator AEnd,
  72. const char *B,const char *BEnd);
  73. int stringcmp(string::const_iterator A,string::const_iterator AEnd,
  74. string::const_iterator B,string::const_iterator BEnd);
  75. int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
  76. const char *B,const char *BEnd);
  77. int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
  78. string::const_iterator B,string::const_iterator BEnd);
  79. inline int stringcmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));};
  80. inline int stringcasecmp(string::const_iterator A,string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));};
  81. #endif
  82. APT_MKSTRCMP2(stringcmp,stringcmp);
  83. APT_MKSTRCMP2(stringcasecmp,stringcasecmp);
  84. inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);};
  85. class URI
  86. {
  87. void CopyFrom(const string &From);
  88. public:
  89. string Access;
  90. string User;
  91. string Password;
  92. string Host;
  93. string Path;
  94. unsigned int Port;
  95. operator string();
  96. inline void operator =(const string &From) {CopyFrom(From);};
  97. inline bool empty() {return Access.empty();};
  98. static string SiteOnly(const string &URI);
  99. static string NoUserPassword(const string &URI);
  100. URI(string Path) {CopyFrom(Path);};
  101. URI() : Port(0) {};
  102. };
  103. struct SubstVar
  104. {
  105. const char *Subst;
  106. const string *Contents;
  107. };
  108. string SubstVar(string Str,const struct SubstVar *Vars);
  109. string SubstVar(const string &Str,const string &Subst,const string &Contents);
  110. struct RxChoiceList
  111. {
  112. void *UserData;
  113. const char *Str;
  114. bool Hit;
  115. };
  116. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  117. const char **ListEnd);
  118. #endif