My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#ifdef PLATFORM_WIN32
#ifndef _MAX_PATH
static const int _MAX_PATH = MAX_PATH;
#endif
#endif

#ifdef PLATFORM_POSIX
static const int _MAX_PATH = PATH_MAX;
dword GetTickCount();
#endif

int msecs(int from = 0);

class TimeStop : Moveable<TimeStop> {
dword starttime;

public:
dword Elapsed() const { return GetTickCount() - starttime; }
String ToString() const;
void Reset();

TimeStop();
};

void SetAssertFailedHook(void (*h)(const char *));

void SetIniFile(const char *path = NULL);
String GetIniKey(const char *id, const String& def);
String GetIniKey(const char *id);

VectorMap<String, String> LoadIniStream(Stream &in);
VectorMap<String, String> LoadIniFile(const char *filename);

String timeFormat(double second);

String Garble(const char *s, const char *e);
String Garble(const String& s);

String Encode64(const String& s);
String Decode64(const String& s);

String HexString(const byte *s, int count, int sep = INT_MAX, int sepchr = ' ');
inline String HexString(const char *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexString((byte *)s, count, sep); }
inline String HexString(const void *s, int count, int sep = INT_MAX, int sepchr = ' ') { return HexString((byte *)s, count, sep); }
String HexString(const String& s, int sep = INT_MAX, int sepchr = ' ');

String ScanHexString(const char *s, const char *lim);
inline String ScanHexString(const char *s, int len) { return ScanHexString(s, s + len); }
inline String ScanHexString(const String& s) { return ScanHexString(~s, s.GetCount()); }

#ifdef PLATFORM_WINCE
WString ToSystemCharset(const String& src);
String FromSystemCharset(const WString& src);
#else
String ToSystemCharset(const String& src);
String FromSystemCharset(const String& src);
WString ToSystemCharsetW(const char *src);
String FromSystemCharsetW(const wchar *src);
#endif

#ifdef PLATFORM_WIN32
String FromOEMCharset(const String& src);
String GetErrorMessage(dword dwError);
#endif

#ifdef PLATFORM_POSIX
inline int GetLastError() { return errno; }
String GetErrorMessage(int errorno);
#endif

String GetLastErrorMessage();

void BeepInformation();
void BeepExclamation();
void BeepQuestion();

inline
void memsetw(void *t, word value, int count)
{
word *w = (word *)t;
word *lim = w + count;
while(w < lim)
*w++ = value;
}

inline
void memsetd(void *t, dword value, int count)
{
dword *w = (dword *)t;
dword *lim = w + count;
while(w < lim)
*w++ = value;
}

inline
void memsetex(void *t, const void *item, int item_size, int count) {
ASSERT(count >= 0);
byte *q = (byte *)t;
while(count--) {
memcpy(q, item, item_size);
q += item_size;
}
}

char *PermanentCopy(const char *s);

int MemICmp(const void *dest, const void *src, int count);

String NormalizeSpaces(const char *s);
String NormalizeSpaces(const char *begin, const char *end);

String CsvString(const String& text);
Vector<String> GetCsvLine(Stream& s, int separator, byte charset);

#ifndef PLATFORM_WIN32
void Sleep(int msec);
#endif

template <class T>
void Zero(T& obj)
{
::memset(&obj, 0, sizeof(obj));
}

template <class T>
T& Reconstruct(T& object)
{
object.~T();
::new(&object) T;
return object;
}

template <class T>
inline void Dbl_Unlink(T *x)
{
x->prev->next = x->next; x->next->prev = x->prev;
}

template <class T>
inline void Dbl_LinkAfter(T *x, T *lnk)
{
x->prev = lnk; x->next = lnk->next; x->next->prev = x; lnk->next = x;
}

template <class T>
inline void Dbl_Self(T *x)
{
x->prev = x->next = x;
}

#define ZeroArray(x) memset((x), 0, sizeof(x))

dword Random();
dword Random(dword n);
qword Random64();
qword Random64(qword n);
double Randomf();

void SeedRandom(dword *seed,int len);
void SeedRandom(dword seed = 0);

// Math utils

inline double sqr (double a) { return a * a; }
inline double argsinh (double s) { return log(s + sqrt(s * s + 1)); }
inline double argcosh (double c) { ASSERT(c >= 1); return log(c + sqrt(c * c - 1)); }
inline double argtanh (double t) { ASSERT(fabs(t) < 1); return log((1 + t) / (1 - t)) / 2; }

int iscale(int x, int y, int z);
int iscalefloor(int x, int y, int z);
int iscaleceil(int x, int y, int z);
int idivfloor(int x, int y);
int idivceil(int x, int y);
int itimesfloor(int x, int y);
int itimesceil(int x, int y);

int fround(double x);
int ffloor(double x);
int fceil(double x);

int64 fround64(double x);
int64 ffloor64(double x);
int64 fceil64(double x);

String AsString(double x, int nDigits);
double modulo(double x, double y);

int ilog10 (double d);
double ipow10 (int i);
double normalize (double d, int& exponent);

double roundr (double d, int digits);
double floorr (double d, int digits);
double ceilr (double d, int digits);

//BW - use max<double>
//inline double fmax(double x, double y) { return x >= y ? x : y; }
//BW - use min<double>
//inline double fmin(double x, double y) { return x <= y ? x : y; }
//BW - use minmax<double>
//inline double fbind(double l, double x, double h) { return x >= h ? h : x <= l ? l : x; }
//BW - use sgn<double>
//inline int fsgn(double x) { return x > 0 ? +1 : x < 0 ? -1 : 0; }

// Constants rounded for 21 decimals.

#ifndef M_E

#define M_E 2.71828182845904523536
#define M_LOG2E 1.44269504088896340736
#define M_LOG10E 0.434294481903251827651
#define M_LN2 0.693147180559945309417
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.785398163397448309616
#define M_1_PI 0.318309886183790671538
#define M_2_PI 0.636619772367581343076
#define M_1_SQRTPI 0.564189583547756286948
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT_2 0.707106781186547524401

#endif

#define M_2PI (2 * M_PI)

// ------

//# System dependent !
class BitAndPtr {
uintptr_t bap;

public:
void SetBit(bool b) { bap = (~1 & bap) | (uintptr_t)b; }
void SetPtr(void *p) { bap = (1 & bap) | (uintptr_t)p; }

bool GetBit() const { return bap & 1; }
void *GetPtr() const { return (void *) (bap & ~1); }

void Set0(void *ptr) { bap = (uintptr_t)ptr; }
void Set1(void *ptr) { bap = (uintptr_t)ptr | 1; }

BitAndPtr() { bap = 0; }
};

class Exc : public String {
public:
Exc(); // throw exception according to GetLastError()
Exc(const String& desc) : String(desc) {}

// void Show() const;
};

class AbortExc : public Exc {
public:
AbortExc();
};

// --------------

/*
template <class T>
va_list va_ptr(const T& obj)
{
va_list temp;
va_start(temp, obj);
return temp;
}
*/

// ---------------

int InScListIndex(const char *s, const char *list);
bool InScList(const char *s, const char *list);

struct TextTest {
virtual const char *Accept(const char *s) const = 0;
virtual ~TextTest() {}
};

class CharFilterTextTest : public TextTest {
int (*filter)(int);

public:
virtual const char *Accept(const char *s) const;
CharFilterTextTest(int (*filter)(int));
virtual ~CharFilterTextTest();
};

Vector<String> Split(const char *s, const TextTest& delim, bool ignoreempty = true);
Vector<String> Split(const char *s, int (*filter)(int), bool ignoreempty = true);
Vector<String> Split(const char *s, int chr, bool ignoreempty = true);
Vector<String> Split(const char *s, const String& delim, bool ignoreempty = true);
String Join(const Vector<String>& im, const String& delim);

WString Join(const Vector<WString>& im, const WString& delim);

class StringC {
BitAndPtr bap;

bool IsString() const { return bap.GetBit(); }
void Free();

public:
void SetString(const String& s);
void SetCharPtr(const char *s);

bool IsEmpty() const;

operator const char *() const;
operator String() const;

~StringC();
};

// ------------------- Linux style text settings -------------

class TextSettings {
VectorMap< String, VectorMap< String, String > > settings;

public:
String Get(const char *group, const char *key) const;
String Get(const char *key) const { return Get("", key); }
String Get(int groupIndex, const char *key) const;
String Get(int groupIndex, int keyIndex) const;

String operator()(const char *group, const char *key) const { return Get(group, key); }
String operator()(const char *key) const { return Get(key); }

void Clear() { settings.Clear(); }
void Load(const char *filename);

int GetGroupCount() { return settings.GetCount(); }
int GetKeyCount(int group) { return settings[group].GetCount(); }

String GetGroupName(int groupIndex) { return settings.GetKey(groupIndex); }
String GetKey(int groupIndex, int keyIndex) { return settings[groupIndex].GetKey(keyIndex); }
};

// ------------------- Advanced streaming --------------------

bool Load(Callback1<Stream&> serialize, Stream& stream, int version = Null);
bool Store(Callback1<Stream&> serialize, Stream& stream, int version = Null);
bool LoadFromFile(Callback1<Stream&> serialize, const char *file = NULL, int version = Null);
bool StoreToFile(Callback1<Stream&> serialize, const char *file = NULL, int version = Null);

template <class T>
void SerializeTFn(Stream &s, T *x)
{
s % *x;
}

template <class T>
Callback1<Stream&> SerializeCb(T& x)
{
return callback1(SerializeTFn<T>, &x);
}

template <class T>
bool Load(T& x, Stream& s, int version = Null) {
return Load(SerializeCb(x), s, version);
}

template <class T>
bool Store(T& x, Stream& s, int version = Null) {
return Store(SerializeCb(x), s, version);
}

template <class T>
bool LoadFromFile(T& x, const char *name = NULL, int version = Null) {
return LoadFromFile(SerializeCb(x), name, version);
}

template <class T>
bool StoreToFile(T& x, const char *name = NULL, int version = Null) {
return StoreToFile(SerializeCb(x), name, version);
}

template <class T>
String StoreAsString(T& x) {
StringStream ss;
Store(x, ss);
return ss;
}

template <class T>
bool LoadFromString(T& x, const String& s) {
StringStream ss(s);
return Load(x, ss);
}

void RegisterGlobalConfig(const char *name) init_;
void RegisterGlobalConfig(const char *name, Callback WhenFlush) init_;

String GetGlobalConfigData(const char *name);
void SetGlobalConfigData(const char *name, const String& data);

template <class T>
bool LoadFromGlobal(T& x, const char *name)
{
StringStream ss(GetGlobalConfigData(name));
return ss.IsEof() || Load(x, ss);
}

template <class T>
void StoreToGlobal(T& x, const char *name)
{
StringStream ss;
Store(x, ss);
SetGlobalConfigData(name, ss);
}

void SerializeGlobalConfigs(Stream& s);

#ifdef PLATFORM_WINCE
inline void abort() { TerminateProcess(NULL, -1); }
#endif

Change log

r4917 by cxl on May 9, 2012   Diff
Core: Fixed issue with Null in Json
Go to: 
Project members, sign in to write a code review

Older revisions

r4785 by cxl on Apr 15, 2012   Diff
Core: SSL support for Socket,
finishing touches...
r4439 by cxl on Jan 18, 2012   Diff
Core: Some new Random number generator
functions
r3933 by cxl on Sep 30, 2011   Diff
CtrlLib: ArrayCtrl::AsText,
SetClipboard, AsQtf, AsCsv
All revisions of this file

File info

Size: 11249 bytes, 418 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting