Make some tiny improvements to iconv_open().
- Remove an unneeded variable. - Fix whitespace bugs. - Fix typoes in comment. - Improve string handling a bit. Don't handroll strstr() and don't terminate a strdup()'ed string. Instead, simply strndup() the part we need.
This commit is contained in:
parent
907b803190
commit
a6f45121b5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=250981
@ -66,37 +66,31 @@ iconv_t _iconv_open(const char *out, const char *in,
|
|||||||
struct _citrus_iconv *prealloc);
|
struct _citrus_iconv *prealloc);
|
||||||
|
|
||||||
iconv_t
|
iconv_t
|
||||||
_iconv_open(const char *out, const char *in, struct _citrus_iconv *prealloc)
|
_iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
|
||||||
{
|
{
|
||||||
struct _citrus_iconv *handle;
|
const char *out_slashes;
|
||||||
char *out_truncated, *p;
|
char *out_noslashes;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
handle = prealloc;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Remove anything following a //, as these are options (like
|
* Remove anything following a //, as these are options (like
|
||||||
* //ignore, //translate, etc) and we just don't handle them.
|
* //ignore, //translate, etc) and we just don't handle them.
|
||||||
* This is for compatibilty with software that uses thees
|
* This is for compatibility with software that uses these
|
||||||
* blindly.
|
* blindly.
|
||||||
*/
|
*/
|
||||||
out_truncated = strdup(out);
|
out_slashes = strstr(out, "//");
|
||||||
if (out_truncated == NULL) {
|
if (out_slashes != NULL) {
|
||||||
errno = ENOMEM;
|
out_noslashes = strndup(out, out_slashes - out);
|
||||||
return ((iconv_t)-1);
|
if (out_noslashes == NULL) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return ((iconv_t)-1);
|
||||||
|
}
|
||||||
|
ret = _citrus_iconv_open(&handle, in, out_noslashes);
|
||||||
|
free(out_noslashes);
|
||||||
|
} else {
|
||||||
|
ret = _citrus_iconv_open(&handle, in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
p = out_truncated;
|
|
||||||
while (*p != 0) {
|
|
||||||
if (p[0] == '/' && p[1] == '/') {
|
|
||||||
*p = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = _citrus_iconv_open(&handle, in, out_truncated);
|
|
||||||
free(out_truncated);
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
errno = ret == ENOENT ? EINVAL : ret;
|
errno = ret == ENOENT ? EINVAL : ret;
|
||||||
return ((iconv_t)-1);
|
return ((iconv_t)-1);
|
||||||
|
Loading…
Reference in New Issue
Block a user