ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ZNCString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZNCSTRING_H
18 #define ZNCSTRING_H
19 
20 #include <znc/zncconfig.h>
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <vector>
25 #include <sstream>
26 #include <sys/types.h>
27 
28 #define _SQL(s) CString("'" + CString(s).Escape_n(CString::ESQL) + "'")
29 #define _URL(s) CString(s).Escape_n(CString::EURL)
30 #define _HTML(s) CString(s).Escape_n(CString::EHTML)
31 #define _NAMEDFMT(s) CString(s).Escape_n(CString::ENAMEDFMT)
32 
33 class CString;
34 class MCString;
35 
36 typedef std::set<CString> SCString;
37 typedef std::vector<CString> VCString;
38 typedef std::vector<std::pair<CString, CString> > VPair;
39 
40 static const unsigned char XX = 0xff;
41 static const unsigned char base64_table[256] = {
42  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
43  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
44  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63,
45  52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX,
46  XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
47  15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX,
48  XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
49  41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX,
50  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
51  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
52  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
53  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
54  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
55  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
56  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
57  XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
58 };
59 
67 class CString : public std::string {
68 public:
69  typedef enum {
76  } EEscape;
77 
78  explicit CString(bool b) : std::string(b ? "true" : "false") {}
79  explicit CString(char c);
80  explicit CString(unsigned char c);
81  explicit CString(short i);
82  explicit CString(unsigned short i);
83  explicit CString(int i);
84  explicit CString(unsigned int i);
85  explicit CString(long i);
86  explicit CString(unsigned long i);
87  explicit CString(long long i);
88  explicit CString(unsigned long long i);
89  explicit CString(double i, int precision = 2);
90  explicit CString(float i, int precision = 2);
91 
92  CString() : std::string() {}
93  CString(const char* c) : std::string(c) {}
94  CString(const char* c, size_t l) : std::string(c, l) {}
95  CString(const std::string& s) : std::string(s) {}
96  CString(size_t n, char c) : std::string(n, c) {}
97  ~CString() {}
98 
105  template <typename T> bool Convert(T *target) const
106  {
107  std::stringstream ss(*this);
108  ss >> *target;
109  return (bool) ss; // we don't care why it failed, only whether it failed
110  }
111 
119  template <typename Iterator> CString Join(Iterator i_start, const Iterator &i_end) const
120  {
121  if (i_start == i_end) return CString("");
122  std::ostringstream output;
123  output << *i_start;
124  while (true)
125  {
126  ++i_start;
127  if (i_start == i_end) return CString(output.str());
128  output << *this;
129  output << *i_start;
130  }
131  }
132 
140  int CaseCmp(const CString& s, CString::size_type uLen = CString::npos) const;
148  int StrCmp(const CString& s, CString::size_type uLen = CString::npos) const;
157  bool Equals(const CString& s, bool bCaseSensitive = false, CString::size_type uLen = CString::npos) const;
166  static bool WildCmp(const CString& sWild, const CString& sString);
172  bool WildCmp(const CString& sWild) const;
173 
178  CString& MakeUpper();
183  CString& MakeLower();
189  CString AsUpper() const;
195  CString AsLower() const;
196 
197  static EEscape ToEscape(const CString& sEsc);
198  CString Escape_n(EEscape eFrom, EEscape eTo) const;
199  CString Escape_n(EEscape eTo) const;
200  CString& Escape(EEscape eFrom, EEscape eTo);
201  CString& Escape(EEscape eTo);
202 
223  static unsigned int Replace(CString& sStr, const CString& sReplace, const CString& sWith, const CString& sLeft = "", const CString& sRight = "", bool bRemoveDelims = false);
224 
235  CString Replace_n(const CString& sReplace, const CString& sWith, const CString& sLeft = "", const CString& sRight = "", bool bRemoveDelims = false) const;
245  unsigned int Replace(const CString& sReplace, const CString& sWith, const CString& sLeft = "", const CString& sRight = "", bool bRemoveDelims = false);
252  CString Ellipsize(unsigned int uLen) const;
257  CString Left(size_type uCount) const;
262  CString Right(size_type uCount) const;
263 
267  CString FirstLine() const { return Token(0, false, "\n"); }
268 
284  CString Token(size_t uPos, bool bRest = false, const CString& sSep = " ", bool bAllowEmpty = false) const;
285 
290  CString Token(size_t uPos, bool bRest, const CString& sSep, bool bAllowEmpty, const CString& sLeft, const CString& sRight, bool bTrimQuotes = true) const;
291 
292  size_type URLSplit(MCString& msRet) const;
293  size_type OptionSplit(MCString& msRet, bool bUpperKeys = false) const;
294  size_type QuoteSplit(VCString& vsRet) const;
295 
311  size_type Split(const CString& sDelim, VCString& vsRet, bool bAllowEmpty = true,
312  const CString& sLeft = "", const CString& sRight = "", bool bTrimQuotes = true,
313  bool bTrimWhiteSpace = false) const;
314 
319  size_type Split(const CString& sDelim, SCString& ssRet, bool bAllowEmpty = true,
320  const CString& sLeft = "", const CString& sRight = "", bool bTrimQuotes = true,
321  bool bTrimWhiteSpace = false) const;
322 
331  static CString NamedFormat(const CString& sFormat, const MCString& msValues);
332 
337  static CString RandomString(unsigned int uLength);
338 
340  CString MD5() const;
342  CString SHA256() const;
343 
348  unsigned long Base64Decode(CString& sRet) const;
353  unsigned long Base64Decode();
357  CString Base64Decode_n() const;
366  bool Base64Encode(CString& sRet, unsigned int uWrap = 0) const;
371  bool Base64Encode(unsigned int uWrap = 0);
376  CString Base64Encode_n(unsigned int uWrap = 0) const;
377 
378 #ifdef HAVE_LIBSSL
379  CString Encrypt_n(const CString& sPass, const CString& sIvec = "") const;
380  CString Decrypt_n(const CString& sPass, const CString& sIvec = "") const;
381  void Encrypt(const CString& sPass, const CString& sIvec = "");
382  void Decrypt(const CString& sPass, const CString& sIvec = "");
383  void Crypt(const CString& sPass, bool bEncrypt, const CString& sIvec = "");
384 #endif
385 
390  static CString ToPercent(double d);
395  static CString ToByteStr(unsigned long long d);
400  static CString ToTimeStr(unsigned long s);
401 
403  bool ToBool() const;
405  short ToShort() const;
407  unsigned short ToUShort() const;
409  int ToInt() const;
411  long ToLong() const;
413  unsigned int ToUInt() const;
415  unsigned long ToULong() const;
417  unsigned long long ToULongLong() const;
419  long long ToLongLong() const;
421  double ToDouble() const;
422 
428  bool Trim(const CString& s = " \t\r\n");
434  bool TrimLeft(const CString& s = " \t\r\n");
440  bool TrimRight(const CString& s = " \t\r\n");
446  CString Trim_n(const CString& s = " \t\r\n") const;
452  CString TrimLeft_n(const CString& s = " \t\r\n") const;
458  CString TrimRight_n(const CString& s = " \t\r\n") const;
459 
464  bool TrimPrefix(const CString& sPrefix = ":");
469  bool TrimSuffix(const CString& sSuffix);
474  CString TrimPrefix_n(const CString& sPrefix = ":") const;
479  CString TrimSuffix_n(const CString& sSuffix) const;
480 
485  bool StartsWith(const CString& sPrefix) const;
490  bool EndsWith(const CString& sSuffix) const;
491 
496  bool LeftChomp(size_type uLen = 1);
501  bool RightChomp(size_type uLen = 1);
507  CString LeftChomp_n(size_type uLen = 1) const;
513  CString RightChomp_n(size_type uLen = 1) const;
526  CString StripControls_n() const;
527 
528 private:
529 protected:
530  unsigned char* strnchr(const unsigned char* src, unsigned char c, unsigned int iMaxBytes, unsigned char* pFill = NULL, unsigned int* piCount = NULL) const;
531 };
532 
538 class MCString : public std::map<CString, CString> {
539 public:
541  MCString() : std::map<CString, CString>() {}
543  virtual ~MCString() { clear(); }
544 
546  static const MCString EmptyMap;
547 
550  enum status_t
551  {
562  };
563 
570  enum status_t WriteToDisk(const CString& sPath, mode_t iMode = 0644) const;
576  enum status_t ReadFromDisk(const CString& sPath);
577 
585  virtual bool WriteFilter(CString& sKey, CString& sValue) const { return true; }
593  virtual bool ReadFilter(CString& sKey, CString& sValue) const { return true; }
594 
598  virtual CString& Encode(CString& sValue) const;
600  virtual CString& Decode(CString& sValue) const;
601 };
602 
603 #endif // !ZNCSTRING_H
virtual bool ReadFilter(CString &sKey, CString &sValue) const
Filter used while reading this map.
Definition: ZNCString.h:593
unsigned long long ToULongLong() const
void Crypt(const CString &sPass, bool bEncrypt, const CString &sIvec="")
size_type URLSplit(MCString &msRet) const
bool Equals(const CString &s, bool bCaseSensitive=false, CString::size_type uLen=CString::npos) const
Check if this string is equal to some other string.
CString AsLower() const
Return a copy of this string with all characters turned into lower-case.
unsigned char * strnchr(const unsigned char *src, unsigned char c, unsigned int iMaxBytes, unsigned char *pFill=NULL, unsigned int *piCount=NULL) const
bool LeftChomp(size_type uLen=1)
Remove characters from the beginning of this string.
CString TrimSuffix_n(const CString &sSuffix) const
Trim a given suffix.
CString Token(size_t uPos, bool bRest=false, const CString &sSep=" ", bool bAllowEmpty=false) const
Get a token out of this string.
CString(const char *c, size_t l)
Definition: ZNCString.h:94
long ToLong() const
Writing to the file failed.
Definition: ZNCString.h:557
virtual CString & Encode(CString &sValue) const
Encode a value so that it can safely be parsed by ReadFromDisk().
void Decrypt(const CString &sPass, const CString &sIvec="")
Definition: ZNCString.h:74
CString()
Definition: ZNCString.h:92
bool TrimSuffix(const CString &sSuffix)
Trim a given suffix.
CString MD5() const
CString & MakeUpper()
Turn all characters in this string into their upper-case equivalent.
status_t
Status codes that can be returned by WriteToDisk() and ReadFromDisk().
Definition: ZNCString.h:550
CString(bool b)
Definition: ZNCString.h:78
~CString()
Definition: ZNCString.h:97
size_type Split(const CString &sDelim, VCString &vsRet, bool bAllowEmpty=true, const CString &sLeft="", const CString &sRight="", bool bTrimQuotes=true, bool bTrimWhiteSpace=false) const
Split up this string into tokens.
CString Base64Encode_n(unsigned int uWrap=0) const
Base64-encode the current string.
CString AsUpper() const
Return a copy of this string with all characters turned into upper-case.
Definition: ZNCString.h:72
CString TrimLeft_n(const CString &s=" \t\r\n") const
Trim this string.
static unsigned int Replace(CString &sStr, const CString &sReplace, const CString &sWith, const CString &sLeft="", const CString &sRight="", bool bRemoveDelims=false)
Replace all occurrences in a string.
bool TrimRight(const CString &s=" \t\r\n")
Trim this string.
ReadFilter() failed.
Definition: ZNCString.h:561
CString RightChomp_n(size_type uLen=1) const
Remove characters from the end of this string.
long long ToLongLong() const
enum status_t WriteToDisk(const CString &sPath, mode_t iMode=0644) const
Write this map to a file.
CString(const std::string &s)
Definition: ZNCString.h:95
CString TrimRight_n(const CString &s=" \t\r\n") const
Trim this string.
CString Join(Iterator i_start, const Iterator &i_end) const
Joins a collection of objects together, using &#39;this&#39; as a delimiter.
Definition: ZNCString.h:119
std::set< CString > SCString
Definition: ZNCString.h:34
Definition: ZNCString.h:75
Definition: ZNCString.h:70
MCString()
Construct an empty MCString.
Definition: ZNCString.h:541
unsigned long ToULong() const
CString StripControls_n() const
Remove controls characters from this string.
EEscape
Definition: ZNCString.h:69
Definition: ZNCString.h:73
bool TrimLeft(const CString &s=" \t\r\n")
Trim this string.
CString FirstLine() const
Get the first line of this string.
Definition: ZNCString.h:267
double ToDouble() const
Opening the file failed.
Definition: ZNCString.h:555
std::vector< CString > VCString
Definition: ZNCString.h:37
No errors.
Definition: ZNCString.h:553
CString Decrypt_n(const CString &sPass, const CString &sIvec="") const
String class that is used inside ZNC.
Definition: ZNCString.h:67
static CString ToPercent(double d)
Pretty-print a percent value.
WriteFilter() failed.
Definition: ZNCString.h:559
int ToInt() const
bool Base64Encode(CString &sRet, unsigned int uWrap=0) const
Base64-encode the current string.
bool StartsWith(const CString &sPrefix) const
Check whether the string starts with a given prefix.
static EEscape ToEscape(const CString &sEsc)
Definition: ZNCString.h:71
bool RightChomp(size_type uLen=1)
Remove characters from the end of this string.
bool Trim(const CString &s=" \t\r\n")
Trim this string.
static bool WildCmp(const CString &sWild, const CString &sString)
Do a wildcard comparision between two strings.
unsigned long Base64Decode()
Treat this string as base64-encoded data and decode it.
static const MCString EmptyMap
A static instance of an empty map.
Definition: ZNCString.h:546
enum status_t ReadFromDisk(const CString &sPath)
Read a map from a file.
CString Left(size_type uCount) const
Return the left part of the string.
unsigned int ToUInt() const
void Encrypt(const CString &sPass, const CString &sIvec="")
int StrCmp(const CString &s, CString::size_type uLen=CString::npos) const
Compare this string case sensitively to some other string.
bool ToBool() const
size_type OptionSplit(MCString &msRet, bool bUpperKeys=false) const
A dictionary for strings.
Definition: ZNCString.h:538
CString & Escape(EEscape eFrom, EEscape eTo)
size_type QuoteSplit(VCString &vsRet) const
CString Trim_n(const CString &s=" \t\r\n") const
Trim this string.
static CString ToByteStr(unsigned long long d)
Pretty-print a number of bytes.
bool EndsWith(const CString &sSuffix) const
Check whether the string ends with a given suffix.
CString Escape_n(EEscape eFrom, EEscape eTo) const
static CString RandomString(unsigned int uLength)
Produces a random string.
CString & MakeLower()
Turn all characters in this string into their lower-case equivalent.
std::vector< std::pair< CString, CString > > VPair
Definition: ZNCString.h:38
CString SHA256() const
unsigned short ToUShort() const
CString(const char *c)
Definition: ZNCString.h:93
int CaseCmp(const CString &s, CString::size_type uLen=CString::npos) const
Compare this string caselessly to some other string.
CString Encrypt_n(const CString &sPass, const CString &sIvec="") const
virtual ~MCString()
Destruct this MCString.
Definition: ZNCString.h:543
CString LeftChomp_n(size_type uLen=1) const
Remove characters from the beginning of this string.
static CString ToTimeStr(unsigned long s)
Pretty-print a time span.
bool Convert(T *target) const
Casts a CString to another type.
Definition: ZNCString.h:105
virtual CString & Decode(CString &sValue) const
Undo the effects of Encode().
CString & StripControls()
Remove controls characters from this string.
short ToShort() const
static CString NamedFormat(const CString &sFormat, const MCString &msValues)
Build a string from a format string, replacing values from a map.
bool TrimPrefix(const CString &sPrefix=":")
Trim a given prefix.
CString Replace_n(const CString &sReplace, const CString &sWith, const CString &sLeft="", const CString &sRight="", bool bRemoveDelims=false) const
Replace all occurrences in the current string.
CString(size_t n, char c)
Definition: ZNCString.h:96
CString Ellipsize(unsigned int uLen) const
Ellipsize the current string.
CString Base64Decode_n() const
Treat this string as base64-encoded data and decode it.
virtual bool WriteFilter(CString &sKey, CString &sValue) const
Filter used while writing this map.
Definition: ZNCString.h:585
CString Right(size_type uCount) const
Return the right part of the string.
CString TrimPrefix_n(const CString &sPrefix=":") const
Trim a given prefix.