mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: use std::string instead of a temporary buffer for iconv calls
This commit is contained in:
parent
0ea3e338ab
commit
d5c75bd655
|
@ -124,29 +124,30 @@ static const char *GetLocalCode()
|
|||
* Convert between locales, which from and which to is set in the calling
|
||||
* functions OTTD2FS() and FS2OTTD().
|
||||
*/
|
||||
static const char *convert_tofrom_fs(iconv_t convd, const char *name, char *outbuf, size_t outlen)
|
||||
static std::string convert_tofrom_fs(iconv_t convd, const std::string &name)
|
||||
{
|
||||
/* There are different implementations of iconv. The older ones,
|
||||
* e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
|
||||
* IEEE 1003.1 (2004), pass a non-const pointer. */
|
||||
#ifdef HAVE_NON_CONST_ICONV
|
||||
char *inbuf = const_cast<char*>(name);
|
||||
char *inbuf = const_cast<char*>(name.data());
|
||||
#else
|
||||
const char *inbuf = name;
|
||||
const char *inbuf = name.data();
|
||||
#endif
|
||||
|
||||
size_t inlen = strlen(name);
|
||||
char *buf = outbuf;
|
||||
|
||||
strecpy(outbuf, name, outbuf + outlen);
|
||||
/* If the output is UTF-32, then 1 ASCII character becomes 4 bytes. */
|
||||
size_t inlen = name.size();
|
||||
std::string buf(inlen * 4, '\0');
|
||||
|
||||
size_t outlen = buf.size();
|
||||
char *outbuf = buf.data();
|
||||
iconv(convd, nullptr, nullptr, nullptr, nullptr);
|
||||
if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) {
|
||||
Debug(misc, 0, "[iconv] error converting '{}'. Errno {}", name, errno);
|
||||
return name;
|
||||
}
|
||||
|
||||
*outbuf = '\0';
|
||||
/* FIX: invalid characters will abort conversion, but they shouldn't occur? */
|
||||
buf.resize(outbuf - buf.data());
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -158,8 +159,6 @@ static const char *convert_tofrom_fs(iconv_t convd, const char *name, char *outb
|
|||
std::string OTTD2FS(const std::string &name)
|
||||
{
|
||||
static iconv_t convd = (iconv_t)(-1);
|
||||
char buf[1024] = {};
|
||||
|
||||
if (convd == (iconv_t)(-1)) {
|
||||
const char *env = GetLocalCode();
|
||||
convd = iconv_open(env, INTERNALCODE);
|
||||
|
@ -169,7 +168,7 @@ std::string OTTD2FS(const std::string &name)
|
|||
}
|
||||
}
|
||||
|
||||
return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf));
|
||||
return convert_tofrom_fs(convd, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,8 +179,6 @@ std::string OTTD2FS(const std::string &name)
|
|||
std::string FS2OTTD(const std::string &name)
|
||||
{
|
||||
static iconv_t convd = (iconv_t)(-1);
|
||||
char buf[1024] = {};
|
||||
|
||||
if (convd == (iconv_t)(-1)) {
|
||||
const char *env = GetLocalCode();
|
||||
convd = iconv_open(INTERNALCODE, env);
|
||||
|
@ -191,7 +188,7 @@ std::string FS2OTTD(const std::string &name)
|
|||
}
|
||||
}
|
||||
|
||||
return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf));
|
||||
return convert_tofrom_fs(convd, name);
|
||||
}
|
||||
|
||||
#endif /* WITH_ICONV */
|
||||
|
|
Loading…
Reference in New Issue