libupnpp  0.16.0
A C++ wrapper for the Portable UPnP reference library
smallut.h
1 /* Copyright (C) 2006-2016 J.F.Dockes
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301 USA
17  */
18 #ifndef _SMALLUT_H_INCLUDED_
19 #define _SMALLUT_H_INCLUDED_
20 
21 #include <stdint.h>
22 #include <time.h>
23 #include <string>
24 #include <vector>
25 #include <map>
26 
27 // Miscellaneous mostly string-oriented small utilities
28 // Note that none of the following code knows about utf-8.
29 
30 // Call this before going multithread.
31 void smallut_init_mt();
32 
33 #ifndef SMALLUT_DISABLE_MACROS
34 #ifndef MIN
35 #define MIN(A,B) (((A)<(B)) ? (A) : (B))
36 #endif
37 #ifndef MAX
38 #define MAX(A,B) (((A)>(B)) ? (A) : (B))
39 #endif
40 #ifndef deleteZ
41 #define deleteZ(X) {delete X;X = 0;}
42 #endif
43 #ifndef PRETEND_USE
44 #define PRETEND_USE(var) ((void)(var))
45 #endif
46 #endif /* SMALLUT_DISABLE_MACROS */
47 
48 // Case-insensitive compare. ASCII ONLY !
49 extern int stringicmp(const std::string& s1, const std::string& s2);
50 
51 // For find_if etc.
53  StringIcmpPred(const std::string& s1)
54  : m_s1(s1) {
55  }
56  bool operator()(const std::string& s2) {
57  return stringicmp(m_s1, s2) == 0;
58  }
59  const std::string& m_s1;
60 };
61 
62 extern int stringlowercmp(const std::string& s1, // already lower
63  const std::string& s2);
64 extern int stringuppercmp(const std::string& s1, // already upper
65  const std::string& s2);
66 
67 extern void stringtolower(std::string& io);
68 extern std::string stringtolower(const std::string& io);
69 extern void stringtoupper(std::string& io);
70 extern std::string stringtoupper(const std::string& io);
71 extern bool beginswith(const std::string& bg, const std::string& sml);
72 
73 // Parse date interval specifier into pair of y,m,d dates. The format
74 // for the time interval is based on a subset of iso 8601 with
75 // the addition of open intervals, and removal of all time indications.
76 // 'P' is the Period indicator, it's followed by a length in
77 // years/months/days (or any subset thereof)
78 // Dates: YYYY-MM-DD YYYY-MM YYYY
79 // Periods: P[nY][nM][nD] where n is an integer value.
80 // At least one of YMD must be specified
81 // The separator for the interval is /. Interval examples
82 // YYYY/ (from YYYY) YYYY-MM-DD/P3Y (3 years after date) etc.
83 // This returns a pair of y,m,d dates.
84 struct DateInterval {
85  int y1;
86  int m1;
87  int d1;
88  int y2;
89  int m2;
90  int d2;
91 };
92 extern bool parsedateinterval(const std::string& s, DateInterval *di);
93 extern int monthdays(int mon, int year);
94 
95 
115 template <class T> bool stringToStrings(const std::string& s, T& tokens,
116  const std::string& addseps = "");
117 
121 template <class T> void stringsToString(const T& tokens, std::string& s);
122 template <class T> std::string stringsToString(const T& tokens);
123 
129 template <class T> void stringsToCSV(const T& tokens, std::string& s,
130  char sep = ',');
131 
135 extern void stringToTokens(const std::string& s,
136  std::vector<std::string>& tokens,
137  const std::string& delims = " \t",
138  bool skipinit = true);
139 
141 extern void stringSplitString(const std::string& str,
142  std::vector<std::string>& tokens,
143  const std::string& sep);
144 
146 extern bool stringToBool(const std::string& s);
147 
150 extern void trimstring(std::string& s, const char *ws = " \t");
151 extern void rtrimstring(std::string& s, const char *ws = " \t");
152 extern void ltrimstring(std::string& s, const char *ws = " \t");
153 
155 extern std::string escapeHtml(const std::string& in);
156 
158 extern std::string makeCString(const std::string& in);
159 
161 extern std::string neutchars(const std::string& str, const std::string& chars,
162  char rep = ' ');
163 extern void neutchars(const std::string& str, std::string& out,
164  const std::string& chars, char rep = ' ');
165 
168 extern std::string escapeShell(const std::string& in);
169 
172 extern std::string truncate_to_word(const std::string& input,
173  std::string::size_type maxlen);
174 
175 void ulltodecstr(uint64_t val, std::string& buf);
176 void lltodecstr(int64_t val, std::string& buf);
177 std::string lltodecstr(int64_t val);
178 std::string ulltodecstr(uint64_t val);
179 
181 std::string displayableBytes(int64_t size);
182 
184 std::string breakIntoLines(const std::string& in, unsigned int ll = 100,
185  unsigned int maxlines = 50);
186 
188 bool pcSubst(const std::string& in, std::string& out,
189  const std::map<char, std::string>& subs);
191 bool pcSubst(const std::string& in, std::string& out,
192  const std::map<std::string, std::string>& subs);
193 
195 void catstrerror(std::string *reason, const char *what, int _errno);
196 
199 struct tm;
200 time_t portable_timegm(struct tm *tm);
201 
202 inline void leftzeropad(std::string& s, unsigned len)
203 {
204  if (s.length() && s.length() < len) {
205  s = s.insert(0, len - s.length(), '0');
206  }
207 }
208 
209 // Print binary string in hexa, separate bytes with character separ if not zero
210 // (e.g. ac:23:0c:4f:46:fd)
211 extern std::string hexprint(const std::string& in, char separ= 0);
212 
213 #ifndef SMALLUT_NO_REGEX
214 // A class to solve platorm/compiler issues for simple regex
215 // matches. Uses the appropriate native lib under the hood.
216 // This always uses extended regexp syntax.
218 public:
219  enum Flags {SRE_NONE = 0, SRE_ICASE = 1, SRE_NOSUB = 2};
221  SimpleRegexp(const std::string& exp, int flags, int nmatch = 0);
222  ~SimpleRegexp();
224  bool simpleMatch(const std::string& val) const;
227  std::string getMatch(const std::string& val, int i) const;
229  bool operator() (const std::string& val) const;
230 
232  std::string simpleSub(const std::string& input, const std::string& repl);
233 
235  bool ok() const;
236 
237 
238  class Internal;
239 private:
240  Internal *m;
241 };
242 #endif // SMALLUT_NO_REGEX
243 
245 
247 struct CharFlags {
248  CharFlags(int v, const char *y, const char *n=nullptr)
249  : value(v), yesname(y), noname(n) {}
250  unsigned int value; // Flag or value
251  const char *yesname;// String to print if flag set or equal
252  const char *noname; // String to print if flag not set (unused for values)
253 };
254 
257 #define CHARFLAGENTRY(NM) {NM, #NM}
258 
260 extern std::string flagsToString(const std::vector<CharFlags>&,
261  unsigned int val);
262 
264 extern std::string valToString(const std::vector<CharFlags>&, unsigned int val);
265 
266 #endif /* _SMALLUT_H_INCLUDED_ */
Definition: smallut.h:217
Definition: smallut.cpp:1207
Definition: smallut.h:52
Utilities for printing names for defined values (Ex: O_RDONLY->"O_RDONLY")
Definition: smallut.h:247
Definition: smallut.h:84