From 2af65fcc504f8242752755e836709762ef7ce062 Mon Sep 17 00:00:00 2001 From: Brodie Thiesfield Date: Fri, 25 Aug 2017 15:17:21 +1000 Subject: 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. --- SimpleIni.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/SimpleIni.h b/SimpleIni.h index 6cf5ced..047c809 100644 --- a/SimpleIni.h +++ b/SimpleIni.h @@ -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,14 +1414,18 @@ CSimpleIniTempl::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) { @@ -1429,6 +1433,7 @@ CSimpleIniTempl::LoadData( } // 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; -- cgit v1.2.3