OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [rtems_webserver/] [misc.c] - Blame information for rev 493

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * misc.c -- Miscellaneous routines.
3
 *
4
 * Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
5
 *
6
 * See the file "license.txt" for usage and redistribution license requirements
7
 */
8
 
9
/********************************* Includes ***********************************/
10
 
11
#if UEMF
12
        #include        "uemf.h"
13
#else
14
        #include        "basic/basicInternal.h"
15
#endif
16
 
17
/********************************* Defines ************************************/
18
/*
19
 *      Sprintf buffer structure. Make the increment 8 less than 64 so that
20
 *      a balloc can use a 64 byte block.
21
 */
22
 
23
#define STR_REALLOC             0x1                             /* Reallocate the buffer as required */
24
#define STR_INC                 58                              /* Growth increment */
25
 
26
typedef struct {
27
        char_t  *s;                                                     /* Pointer to buffer */
28
        int             size;                                           /* Current buffer size */
29
        int             max;                                            /* Maximum buffer size */
30
        int             count;                                          /* Buffer count */
31
        int             flags;                                          /* Allocation flags */
32
} strbuf_t;
33
 
34
/*
35
 *      Sprintf formatting flags
36
 */
37
enum flag {
38
        flag_none = 0,
39
        flag_minus = 1,
40
        flag_plus = 2,
41
        flag_space = 4,
42
        flag_hash = 8,
43
        flag_zero = 16,
44
        flag_short = 32,
45
        flag_long = 64
46
};
47
 
48
/************************** Forward Declarations ******************************/
49
 
50
static int      dsnprintf(char_t **s, int size, char_t *fmt, va_list arg,
51
                                int msize);
52
static int      strnlen(char_t *s, unsigned int n);
53
static void     put_char(strbuf_t *buf, char_t c);
54
static void     put_string(strbuf_t *buf, char_t *s, int len,
55
                                int width, int prec, enum flag f);
56
static void     put_ulong(strbuf_t *buf, unsigned long int value, int base,
57
                                int upper, char_t *prefix, int width, int prec, enum flag f);
58
 
59
/************************************ Code ************************************/
60
/*
61
 *      "basename" returns a pointer to the last component of a pathname
62
 *  LINUX, RTEMS, and LynxOS have their own basename function
63
 */
64
 
65
#if ! LINUX & ! __rtems__ & ! LYNX
66
char_t *basename(char_t* name)
67
{
68
        char_t  *cp;
69
 
70
#if NW || WIN
71
        if (((cp = gstrrchr(name, '\\')) == NULL) &&
72
                        ((cp = gstrrchr(name, '/')) == NULL)) {
73
                return name;
74
#else
75
        if ((cp = gstrrchr(name, '/')) == NULL) {
76
                return name;
77
#endif
78
        } else if (*(cp + 1) == '\0' && cp == name) {
79
                return name;
80
        } else if (*(cp + 1) == '\0' && cp != name) {
81
                return T("");
82
        } else {
83
                return ++cp;
84
        }
85
}
86
#endif /* ! LINUX && ! __rtems__ && ! LYNX */
87
 
88
/******************************************************************************/
89
/*
90
 *      Returns a pointer to the directory component of a pathname. bufsize is
91
 *      the size of the buffer in BYTES!
92
 */
93
 
94
char_t *dirname(char_t* buf, char_t* name, int bufsize)
95
{
96
        char_t* cp;
97
        int             len;
98
 
99
        a_assert(name);
100
        a_assert(buf);
101
        a_assert(bufsize > 0);
102
 
103
#if WIN || NW
104
        if ((cp = gstrrchr(name, '/')) == NULL &&
105
                (cp = gstrrchr(name, '\\')) == NULL)
106
#else
107
        if ((cp = gstrrchr(name, '/')) == NULL)
108
#endif
109
        {
110
                gstrcpy(buf, T("."));
111
                return buf;
112
        }
113
 
114
        if ((*(cp + 1) == '\0' && cp == name)) {
115
                gstrncpy(buf, T("."), TSZ(bufsize));
116
                gstrcpy(buf, T("."));
117
                return buf;
118
        }
119
 
120
        len = cp - name;
121
 
122
        if (len < bufsize) {
123
                gstrncpy(buf, name, len);
124
                buf[len] = '\0';
125
        } else {
126
                gstrncpy(buf, name, TSZ(bufsize));
127
                buf[bufsize - 1] = '\0';
128
        }
129
 
130
        return buf;
131
}
132
 
133
/******************************************************************************/
134
/*
135
 *      sprintf and vsprintf are bad, ok. You can easily clobber memory. Use
136
 *      gsnprintf and gvsnprintf instead! These functions do _not_ support floating
137
 *      point, like %e, %f, %g...
138
 */
139
 
140
int gsnprintf(char_t **s, int n, char_t *fmt, ...)
141
{
142
        va_list ap;
143
        int             result;
144
 
145
        a_assert(s);
146
        a_assert(fmt);
147
 
148
        *s = NULL;
149
        va_start(ap, fmt);
150
        result = gvsnprintf(s, n, fmt, ap);
151
        va_end(ap);
152
        return result;
153
}
154
 
155
/******************************************************************************/
156
/*
157
 *      This function appends the formatted string to the supplied string,
158
 *      reallocing if required.
159
 */
160
 
161
int gsprintfRealloc(char_t **s, int n, int msize, char_t *fmt, ...)
162
{
163
        va_list ap;
164
        int             result;
165
 
166
        a_assert(s);
167
        a_assert(fmt);
168
 
169
        if (msize == -1) {
170
                *s = NULL;
171
        }
172
        va_start(ap, fmt);
173
        result = dsnprintf(s, n, fmt, ap, msize);
174
        va_end(ap);
175
        return result;
176
}
177
 
178
/******************************************************************************/
179
/*
180
 *      A vsprintf replacement.
181
 */
182
 
183
int gvsnprintf(char_t **s, int n, char_t *fmt, va_list arg)
184
{
185
        a_assert(s);
186
        a_assert(fmt);
187
 
188
        return dsnprintf(s, n, fmt, arg, 0);
189
}
190
 
191
/******************************************************************************/
192
/*
193
 *      Dynamic sprintf implementation. Supports dynamic buffer allocation also.
194
 *      This function can be called multiple times to grow an existing allocated
195
 *      buffer. In this case, msize is set to the size of the previously allocated
196
 *      buffer. The buffer will be realloced, as required. If msize is set, we
197
 *      return the size of the allocated buffer for use with the next call. For
198
 *      the first call, msize can be set to -1.
199
 */
200
 
201
static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg, int msize)
202
{
203
        strbuf_t        buf;
204
        char_t          c;
205
 
206
        a_assert(s);
207
        a_assert(fmt);
208
 
209
        memset(&buf, 0, sizeof(buf));
210
        buf.s = *s;
211
 
212
        if (*s == NULL || msize != 0) {
213
                buf.max = size;
214
                buf.flags |= STR_REALLOC;
215
                if (msize != 0) {
216
                        buf.size = max(msize, 0);
217
                }
218
                if (*s != NULL && msize != 0) {
219
                        buf.count = gstrlen(*s);
220
                }
221
        } else {
222
                buf.size = size;
223
        }
224
 
225
        while ((c = *fmt++) != '\0') {
226
                if (c != '%' || (c = *fmt++) == '%') {
227
                        put_char(&buf, c);
228
                } else {
229
                        enum flag f = flag_none;
230
                        int width = 0;
231
                        int prec = -1;
232
                        for ( ; c != '\0'; c = *fmt++) {
233
                                if (c == '-') {
234
                                        f |= flag_minus;
235
                                } else if (c == '+') {
236
                                        f |= flag_plus;
237
                                } else if (c == ' ') {
238
                                        f |= flag_space;
239
                                } else if (c == '#') {
240
                                        f |= flag_hash;
241
                                } else if (c == '0') {
242
                                        f |= flag_zero;
243
                                } else {
244
                                        break;
245
                                }
246
                        }
247
                        if (c == '*') {
248
                                width = va_arg(arg, int);
249
                                if (width < 0) {
250
                                        f |= flag_minus;
251
                                        width = -width;
252
                                }
253
                                c = *fmt++;
254
                        } else {
255
                                for ( ; gisdigit(c); c = *fmt++) {
256
                                        width = width * 10 + (c - '0');
257
                                }
258
                        }
259
                        if (c == '.') {
260
                                f &= ~flag_zero;
261
                                c = *fmt++;
262
                                if (c == '*') {
263
                                        prec = va_arg(arg, int);
264
                                        c = *fmt++;
265
                                } else {
266
                                        for (prec = 0; gisdigit(c); c = *fmt++) {
267
                                                prec = prec * 10 + (c - '0');
268
                                        }
269
                                }
270
                        }
271
                        if (c == 'h' || c == 'l') {
272
                                f |= (c == 'h' ? flag_short : flag_long);
273
                                c = *fmt++;
274
                        }
275
                        if (c == 'd' || c == 'i') {
276
                                long int value;
277
                                if (f & flag_short) {
278
                                        value = (short int) va_arg(arg, int);
279
                                } else if (f & flag_long) {
280
                                        value = va_arg(arg, long int);
281
                                } else {
282
                                        value = va_arg(arg, int);
283
                                }
284
                                if (value >= 0) {
285
                                        if (f & flag_plus) {
286
                                                put_ulong(&buf, value, 10, 0, T("+"), width, prec, f);
287
                                        } else if (f & flag_space) {
288
                                                put_ulong(&buf, value, 10, 0, T(" "), width, prec, f);
289
                                        } else {
290
                                                put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
291
                                        }
292
                                } else {
293
                                        put_ulong(&buf, -value, 10, 0, T("-"), width, prec, f);
294
                                }
295
                        } else if (c == 'o' || c == 'u' || c == 'x' || c == 'X') {
296
                                unsigned long int value;
297
                                if (f & flag_short) {
298
                                        value = (unsigned short int) va_arg(arg, unsigned int);
299
                                } else if (f & flag_long) {
300
                                        value = va_arg(arg, unsigned long int);
301
                                } else {
302
                                        value = va_arg(arg, unsigned int);
303
                                }
304
                                if (c == 'o') {
305
                                        if (f & flag_hash && value != 0) {
306
                                                put_ulong(&buf, value, 8, 0, T("0"), width, prec, f);
307
                                        } else {
308
                                                put_ulong(&buf, value, 8, 0, NULL, width, prec, f);
309
                                        }
310
                                } else if (c == 'u') {
311
                                        put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
312
                                } else {
313
                                        if (f & flag_hash && value != 0) {
314
                                                if (c == 'x') {
315
                                                        put_ulong(&buf, value, 16, 0, T("0x"), width,
316
                                                                prec, f);
317
                                                } else {
318
                                                        put_ulong(&buf, value, 16, 1, T("0X"), width,
319
                                                                prec, f);
320
                                                }
321
                                        } else {
322
                                                put_ulong(&buf, value, 16, 0, NULL, width, prec, f);
323
                                        }
324
                                }
325
 
326
                        } else if (c == 'c') {
327
                                char_t value = va_arg(arg, int);
328
                                put_char(&buf, value);
329
 
330
                        } else if (c == 's' || c == 'S') {
331
                                char_t *value = va_arg(arg, char_t *);
332
                                if (value == NULL) {
333
                                        put_string(&buf, T("(null)"), -1, width, prec, f);
334
                                } else if (f & flag_hash) {
335
                                        put_string(&buf,
336
                                                value + 1, (char_t) *value, width, prec, f);
337
                                } else {
338
                                        put_string(&buf, value, -1, width, prec, f);
339
                                }
340
                        } else if (c == 'p') {
341
                                void *value = va_arg(arg, void *);
342
                                put_ulong(&buf,
343
                                        (unsigned long int) value, 16, 0, T("0x"), width, prec, f);
344
                        } else if (c == 'n') {
345
                                if (f & flag_short) {
346
                                        short int *value = va_arg(arg, short int *);
347
                                        *value = buf.count;
348
                                } else if (f & flag_long) {
349
                                        long int *value = va_arg(arg, long int *);
350
                                        *value = buf.count;
351
                                } else {
352
                                        int *value = va_arg(arg, int *);
353
                                        *value = buf.count;
354
                                }
355
                        } else {
356
                                put_char(&buf, c);
357
                        }
358
                }
359
        }
360
        if (buf.s == NULL) {
361
                put_char(&buf, '\0');
362
        }
363
 
364
/*
365
 *      If the user requested a dynamic buffer (*s == NULL), ensure it is returned.
366
 */
367
        if (*s == NULL || msize != 0) {
368
                *s = buf.s;
369
        }
370
 
371
        if (*s != NULL && size > 0) {
372
                if (buf.count < size) {
373
                        (*s)[buf.count] = '\0';
374
                } else {
375
                        (*s)[buf.size - 1] = '\0';
376
                }
377
        }
378
 
379
        if (msize != 0) {
380
                return buf.size;
381
        }
382
        return buf.count;
383
}
384
 
385
/******************************************************************************/
386
/*
387
 *      Return the length of a string limited by a given length
388
 */
389
 
390
static int strnlen(char_t *s, unsigned int n)
391
{
392
        unsigned int    len;
393
 
394
        len = gstrlen(s);
395
        return min(len, n);
396
}
397
 
398
/******************************************************************************/
399
/*
400
 *      Add a character to a string buffer
401
 */
402
 
403
static void put_char(strbuf_t *buf, char_t c)
404
{
405
        if (buf->count >= buf->size) {
406
                if (! (buf->flags & STR_REALLOC)) {
407
                        return;
408
                }
409
                buf->size += STR_INC;
410
                if (buf->size > buf->max && buf->size > STR_INC) {
411
                        a_assert(buf->size <= buf->max);
412
                        buf->size -= STR_INC;
413
                        return;
414
                }
415
                if (buf->s == NULL) {
416
                        buf->s = balloc(B_L, buf->size * sizeof(char_t*));
417
                } else {
418
                        buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t*));
419
                }
420
        }
421
        buf->s[buf->count] = c;
422
        ++buf->count;
423
}
424
 
425
/******************************************************************************/
426
/*
427
 *      Add a string to a string buffer
428
 */
429
 
430
static void put_string(strbuf_t *buf, char_t *s, int len, int width,
431
        int prec, enum flag f)
432
{
433
        int             i;
434
 
435
        if (len < 0) {
436
                len = strnlen(s, prec >= 0 ? prec : ULONG_MAX);
437
        } else if (prec >= 0 && prec < len) {
438
                len = prec;
439
        }
440
        if (width > len && !(f & flag_minus)) {
441
                for (i = len; i < width; ++i) {
442
                        put_char(buf, ' ');
443
                }
444
        }
445
        for (i = 0; i < len; ++i) {
446
                put_char(buf, s[i]);
447
        }
448
        if (width > len && f & flag_minus) {
449
                for (i = len; i < width; ++i) {
450
                        put_char(buf, ' ');
451
                }
452
        }
453
}
454
 
455
/******************************************************************************/
456
/*
457
 *      Add a long to a string buffer
458
 */
459
 
460
static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
461
        int upper, char_t *prefix, int width, int prec, enum flag f)
462
{
463
        unsigned long   x, x2;
464
        int                             len, zeros, i;
465
 
466
        for (len = 1, x = 1; x < ULONG_MAX / base; ++len, x = x2) {
467
                x2 = x * base;
468
                if (x2 > value) {
469
                        break;
470
                }
471
        }
472
        zeros = (prec > len) ? prec - len : 0;
473
        width -= zeros + len;
474
        if (prefix != NULL) {
475
                width -= strnlen(prefix, ULONG_MAX);
476
        }
477
        if (!(f & flag_minus)) {
478
                for (i = 0; i < width; ++i) {
479
                        put_char(buf, ' ');
480
                }
481
        }
482
        if (prefix != NULL) {
483
                put_string(buf, prefix, -1, 0, -1, flag_none);
484
        }
485
        for (i = 0; i < zeros; ++i) {
486
                put_char(buf, '0');
487
        }
488
        for ( ; x > 0; x /= base) {
489
                int digit = (value / x) % base;
490
                put_char(buf, (char) ((digit < 10 ? '0' : (upper ? 'A' : 'a') - 10) +
491
                        digit));
492
        }
493
        if (f & flag_minus) {
494
                for (i = 0; i < width; ++i) {
495
                        put_char(buf, ' ');
496
                }
497
        }
498
}
499
 
500
/******************************************************************************/
501
/*
502
 *      Convert an ansi string to a unicode string. On an error, we return the
503
 *      original ansi string which is better than returning NULL. nBytes is the
504
 *      size of the destination buffer (ubuf) in _bytes_.
505
 */
506
 
507
char_t *ascToUni(char_t *ubuf, char *str, int nBytes)
508
{
509
#if UNICODE
510
        if (MultiByteToWideChar(CP_ACP, 0, str, nBytes / sizeof(char_t), ubuf,
511
                        nBytes / sizeof(char_t)) < 0) {
512
                return (char_t*) str;
513
        }
514
#else
515
        memcpy(ubuf, str, nBytes);
516
#endif
517
        return ubuf;
518
}
519
 
520
/******************************************************************************/
521
/*
522
 *      Convert a unicode string to an ansi string. On an error, return the
523
 *      original unicode string which is better than returning NULL.
524
 *      N.B. nBytes is the number of _bytes_ in the destination buffer, buf.
525
 */
526
 
527
char *uniToAsc(char *buf, char_t* ustr, int nBytes)
528
{
529
#if UNICODE
530
        if (WideCharToMultiByte(CP_ACP, 0, ustr, nBytes, buf, nBytes, NULL,
531
                        NULL) < 0) {
532
                return (char*) ustr;
533
        }
534
#else
535
        memcpy(buf, ustr, nBytes);
536
#endif
537
        return (char*) buf;
538
}
539
 
540
 
541
/******************************************************************************/
542
/*
543
 *      allocate (balloc) a buffer and do ascii to unicode conversion into it.
544
 *      cp points to the ascii string which must be NULL terminated.
545
 *      Return a pointer to the unicode buffer which must be bfree'd later.
546
 *      Return NULL on failure to get buffer.
547
 */
548
char_t *ballocAscToUni(char * cp)
549
{
550
        char_t * unip;
551
        int ulen;
552
 
553
        ulen = (strlen(cp) + 1) * sizeof(char_t);
554
        if ((unip = balloc(B_L, ulen)) == NULL) {
555
                return NULL;
556
        }
557
        ascToUni(unip, cp, ulen);
558
        return unip;
559
}
560
 
561
/******************************************************************************/
562
/*
563
 *      allocate (balloc) a buffer and do unicode to ascii conversion into it.
564
 *      unip points to the unicoded string. ulen is the number of characters
565
 *      in the unicode string including teminating null, if there is one.
566
 *      Return a pointer to the ascii buffer which must be bfree'd later.
567
 *      Return NULL on failure to get buffer.
568
 */
569
char *ballocUniToAsc(char_t * unip, int ulen)
570
{
571
        char * cp;
572
 
573
        if ((cp = balloc(B_L, ulen)) == NULL) {
574
                return NULL;
575
        }
576
        uniToAsc(cp, unip, ulen);
577
        return cp;
578
}
579
 
580
/******************************************************************************/
581
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.