18#ifndef _SMALLUT_H_INCLUDED_
19#define _SMALLUT_H_INCLUDED_
22#define _POSIX_C_SOURCE 200809L
41void smallut_init_mt();
43#ifndef SMALLUT_DISABLE_MACROS
45#define MIN(A,B) (((A)<(B)) ? (A) : (B))
48#define MAX(A,B) (((A)>(B)) ? (A) : (B))
51#define deleteZ(X) do {delete X;X = nullptr;} while(0)
54#define PRETEND_USE(var) ((void)(var))
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)))))
66extern int stringicmp(
const std::string& s1,
const std::string& s2);
73 bool operator()(
const std::string& s2)
const {
74 return stringicmp(m_s1, s2) == 0;
76 const std::string& m_s1;
79extern int stringlowercmp(
const std::string& s1,
80 const std::string& s2);
81extern int stringuppercmp(
const std::string& s1,
82 const std::string& s2);
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);
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
122template <
class T>
bool stringToStrings(
const std::string& s, T& tokens,
123 const std::string& addseps =
"");
128template <
class T>
void stringsToString(
const T& tokens, std::string& s);
129template <
class T> std::string stringsToString(
const T& tokens);
136template <
class T> std::string stringsToCSV(
const T& tokens,
char sep =
',');
139template <
class T> std::string commonprefix(
const T& values,
bool aspaths =
false);
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);
150extern void stringSplitString(
const std::string& str,
151 std::vector<std::string>& tokens,
152 const std::string& sep);
155extern std::string tokensToString(
const std::vector<std::string>& tokens,
156 const std::string& sep =
" ");
159extern bool stringToBool(
const std::string& s);
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");
168extern std::string escapeHtml(
const std::string& in);
171extern std::string makeCString(
const std::string& in);
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 =
' ');
180extern std::string escapeShell(
const std::string& in);
183extern std::string truncate_to_word(
const std::string& input, std::string::size_type maxlen);
186std::string displayableBytes(int64_t size);
189std::string breakIntoLines(
const std::string& in,
unsigned int ll = 100,
unsigned int maxlines = 50);
192bool pcSubst(
const std::string& in, std::string& out,
const std::map<char, std::string>& subs);
195 const std::string& in, std::string& out,
const std::map<std::string, std::string>& subs);
198 const std::string& i, std::string& o,
const std::function<std::string(
const std::string&)>&);
208 char *buf() {
return m_buf; }
214void catstrerror(std::string *reason,
const char *what,
int _errno);
217time_t portable_timegm(
struct tm *tm);
219inline void leftzeropad(std::string &s,
unsigned len) {
220 if (!s.empty() && s.length() < len) {
221 s = s.insert(0, len - s.length(),
'0');
227extern std::string hexprint(
const std::string& in,
char separ= 0);
229#ifndef SMALLUT_NO_REGEX
235 enum Flags {SRE_NONE = 0, SRE_ICASE = 1, SRE_NOSUB = 2};
237 SimpleRegexp(
const std::string& exp,
int flags,
int nmatch = 0);
245 std::string
getMatch(
const std::string& val,
int i)
const;
247 bool operator() (
const std::string& val)
const;
250 std::string
simpleSub(
const std::string& input,
const std::string& repl);
257 std::unique_ptr<Internal> m;
261inline void copybits(
unsigned int& to,
unsigned int from,
unsigned int mask)
263 to = (to & ~mask) | (from & mask);
270 CharFlags(
int v,
const char *y,
const char *n=
nullptr)
271 : value(v), yesname(y), noname(n) {}
279#define CHARFLAGENTRY(NM) {NM, #NM}
282extern std::string flagsToString(
const std::vector<CharFlags>&,
unsigned int val);
285extern std::string valToString(
const std::vector<CharFlags>&,
unsigned int val);
288extern std::string pc_decode(
const std::string&);
293bool parseHTTPRanges(
const std::string& ranges, std::vector<std::pair<int64_t, int64_t>>& oranges);
295void millisleep(
int millis);
300using namespace MedocUtils;
Stupid little smart buffer handler avoiding value-initialization when not needed (e....
Definition smallut.h:202
Definition smallut.cpp:1005
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