| 1 |
/* |
| 2 |
* ustring.c: Unicode string routines |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "halibut.h" |
| 6 |
#include <wchar.h> |
| 7 |
#include <time.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
static void asciitou(wchar_t *dest, const char *src) |
| 11 |
{ |
| 12 |
for (;;) |
| 13 |
if (!(*dest++ = *src++)) |
| 14 |
break; |
| 15 |
} |
| 16 |
|
| 17 |
wchar_t *ustrdup(const wchar_t * s) |
| 18 |
{ |
| 19 |
wchar_t *r; |
| 20 |
if (s) |
| 21 |
{ |
| 22 |
r = mknewa(wchar_t, 1 + ustrlen(s)); |
| 23 |
ustrcpy(r, s); |
| 24 |
} else |
| 25 |
{ |
| 26 |
r = mknew(wchar_t); |
| 27 |
*r = 0; |
| 28 |
} |
| 29 |
return r; |
| 30 |
} |
| 31 |
|
| 32 |
wchar_t *ustrreplacedup(wchar_t **dest, const wchar_t *src) |
| 33 |
{ |
| 34 |
sfree(*dest); |
| 35 |
return *dest = ustrdup(src); |
| 36 |
} |
| 37 |
|
| 38 |
char *ustrtoa(const wchar_t * s, char *outbuf, int size) |
| 39 |
{ |
| 40 |
char *p; |
| 41 |
if (!s) |
| 42 |
{ |
| 43 |
*outbuf = '\0'; |
| 44 |
return outbuf; |
| 45 |
} |
| 46 |
for (p = outbuf; *s && p < outbuf + size; p++, s++) |
| 47 |
*p = *(char*)s; |
| 48 |
if (p < outbuf + size) |
| 49 |
*p = '\0'; |
| 50 |
else |
| 51 |
outbuf[size - 1] = '\0'; |
| 52 |
return outbuf; |
| 53 |
} |
| 54 |
|
| 55 |
int ustrlen(const wchar_t * s) |
| 56 |
{ |
| 57 |
int len = 0; |
| 58 |
while (*s++) |
| 59 |
len++; |
| 60 |
return len; |
| 61 |
} |
| 62 |
|
| 63 |
wchar_t *uadv(const wchar_t * s) |
| 64 |
{ |
| 65 |
return ((wchar_t*) s) + 1 + ustrlen(s); |
| 66 |
} |
| 67 |
|
| 68 |
wchar_t *ustrcpy(wchar_t * dest, const wchar_t * source) |
| 69 |
{ |
| 70 |
wchar_t *ret = dest; |
| 71 |
do |
| 72 |
{ |
| 73 |
*dest++ = *source; |
| 74 |
} |
| 75 |
while (*source++); |
| 76 |
return ret; |
| 77 |
} |
| 78 |
|
| 79 |
int ustrcmp(const wchar_t * lhs, const wchar_t * rhs) |
| 80 |
{ |
| 81 |
if (!lhs && !rhs) |
| 82 |
return 0; |
| 83 |
if (!lhs) |
| 84 |
return -1; |
| 85 |
if (!rhs) |
| 86 |
return +1; |
| 87 |
while (*lhs && *rhs && *lhs == *rhs) |
| 88 |
lhs++, rhs++; |
| 89 |
if (*lhs < *rhs) |
| 90 |
return -1; |
| 91 |
else if (*lhs > *rhs) |
| 92 |
return 1; |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
wchar_t utolower(wchar_t c) |
| 97 |
{ |
| 98 |
if (c == L'\0') |
| 99 |
return c; /* this property needed by ustricmp */ |
| 100 |
/* FIXME: this doesn't even come close */ |
| 101 |
if (c >= 'A' && c <= 'Z') |
| 102 |
c += 'a' - 'A'; |
| 103 |
return c; |
| 104 |
} |
| 105 |
|
| 106 |
int ustricmp(const wchar_t * lhs, const wchar_t * rhs) |
| 107 |
{ |
| 108 |
wchar_t lc, rc; |
| 109 |
while ((lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc) |
| 110 |
lhs++, rhs++; |
| 111 |
if (!lc && !rc) |
| 112 |
return 0; |
| 113 |
if (lc < rc) |
| 114 |
return -1; |
| 115 |
else |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
wchar_t *ustrlow(wchar_t * s) |
| 120 |
{ |
| 121 |
wchar_t *p = s; |
| 122 |
while (*p) |
| 123 |
{ |
| 124 |
*p = utolower(*p); |
| 125 |
p++; |
| 126 |
} |
| 127 |
return s; |
| 128 |
} |
| 129 |
|
| 130 |
void ultou(unsigned long v, wchar_t *o) |
| 131 |
{ |
| 132 |
char buf[42]; |
| 133 |
sprintf(buf, "%lu", v); |
| 134 |
asciitou(o, buf); |
| 135 |
} |
| 136 |
|
| 137 |
int utoi(const wchar_t *s) |
| 138 |
{ |
| 139 |
int sign = +1; |
| 140 |
int n; |
| 141 |
|
| 142 |
if (*s == L'-') |
| 143 |
{ |
| 144 |
s++; |
| 145 |
sign = -1; |
| 146 |
} |
| 147 |
|
| 148 |
n = 0; |
| 149 |
while (*s && *s >= L'0' && *s <= L'9') |
| 150 |
{ |
| 151 |
n *= 10; |
| 152 |
n += (*s - '0'); |
| 153 |
s++; |
| 154 |
} |
| 155 |
|
| 156 |
return n * sign; |
| 157 |
} |
| 158 |
|
| 159 |
int utob(const wchar_t * s) |
| 160 |
{ |
| 161 |
if (!ustricmp(s, L"yes") || !ustricmp(s, L"y") || |
| 162 |
!ustricmp(s, L"true") || !ustricmp(s, L"t")) |
| 163 |
return TRUE; |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
int uisdigit(wchar_t c) |
| 168 |
{ |
| 169 |
return c >= L'0' && c <= L'9'; |
| 170 |
} |
| 171 |
|
| 172 |
#define USTRFTIME_DELTA 128 |
| 173 |
wchar_t *ustrftime(wchar_t * wfmt, struct tm * timespec) |
| 174 |
{ |
| 175 |
void *blk = NULL; |
| 176 |
wchar_t *wblk, *wp; |
| 177 |
char *fmt, *text, *p; |
| 178 |
size_t size = 0; |
| 179 |
size_t len; |
| 180 |
|
| 181 |
/* |
| 182 |
* strftime has the entertaining property that it returns 0 |
| 183 |
* _either_ on out-of-space _or_ on successful generation of |
| 184 |
* the empty string. Hence we must ensure our format can never |
| 185 |
* generate the empty string. Somebody throw a custard pie at |
| 186 |
* whoever was responsible for that. Please? |
| 187 |
*/ |
| 188 |
if (wfmt) |
| 189 |
{ |
| 190 |
len = ustrlen(wfmt); |
| 191 |
fmt = mknewa(char, 2 + len); |
| 192 |
ustrtoa(wfmt, fmt + 1, len + 1); |
| 193 |
fmt[0] = ' '; |
| 194 |
} else |
| 195 |
fmt = " %c"; |
| 196 |
|
| 197 |
while (1) |
| 198 |
{ |
| 199 |
size += USTRFTIME_DELTA; |
| 200 |
blk = resize((char *) blk, size); |
| 201 |
len = strftime((char *) blk, size - 1, fmt, timespec); |
| 202 |
if (len > 0) |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
/* Note: +1 for the terminating 0, -1 for the initial space in fmt */ |
| 207 |
wblk = resize((wchar_t *) blk, len); |
| 208 |
text = mknewa(char, len); |
| 209 |
strftime(text, len, fmt + 1, timespec); |
| 210 |
/* |
| 211 |
* We operate in the C locale, so this all ought to be kosher |
| 212 |
* ASCII. If we ever move outside ASCII machines, we may need |
| 213 |
* to make this more portable... |
| 214 |
*/ |
| 215 |
for (wp = wblk, p = text; *p; p++, wp++) |
| 216 |
*wp = *p; |
| 217 |
*wp = 0; |
| 218 |
if (wfmt) |
| 219 |
sfree(fmt); |
| 220 |
sfree(text); |
| 221 |
return wblk; |
| 222 |
} |
| 223 |
|
| 224 |
ustr_slist* ustr_slist_append(ustr_slist**headaddr, const wchar_t*str) |
| 225 |
{ |
| 226 |
size_t cch = ustrlen(str) + 1; |
| 227 |
size_t cb = sizeof(ustr_slist) + (cch * sizeof(wchar_t)); |
| 228 |
ustr_slist *p = (ustr_slist*) mknewa(char, cb), *walk; |
| 229 |
if (p) |
| 230 |
{ |
| 231 |
ustrcpy(p->string, str); |
| 232 |
p->next = NULL; |
| 233 |
for (walk = *headaddr; walk; walk = walk->next) |
| 234 |
if (!walk->next) |
| 235 |
walk->next = p, walk = p; |
| 236 |
if (!*headaddr) |
| 237 |
*headaddr = p; |
| 238 |
} |
| 239 |
return p; |
| 240 |
} |