|
a |
|
b/src/smallut.h.orig |
|
|
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 <sys/types.h>
|
|
|
22 |
|
|
|
23 |
#include <string>
|
|
|
24 |
#include <vector>
|
|
|
25 |
#include <map>
|
|
|
26 |
#include <set>
|
|
|
27 |
|
|
|
28 |
// Miscellaneous mostly string-oriented small utilities
|
|
|
29 |
// Note that none of the following code knows about utf-8.
|
|
|
30 |
|
|
|
31 |
// Call this before going multithread.
|
|
|
32 |
void smallut_init_mt();
|
|
|
33 |
|
|
|
34 |
#ifndef SMALLUT_DISABLE_MACROS
|
|
|
35 |
#ifndef MIN
|
|
|
36 |
#define MIN(A,B) (((A)<(B)) ? (A) : (B))
|
|
|
37 |
#endif
|
|
|
38 |
#ifndef MAX
|
|
|
39 |
#define MAX(A,B) (((A)>(B)) ? (A) : (B))
|
|
|
40 |
#endif
|
|
|
41 |
#ifndef deleteZ
|
|
|
42 |
#define deleteZ(X) {delete X;X = 0;}
|
|
|
43 |
#endif
|
|
|
44 |
#endif /* SMALLUT_DISABLE_MACROS */
|
|
|
45 |
|
|
|
46 |
// Case-insensitive compare. ASCII ONLY !
|
|
|
47 |
extern int stringicmp(const std::string& s1, const std::string& s2);
|
|
|
48 |
|
|
|
49 |
// For find_if etc.
|
|
|
50 |
struct StringIcmpPred {
|
|
|
51 |
StringIcmpPred(const std::string& s1)
|
|
|
52 |
: m_s1(s1) {
|
|
|
53 |
}
|
|
|
54 |
bool operator()(const std::string& s2) {
|
|
|
55 |
return stringicmp(m_s1, s2) == 0;
|
|
|
56 |
}
|
|
|
57 |
const std::string& m_s1;
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
extern int stringlowercmp(const std::string& alreadylower,
|
|
|
61 |
const std::string& s2);
|
|
|
62 |
extern int stringuppercmp(const std::string& alreadyupper,
|
|
|
63 |
const std::string& s2);
|
|
|
64 |
|
|
|
65 |
extern void stringtolower(std::string& io);
|
|
|
66 |
extern std::string stringtolower(const std::string& io);
|
|
|
67 |
extern void stringtoupper(std::string& io);
|
|
|
68 |
extern std::string stringtoupper(const std::string& io);
|
|
|
69 |
|
|
|
70 |
// Is one string the end part of the other ?
|
|
|
71 |
extern int stringisuffcmp(const std::string& s1, const std::string& s2);
|
|
|
72 |
|
|
|
73 |
// Divine language from locale
|
|
|
74 |
extern std::string localelang();
|
|
|
75 |
// Divine 8bit charset from language
|
|
|
76 |
extern std::string langtocode(const std::string& lang);
|
|
|
77 |
|
|
|
78 |
// Compare charset names, removing the more common spelling variations
|
|
|
79 |
extern bool samecharset(const std::string& cs1, const std::string& cs2);
|
|
|
80 |
|
|
|
81 |
// Parse date interval specifier into pair of y,m,d dates. The format
|
|
|
82 |
// for the time interval is based on a subset of iso 8601 with
|
|
|
83 |
// the addition of open intervals, and removal of all time indications.
|
|
|
84 |
// 'P' is the Period indicator, it's followed by a length in
|
|
|
85 |
// years/months/days (or any subset thereof)
|
|
|
86 |
// Dates: YYYY-MM-DD YYYY-MM YYYY
|
|
|
87 |
// Periods: P[nY][nM][nD] where n is an integer value.
|
|
|
88 |
// At least one of YMD must be specified
|
|
|
89 |
// The separator for the interval is /. Interval examples
|
|
|
90 |
// YYYY/ (from YYYY) YYYY-MM-DD/P3Y (3 years after date) etc.
|
|
|
91 |
// This returns a pair of y,m,d dates.
|
|
|
92 |
struct DateInterval {
|
|
|
93 |
int y1;
|
|
|
94 |
int m1;
|
|
|
95 |
int d1;
|
|
|
96 |
int y2;
|
|
|
97 |
int m2;
|
|
|
98 |
int d2;
|
|
|
99 |
};
|
|
|
100 |
extern bool parsedateinterval(const std::string& s, DateInterval *di);
|
|
|
101 |
extern int monthdays(int mon, int year);
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Parse input string into list of strings.
|
|
|
105 |
*
|
|
|
106 |
* Token delimiter is " \t\n" except inside dquotes. dquote inside
|
|
|
107 |
* dquotes can be escaped with \ etc...
|
|
|
108 |
* Input is handled a byte at a time, things will work as long as
|
|
|
109 |
* space tab etc. have the ascii values and can't appear as part of a
|
|
|
110 |
* multibyte char. utf-8 ok but so are the iso-8859-x and surely
|
|
|
111 |
* others. addseps do have to be single-bytes
|
|
|
112 |
*/
|
|
|
113 |
template <class T> bool stringToStrings(const std::string& s, T& tokens,
|
|
|
114 |
const std::string& addseps = "");
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Inverse operation:
|
|
|
118 |
*/
|
|
|
119 |
template <class T> void stringsToString(const T& tokens, std::string& s);
|
|
|
120 |
template <class T> std::string stringsToString(const T& tokens);
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Strings to CSV string. tokens containing the separator are quoted (")
|
|
|
124 |
* " inside tokens is escaped as "" ([word "quote"] =>["word ""quote"""]
|
|
|
125 |
*/
|
|
|
126 |
template <class T> void stringsToCSV(const T& tokens, std::string& s,
|
|
|
127 |
char sep = ',');
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Split input string. No handling of quoting
|
|
|
131 |
*/
|
|
|
132 |
extern void stringToTokens(const std::string& s,
|
|
|
133 |
std::vector<std::string>& tokens,
|
|
|
134 |
const std::string& delims = " \t",
|
|
|
135 |
bool skipinit = true);
|
|
|
136 |
|
|
|
137 |
/** Convert string to boolean */
|
|
|
138 |
extern bool stringToBool(const std::string& s);
|
|
|
139 |
|
|
|
140 |
/** Remove instances of characters belonging to set (default {space,
|
|
|
141 |
tab}) at beginning and end of input string */
|
|
|
142 |
extern void trimstring(std::string& s, const char *ws = " \t");
|
|
|
143 |
|
|
|
144 |
/** Escape things like < or & by turning them into entities */
|
|
|
145 |
extern std::string escapeHtml(const std::string& in);
|
|
|
146 |
|
|
|
147 |
/** Double-quote and escape to produce C source code string (prog generation) */
|
|
|
148 |
extern std::string makeCString(const std::string& in);
|
|
|
149 |
|
|
|
150 |
/** Replace some chars with spaces (ie: newline chars). */
|
|
|
151 |
extern std::string neutchars(const std::string& str, const std::string& chars);
|
|
|
152 |
extern void neutchars(const std::string& str, std::string& out,
|
|
|
153 |
const std::string& chars);
|
|
|
154 |
|
|
|
155 |
/** Turn string into something that won't be expanded by a shell. In practise
|
|
|
156 |
* quote with double-quotes and escape $`\ */
|
|
|
157 |
extern std::string escapeShell(const std::string& str);
|
|
|
158 |
|
|
|
159 |
/** Truncate a string to a given maxlength, avoiding cutting off midword
|
|
|
160 |
* if reasonably possible. */
|
|
|
161 |
extern std::string truncate_to_word(const std::string& input,
|
|
|
162 |
std::string::size_type maxlen);
|
|
|
163 |
|
|
|
164 |
void ulltodecstr(unsigned long long val, std::string& buf);
|
|
|
165 |
void lltodecstr(long long val, std::string& buf);
|
|
|
166 |
std::string lltodecstr(long long val);
|
|
|
167 |
std::string ulltodecstr(unsigned long long val);
|
|
|
168 |
|
|
|
169 |
/** Convert byte count into unit (KB/MB...) appropriate for display */
|
|
|
170 |
std::string displayableBytes(off_t size);
|
|
|
171 |
|
|
|
172 |
/** Break big string into lines */
|
|
|
173 |
std::string breakIntoLines(const std::string& in, unsigned int ll = 100,
|
|
|
174 |
unsigned int maxlines = 50);
|
|
|
175 |
|
|
|
176 |
/** Small utility to substitute printf-like percents cmds in a string */
|
|
|
177 |
bool pcSubst(const std::string& in, std::string& out,
|
|
|
178 |
const std::map<char, std::string>& subs);
|
|
|
179 |
/** Substitute printf-like percents and also %(key) */
|
|
|
180 |
bool pcSubst(const std::string& in, std::string& out,
|
|
|
181 |
const std::map<std::string, std::string>& subs);
|
|
|
182 |
|
|
|
183 |
/** Append system error message */
|
|
|
184 |
void catstrerror(std::string *reason, const char *what, int _errno);
|
|
|
185 |
|
|
|
186 |
/** Portable timegm. MS C has _mkgmtime, but there is a bug in Gminw which
|
|
|
187 |
* makes it inaccessible */
|
|
|
188 |
struct tm;
|
|
|
189 |
time_t portable_timegm(struct tm *tm);
|
|
|
190 |
|
|
|
191 |
inline void leftzeropad(std::string& s, unsigned len)
|
|
|
192 |
{
|
|
|
193 |
if (s.length() && s.length() < len) {
|
|
|
194 |
s = s.insert(0, len - s.length(), '0');
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
// A class to solve platorm/compiler issues for simple regex
|
|
|
199 |
// matches. Uses the appropriate native lib under the hood.
|
|
|
200 |
// This always uses extended regexp syntax.
|
|
|
201 |
class SimpleRegexp {
|
|
|
202 |
public:
|
|
|
203 |
enum Flags {SRE_NONE = 0, SRE_ICASE = 1, SRE_NOSUB = 2};
|
|
|
204 |
/// @param nmatch must be >= the number of parenthesed subexp in exp
|
|
|
205 |
SimpleRegexp(const std::string& exp, int flags, int nmatch = 0);
|
|
|
206 |
~SimpleRegexp();
|
|
|
207 |
/// Match input against exp, return true if matches
|
|
|
208 |
bool simpleMatch(const std::string& val) const;
|
|
|
209 |
/// After simpleMatch success, get nth submatch, 0 is the whole
|
|
|
210 |
/// match, 1 first parentheses, etc.
|
|
|
211 |
std::string getMatch(const std::string& val, int matchidx) const;
|
|
|
212 |
/// Calls simpleMatch()
|
|
|
213 |
bool operator() (const std::string& val) const;
|
|
|
214 |
/// Check after construction
|
|
|
215 |
bool ok() const;
|
|
|
216 |
|
|
|
217 |
class Internal;
|
|
|
218 |
private:
|
|
|
219 |
Internal *m;
|
|
|
220 |
};
|
|
|
221 |
|
|
|
222 |
/// Utilities for printing names for defined values (Ex: O_RDONLY->"O_RDONLY")
|
|
|
223 |
|
|
|
224 |
/// Entries for the descriptive table
|
|
|
225 |
struct CharFlags {
|
|
|
226 |
unsigned int value; // Flag or value
|
|
|
227 |
const char *yesname;// String to print if flag set or equal
|
|
|
228 |
const char *noname; // String to print if flag not set (unused for values)
|
|
|
229 |
};
|
|
|
230 |
|
|
|
231 |
/// Helper macro for the common case where we want to print the
|
|
|
232 |
/// flag/value defined name
|
|
|
233 |
#define CHARFLAGENTRY(NM) {NM, #NM}
|
|
|
234 |
|
|
|
235 |
/// Translate a bitfield into string description
|
|
|
236 |
extern std::string flagsToString(const std::vector<CharFlags>&,
|
|
|
237 |
unsigned int flags);
|
|
|
238 |
|
|
|
239 |
/// Translate a value into a name
|
|
|
240 |
extern std::string valToString(const std::vector<CharFlags>&, unsigned int val);
|
|
|
241 |
|
|
|
242 |
/// Reverse operation: translate string into bitfield
|
|
|
243 |
extern unsigned int
|
|
|
244 |
stringToFlags(const std::vector<CharFlags>&, const std::string& input,
|
|
|
245 |
const char *sep = "|");
|
|
|
246 |
|
|
|
247 |
#endif /* _SMALLUT_H_INCLUDED_ */
|