Fix #8713: Change OTTD2FS and FS2OTTD to return string objects instead of static buffers

This commit is contained in:
Niels Martin Hansen
2021-02-21 17:03:19 +01:00
parent 825867f2c5
commit e0561dbded
17 changed files with 109 additions and 100 deletions

View File

@@ -146,9 +146,8 @@ 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)
static const char *convert_tofrom_fs(iconv_t convd, const char *name, char *outbuf, size_t outlen)
{
static char buf[1024];
/* 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. */
@@ -158,9 +157,8 @@ static const char *convert_tofrom_fs(iconv_t convd, const char *name)
const char *inbuf = name;
#endif
char *outbuf = buf;
size_t outlen = sizeof(buf) - 1;
size_t inlen = strlen(name);
char *buf = outbuf;
strecpy(outbuf, name, outbuf + outlen);
@@ -179,9 +177,10 @@ static const char *convert_tofrom_fs(iconv_t convd, const char *name)
* @param name pointer to a valid string that will be converted
* @return pointer to a new stringbuffer that contains the converted string
*/
const char *OTTD2FS(const char *name)
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();
@@ -192,17 +191,18 @@ const char *OTTD2FS(const char *name)
}
}
return convert_tofrom_fs(convd, name);
return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf));
}
/**
* Convert to OpenTTD's encoding from that of the local environment
* @param name pointer to a valid string that will be converted
* @param name valid string that will be converted
* @return pointer to a new stringbuffer that contains the converted string
*/
const char *FS2OTTD(const char *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();
@@ -213,12 +213,9 @@ const char *FS2OTTD(const char *name)
}
}
return convert_tofrom_fs(convd, name);
return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf));
}
#else
const char *FS2OTTD(const char *name) {return name;}
const char *OTTD2FS(const char *name) {return name;}
#endif /* WITH_ICONV */
void ShowInfo(const char *str)