wchar_t *ustrdup(wchar_t const *s) {
wchar_t *r;
if (s) {
- r = mknewa(wchar_t, 1+ustrlen(s));
+ r = snewn(1+ustrlen(s), wchar_t);
ustrcpy(r, s);
} else {
- r = mknew(wchar_t);
+ r = snew(wchar_t);
*r = 0;
}
return r;
len = ustrlen(s);
outlen = len + 10;
- outbuf = mknewa(char, outlen);
+ outbuf = snewn(outlen, char);
outpos = 0;
outbuf[outpos] = '\0';
}
if (!ret) {
outlen = outlen * 3 / 2;
- outbuf = resize(outbuf, outlen);
+ outbuf = sresize(outbuf, outlen, char);
}
outpos += ret;
outbuf[outpos] = '\0';
* Clean up
*/
outlen = outpos + 32;
- outbuf = resize(outbuf, outlen);
+ outbuf = sresize(outbuf, outlen, char);
ret = charset_from_unicode(NULL, 0,
outbuf + outpos, outlen - outpos + 1,
charset, &state, NULL);
len = strlen(s) + 1;
do {
- buf = resize(buf, len);
+ buf = sresize(buf, len, wchar_t);
ustrfroma(s, buf, len, charset);
len = (3 * len) / 2 + 1; /* this guarantees a strict increase */
} while (ustrlen(buf) >= len-1);
- buf = resize(buf, ustrlen(buf)+1);
+ buf = sresize(buf, ustrlen(buf)+1, wchar_t);
return buf;
}
len = ustrlen(s);
- ret = mknewa(char, 1 + MB_CUR_MAX * len);
+ ret = snewn(1 + MB_CUR_MAX * len, char);
siz = wcstombs(ret, s, len);
if (siz) {
- assert(siz <= MB_CUR_MAX * len);
+ assert(siz <= (size_t)(MB_CUR_MAX * len));
ret[siz] = '\0';
- ret = resize(ret, siz+1);
+ ret = sresize(ret, siz+1, char);
return ret;
}
len = strlen(s);
- ret = mknewa(wchar_t, 1 + 2*len); /* be conservative */
+ ret = snewn(1 + 2*len, wchar_t); /* be conservative */
siz = mbstowcs(ret, s, len);
if (siz) {
assert(siz <= (size_t)(2 * len));
ret[siz] = L'\0';
- ret = resize(ret, siz+1);
+ ret = sresize(ret, siz+1, wchar_t);
return ret;
}
#endif
}
-int ustricmp(wchar_t *lhs, wchar_t *rhs) {
+int ustricmp(wchar_t const *lhs, wchar_t const *rhs) {
wchar_t lc, rc;
while ((lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc)
lhs++, rhs++;
return 1;
}
+int ustrnicmp(wchar_t const *lhs, wchar_t const *rhs, int maxlen) {
+ wchar_t lc = 0, rc = 0;
+ while (maxlen-- > 0 &&
+ (lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc)
+ lhs++, rhs++;
+ if (lc < rc)
+ return -1;
+ else if (lc > rc)
+ return 1;
+ else
+ return 0;
+}
+
wchar_t *ustrlow(wchar_t *s) {
wchar_t *p = s;
while (*p) {
return s;
}
-int utoi(wchar_t *s) {
+int utoi(wchar_t const *s) {
int sign = +1;
int n;
return n;
}
-int utob(wchar_t *s) {
+double utof(wchar_t const *s)
+{
+ char *cs = utoa_dup(s, CS_ASCII);
+ double ret = atof(cs);
+ sfree(cs);
+ return ret;
+}
+
+int utob(wchar_t const *s) {
if (!ustricmp(s, L"yes") || !ustricmp(s, L"y") ||
!ustricmp(s, L"true") || !ustricmp(s, L"t"))
return TRUE;
size = 0;
do {
size += USTRFTIME_DELTA;
- buf = resize(buf, size);
+ buf = sresize(buf, size, wchar_t);
ret = (int) wcsftime(buf, size, fmt, timespec);
} while (ret == 0);
size = 0;
do {
size += USTRFTIME_DELTA;
- buf = resize(buf, size);
+ buf = sresize(buf, size, char);
ret = (int) strftime(buf, size, fmt, timespec);
} while (ret == 0);
}
return TRUE;
}
+
+/*
+ * Wrapper around charset_from_localenc which accepts the charset
+ * name as a wide string (since that happens to be more useful).
+ * Also throws a Halibut error and falls back to CS_ASCII if the
+ * charset is unrecognised, meaning the rest of the program can
+ * rely on always getting a valid charset id back from this
+ * function.
+ */
+int charset_from_ustr(filepos *fpos, const wchar_t *name)
+{
+ char *csname;
+ int charset;
+
+ csname = utoa_dup(name, CS_ASCII);
+ charset = charset_from_localenc(csname);
+
+ if (charset == CS_NONE) {
+ charset = CS_ASCII;
+ error(err_charset, fpos, name);
+ }
+
+ sfree(csname);
+ return charset;
+}