libupnpp 0.16.0
A C++ wrapper for the Portable UPnP reference library
smallut.h
1/* Copyright (C) 2006-2022 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#ifdef __MINGW32__
22#define _POSIX_C_SOURCE 200809L
23#endif
24
25#include <cstdint>
26#include <ctime>
27#include <functional>
28#include <map>
29#include <memory>
30#include <string>
31#include <vector>
32
33struct tm;
34
35namespace MedocUtils {
36
37// Miscellaneous mostly string-oriented small utilities
38// Note that none of the following code knows about utf-8.
39
40// Call this before going multithread.
41void smallut_init_mt();
42
43#ifndef SMALLUT_DISABLE_MACROS
44#ifndef MIN
45#define MIN(A,B) (((A)<(B)) ? (A) : (B))
46#endif
47#ifndef MAX
48#define MAX(A,B) (((A)>(B)) ? (A) : (B))
49#endif
50#ifndef deleteZ
51#define deleteZ(X) do {delete X;X = nullptr;} while(0)
52#endif
53#ifndef PRETEND_USE
54#define PRETEND_USE(var) ((void)(var))
55#endif
56#ifndef VERSION_AT_LEAST
57#define VERSION_AT_LEAST(LIBMAJ,LIBMIN,LIBREV,TARGMAJ,TARGMIN,TARGREV) \
58 ((LIBMAJ) > (TARGMAJ) || \
59 ((LIBMAJ) == (TARGMAJ) && \
60 ((LIBMIN) > (TARGMIN) || \
61 ((LIBMIN) == (TARGMIN) && (LIBREV) >= (TARGREV)))))
62#endif
63#endif /* SMALLUT_DISABLE_MACROS */
64
65// Case-insensitive compare. ASCII ONLY !
66extern int stringicmp(const std::string& s1, const std::string& s2);
67
68// For find_if etc.
70 explicit StringIcmpPred(const std::string& s1)
71 : m_s1(s1) {
72 }
73 bool operator()(const std::string& s2) const {
74 return stringicmp(m_s1, s2) == 0;
75 }
76 const std::string& m_s1;
77};
78
79extern int stringlowercmp(const std::string& s1, // already lower
80 const std::string& s2);
81extern int stringuppercmp(const std::string& s1, // already upper
82 const std::string& s2);
83
84extern void stringtolower(std::string& io);
85extern std::string stringtolower(const std::string& io);
86extern void stringtoupper(std::string& io);
87extern std::string stringtoupper(const std::string& io);
88extern bool beginswith(const std::string& b, const std::string& sml);
89extern bool endswith(const std::string& bg, const std::string& sml);
90
91#ifdef _WIN32
92// Conversion between utf-8 and wide char file names.
93bool wchartoutf8(const wchar_t *in, std::string& out, int len = 0);
94std::string wchartoutf8(const wchar_t *in, int len = 0);
95bool utf8towchar(const std::string& in, wchar_t *out, int obytescap);
96std::unique_ptr<wchar_t[]> utf8towchar(const std::string& in);
97#define strcasecmp _stricmp
98#define strncasecmp _strnicmp
99#define localtime_r(a,b) localtime_s(b,a)
100#define strtok_r strtok_s
101#endif // _WIN32
102
122template <class T> bool stringToStrings(const std::string& s, T& tokens,
123 const std::string& addseps = "");
124
128template <class T> void stringsToString(const T& tokens, std::string& s);
129template <class T> std::string stringsToString(const T& tokens);
130
136template <class T> std::string stringsToCSV(const T& tokens, char sep = ',');
137
139template <class T> std::string commonprefix(const T& values, bool aspaths = false);
140
144extern void stringToTokens(const std::string& s,
145 std::vector<std::string>& tokens,
146 const std::string& delims = " \t",
147 bool skipinit = true, bool allowempty = false);
148
150extern void stringSplitString(const std::string& str,
151 std::vector<std::string>& tokens,
152 const std::string& sep);
153
155extern std::string tokensToString(const std::vector<std::string>& tokens,
156 const std::string& sep = " ");
157
159extern bool stringToBool(const std::string& s);
160
163extern std::string& trimstring(std::string& s, const char *ws = " \t");
164extern std::string& rtrimstring(std::string& s, const char *ws = " \t");
165extern std::string& ltrimstring(std::string& s, const char *ws = " \t");
166
168extern std::string escapeHtml(const std::string& in);
169
171extern std::string makeCString(const std::string& in);
172
174extern std::string neutchars(const std::string& str, const std::string& chars, char rep = ' ');
175extern void neutchars(const std::string& str, std::string& out,
176 const std::string& chars, char rep = ' ');
177
180extern std::string escapeShell(const std::string& in);
181
183extern std::string truncate_to_word(const std::string& input, std::string::size_type maxlen);
184
186std::string displayableBytes(int64_t size);
187
189std::string breakIntoLines(const std::string& in, unsigned int ll = 100, unsigned int maxlines = 50);
190
192bool pcSubst(const std::string& in, std::string& out, const std::map<char, std::string>& subs);
194bool pcSubst(
195 const std::string& in, std::string& out, const std::map<std::string, std::string>& subs);
197bool pcSubst(
198 const std::string& i, std::string& o, const std::function<std::string(const std::string&)>&);
199
203public:
204 explicit DirtySmartBuf(size_t sz) : m_buf(new char[sz]) {}
205 ~DirtySmartBuf() { delete [] m_buf; }
206 DirtySmartBuf(const DirtySmartBuf&) = delete;
207 DirtySmartBuf& operator=(const DirtySmartBuf&) = delete;
208 char *buf() { return m_buf; }
209 private:
210 char *m_buf;
211};
212
214void catstrerror(std::string *reason, const char *what, int _errno);
215
217time_t portable_timegm(struct tm *tm);
218
219inline void leftzeropad(std::string &s, unsigned len) {
220 if (!s.empty() && s.length() < len) {
221 s = s.insert(0, len - s.length(), '0');
222 }
223}
224
225// Print binary string in hexa, separate bytes with character separ if not zero
226// (e.g. ac:23:0c:4f:46:fd)
227extern std::string hexprint(const std::string& in, char separ= 0);
228
229#ifndef SMALLUT_NO_REGEX
230// A class to solve platorm/compiler issues for simple regex
231// matches. Uses the appropriate native lib under the hood.
232// This always uses extended regexp syntax.
234public:
235 enum Flags {SRE_NONE = 0, SRE_ICASE = 1, SRE_NOSUB = 2};
237 SimpleRegexp(const std::string& exp, int flags, int nmatch = 0);
239 SimpleRegexp(const SimpleRegexp&) = delete;
240 SimpleRegexp& operator=(const SimpleRegexp&) = delete;
242 bool simpleMatch(const std::string& val) const;
245 std::string getMatch(const std::string& val, int i) const;
247 bool operator() (const std::string& val) const;
248
250 std::string simpleSub(const std::string& input, const std::string& repl);
251
253 bool ok() const;
254
255 class Internal;
256private:
257 std::unique_ptr<Internal> m;
258};
259#endif // SMALLUT_NO_REGEX
260
261inline void copybits(unsigned int& to, unsigned int from, unsigned int mask)
262{
263 to = (to & ~mask) | (from & mask);
264}
265
267
269struct CharFlags {
270 CharFlags(int v, const char *y, const char *n=nullptr)
271 : value(v), yesname(y), noname(n) {}
272 unsigned int value; // Flag or value
273 const char *yesname;// String to print if flag set or equal
274 const char *noname; // String to print if flag not set (unused for values)
275};
276
279#define CHARFLAGENTRY(NM) {NM, #NM}
280
282extern std::string flagsToString(const std::vector<CharFlags>&, unsigned int val);
283
285extern std::string valToString(const std::vector<CharFlags>&, unsigned int val);
286
288extern std::string pc_decode(const std::string&);
289
293bool parseHTTPRanges(const std::string& ranges, std::vector<std::pair<int64_t, int64_t>>& oranges);
294
295void millisleep(int millis);
296
297
298} // End namespace MedocUtils
299
300using namespace MedocUtils;
301
302#endif /* _SMALLUT_H_INCLUDED_ */
Stupid little smart buffer handler avoiding value-initialization when not needed (e....
Definition smallut.h:202
Definition smallut.cpp:1005
Definition smallut.h:233
bool simpleMatch(const std::string &val) const
Match input against exp, return true if matches.
Definition smallut.cpp:1021
bool ok() const
Check after construction.
Definition smallut.cpp:1115
std::string simpleSub(const std::string &input, const std::string &repl)
Replace the first occurrence of regexp.
Definition smallut.cpp:1029
bool operator()(const std::string &val) const
Calls simpleMatch()
Definition smallut.cpp:1120
std::string getMatch(const std::string &val, int i) const
After simpleMatch success, get nth submatch, 0 is the whole match, 1 first parentheses,...
Definition smallut.cpp:1038
Utilities for printing names for defined values (Ex: O_RDONLY->"O_RDONLY")
Definition smallut.h:269
Definition smallut.h:69