Merge pull request #31 from libAthena/codecvt-removal

removed codecvt headers
This commit is contained in:
Jack Andersen 2015-08-24 20:53:43 -10:00
commit 4d13caeaab
4 changed files with 93 additions and 92 deletions

View File

@ -7,8 +7,6 @@
* Any changes to the types or namespacing must be reflected in 'atdna/main.cpp' * Any changes to the types or namespacing must be reflected in 'atdna/main.cpp'
*/ */
#include <locale>
#include <codecvt>
#include <string.h> #include <string.h>
#include <yaml.h> #include <yaml.h>
#include "DNA.hpp" #include "DNA.hpp"
@ -376,16 +374,29 @@ inline std::unique_ptr<YAMLNode> ValToNode(const char* val)
template <> template <>
inline std::wstring NodeToVal(const YAMLNode* node) inline std::wstring NodeToVal(const YAMLNode* node)
{ {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; std::wstring retval;
return conv.from_bytes(node->m_scalarString); retval.reserve(node->m_scalarString.length());
const char* buf = node->m_scalarString.c_str();
while (*buf)
{
wchar_t wc;
buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
retval += wc;
}
return retval;
} }
template <> template <>
inline std::unique_ptr<YAMLNode> ValToNode(const std::wstring& val) inline std::unique_ptr<YAMLNode> ValToNode(const std::wstring& val)
{ {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = conv.to_bytes(val); ret->m_scalarString.reserve(val.length());
for (wchar_t ch : val)
{
char mb[4];
int c = std::wctomb(mb, ch);
ret->m_scalarString.append(mb, c);
}
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }

View File

@ -1,8 +1,7 @@
#ifndef ISTREAMREADER_HPP #ifndef ISTREAMREADER_HPP
#define ISTREAMREADER_HPP #define ISTREAMREADER_HPP
#include <locale> #include <memory>
#include <codecvt>
#include <functional> #include <functional>
#include "IStream.hpp" #include "IStream.hpp"
@ -591,7 +590,7 @@ public:
*/ */
inline std::string readWStringAsString(atInt32 fixedLen = -1) inline std::string readWStringAsString(atInt32 fixedLen = -1)
{ {
std::wstring tmp; std::string retval;
atUint16 chr = readUint16(); atUint16 chr = readUint16();
atInt32 i; atInt32 i;
@ -603,20 +602,21 @@ public:
if (!chr) if (!chr)
break; break;
tmp.push_back(chr); char mb[4];
int c = std::wctomb(mb, chr);
retval.append(mb, c);
chr = readUint16(); chr = readUint16();
} }
if (fixedLen >= 0 && i < fixedLen) if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i); seek(fixedLen - i);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; return retval;
return conv.to_bytes(tmp);
} }
inline std::string readWStringAsStringLittle(atInt32 fixedLen = -1) inline std::string readWStringAsStringLittle(atInt32 fixedLen = -1)
{ {
std::wstring tmp; std::string retval;
atUint16 chr = readUint16Little(); atUint16 chr = readUint16Little();
atInt32 i; atInt32 i;
@ -628,20 +628,21 @@ public:
if (!chr) if (!chr)
break; break;
tmp.push_back(chr); char mb[4];
int c = std::wctomb(mb, chr);
retval.append(mb, c);
chr = readUint16Little(); chr = readUint16Little();
} }
if (fixedLen >= 0 && i < fixedLen) if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i); seek(fixedLen - i);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; return retval;
return conv.to_bytes(tmp);
} }
inline std::string readWStringAsStringBig(atInt32 fixedLen = -1) inline std::string readWStringAsStringBig(atInt32 fixedLen = -1)
{ {
std::wstring tmp; std::string retval;
atUint16 chr = readUint16Big(); atUint16 chr = readUint16Big();
atInt32 i; atInt32 i;
@ -653,15 +654,16 @@ public:
if (!chr) if (!chr)
break; break;
tmp.push_back(chr); char mb[4];
int c = std::wctomb(mb, chr);
retval.append(mb, c);
chr = readUint16Big(); chr = readUint16Big();
} }
if (fixedLen >= 0 && i < fixedLen) if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i); seek(fixedLen - i);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; return retval;
return conv.to_bytes(tmp);
} }
/*! \brief Reads a string and advances the position in the file /*! \brief Reads a string and advances the position in the file

View File

@ -1,8 +1,6 @@
#ifndef ISTREAMWRITER_HPP #ifndef ISTREAMWRITER_HPP
#define ISTREAMWRITER_HPP #define ISTREAMWRITER_HPP
#include <locale>
#include <codecvt>
#include "IStream.hpp" #include "IStream.hpp"
namespace Athena namespace Athena
@ -438,76 +436,70 @@ public:
*/ */
inline void writeStringAsWString(const std::string& str, atInt32 fixedLen = -1) inline void writeStringAsWString(const std::string& str, atInt32 fixedLen = -1)
{ {
std::string tmpStr = "\xEF\xBB\xBF" + str; std::string tmpStr = "\xEF\xBB\xBF" + str;
const char* buf = tmpStr.c_str();
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; if (fixedLen < 0)
std::wstring tmp = conv.from_bytes(tmpStr); {
while (*buf)
{
wchar_t wc;
buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
if (wc != 0xFEFF)
writeUint16(wc);
}
writeUint16(0);
}
else
{
for (atInt32 i=0 ; i<fixedLen ; ++i)
{
wchar_t wc = 0;
if (*buf)
buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
if (fixedLen < 0) if (wc == 0xFEFF)
{ {
for (atUint16 chr : tmp) --i;
{ continue;
if (chr != 0xFEFF) }
writeUint16(chr);
}
writeUint16(0);
}
else
{
auto it = tmp.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i)
{
atUint16 chr;
if (it == tmp.end())
chr = 0;
else
chr = *it++;
if (chr == 0xFEFF) writeUint16(wc);
{ }
--i; }
continue;
}
writeUint16(chr);
}
}
} }
inline void writeStringAsWStringLittle(const std::string& str, atInt32 fixedLen = -1) inline void writeStringAsWStringLittle(const std::string& str, atInt32 fixedLen = -1)
{ {
std::string tmpStr = "\xEF\xBB\xBF" + str; std::string tmpStr = "\xEF\xBB\xBF" + str;
const char* buf = tmpStr.c_str();
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::wstring tmp = conv.from_bytes(tmpStr);
if (fixedLen < 0) if (fixedLen < 0)
{ {
for (atUint16 chr : tmp) while (*buf)
{ {
if (chr != 0xFEFF) wchar_t wc;
writeUint16Little(chr); buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
if (wc != 0xFEFF)
writeUint16Little(wc);
} }
writeUint16Little(0); writeUint16Little(0);
} }
else else
{ {
auto it = tmp.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i) for (atInt32 i=0 ; i<fixedLen ; ++i)
{ {
atUint16 chr; wchar_t wc = 0;
if (it == tmp.end()) if (*buf)
chr = 0; buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
else
chr = *it++;
if (chr == 0xFEFF) if (wc == 0xFEFF)
{ {
--i; --i;
continue; continue;
} }
writeUint16Little(chr); writeUint16Little(wc);
} }
} }
} }
@ -515,37 +507,34 @@ public:
inline void writeStringAsWStringBig(const std::string& str, atInt32 fixedLen = -1) inline void writeStringAsWStringBig(const std::string& str, atInt32 fixedLen = -1)
{ {
std::string tmpStr = "\xEF\xBB\xBF" + str; std::string tmpStr = "\xEF\xBB\xBF" + str;
const char* buf = tmpStr.c_str();
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::wstring tmp = conv.from_bytes(tmpStr);
if (fixedLen < 0) if (fixedLen < 0)
{ {
for (atUint16 chr : tmp) while (*buf)
{ {
if (chr != 0xFEFF) wchar_t wc;
writeUint16Big(chr); buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
if (wc != 0xFEFF)
writeUint16Big(wc);
} }
writeUint16Big(0); writeUint16Big(0);
} }
else else
{ {
auto it = tmp.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i) for (atInt32 i=0 ; i<fixedLen ; ++i)
{ {
atUint16 chr; wchar_t wc = 0;
if (it == tmp.end()) if (*buf)
chr = 0; buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
else
chr = *it++;
if (chr == 0xFEFF) if (wc == 0xFEFF)
{ {
--i; --i;
continue; continue;
} }
writeUint16Big(chr); writeUint16Big(wc);
} }
} }
} }

View File

@ -17,8 +17,6 @@
#include "Athena/SkywardSwordQuest.hpp" #include "Athena/SkywardSwordQuest.hpp"
#include "Athena/Checksums.hpp" #include "Athena/Checksums.hpp"
#include <sstream> #include <sstream>
#include <locale>
#include <codecvt>
namespace Athena namespace Athena
{ {
@ -74,27 +72,27 @@ void SkywardSwordQuest::setPlayerName(const std::string& name)
if (name.length() > 8) if (name.length() > 8)
atDebug("WARNING: name cannot be greater than 8 characters, automatically truncating"); atDebug("WARNING: name cannot be greater than 8 characters, automatically truncating");
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; const char* buf = name.c_str();
std::wstring val = conv.from_bytes(name);
for (atUint32 i = 0; i < 8; i++) for (atUint32 i = 0; i < 8; i++)
{ {
atUint16& c = *(atUint16*)(m_data.get() + priv::NAME_OFFSET + (i * 2)); atUint16& c = *(atUint16*)(m_data.get() + priv::NAME_OFFSET + (i * 2));
if (i >= val.size()) if (!*buf)
{ {
c = 0; c = 0;
continue; continue;
} }
c = val[i]; wchar_t wc;
buf += std::mbtowc(&wc, buf, MB_CUR_MAX);
c = wc;
utility::BigUint16(c); utility::BigUint16(c);
} }
} }
std::string SkywardSwordQuest::playerName() const std::string SkywardSwordQuest::playerName() const
{ {
std::wstring val; std::string val;
for (atUint32 i = 0; i < 8; i++) for (atUint32 i = 0; i < 8; i++)
{ {
@ -104,11 +102,12 @@ std::string SkywardSwordQuest::playerName() const
break; break;
utility::BigUint16(c); utility::BigUint16(c);
val.push_back(c); char mb[4];
int cs = std::wctomb(mb, c);
val.append(mb, cs);
} }
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; return val;
return conv.to_bytes(val);
} }
void SkywardSwordQuest::setRupeeCount(atUint16 value) void SkywardSwordQuest::setRupeeCount(atUint16 value)