1 #ifdef PLATFORM_WIN32
2 #ifndef _MAX_PATH
3 static const int _MAX_PATH = MAX_PATH;
4 #endif
5 #endif
6 
7 #ifdef PLATFORM_POSIX
8 static const int _MAX_PATH = PATH_MAX;
9 dword  GetTickCount();
10 #endif
11 
12 int64 usecs(int64 prev = 0);
13 int msecs(int prev = 0);
14 
15 class TimeStop : Moveable<TimeStop> {
16 	double starttime;
17 
18 public:
Elapsed()19 	double Elapsed() const           { return usecs() - starttime; }
Seconds()20 	double Seconds() const           { return (double)Elapsed() / 1000000; }
21 	String ToString() const;
22 	void   Reset();
23 
24 	TimeStop();
25 };
26 
27 struct TimeStopper {
28 	const char *name;
29 	TimeStop tm;
30 
TimeStopperTimeStopper31 	TimeStopper(const char *name) : name(name) {}
~TimeStopperTimeStopper32 	~TimeStopper() { RLOG(name << " " << tm); }
33 };
34 
35 #define RTIMESTOP(name) TimeStopper COMBINE(sTmStop, __LINE__)(name);
36 
37 void   SetAssertFailedHook(void (*h)(const char *));
38 
39 void   ReloadIniFile();
40 void   SetIniFile(const char *path = NULL);
41 String GetIniFile();
42 String GetIniKey(const char *id, const String& def);
43 String GetIniKey(const char *id);
44 
45 VectorMap<String, String> GetIniKeys();
46 
47 extern int  ini_version__;
IniChanged__(int version)48 inline bool IniChanged__(int version) { return version != ini_version__; }
49 
50 struct IniString {
51 // "private":
52 	const char   *id;
53 	String      (*def)();
54 	String&     (*ref_fn)();
55 	int           version;
56 
57 // "public:"
58 	operator String();
59 	String   operator=(const String& s);
60 	String   ToString() const;
61 };
62 
63 struct IniInt {
64 // "private":
65 	const char *id;
66 	int       (*def)();
67 	int         version;
68 	int         value;
69 	int         Load();
70 
71 // "public:"
72 	operator    int()             { int h = value; if(IniChanged__(version)) return Load(); return h; }
73 	int         operator=(int b);
74 	String      ToString() const;
75 };
76 
77 struct IniInt64 {
78 // "private":
79 	const char *id;
80 	int64     (*def)();
81 	int         version;
82 	int64       value;
83 
84 // "public:"
85 	operator    int64();
86 	int64       operator=(int64 b);
87 	String      ToString() const;
88 };
89 
90 struct IniDouble {
91 // "private":
92 	const char *id;
93 	double    (*def)();
94 	int         version;
95 	double      value;
96 	double      Load();
97 
98 // "public:"
99 	operator    double()           { double h = value; if(IniChanged__(version)) return Load(); return h; }
100 	double      operator=(double b);
101 	String      ToString() const;
102 };
103 
104 struct IniBool {
105 // "private":
106 	const char *id;
107 	bool      (*def)();
108 	int         version;
109 	bool        value;
110 	bool        Load();
111 
112 // "public:"
113 	operator     bool()            { bool h = value; if(IniChanged__(version)) return Load(); return h; }
114 	bool         operator=(bool b);
115 	String       ToString() const;
116 };
117 
118 void AddIniInfo(const char *id, String (*current)(), String (*def)(), const char *info);
119 
120 struct IniInfo {
121 	String id;
122 	String info;
123 	String (*current)();
124 	String (*def)();
125 };
126 
127 const Array<IniInfo>& GetIniInfo();
128 String GetIniInfoFormatted();
129 String DefaultIniFileContent();
130 String CurrentIniFileContent(bool comment_defaults);
131 
132 #define INI_TYPE(var, def, info, type, decl, ref)\
133 type DefIni_##var() { return def; }\
134 decl var = { #var, DefIni_##var, ref };\
135 String AsStringIniCurrent_##var() { return AsString(var); } \
136 String AsStringIniDefault_##var() { return AsString(DefIni_##var()); } \
137 INITBLOCK { AddIniInfo(#var, AsStringIniCurrent_##var, AsStringIniDefault_##var, info); }
138 
139 #define INI_BOOL(var, def, info)   INI_TYPE(var, def, info, bool, IniBool, 0);
140 #define INI_INT(var, def, info)    INI_TYPE(var, def, info, int, IniInt, 0);
141 #define INI_INT64(var, def, info)  INI_TYPE(var, def, info, int64, IniInt64, 0);
142 #define INI_DOUBLE(var, def, info) INI_TYPE(var, def, info, double, IniDouble, 0);
143 
144 #define INI_STRING(var, def, info) String& DefRef_##var() { static String x; return x; }\
145                                    INI_TYPE(var, def, info, String, IniString, DefRef_##var);
146 
147 VectorMap<String, String> LoadIniStream(Stream &in);
148 VectorMap<String, String> LoadIniFile(const char *filename);
149 
150 String timeFormat(double second);
151 
152 String HexEncode(const byte *s, int count, int sep = INT_MAX, int sepchr = ' ');
153 inline String HexEncode(const char *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexEncode((byte *)s, count, sep, sepchr); }
154 inline String HexEncode(const void *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexEncode((byte *)s, count, sep, sepchr); }
155 String HexEncode(const String& s, int sep = INT_MAX, int sepchr = ' ');
156 
157 String HexDecode(const char *s, const char *lim);
HexDecode(const char * s,int len)158 inline String HexDecode(const char *s, int len) { return HexDecode(s, s + len); }
HexDecode(const String & s)159 inline String HexDecode(const String& s)        { return HexDecode(~s, s.GetCount()); }
160 
161 #ifdef PLATFORM_WINCE
162 WString ToSystemCharset(const String& src);
163 String  FromSystemCharset(const WString& src);
164 #else
165 String  ToSystemCharset(const String& src, int cp);
166 String  ToSystemCharset(const String& src);
167 String  FromWin32Charset(const String& src, int cp);
168 String  FromSystemCharset(const String& src);
169 WString ToSystemCharsetW(const char *src);
170 String  FromSystemCharsetW(const wchar *src);
171 #endif
172 
173 #ifdef PLATFORM_WIN32
174 String FromOEMCharset(const String& src);
175 String GetErrorMessage(dword dwError);
176 #endif
177 
178 #ifdef PLATFORM_POSIX
GetLastError()179 inline int GetLastError() { return errno; }
180 String GetErrorMessage(int errorno);
181 #endif
182 
183 String GetLastErrorMessage();
184 
185 void   BeepInformation();
186 void   BeepExclamation();
187 void   BeepQuestion();
188 void   BeepError();
189 
190 inline
memsetex(void * t,const void * item,int item_size,int count)191 void memsetex(void *t, const void *item, int item_size, int count) {
192 	ASSERT(count >= 0);
193 	byte *q = (byte *)t;
194 	while(count--) {
195 		memcpy8(q, item, item_size);
196 		q += item_size;
197 	}
198 }
199 
200 char *PermanentCopy(const char *s);
201 
202 int MemICmp(const void *dest, const void *src, int count);
203 
204 String NormalizeSpaces(const char *s);
205 String NormalizeSpaces(const char *begin, const char *end);
206 
207 String         CsvString(const String& text);
208 Vector<String> GetCsvLine(Stream& s, int separator, byte charset);
209 
210 String         CompressLog(const char *s);
211 
212 #ifndef PLATFORM_WIN32
213 void Sleep(int msec);
214 #endif
215 
216 template <class T>
Zero(T & obj)217 void Zero(T& obj)
218 {
219 	::memset(&obj, 0, sizeof(obj));
220 }
221 
222 template <class T>
Reconstruct(T & object)223 T& Reconstruct(T& object)
224 {
225 	object.~T();
226 	::new(&object) T;
227 	return object;
228 }
229 
230 template <class T>
Dbl_Unlink(T * x)231 inline void Dbl_Unlink(T *x)
232 {
233 	x->prev->next = x->next; x->next->prev = x->prev;
234 }
235 
236 template <class T>
Dbl_LinkAfter(T * x,T * lnk)237 inline void Dbl_LinkAfter(T *x, T *lnk)
238 {
239 	x->prev = lnk; x->next = lnk->next; x->next->prev = x; lnk->next = x;
240 }
241 
242 template <class T>
Dbl_Self(T * x)243 inline void Dbl_Self(T *x)
244 {
245 	x->prev = x->next = x;
246 }
247 
248 #define ZeroArray(x)       memset((x), 0, sizeof(x))
249 
250 dword   Random();
251 dword   Random(dword n);
252 uint64  Random64();
253 uint64  Random64(uint64 n);
254 void    Random64(uint64 *t, int n);
255 double  Randomf();
256 
257 void  SeedRandom(dword *seed, int len);
258 void  SeedRandom(dword seed);
259 void  SeedRandom();
260 
261 // Math utils
262 
sqr(double a)263 inline double  sqr          (double a)                      { return a * a; }
argsinh(double s)264 inline double  argsinh      (double s)                      { return log(s + sqrt(s * s + 1)); }
argcosh(double c)265 inline double  argcosh      (double c)                      { ASSERT(c >= 1); return log(c + sqrt(c * c - 1)); }
argtanh(double t)266 inline double  argtanh      (double t)                      { ASSERT(fabs(t) < 1); return log((1 + t) / (1 - t)) / 2; }
267 
268 int            iscale(int x, int y, int z);
269 int            iscalefloor(int x, int y, int z);
270 int            iscaleceil(int x, int y, int z);
271 int            idivfloor(int x, int y);
272 int            idivceil(int x, int y);
273 int            itimesfloor(int x, int y);
274 int            itimesceil(int x, int y);
275 
276 int            fround(double x);
277 int            ffloor(double x);
278 int            fceil(double x);
279 
280 int64          fround64(double x);
281 int64          ffloor64(double x);
282 int64          fceil64(double x);
283 
284 String         AsString(double x, int nDigits);
285 double         modulo(double x, double y);
286 
287 int            ilog10       (double d);
288 double         ipow10       (int i);
289 double         normalize    (double d, int& exponent);
290 
291 double         roundr       (double d, int digits);
292 double         floorr       (double d, int digits);
293 double         ceilr        (double d, int digits);
294 
295 int SolveQuadraticEquation(double A, double B, double C, double *r);
296 
297 //BW - use max<double>
298 //inline double fmax(double x, double y)            { return x >= y ? x : y; }
299 //BW - use min<double>
300 //inline double fmin(double x, double y)            { return x <= y ? x : y; }
301 //BW - use minmax<double>
302 //inline double fbind(double l, double x, double h) { return x >= h ? h : x <= l ? l : x; }
303 //BW - use sgn<double>
304 //inline int    fsgn(double x)                      { return x > 0 ? +1 : x < 0 ? -1 : 0; }
305 
306 // Constants rounded for 21 decimals.
307 
308 #ifndef M_E
309 
310 #define M_E         2.71828182845904523536
311 #define M_LOG2E     1.44269504088896340736
312 #define M_LOG10E    0.434294481903251827651
313 #define M_LN2       0.693147180559945309417
314 #define M_LN10      2.30258509299404568402
315 #define M_PI        3.14159265358979323846
316 #define M_PI_2      1.57079632679489661923
317 #define M_PI_4      0.785398163397448309616
318 #define M_1_PI      0.318309886183790671538
319 #define M_2_PI      0.636619772367581343076
320 #define M_1_SQRTPI  0.564189583547756286948
321 #define M_2_SQRTPI  1.12837916709551257390
322 #define M_SQRT2     1.41421356237309504880
323 #define M_SQRT_2    0.707106781186547524401
324 
325 #endif
326 
327 #define M_2PI       (2 * M_PI)
328 
329 // ------
330 
331 //# System dependent !
332 class BitAndPtr {
333 	uintptr_t bap;
334 
335 public:
SetBit(bool b)336 	void  SetBit(bool b)  { bap = (~1 & bap) | (uintptr_t)b; }
SetPtr(void * p)337 	void  SetPtr(void *p) { bap = (1 & bap) | (uintptr_t)p; }
338 
GetBit()339 	bool  GetBit() const  { return bap & 1; }
GetPtr()340 	void *GetPtr() const  { return (void *) (bap & ~1); }
341 
Set0(void * ptr)342 	void  Set0(void *ptr) { bap = (uintptr_t)ptr; }
Set1(void * ptr)343 	void  Set1(void *ptr) { bap = (uintptr_t)ptr | 1; }
344 
BitAndPtr()345 	BitAndPtr()           { bap = 0; }
346 };
347 
348 class AbortExc : public Exc {
349 public:
350 	AbortExc();
351 };
352 
353 // ---------------
354 
355 int  InScListIndex(const char *s, const char *list);
356 bool InScList(const char *s, const char *list);
357 
358 // ------------------- Linux style text settings -------------
359 
360 class TextSettings {
361 	VectorMap< String, VectorMap< String, String > > settings;
362 
363 public:
364 	String Get(const char *group, const char *key) const;
Get(const char * key)365 	String Get(const char *key) const                            { return Get("", key); }
366 	String Get(int groupIndex, const char *key) const;
367 	String Get(int groupIndex, int keyIndex) const;
368 
operator()369 	String operator()(const char *group, const char *key) const  { return Get(group, key); }
operator()370 	String operator()(const char *key) const                     { return Get(key); }
371 
Clear()372 	void Clear()                                                 { settings.Clear(); }
373 	void Load(const char *filename);
374 
GetGroupCount()375 	int GetGroupCount()                                          { return settings.GetCount(); }
GetKeyCount(int group)376 	int GetKeyCount(int group)                                   { return settings[group].GetCount(); }
377 
GetGroupName(int groupIndex)378 	String GetGroupName(int groupIndex)                          { return settings.GetKey(groupIndex); }
GetKey(int groupIndex,int keyIndex)379 	String GetKey(int groupIndex, int keyIndex)                  { return settings[groupIndex].GetKey(keyIndex); }
380 };
381 
382 // ------------------- Advanced streaming --------------------
383 
384 void CheckedSerialize(const Event<Stream&> serialize, Stream& stream, int version = Null);
385 
386 bool Load(Event<Stream&> serialize, Stream& stream, int version = Null);
387 bool Store(Event<Stream&> serialize, Stream& stream, int version = Null);
388 bool LoadFromFile(Event<Stream&> serialize, const char *file = NULL, int version = Null);
389 bool StoreToFile(Event<Stream&> serialize, const char *file = NULL, int version = Null);
390 
391 template <class T>
SerializeTFn(Stream & s,T * x)392 void SerializeTFn(Stream &s, T *x)
393 {
394 	s % *x;
395 }
396 
397 template <class T>
SerializeCb(T & x)398 Event<Stream&> SerializeCb(T& x)
399 {
400 	return callback1(SerializeTFn<T>, &x);
401 }
402 
403 template <class T>
404 bool Load(T& x, Stream& s, int version = Null) {
405 	return Load(SerializeCb(x), s, version);
406 }
407 
408 template <class T>
409 bool Store(T& x, Stream& s, int version = Null) {
410 	return Store(SerializeCb(x), s, version);
411 }
412 
413 template <class T>
414 bool LoadFromFile(T& x, const char *name = NULL, int version = Null) {
415 	return LoadFromFile(SerializeCb(x), name, version);
416 }
417 
418 template <class T>
419 bool StoreToFile(T& x, const char *name = NULL, int version = Null) {
420 	return StoreToFile(SerializeCb(x), name, version);
421 }
422 
423 template <class T>
StoreAsString(T & x)424 String StoreAsString(T& x) {
425 	StringStream ss;
426 	Store(x, ss);
427 	return ss;
428 }
429 
430 template <class T>
LoadFromString(T & x,const String & s)431 bool LoadFromString(T& x, const String& s) {
432 	StringStream ss(s);
433 	return Load(x, ss);
434 }
435 
436 void             RegisterGlobalConfig(const char *name);
437 void             RegisterGlobalSerialize(const char *name, Event<Stream&> WhenSerialize);
438 void             RegisterGlobalConfig(const char *name, Event<>  WhenFlush);
439 
440 String           GetGlobalConfigData(const char *name);
441 void             SetGlobalConfigData(const char *name, const String& data);
442 
443 template <class T>
LoadFromGlobal(T & x,const char * name)444 bool LoadFromGlobal(T& x, const char *name)
445 {
446 	StringStream ss(GetGlobalConfigData(name));
447 	return ss.IsEof() || Load(x, ss);
448 }
449 
450 template <class T>
StoreToGlobal(T & x,const char * name)451 void StoreToGlobal(T& x, const char *name)
452 {
453 	StringStream ss;
454 	Store(x, ss);
455 	SetGlobalConfigData(name, ss);
456 }
457 
458 bool LoadFromGlobal(Event<Stream&> serialize, const char *name);
459 void StoreToGlobal(Event<Stream&> serialize, const char *name);
460 
461 void SerializeGlobalConfigs(Stream& s);
462 
463 #ifdef PLATFORM_WINCE
abort()464 inline void abort() { TerminateProcess(NULL, -1); }
465 #endif
466 
467 template <class T>
HashBySerialize(const T & cont)468 hash_t HashBySerialize(const T& cont)
469 {
470 	TimeStop tm;
471 	xxHashStream xxh;
472 	const_cast<T&>(cont).Serialize(xxh);
473 	return xxh.Finish();
474 }
475 
476 template <class T>
SizeBySerialize(const T & cont)477 size_t SizeBySerialize(const T& cont)
478 {
479 	TimeStop tm;
480 	SizeStream szs;
481 	const_cast<T&>(cont).Serialize(szs);
482 	return (size_t)szs;
483 }
484 
485 template <class T>
IsEqualBySerialize(const T & a,const T & b)486 bool IsEqualBySerialize(const T& a, const T& b)
487 {
488 	StringStream sa, sb;
489 	const_cast<T&>(a).Serialize(sa);
490 	const_cast<T&>(b).Serialize(sb);
491 	return sa.GetResult() == sb.GetResult();
492 }
493 
494 String  Replace(const String& s, const Vector<String>& find, const Vector<String>& replace);
495 String  Replace(const String& s, const VectorMap<String, String>& fr);
496 WString Replace(const WString& s, const Vector<WString>& find, const Vector<WString>& replace);
497 WString Replace(const WString& s, const VectorMap<WString, WString>& fr);
498 
499 bool SpellWordRaw(const WString& wrd, int lang, Vector<String> *withdia = NULL);
500 bool SpellWord(const WString& ws, int lang);
501 bool SpellWord(const wchar *ws, int len, int lang);
502 void SpellerAdd(const WString& w, int lang);
503 bool AllSpellerWords(int lang, Gate<String> iter);
504 Vector<String> SpellerFindCloseWords(int lang, const String& w, int n);
505 
506 String GetP7Signature(const void *data, int length, const String& cert_pem, const String& pkey_pem);
507 String GetP7Signature(const String& data, const String& cert_pem, const String& pkey_pem);
508 
509 // deprecated
510 String HexString(const byte *s, int count, int sep = INT_MAX, int sepchr = ' ');
511 inline String HexString(const char *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexString((byte *)s, count, sep, sepchr); }
512 inline String HexString(const void *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexString((byte *)s, count, sep, sepchr); }
513 String HexString(const String& s, int sep = INT_MAX, int sepchr = ' ');
514 
515 String ScanHexString(const char *s, const char *lim);
ScanHexString(const char * s,int len)516 inline String ScanHexString(const char *s, int len) { return ScanHexString(s, s + len); }
ScanHexString(const String & s)517 inline String ScanHexString(const String& s)        { return ScanHexString(~s, s.GetCount()); }
518 
519 String Garble(const char *s, const char *e);
520 String Garble(const String& s);
521 
522 String Encode64(const String& s);
523 String Decode64(const String& s);
524