~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/util_xt.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 *
 
19
 * 2004-01-03   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 */
 
23
 
 
24
#include "xt_config.h"
 
25
 
 
26
#include <stdio.h>
 
27
#include <time.h>
 
28
#include <ctype.h>
 
29
#ifndef XT_WIN
 
30
#include <sys/param.h>
 
31
#endif
 
32
 
 
33
#include "util_xt.h"
 
34
#include "strutil_xt.h"
 
35
#include "memory_xt.h"
 
36
 
 
37
xtPublic int xt_comp_log_pos(xtLogID id1, off_t off1, xtLogID id2, off_t off2)
 
38
{
 
39
        if (id1 < id2)
 
40
                return -1;
 
41
        if (id1 > id2)
 
42
                return 1;
 
43
        if (off1 < off2)
 
44
                return -1;
 
45
        if (off1 > off2)
 
46
                return 1;
 
47
        return 0;
 
48
}
 
49
 
 
50
/*
 
51
 * This function returns the current time in micorsonds since
 
52
 * 00:00:00 UTC, January 1, 1970.
 
53
 * Currently it is accurate to the second :(
 
54
 */
 
55
xtPublic xtWord8 xt_time_now(void)
 
56
{
 
57
        xtWord8 ms;
 
58
 
 
59
        ms = (xtWord8) time(NULL);
 
60
        ms *= 1000000;
 
61
        return ms;
 
62
}
 
63
 
 
64
xtPublic void xt_free_nothing(struct XTThread *XT_UNUSED(thread), void *XT_UNUSED(x))
 
65
{
 
66
}
 
67
 
 
68
/*
 
69
 * A file name has the form:
 
70
 * <text>-<number>[.<ext>]
 
71
 * This function return the number part as a
 
72
 * u_long.
 
73
 */
 
74
xtPublic xtWord4 xt_file_name_to_id(char *file_name)
 
75
{
 
76
        u_long value = 0;
 
77
 
 
78
        if (file_name) {
 
79
                char    *num = file_name +  strlen(file_name) - 1;
 
80
                
 
81
                while (num >= file_name && *num != '-')
 
82
                        num--;
 
83
                num++;
 
84
                if (isdigit(*num))
 
85
                        sscanf(num, "%lu", &value);
 
86
        }
 
87
        return (xtWord4) value;
 
88
}
 
89
 
 
90
/*
 
91
 * now is moving forward. then is a static time in the
 
92
 * future. What is the time difference?
 
93
 *
 
94
 * These variables can overflow.
 
95
 */ 
 
96
xtPublic int xt_time_difference(register xtWord4 now, register xtWord4 then)
 
97
{
 
98
        /* now is after then, so the now time has passed 
 
99
         * then. So we return a negative difference.
 
100
         */
 
101
        if (now >= then) {
 
102
                /* now has gone past then. If the difference is
 
103
                 * great, then we assume an overflow, and reverse!
 
104
                 */
 
105
                if ((now - then) > (xtWord4) 0xFFFFFFFF/2)
 
106
                        return (int) (0xFFFFFFFF - (now - then));
 
107
 
 
108
                return (int) now - (int) then;
 
109
        }
 
110
        /* If now is before then, we check the difference.
 
111
         * If the difference is very large, then we assume
 
112
         * that now has gone past then, and overflowed.
 
113
         */
 
114
        if ((then - now) > (xtWord4) 0xFFFFFFFF/2)
 
115
                return - (int) (0xFFFFFFFF - (then - now));
 
116
        return then - now;
 
117
}
 
118
 
 
119
xtPublic xtWord2 xt_get_checksum(xtWord1 *data, size_t len, u_int interval)
 
120
{
 
121
        register xtWord4        sum = 0, g;
 
122
        xtWord1                         *chk;
 
123
 
 
124
        chk = data + len - 1;
 
125
        while (chk > data) {
 
126
                sum = (sum << 4) + *chk;
 
127
                if ((g = sum & 0xF0000000)) {
 
128
                        sum = sum ^ (g >> 24);
 
129
                        sum = sum ^ g;
 
130
                }
 
131
                chk -= interval;
 
132
        }
 
133
        return (xtWord2) (sum ^ (sum >> 16));
 
134
}
 
135
 
 
136
xtPublic xtWord1 xt_get_checksum1(xtWord1 *data, size_t len)
 
137
{
 
138
        register xtWord4        sum = 0, g;
 
139
        xtWord1                         *chk;
 
140
 
 
141
        chk = data + len - 1;
 
142
        while (chk > data) {
 
143
                sum = (sum << 4) + *chk;
 
144
                if ((g = sum & 0xF0000000)) {
 
145
                        sum = sum ^ (g >> 24);
 
146
                        sum = sum ^ g;
 
147
                }
 
148
                chk--;
 
149
        }
 
150
        return (xtWord1) (sum ^ (sum >> 24) ^ (sum >> 16) ^ (sum >> 8));
 
151
}
 
152
 
 
153
xtPublic xtWord4 xt_get_checksum4(xtWord1 *data, size_t len)
 
154
{
 
155
        register xtWord4        sum = 0, g;
 
156
        xtWord1                         *chk;
 
157
 
 
158
        chk = data + len - 1;
 
159
        while (chk > data) {
 
160
                sum = (sum << 4) + *chk;
 
161
                if ((g = sum & 0xF0000000)) {
 
162
                        sum = sum ^ (g >> 24);
 
163
                        sum = sum ^ g;
 
164
                }
 
165
                chk--;
 
166
        }
 
167
        return sum;
 
168
}
 
169
 
 
170
/*
 
171
 * --------------- Data Buffer ------------------
 
172
 */
 
173
 
 
174
xtPublic xtBool xt_db_set_size(struct XTThread *self, XTDataBufferPtr dbuf, size_t size)
 
175
{
 
176
        if (dbuf->db_size < size) {
 
177
                if (!xt_realloc(self, (void **) &dbuf->db_data, size))
 
178
                        return FAILED;
 
179
                dbuf->db_size = size;
 
180
        }
 
181
        else if (!size) {
 
182
                if (dbuf->db_data)
 
183
                        xt_free(self, dbuf->db_data);
 
184
                dbuf->db_data = NULL;
 
185
                dbuf->db_size = 0;
 
186
        }
 
187
        return OK;
 
188
}
 
189
 
 
190
/*
 
191
 * --------------- Data Buffer ------------------
 
192
 */
 
193
 
 
194
xtPublic xtBool xt_ib_alloc(struct XTThread *self, XTInfoBufferPtr ib, size_t size)
 
195
{
 
196
        if (!ib->ib_free) {
 
197
                ib->ib_db.db_size = 0;
 
198
                ib->ib_db.db_data = NULL;
 
199
        }
 
200
        if (size <= ib->ib_db.db_size)
 
201
                return OK;
 
202
 
 
203
        if (size <= XT_IB_DEFAULT_SIZE) {
 
204
                ib->ib_db.db_size = XT_IB_DEFAULT_SIZE;
 
205
                ib->ib_db.db_data = ib->ib_data;
 
206
                return OK;
 
207
        }
 
208
 
 
209
        if (ib->ib_db.db_data == ib->ib_data) {
 
210
                ib->ib_db.db_size = 0;
 
211
                ib->ib_db.db_data = NULL;
 
212
        }
 
213
 
 
214
        ib->ib_free = TRUE;
 
215
        return xt_db_set_size(self, &ib->ib_db, size);
 
216
}
 
217
 
 
218
void xt_ib_free(struct XTThread *self, XTInfoBufferPtr ib)
 
219
{
 
220
        if (ib->ib_free) {
 
221
                xt_db_set_size(self, &ib->ib_db, 0);
 
222
                ib->ib_free = FALSE;
 
223
        }
 
224
}
 
225
 
 
226
/*
 
227
 * --------------- Basic List ------------------
 
228
 */
 
229
 
 
230
xtPublic xtBool xt_bl_set_size(struct XTThread *self, XTBasicListPtr bl, size_t size)
 
231
{
 
232
        if (bl->bl_size < size) {
 
233
                if (!xt_realloc(self, (void **) &bl->bl_data, size * bl->bl_item_size))
 
234
                        return FAILED;
 
235
                bl->bl_size = size;
 
236
        }
 
237
        else if (!size) {
 
238
                if (bl->bl_data)
 
239
                        xt_free(self, bl->bl_data);
 
240
                bl->bl_data = NULL;
 
241
                bl->bl_size = 0;
 
242
                bl->bl_count = 0;
 
243
        }
 
244
        return OK;
 
245
}
 
246
 
 
247
xtPublic xtBool xt_bl_dup(struct XTThread *self, XTBasicListPtr from_bl, XTBasicListPtr to_bl)
 
248
{
 
249
        to_bl->bl_item_size = from_bl->bl_item_size;
 
250
        to_bl->bl_size = 0;
 
251
        to_bl->bl_count = from_bl->bl_count;
 
252
        to_bl->bl_data = NULL;
 
253
        if (!xt_bl_set_size(self, to_bl, from_bl->bl_count))
 
254
                return FAILED;
 
255
        memcpy(to_bl->bl_data, from_bl->bl_data, to_bl->bl_count * to_bl->bl_item_size);
 
256
        return OK;
 
257
}
 
258
 
 
259
xtPublic xtBool xt_bl_append(struct XTThread *self, XTBasicListPtr bl, void *value)
 
260
{
 
261
        if (bl->bl_count == bl->bl_size) {
 
262
                if (!xt_bl_set_size(self, bl, bl->bl_count+1))
 
263
                        return FAILED;
 
264
        }
 
265
        memcpy(&bl->bl_data[bl->bl_count * bl->bl_item_size], value, bl->bl_item_size);
 
266
        bl->bl_count++;
 
267
        return OK;
 
268
}
 
269
 
 
270
xtPublic void *xt_bl_last_item(XTBasicListPtr bl)
 
271
{
 
272
        if (!bl->bl_count)
 
273
                return NULL;
 
274
        return &bl->bl_data[(bl->bl_count-1) * bl->bl_item_size];
 
275
}
 
276
 
 
277
xtPublic void *xt_bl_item_at(XTBasicListPtr bl, u_int i)
 
278
{
 
279
        if (i >= bl->bl_count)
 
280
                return NULL;
 
281
        return &bl->bl_data[i * bl->bl_item_size];
 
282
}
 
283
 
 
284
xtPublic void xt_bl_free(struct XTThread *self, XTBasicListPtr wl)
 
285
{
 
286
        xt_bl_set_size(self, wl, 0);
 
287
}
 
288
 
 
289
/*
 
290
 * --------------- Basic Queue ------------------
 
291
 */
 
292
 
 
293
xtPublic xtBool xt_bq_set_size(struct XTThread *self, XTBasicQueuePtr bq, size_t size)
 
294
{
 
295
        if (bq->bq_size < size) {
 
296
                if (!xt_realloc(self, (void **) &bq->bq_data, size * bq->bq_item_size))
 
297
                        return FAILED;
 
298
                bq->bq_size = size;
 
299
        }
 
300
        else if (!size) {
 
301
                if (bq->bq_data)
 
302
                        xt_free(self, bq->bq_data);
 
303
                bq->bq_data = NULL;
 
304
                bq->bq_size = 0;
 
305
                bq->bq_front = 0;
 
306
                bq->bq_back = 0;
 
307
        }
 
308
        return OK;
 
309
}
 
310
 
 
311
xtPublic void *xt_bq_get(XTBasicQueuePtr bq)
 
312
{
 
313
        if (bq->bq_back == bq->bq_front)
 
314
                return NULL;
 
315
        return &bq->bq_data[bq->bq_back * bq->bq_item_size];
 
316
}
 
317
 
 
318
xtPublic void xt_bq_next(XTBasicQueuePtr bq)
 
319
{
 
320
        if (bq->bq_back < bq->bq_front) {
 
321
                bq->bq_back++;
 
322
                if (bq->bq_front == bq->bq_back) {
 
323
                        bq->bq_front = 0;
 
324
                        bq->bq_back = 0;
 
325
                }
 
326
        }
 
327
}
 
328
 
 
329
xtPublic xtBool xt_bq_add(struct XTThread *self, XTBasicQueuePtr bq, void *value)
 
330
{
 
331
        if (bq->bq_front == bq->bq_size) {
 
332
                if (bq->bq_back >= bq->bq_max_waste) {
 
333
                        bq->bq_front -= bq->bq_back;
 
334
                        memmove(bq->bq_data, &bq->bq_data[bq->bq_back * bq->bq_item_size], bq->bq_front * bq->bq_item_size);
 
335
                        bq->bq_back = 0;
 
336
                }
 
337
                else {
 
338
                        if (!xt_bq_set_size(self, bq, bq->bq_front+bq->bq_item_inc))
 
339
                                return FAILED;
 
340
                }
 
341
        }
 
342
        memcpy(&bq->bq_data[bq->bq_front * bq->bq_item_size], value, bq->bq_item_size);
 
343
        bq->bq_front++;
 
344
        return OK;
 
345
}
 
346
 
 
347
xtPublic void xt_sb_free(struct XTThread *self, XTStringBufferPtr dbuf)
 
348
{
 
349
        xt_sb_set_size(self, dbuf, 0);
 
350
}
 
351
 
 
352
xtPublic xtBool xt_sb_set_size(struct XTThread *self, XTStringBufferPtr dbuf, size_t size)
 
353
{
 
354
        if (dbuf->sb_size < size) {
 
355
                if (!xt_realloc(self, (void **) &dbuf->sb_cstring, size))
 
356
                        return FAILED;
 
357
                dbuf->sb_size = size;
 
358
        }
 
359
        else if (!size) {
 
360
                if (dbuf->sb_cstring)
 
361
                        xt_free(self, dbuf->sb_cstring);
 
362
                dbuf->sb_cstring = NULL;
 
363
                dbuf->sb_size = 0;
 
364
                dbuf->sb_len = 0;
 
365
        }
 
366
        return OK;
 
367
}
 
368
 
 
369
xtPublic xtBool xt_sb_concat_len(struct XTThread *self, XTStringBufferPtr dbuf, c_char *str, size_t len)
 
370
{
 
371
        if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + len + 1))
 
372
                return FAILED;
 
373
        memcpy(dbuf->sb_cstring + dbuf->sb_len, str, len);
 
374
        dbuf->sb_len += len;
 
375
        dbuf->sb_cstring[dbuf->sb_len] = 0;
 
376
        return OK;
 
377
}
 
378
 
 
379
xtPublic xtBool xt_sb_concat(struct XTThread *self, XTStringBufferPtr dbuf, c_char *str)
 
380
{
 
381
        return xt_sb_concat_len(self, dbuf, str, strlen(str));
 
382
}
 
383
 
 
384
xtPublic xtBool xt_sb_concat_char(struct XTThread *self, XTStringBufferPtr dbuf, int ch)
 
385
{
 
386
        if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + 1 + 1))
 
387
                return FAILED;
 
388
        dbuf->sb_cstring[dbuf->sb_len] = (char) ch;
 
389
        dbuf->sb_len++;
 
390
        dbuf->sb_cstring[dbuf->sb_len] = 0;
 
391
        return OK;
 
392
}
 
393
 
 
394
xtPublic xtBool xt_sb_concat_int8(struct XTThread *self, XTStringBufferPtr dbuf, xtInt8 val)
 
395
{
 
396
        char buffer[200];
 
397
 
 
398
        sprintf(buffer, "%"PRId64, val);
 
399
        return xt_sb_concat(self, dbuf, buffer);
 
400
}
 
401
 
 
402
xtPublic char *xt_sb_take_cstring(XTStringBufferPtr sbuf)
 
403
{
 
404
        char *str = sbuf->sb_cstring;
 
405
        
 
406
        sbuf->sb_cstring = NULL;
 
407
        sbuf->sb_size = 0; 
 
408
        sbuf->sb_len = 0; 
 
409
        return str;
 
410
}
 
411
 
 
412
xtPublic xtBool xt_sb_concat_url_len(struct XTThread *self, XTStringBufferPtr dbuf, c_char *from, size_t len_from)
 
413
{
 
414
        if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + len_from + 1))
 
415
                return FAILED;
 
416
        while (len_from--) {
 
417
                if (*from == '%' && len_from >= 2 && isxdigit(*(from+1)) && isxdigit(*(from+2))) {
 
418
                        unsigned char a = xt_hex_digit(*(from+1));
 
419
                        unsigned char b = xt_hex_digit(*(from+2));
 
420
                        dbuf->sb_cstring[dbuf->sb_len] = a << 4 | b;
 
421
                        from += 3;
 
422
                }
 
423
                else
 
424
                        dbuf->sb_cstring[dbuf->sb_len] = *from++;
 
425
                dbuf->sb_len++;
 
426
        }
 
427
        dbuf->sb_cstring[dbuf->sb_len] = 0;
 
428
        return OK;
 
429
}
 
430
 
 
431