src/server/util/util.c

changeset 14
b8bf95b39952
parent 1
3c066d52342d
child 24
1a7853a4257e
equal deleted inserted replaced
13:1fdbf4170ef4 14:b8bf95b39952
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5 *
6 * THE BSD LICENSE
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * Neither the name of the nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * util.c: A hodge podge of utility functions and standard functions which
36 * are unavailable on certain systems
37 *
38 * Rob McCool
39 */
40
41 #ifdef XP_UNIX
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include "prthread.h"
48 #endif /* XP_UNIX */
49
50
51 #include "nspr.h"
52 #include "../public/nsapi.h"
53
54 #include "util.h"
55
56 /*
57 NSAPI_PUBLIC int util_getboolean(const char *v, int def) {
58 if(v[0] == 'T' || v[0] == 't') {
59 return 1;
60 }
61 if(v[0] == 'F' || v[0] == 'f') {
62 return 0;
63 }
64 return def;
65 }
66 */
67
68 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
69 if(v[0] == 'T' || v[0] == 't') {
70 return 1;
71 }
72 if(v[0] == 'F' || v[0] == 'f') {
73 return 0;
74 }
75 return def;
76 }
77
78
79 /* ------------------------------ util_itoa ------------------------------- */
80 /*
81 NSAPI_PUBLIC int util_itoa(int i, char *a)
82 {
83 int len = util_i64toa(i, a);
84
85 PR_ASSERT(len < UTIL_ITOA_SIZE);
86
87 return len;
88 }
89 */
90 NSAPI_PUBLIC int INTutil_itoa(int i, char *a) {
91 return INTutil_i64toa(i, a);
92 }
93
94
95 /* ----------------------------- util_i64toa ------------------------------ */
96
97 /*
98 * Assumption: Reversing the digits will be faster in the general case
99 * than doing a log10 or some nasty trick to find the # of digits.
100 */
101
102 NSAPI_PUBLIC int INTutil_i64toa(PRInt64 i, char *a)
103 {
104 register int x, y, p;
105 register char c;
106 int negative;
107
108 negative = 0;
109 if(i < 0) {
110 *a++ = '-';
111 negative = 1;
112 i = -i;
113 }
114 p = 0;
115 while(i > 9) {
116 a[p++] = (i%10) + '0';
117 i /= 10;
118 }
119 a[p++] = i + '0';
120
121 if(p > 1) {
122 for(x = 0, y = p - 1; x < y; ++x, --y) {
123 c = a[x];
124 a[x] = a[y];
125 a[y] = c;
126 }
127 }
128 a[p] = '\0';
129
130 //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE);
131
132 return p + negative;
133 }

mercurial