Permalink
Browse files

Enable unicode when data starts with UTF-8 BOM

Automatically enable the Unicode mode if the data being loaded starts with the UTF-8 BOM.
  • Loading branch information...
brofield committed Aug 25, 2017
1 parent 244c3f2 commit 2af65fcc504f8242752755e836709762ef7ce062
Showing with 14 additions and 9 deletions.
  1. +14 −9 SimpleIni.h
View
@@ -20,7 +20,7 @@
@section features FEATURES
- MIT Licence allows free use in all software (including GPL and commercial)
- multi-platform (Windows 95/98/ME/NT/2K/XP/2003, Windows CE, Linux, Unix)
- multi-platform (Windows CE/9x/NT..10/etc, Linux, MacOSX, Unix)
- loading and saving of INI-style configuration files
- configuration files can have any newline format on all platforms
- liberal acceptance of file format
@@ -1414,21 +1414,26 @@ CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadData(
size_t a_uDataLen
)
{
SI_CONVERTER converter(m_bStoreIsUtf8);
// consume the UTF-8 BOM if it exists
if (m_bStoreIsUtf8 && a_uDataLen >= 3) {
if (memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) {
a_pData += 3;
a_uDataLen -= 3;
}
if (!a_pData) {
return SI_OK;
}
// if the UTF-8 BOM exists, consume it and set mode to unicode, if we have
// already loaded data and try to change mode half-way through then this will
// be ignored and we will assert in debug versions
if (a_uDataLen >= 3 && memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) {
a_pData += 3;
a_uDataLen -= 3;
SI_ASSERT(m_bStoreIsUtf8 || !m_pData); // we don't expect mixed mode data
SetUnicode();
}
if (a_uDataLen == 0) {
return SI_OK;
}
// determine the length of the converted data
SI_CONVERTER converter(m_bStoreIsUtf8);
size_t uLen = converter.SizeFromStore(a_pData, a_uDataLen);
if (uLen == (size_t)(-1)) {
return SI_FAIL;

0 comments on commit 2af65fc

Please sign in to comment.