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
#ifndef __tweb_util__
#define __tweb_util__

String FormatIP(dword _ip);
String UrlEncode(const String& s, const char *specials);
bool IsSameTextFile(const char *p, const char *q);
String StringSample(const char *s, int limit);
String GetRandomIdent(int length);
String OtpEncode(const String& password, const String& otp_key);
String EncryptString(const String& password, const String& otp_key);
String BinHexEncode(const char *b, const char *e);
inline String BinHexEncode(const String& data) { return BinHexEncode(data.Begin(), data.End()); }
String BinhexEncode(const char *b, const char *e);
inline String BinhexEncode(const String& data) { return BinhexEncode(data.Begin(), data.End()); }
String BinHexDecode(const char *b, const char *e);
inline String BinHexDecode(const String& data) { return BinHexDecode(data.Begin(), data.End()); }
String ASCII85Encode(const byte *p, int length);
inline String ASCII85Encode(const String& s) { return ASCII85Encode(s, s.GetLength()); }
String ASCII85Decode(const byte *p, int length);
inline String ASCII85Decode(const String& s) { return ASCII85Decode(s, s.GetLength()); }
dword AddCRC(dword crc, const byte *data, int count);
inline dword AddCRC(dword crc, const String& s) { return AddCRC(crc, s, s.GetLength()); }
inline dword GetCRC(const byte *data, int count) { return AddCRC(0x80000000, data, count); }
inline dword GetCRC(const String& s) { return AddCRC(0x80000000, s, s.GetLength()); }
int LocateLine(String old_file, int old_line, String new_file);
void AppVersion(const char *ver);
String MD5Digest(const char *text, int length);
inline String MD5Digest(String s) { return MD5Digest(s.Begin(), s.GetLength()); }

#define WID(s) static const String COMBINE(WID_, s)(#s);

class RefBase
{
public:
RefBase()
{
refcount = 0;
#ifdef REF_DEBUG
allocindex = ++nextindex;
#endif//REF_DEBUG
}

RefBase(const RefBase& rb)
{
refcount = 0;
#ifdef REF_DEBUG
allocindex = ++nextindex;
#endif//REF_DEBUG
}

virtual ~RefBase()
{
ASSERT(refcount == 0);
}

void AddRef() const { if(this) AtomicInc(refcount); }
int GetRefCount() const { return AtomicXAdd(refcount, 0); }
void Release() const { if(this && !AtomicDec(refcount)) delete this; }
#ifdef REF_DEBUG
int GetAllocIndex() const { return allocindex; }
#endif//REF_DEBUG

private:
mutable Atomic refcount;
#ifdef REF_DEBUG
int allocindex;
static int nextindex;
#endif//REF_DEBUG

private:
RefBase& operator = (const RefBase& rb) { NEVER(); return *this; }
};

template <class T>
class RefCon : Moveable< RefCon<T> >
{
public:
RefCon(const Nuller& = Null) : t(0) {}
RefCon(const T *t);
RefCon(const RefCon<T>& rp);
~RefCon();

void Clear() { if(t) { t->Release(); t = NULL; } }
bool IsNullInstance() const { return !t; }
dword GetHashValue() const { return UPP::GetHashValue((unsigned)(uintptr_t)t); }

RefCon<T>& operator = (const RefCon<T>& rp);

bool operator ! () const { return !t; }
const T *Get() const { return t; }
const T *operator ~ () const { return t; }
const T *operator -> () const { ASSERT(t); return t; }
const T& operator * () const { ASSERT(t); return *t; }

String ToString() const { return t ? AsString(*t) : String("NULL"); }

friend bool operator == (RefCon<T> a, RefCon<T> b) { return a.t == b.t; }
friend bool operator != (RefCon<T> a, RefCon<T> b) { return a.t != b.t; }

protected:
const T *t;
};

template <class T>
RefCon<T>::RefCon(const T *t)
: t(t)
{
t->AddRef();
#ifdef REF_DEBUG
if(t && t->GetRefCount() == 1)
RefMemStat::App().Add(typeid(*t).name(), t->GetAllocIndex());
#endif//REF_DEBUG
}

template <class T>
RefCon<T>::RefCon(const RefCon<T>& rp)
: t(rp.t)
{ t->AddRef(); }

template <class T>
RefCon<T>::~RefCon()
{
#ifdef REF_DEBUG
if(t && t->GetRefCount() == 1)
RefMemStat::App().Remove(typeid(*t).name(), t->GetAllocIndex());
#endif//REF_DEBUG
t->Release();
}

template <class T>
RefCon<T>& RefCon<T>::operator = (const RefCon<T>& rp)
{
const T *old = t;
t = rp.t;
t->AddRef();
#ifdef REF_DEBUG
if(old && old->GetRefCount() == 1)
RefMemStat::App().Remove(typeid(*old).name(), old->GetAllocIndex());
#endif//REF_DEBUG
old->Release();
return *this;
}

template <class T>
class RefPtr : public RefCon<T>, public Moveable< RefPtr<T> >
{
public:
RefPtr(const Nuller& = Null) {}
RefPtr(T *t) : RefCon<T>(t) {}
RefPtr(const RefPtr<T>& rp) : RefCon<T>(rp) {}

RefPtr<T>& operator = (const RefPtr<T>& rp) { RefCon<T>::operator = (rp); return *this; }

T *Get() const { return const_cast<T *>(this->t); }
T *operator ~ () const { return Get(); }
T *operator -> () const { ASSERT(this->t); return Get(); }
T& operator * () const { ASSERT(this->t); return *Get(); }
};

template <class T>
class RefValueRep : public RawValueRep<T>
{
public:
RefValueRep(T v) : RawValueRep<T>(v) {}

virtual bool IsNull() const { return this->v == 0; }
virtual unsigned GetHashValue() const { return (unsigned)~this->v; }
virtual bool IsEqual(const Value::Void *p);
virtual String AsString() { return !!this->v ? UPP::AsString(*this->v) : String(Null); }

static const RawValueRep<T> *Cast(const Value::Void *p)
{ ASSERT(dynamic_cast<const RawValueRep<T> *>(p)); return (const RawValueRep<T> *)p; }
};

template <class T>
bool RefValueRep<T>::IsEqual(const Value::Void *p)
{
const RawValueRep<T> *cast = dynamic_cast<const RawValueRep<T> *>(p);
return cast && cast->Get() == this->v;
}

template <class T>
class RefPCValue : public Value
{
protected:
typedef RefValueRep<T> Rep;

public:
RefPCValue(T x) : Value(new Rep(x)) {}
static T Get(const Value& v) { return UPP::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); }
static T Extract(const Value& v) { return UPP::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); }
};

template <class T>
inline Value RefConToValue(RefCon<T> p) { return RefPCValue< RefCon<T> >(p); }

template <class T>
inline Value RefPtrToValue(RefPtr<T> p) { return RefPCValue< RefPtr<T> >(p); }

class HttpQuery
{
private:
class Data : public RefBase
{
public:
Data() {}
Data(const Data& d) : map(d.map, 0) {}

VectorMap<String, String> map;
};

bool case_sensitive;

public:
HttpQuery(const Nuller& = Null) : data(Empty()) { case_sensitive = false; }
explicit HttpQuery(String url) { data = Empty(); SetURL(url); case_sensitive = false; }

void CaseSensitive(bool b = true) { case_sensitive = b; }

void Serialize(Stream& stream);

String GetHidden() const;
String GetQuery(bool empty = false) const;
String GetQuery(HttpQuery patch, bool empty = false) const;

void Clear() { data = Empty(); }

bool IsEmpty() const { return data -> map.IsEmpty(); }
bool IsEmpty(String key) const;

int GetCount() const { return data -> map.GetCount(); }
int Find(String key) const { return data -> map.Find(key); }
String GetKey(int i) const { return data -> map.GetKey(i); }
String GetValue(int i) const { return data -> map[i]; }
bool IsInternal(int i) const;

String operator [] (int i) const { return GetValue(i); }
String operator [] (const String& key) const { return GetString(key); }

void Get(const String& key, Ref p1) const;
HttpQuery& SetValue(const String& key, const Value& v);
HttpQuery& Set(const String& key, const String& value);
HttpQuery& SetRaw(const String& key, const String& value);

bool GetBool(const String& key) const;
bool GetBool(const String& key, bool dflt) const;
HttpQuery& SetBool(const String& key, bool b);

int GetInt(const String& key) const;
int GetInt(const String& key, int min, int max, int dflt = 0) const;
HttpQuery& SetInt(const String& key, int i);

double GetDouble(const String& key) const;
double GetDouble(const String& key, double min, double max, double dflt = Null) const;
HttpQuery& SetDouble(const String& key, double f);

String GetString(const String& key) const;
String GetString(const String& key, const String& dflt) const;
HttpQuery& SetString(const String& key, const String& s) { return Set(key, s); }

Date GetDate(const String& key) const;
Date GetDate(const String& key, Date dflt) const { return Nvl(GetDate(key), dflt); }
HttpQuery& SetDate(const String& key, Date d);

Time GetTime(const String& key) const;
Time GetTime(const String& key, Time dflt) const { return Nvl(GetTime(key), dflt); }
HttpQuery& SetTime(const String& key, Time t);

Color GetColor(const String& key) const;
Color GetColor(const String& key, Color dflt) const { return Nvl(GetColor(key), dflt); }
HttpQuery& SetColor(const String& key, Color c);

HttpQuery& Set(HttpQuery query);
HttpQuery& SetURL(const String& url);

HttpQuery& Remove(const String& key);
HttpQuery& Remove(const Vector<String>& keys);
HttpQuery& Remove(const Vector<Id>& keys);

String ToString() const;

private:
static RefPtr<Data> Empty();
void Clone() { if(data -> GetRefCount() > 1) data = new Data(*data); }

private:
RefPtr<Data> data;
};

#endif//__tweb_util__

Change log

r4789 by cxl on Apr 15, 2012   Diff
Core: Improved inet utilities
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...
r4729 by cxl on Mar 30, 2012   Diff
TcpSocket now in Core
r4580 by cxl on Feb 12, 2012   Diff
HttpQuery: CaseSensitive
All revisions of this file

File info

Size: 10602 bytes, 290 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting