SimpleIni
Loading...
Searching...
No Matches
SimpleIni.h
1
225
226#ifndef INCLUDED_SimpleIni_h
227#define INCLUDED_SimpleIni_h
228
229#if defined(_MSC_VER) && (_MSC_VER >= 1020)
230#pragma once
231#endif
232
233// Disable these warnings in MSVC:
234// 4127 "conditional expression is constant" as the conversion classes trigger
235// it with the statement if (sizeof(SI_CHAR) == sizeof(char)). This test will
236// be optimized away in a release build.
237// 4503 'insert' : decorated name length exceeded, name was truncated
238// 4702 "unreachable code" as the MS STL header causes it in release mode.
239// Again, the code causing the warning will be cleaned up by the compiler.
240// 4786 "identifier truncated to 256 characters" as this is thrown hundreds
241// of times VC6 as soon as STL is used.
242#ifdef _MSC_VER
243#pragma warning(push)
244#pragma warning(disable : 4127 4503 4702 4786)
245#endif
246
247#include <algorithm>
248#include <cerrno>
249#include <cstdlib>
250#include <cstring>
251#include <limits>
252#include <list>
253#include <map>
254#include <stdio.h>
255#include <string>
256
257#ifdef SI_SUPPORT_IOSTREAMS
258#include <iostream>
259#endif // SI_SUPPORT_IOSTREAMS
260
261#ifdef _DEBUG
262#ifndef assert
263#include <cassert>
264#endif
265#define SI_ASSERT(x) assert(x)
266#else
267#define SI_ASSERT(x)
268#endif
269
270using SI_Error = int;
271
272constexpr int SI_OK = 0;
273constexpr int SI_UPDATED = 1;
274constexpr int SI_INSERTED = 2;
275
276// note: test for any error with (retval < 0)
277constexpr int SI_FAIL = -1;
278constexpr int SI_NOMEM = -2;
279constexpr int SI_FILE = -3;
280
283constexpr size_t SI_MAX_FILE_SIZE = 1024ULL * 1024ULL * 1024ULL;
284
285#define SI_UTF8_SIGNATURE "\xEF\xBB\xBF"
286
287#ifdef _WIN32
288#define SI_NEWLINE_A "\r\n"
289#define SI_NEWLINE_W L"\r\n"
290#else // !_WIN32
291#define SI_NEWLINE_A "\n"
292#define SI_NEWLINE_W L"\n"
293#endif // _WIN32
294
295#if defined(SI_CONVERT_ICU)
296#include <unicode/ustring.h>
297#endif
298
299#if defined(_WIN32)
300#define SI_HAS_WIDE_FILE
301#define SI_WCHAR_T wchar_t
302#elif defined(SI_CONVERT_ICU)
303#define SI_HAS_WIDE_FILE
304#define SI_WCHAR_T UChar
305#endif
306
307// ---------------------------------------------------------------------------
308// MAIN TEMPLATE CLASS
309// ---------------------------------------------------------------------------
310
330template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
332public:
333 typedef SI_CHAR SI_CHAR_T;
334
336 struct Entry {
337 const SI_CHAR *pItem;
338 const SI_CHAR *pComment;
339 int nOrder;
340
341 Entry(const SI_CHAR *a_pszItem = NULL, int a_nOrder = 0)
342 : pItem(a_pszItem), pComment(NULL), nOrder(a_nOrder) {}
343 Entry(const SI_CHAR *a_pszItem, const SI_CHAR *a_pszComment, int a_nOrder)
344 : pItem(a_pszItem), pComment(a_pszComment), nOrder(a_nOrder) {}
345 Entry(const Entry &rhs) { operator=(rhs); }
346 Entry &operator=(const Entry &rhs) {
347 pItem = rhs.pItem;
348 pComment = rhs.pComment;
349 nOrder = rhs.nOrder;
350 return *this;
351 }
352
353#if defined(_MSC_VER) && _MSC_VER <= 1200
355 bool operator<(const Entry &rhs) const { return LoadOrder()(*this, rhs); }
356 bool operator>(const Entry &rhs) const { return LoadOrder()(rhs, *this); }
357#endif
358
360 struct KeyOrder {
361 bool operator()(const Entry &lhs, const Entry &rhs) const {
362 const static SI_STRLESS isLess = SI_STRLESS();
363 return isLess(lhs.pItem, rhs.pItem);
364 }
365 };
366
368 struct LoadOrder {
369 bool operator()(const Entry &lhs, const Entry &rhs) const {
370 if (lhs.nOrder != rhs.nOrder) {
371 return lhs.nOrder < rhs.nOrder;
372 }
373 return KeyOrder()(lhs, rhs);
374 }
375 };
376 };
377
379 typedef std::multimap<Entry, const SI_CHAR *, typename Entry::KeyOrder>
381
383 typedef std::map<Entry, TKeyVal, typename Entry::KeyOrder> TSection;
384
388 typedef std::list<Entry> TNamesDepend;
389
393 class OutputWriter {
394 public:
395 OutputWriter() {}
396 virtual ~OutputWriter() {}
397 virtual void Write(const char *a_pBuf) = 0;
398
399 private:
400 OutputWriter(const OutputWriter &); // disable
401 OutputWriter &operator=(const OutputWriter &); // disable
402 };
403
405 class FileWriter : public OutputWriter {
406 FILE *m_file;
407
408 public:
409 FileWriter(FILE *a_file) : m_file(a_file) {}
410 void Write(const char *a_pBuf) { fputs(a_pBuf, m_file); }
411
412 private:
413 FileWriter(const FileWriter &); // disable
414 FileWriter &operator=(const FileWriter &); // disable
415 };
416
418 class StringWriter : public OutputWriter {
419 std::string &m_string;
420
421 public:
422 StringWriter(std::string &a_string) : m_string(a_string) {}
423 void Write(const char *a_pBuf) { m_string.append(a_pBuf); }
424
425 private:
426 StringWriter(const StringWriter &); // disable
427 StringWriter &operator=(const StringWriter &); // disable
428 };
429
430#ifdef SI_SUPPORT_IOSTREAMS
432 class StreamWriter : public OutputWriter {
433 std::ostream &m_ostream;
434
435 public:
436 StreamWriter(std::ostream &a_ostream) : m_ostream(a_ostream) {}
437 void Write(const char *a_pBuf) { m_ostream << a_pBuf; }
438
439 private:
440 StreamWriter(const StreamWriter &); // disable
441 StreamWriter &operator=(const StreamWriter &); // disable
442 };
443#endif // SI_SUPPORT_IOSTREAMS
444
448 class Converter : private SI_CONVERTER {
449 public:
450 Converter(bool a_bStoreIsUtf8) : SI_CONVERTER(a_bStoreIsUtf8) {
451 m_scratch.resize(1024);
452 }
453 Converter(const Converter &rhs)
454 : SI_CONVERTER(rhs), m_scratch(rhs.m_scratch) {}
455 Converter &operator=(const Converter &rhs) {
456 SI_CONVERTER::operator=(rhs);
457 m_scratch = rhs.m_scratch;
458 return *this;
459 }
460 bool ConvertToStore(const SI_CHAR *a_pszString) {
461 size_t uLen = SI_CONVERTER::SizeToStore(a_pszString);
462 if (uLen == (size_t)(-1)) {
463 return false;
464 }
465 while (uLen > m_scratch.size()) {
466 m_scratch.resize(m_scratch.size() * 2);
467 }
468 return SI_CONVERTER::ConvertToStore(
469 a_pszString, const_cast<char *>(m_scratch.data()), m_scratch.size());
470 }
471 const char *Data() { return m_scratch.data(); }
472
473 private:
474 std::string m_scratch;
475 };
476
477public:
478 /*-----------------------------------------------------------------------*/
479
486 CSimpleIniTempl(bool a_bIsUtf8 = false, bool a_bMultiKey = false,
487 bool a_bMultiLine = false);
488
491
493 void Reset();
494
496 bool IsEmpty() const { return m_data.empty(); }
497
498 /*-----------------------------------------------------------------------*/
500
515 void SetUnicode(bool a_bIsUtf8 = true) {
516 if (!m_pData)
517 m_bStoreIsUtf8 = a_bIsUtf8;
518 }
519
521 bool IsUnicode() const { return m_bStoreIsUtf8; }
522
541 void SetMultiKey(bool a_bAllowMultiKey = true) {
542 m_bAllowMultiKey = a_bAllowMultiKey;
543 }
544
546 bool IsMultiKey() const { return m_bAllowMultiKey; }
547
555 void SetMultiLine(bool a_bAllowMultiLine = true) {
556 m_bAllowMultiLine = a_bAllowMultiLine;
557 }
558
560 bool IsMultiLine() const { return m_bAllowMultiLine; }
561
568 void SetSpaces(bool a_bSpaces = true) { m_bSpaces = a_bSpaces; }
569
571 bool UsingSpaces() const { return m_bSpaces; }
572
577 void SetQuotes(bool a_bParseQuotes = true) {
578 m_bParseQuotes = a_bParseQuotes;
579 }
580
582 bool UsingQuotes() const { return m_bParseQuotes; }
583
592 void SetAllowKeyOnly(bool a_bAllowKeyOnly = true) {
593 m_bAllowKeyOnly = a_bAllowKeyOnly;
594 }
595
597 bool GetAllowKeyOnly() const { return m_bAllowKeyOnly; }
598
599 /*-----------------------------------------------------------------------*/
602
611 SI_Error LoadFile(const char *a_pszFile);
612
613#ifdef SI_HAS_WIDE_FILE
620 SI_Error LoadFile(const SI_WCHAR_T *a_pwszFile);
621#endif // SI_HAS_WIDE_FILE
622
630 SI_Error LoadFile(FILE *a_fpFile);
631
632#ifdef SI_SUPPORT_IOSTREAMS
643 SI_Error LoadData(std::istream &a_istream);
644#endif // SI_SUPPORT_IOSTREAMS
645
652 SI_Error LoadData(const std::string &a_strData) {
653 return LoadData(a_strData.c_str(), a_strData.size());
654 }
655
663 SI_Error LoadData(const char *a_pData, size_t a_uDataLen);
664
665 /*-----------------------------------------------------------------------*/
668
681 SI_Error SaveFile(const char *a_pszFile, bool a_bAddSignature = true) const;
682
683#ifdef SI_HAS_WIDE_FILE
694 SI_Error SaveFile(const SI_WCHAR_T *a_pwszFile,
695 bool a_bAddSignature = true) const;
696#endif // _WIN32
697
710 SI_Error SaveFile(FILE *a_pFile, bool a_bAddSignature = false) const;
711
743 SI_Error Save(OutputWriter &a_oOutput, bool a_bAddSignature = false) const;
744
745#ifdef SI_SUPPORT_IOSTREAMS
757 SI_Error Save(std::ostream &a_ostream, bool a_bAddSignature = false) const {
758 StreamWriter writer(a_ostream);
759 return Save(writer, a_bAddSignature);
760 }
761#endif // SI_SUPPORT_IOSTREAMS
762
774 SI_Error Save(std::string &a_sBuffer, bool a_bAddSignature = false) const {
775 StringWriter writer(a_sBuffer);
776 return Save(writer, a_bAddSignature);
777 }
778
779 /*-----------------------------------------------------------------------*/
782
797 void GetAllSections(TNamesDepend &a_names) const;
798
816 bool GetAllKeys(const SI_CHAR *a_pSection, TNamesDepend &a_names) const;
817
834 bool GetAllValues(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
835 TNamesDepend &a_values) const;
836
846 int GetSectionSize(const SI_CHAR *a_pSection) const;
847
861 const TKeyVal *GetSection(const SI_CHAR *a_pSection) const;
862
864 inline bool SectionExists(const SI_CHAR *a_pSection) const {
865 return GetSection(a_pSection) != NULL;
866 }
867
869 inline bool KeyExists(const SI_CHAR *a_pSection,
870 const SI_CHAR *a_pKey) const {
871 return GetValue(a_pSection, a_pKey) != NULL;
872 }
873
891 const SI_CHAR *GetValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
892 const SI_CHAR *a_pDefault = NULL,
893 bool *a_pHasMultiple = NULL) const;
894
908 long GetLongValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
909 long a_nDefault = 0, bool *a_pHasMultiple = NULL) const;
910
924 double GetDoubleValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
925 double a_nDefault = 0,
926 bool *a_pHasMultiple = NULL) const;
927
946 bool GetBoolValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
947 bool a_bDefault = false, bool *a_pHasMultiple = NULL) const;
948
978 SI_Error SetValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
979 const SI_CHAR *a_pValue, const SI_CHAR *a_pComment = NULL,
980 bool a_bForceReplace = false) {
981 if (!a_pSection)
982 return SI_FAIL;
983 return AddEntry(a_pSection, a_pKey, a_pValue, a_pComment, a_bForceReplace,
984 true);
985 }
986
1010 SI_Error SetLongValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1011 long a_nValue, const SI_CHAR *a_pComment = NULL,
1012 bool a_bUseHex = false, bool a_bForceReplace = false);
1013
1034 SI_Error SetDoubleValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1035 double a_nValue, const SI_CHAR *a_pComment = NULL,
1036 bool a_bForceReplace = false);
1037
1058 SI_Error SetBoolValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1059 bool a_bValue, const SI_CHAR *a_pComment = NULL,
1060 bool a_bForceReplace = false);
1061
1080 bool Delete(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1081 bool a_bRemoveEmpty = false);
1082
1103 bool DeleteValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1104 const SI_CHAR *a_pValue, bool a_bRemoveEmpty = false);
1105
1106 /*-----------------------------------------------------------------------*/
1109
1115 Converter GetConverter() const { return Converter(m_bStoreIsUtf8); }
1116
1117 /*-----------------------------------------------------------------------*/
1119
1120private:
1121 // copying is not permitted
1122 CSimpleIniTempl(const CSimpleIniTempl &); // disabled
1123 CSimpleIniTempl &operator=(const CSimpleIniTempl &); // disabled
1124
1127 SI_Error FindFileComment(SI_CHAR *&a_pData, bool a_bCopyStrings);
1128
1133 bool FindEntry(SI_CHAR *&a_pData, const SI_CHAR *&a_pSection,
1134 const SI_CHAR *&a_pKey, const SI_CHAR *&a_pVal,
1135 const SI_CHAR *&a_pComment) const;
1136
1159 SI_Error AddEntry(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
1160 const SI_CHAR *a_pValue, const SI_CHAR *a_pComment,
1161 bool a_bForceReplace, bool a_bCopyStrings);
1162
1164 inline bool IsSpace(SI_CHAR ch) const {
1165 return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
1166 }
1167
1169 inline bool IsComment(SI_CHAR ch) const { return (ch == ';' || ch == '#'); }
1170
1172 inline void SkipNewLine(SI_CHAR *&a_pData) const {
1173 a_pData += (*a_pData == '\r' && *(a_pData + 1) == '\n') ? 2 : 1;
1174 }
1175
1177 SI_Error CopyString(const SI_CHAR *&a_pString);
1178
1185 void UndoIncrementalLoadData(const TNamesDepend &a_oAddedSections,
1186 const TNamesDepend &a_oAddedKeys);
1187
1189 void DeleteString(const SI_CHAR *a_pString);
1190
1192 bool IsLess(const SI_CHAR *a_pLeft, const SI_CHAR *a_pRight) const {
1193 const static SI_STRLESS isLess = SI_STRLESS();
1194 return isLess(a_pLeft, a_pRight);
1195 }
1196
1197 bool IsMultiLineTag(const SI_CHAR *a_pData) const;
1198 bool IsMultiLineData(const SI_CHAR *a_pData) const;
1199 bool IsSingleLineQuotedValue(const SI_CHAR *a_pData) const;
1200 bool MultiLineTextHasTag(const SI_CHAR *a_pText, const SI_CHAR *a_pTag) const;
1201 bool LoadMultiLineText(SI_CHAR *&a_pData, const SI_CHAR *&a_pVal,
1202 const SI_CHAR *a_pTagName,
1203 bool a_bAllowBlankLinesInComment = false) const;
1204 bool IsNewLineChar(SI_CHAR a_c) const;
1205
1206 bool OutputMultiLineText(OutputWriter &a_oOutput, Converter &a_oConverter,
1207 const SI_CHAR *a_pText) const;
1208
1209private:
1215 SI_CHAR *m_pData;
1216
1221 size_t m_uDataLen;
1222
1224 const SI_CHAR *m_pFileComment;
1225
1227 const SI_CHAR m_cEmptyString;
1228
1230 TSection m_data;
1231
1236 TNamesDepend m_strings;
1237
1239 bool m_bStoreIsUtf8;
1240
1242 bool m_bAllowMultiKey;
1243
1245 bool m_bAllowMultiLine;
1246
1248 bool m_bSpaces;
1249
1251 bool m_bParseQuotes;
1252
1254 bool m_bAllowKeyOnly;
1255
1259 int m_nOrder;
1260};
1261
1262// ---------------------------------------------------------------------------
1263// IMPLEMENTATION
1264// ---------------------------------------------------------------------------
1265
1266template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1268 bool a_bIsUtf8, bool a_bAllowMultiKey, bool a_bAllowMultiLine)
1269 : m_pData(0), m_uDataLen(0), m_pFileComment(NULL), m_cEmptyString(0),
1270 m_bStoreIsUtf8(a_bIsUtf8), m_bAllowMultiKey(a_bAllowMultiKey),
1271 m_bAllowMultiLine(a_bAllowMultiLine), m_bSpaces(true),
1272 m_bParseQuotes(false), m_bAllowKeyOnly(false), m_nOrder(0) {}
1273
1274template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1278
1279template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1281 // remove all data
1282 delete[] m_pData;
1283 m_pData = NULL;
1284 m_uDataLen = 0;
1285 m_pFileComment = NULL;
1286 m_nOrder = 0;
1287 if (!m_data.empty()) {
1288 m_data.erase(m_data.begin(), m_data.end());
1289 }
1290
1291 // remove all strings
1292 if (!m_strings.empty()) {
1293 typename TNamesDepend::iterator i = m_strings.begin();
1294 for (; i != m_strings.end(); ++i) {
1295 delete[] const_cast<SI_CHAR *>(i->pItem);
1296 }
1297 m_strings.erase(m_strings.begin(), m_strings.end());
1298 }
1299}
1300
1301namespace SI_Internal {
1302#ifdef SI_CONVERT_ICU
1304inline SI_Error UCharPathToUtf8(const UChar *a_pPath, char *&a_pUtf8Path) {
1305 a_pUtf8Path = NULL;
1306 UErrorCode status = U_ZERO_ERROR;
1307 int32_t uLen = 0;
1308 u_strToUTF8(NULL, 0, &uLen, a_pPath, -1, &status);
1309 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
1310 return SI_FILE;
1311 }
1312 if (uLen < 0) {
1313 return SI_FILE;
1314 }
1315 char *pBuf = new (std::nothrow) char[static_cast<size_t>(uLen) + 1];
1316 if (!pBuf) {
1317 return SI_NOMEM;
1318 }
1319 status = U_ZERO_ERROR;
1320 u_strToUTF8(pBuf, uLen + 1, NULL, a_pPath, -1, &status);
1321 if (U_FAILURE(status)) {
1322 delete[] pBuf;
1323 return SI_FILE;
1324 }
1325 a_pUtf8Path = pBuf;
1326 return SI_OK;
1327}
1328#endif // SI_CONVERT_ICU
1329
1331inline bool GetFileSize(FILE *a_fpFile, size_t &a_uSize) {
1332 a_uSize = 0;
1333
1334#if defined(_WIN32) && !defined(_WIN32_WCE)
1335 if (_fseeki64(a_fpFile, 0, SEEK_END) != 0) {
1336 return false;
1337 }
1338 const __int64 nSize = _ftelli64(a_fpFile);
1339 if (nSize < 0) {
1340 return false;
1341 }
1342 if (static_cast<unsigned long long>(nSize) >
1343 static_cast<unsigned long long>(SI_MAX_FILE_SIZE)) {
1344 return false;
1345 }
1346 if (static_cast<unsigned long long>(nSize) >
1347 static_cast<unsigned long long>(std::numeric_limits<size_t>::max())) {
1348 return false;
1349 }
1350 a_uSize = static_cast<size_t>(nSize);
1351 if (_fseeki64(a_fpFile, 0, SEEK_SET) != 0) {
1352 return false;
1353 }
1354 return true;
1355#elif defined(_POSIX_VERSION) || defined(__unix__) || defined(__APPLE__)
1356 if (fseeko(a_fpFile, 0, SEEK_END) != 0) {
1357 return false;
1358 }
1359 const off_t nSize = ftello(a_fpFile);
1360 if (nSize < 0) {
1361 return false;
1362 }
1363 if (static_cast<unsigned long long>(nSize) >
1364 static_cast<unsigned long long>(SI_MAX_FILE_SIZE)) {
1365 return false;
1366 }
1367 if (static_cast<unsigned long long>(nSize) >
1368 static_cast<unsigned long long>(std::numeric_limits<size_t>::max())) {
1369 return false;
1370 }
1371 a_uSize = static_cast<size_t>(nSize);
1372 if (fseeko(a_fpFile, 0, SEEK_SET) != 0) {
1373 return false;
1374 }
1375 return true;
1376#else
1377 if (fseek(a_fpFile, 0, SEEK_END) != 0) {
1378 return false;
1379 }
1380 const long nSize = ftell(a_fpFile);
1381 if (nSize < 0) {
1382 return false;
1383 }
1384 if (static_cast<unsigned long long>(nSize) >
1385 static_cast<unsigned long long>(SI_MAX_FILE_SIZE)) {
1386 return false;
1387 }
1388 a_uSize = static_cast<size_t>(nSize);
1389 if (fseek(a_fpFile, 0, SEEK_SET) != 0) {
1390 return false;
1391 }
1392 return true;
1393#endif
1394}
1395} // namespace SI_Internal
1396
1397template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1399 const char *a_pszFile) {
1400 FILE *fp = NULL;
1401#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
1402 fopen_s(&fp, a_pszFile, "rb");
1403#else // !__STDC_WANT_SECURE_LIB__
1404 fp = fopen(a_pszFile, "rb");
1405#endif // __STDC_WANT_SECURE_LIB__
1406 if (!fp) {
1407 return SI_FILE;
1408 }
1409 SI_Error rc = LoadFile(fp);
1410 fclose(fp);
1411 return rc;
1412}
1413
1414#ifdef SI_HAS_WIDE_FILE
1415template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1417 const SI_WCHAR_T *a_pwszFile) {
1418#ifdef _WIN32
1419 FILE *fp = NULL;
1420#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
1421 _wfopen_s(&fp, a_pwszFile, L"rb");
1422#else // !__STDC_WANT_SECURE_LIB__
1423 fp = _wfopen(a_pwszFile, L"rb");
1424#endif // __STDC_WANT_SECURE_LIB__
1425 if (!fp)
1426 return SI_FILE;
1427 SI_Error rc = LoadFile(fp);
1428 fclose(fp);
1429 return rc;
1430#else // !_WIN32 (therefore SI_CONVERT_ICU)
1431 char *szFile = NULL;
1432 SI_Error rcPath = SI_Internal::UCharPathToUtf8(a_pwszFile, szFile);
1433 if (rcPath < 0) {
1434 return rcPath;
1435 }
1436 SI_Error rc = LoadFile(szFile);
1437 delete[] szFile;
1438 return rc;
1439#endif // _WIN32
1440}
1441#endif // SI_HAS_WIDE_FILE
1442
1443template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1444SI_Error
1446 size_t uSize = 0;
1447 if (!SI_Internal::GetFileSize(a_fpFile, uSize)) {
1448 return SI_FILE;
1449 }
1450 if (uSize == 0) {
1451 return SI_OK;
1452 }
1453
1454 // allocate and ensure NULL terminated
1455 char *pData = new (std::nothrow) char[uSize + 1];
1456 if (!pData) {
1457 return SI_NOMEM;
1458 }
1459 pData[uSize] = 0;
1460
1461 // load data into buffer
1462 size_t uRead = fread(pData, sizeof(char), uSize, a_fpFile);
1463 if (uRead != uSize) {
1464 delete[] pData;
1465 return SI_FILE;
1466 }
1467
1468 // convert the raw data to unicode
1469 SI_Error rc = LoadData(pData, uRead);
1470 delete[] pData;
1471 return rc;
1472}
1473
1474template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1476 const char *a_pData, size_t a_uDataLen) {
1477 if (!a_pData) {
1478 return SI_OK;
1479 }
1480
1481 // if the UTF-8 BOM exists, consume it and set mode to unicode, if we have
1482 // already loaded data and try to change mode half-way through then this will
1483 // be ignored and we will assert in debug versions
1484 if (a_uDataLen >= 3 && memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) {
1485 a_pData += 3;
1486 a_uDataLen -= 3;
1487 SI_ASSERT(m_bStoreIsUtf8 || !m_pData); // we don't expect mixed mode data
1488 SetUnicode();
1489 }
1490
1491 if (a_uDataLen == 0) {
1492 return SI_OK;
1493 }
1494
1495 // determine the length of the converted data
1496 SI_CONVERTER converter(m_bStoreIsUtf8);
1497 size_t uLen = converter.SizeFromStore(a_pData, a_uDataLen);
1498 if (uLen == (size_t)(-1)) {
1499 return SI_FAIL;
1500 }
1501
1502 // check converted data size is within supported limits (SI_MAX_FILE_SIZE)
1503 if (uLen >= (SI_MAX_FILE_SIZE / sizeof(SI_CHAR))) {
1504 return SI_FILE;
1505 }
1506
1507 // allocate memory for the data, ensure that there is a NULL
1508 // terminator wherever the converted data ends
1509 SI_CHAR *pData = new (std::nothrow) SI_CHAR[uLen + 1];
1510 if (!pData) {
1511 return SI_NOMEM;
1512 }
1513 memset(pData, 0, sizeof(SI_CHAR) * (uLen + 1));
1514
1515 // convert the data
1516 if (!converter.ConvertFromStore(a_pData, a_uDataLen, pData, uLen)) {
1517 delete[] pData;
1518 return SI_FAIL;
1519 }
1520
1521 // parse it
1522 const static SI_CHAR empty = 0;
1523 SI_CHAR *pWork = pData;
1524 const SI_CHAR *pSection = &empty;
1525 const SI_CHAR *pItem = NULL;
1526 const SI_CHAR *pVal = NULL;
1527 const SI_CHAR *pComment = NULL;
1528
1529 // We copy the strings if we are loading data into this class when we
1530 // already have stored some.
1531 bool bCopyStrings = (m_pData != NULL);
1532
1533 size_t nStringsBefore = 0;
1534 const SI_CHAR *pFileCommentBefore = NULL;
1535 TNamesDepend oAddedSections;
1536 TNamesDepend oAddedKeys;
1537 if (bCopyStrings) {
1538 nStringsBefore = m_strings.size();
1539 pFileCommentBefore = m_pFileComment;
1540 }
1541
1542 // find a file comment if it exists, this is a comment that starts at the
1543 // beginning of the file and continues until the first blank line.
1544 SI_Error rc = FindFileComment(pWork, bCopyStrings);
1545 if (rc < 0) {
1546 delete[] pData;
1547 if (bCopyStrings) {
1548 m_pFileComment = pFileCommentBefore;
1549 while (m_strings.size() > nStringsBefore) {
1550 delete[] const_cast<SI_CHAR *>(m_strings.back().pItem);
1551 m_strings.pop_back();
1552 }
1553 } else {
1554 m_pFileComment = NULL;
1555 }
1556 return rc;
1557 }
1558
1559 // add every entry in the file to the data table
1560 while (FindEntry(pWork, pSection, pItem, pVal, pComment)) {
1561 bool bSectionExisted = SectionExists(pSection);
1562 bool bKeyExisted = pItem && bSectionExisted && KeyExists(pSection, pItem);
1563
1564 rc = AddEntry(pSection, pItem, pVal, pComment, false, bCopyStrings);
1565 if (rc < 0) {
1566 if (bCopyStrings) {
1567 m_pFileComment = pFileCommentBefore;
1568 UndoIncrementalLoadData(oAddedSections, oAddedKeys);
1569 while (m_strings.size() > nStringsBefore) {
1570 delete[] const_cast<SI_CHAR *>(m_strings.back().pItem);
1571 m_strings.pop_back();
1572 }
1573 } else {
1574 m_data.clear();
1575 m_pFileComment = NULL;
1576 m_nOrder = 0;
1577 }
1578 delete[] pData;
1579 return rc;
1580 }
1581
1582 if (bCopyStrings) {
1583 if (!bSectionExisted && *pSection) {
1584 oAddedSections.push_back(Entry(pSection, NULL, 0));
1585 }
1586 if (pItem && !bKeyExisted) {
1587 oAddedKeys.push_back(Entry(pItem, pSection, 0));
1588 }
1589 }
1590 }
1591
1592 // store these strings if we didn't copy them
1593 if (bCopyStrings) {
1594 delete[] pData;
1595 } else {
1596 m_pData = pData;
1597 m_uDataLen = uLen + 1;
1598 }
1599
1600 return SI_OK;
1601}
1602
1603#ifdef SI_SUPPORT_IOSTREAMS
1604template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1606 std::istream &a_istream) {
1607 std::string strData;
1608 char buf[4096];
1609 while (a_istream.read(buf, sizeof(buf)) || a_istream.gcount() > 0) {
1610 const std::streamsize nRead = a_istream.gcount();
1611 for (std::streamsize i = 0; i < nRead; ++i) {
1612 if (buf[i] == '\0') {
1613 return SI_FAIL;
1614 }
1615 }
1616 if (strData.size() + static_cast<size_t>(nRead) > SI_MAX_FILE_SIZE) {
1617 return SI_FILE;
1618 }
1619 strData.append(buf, static_cast<size_t>(nRead));
1620 }
1621 return LoadData(strData.data(), strData.size());
1622}
1623#endif // SI_SUPPORT_IOSTREAMS
1624
1625template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1626SI_Error CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::FindFileComment(
1627 SI_CHAR *&a_pData, bool a_bCopyStrings) {
1628 // there can only be a single file comment
1629 if (m_pFileComment) {
1630 return SI_OK;
1631 }
1632
1633 // Load the file comment as multi-line text, this will modify all of
1634 // the newline characters to be single \n chars
1635 if (!LoadMultiLineText(a_pData, m_pFileComment, NULL, false)) {
1636 return SI_OK;
1637 }
1638
1639 // copy the string if necessary
1640 if (a_bCopyStrings) {
1641 SI_Error rc = CopyString(m_pFileComment);
1642 if (rc < 0)
1643 return rc;
1644 }
1645
1646 return SI_OK;
1647}
1648
1649template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1650bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::FindEntry(
1651 SI_CHAR *&a_pData, const SI_CHAR *&a_pSection, const SI_CHAR *&a_pKey,
1652 const SI_CHAR *&a_pVal, const SI_CHAR *&a_pComment) const {
1653 a_pComment = NULL;
1654 a_pVal = NULL;
1655
1656 bool bHaveValue = false;
1657 SI_CHAR *pTrail = NULL;
1658 while (*a_pData) {
1659 // skip spaces and empty lines
1660 while (*a_pData && IsSpace(*a_pData)) {
1661 ++a_pData;
1662 }
1663 if (!*a_pData) {
1664 break;
1665 }
1666
1667 // skip processing of comment lines but keep a pointer to
1668 // the start of the comment.
1669 if (IsComment(*a_pData)) {
1670 LoadMultiLineText(a_pData, a_pComment, NULL, true);
1671 continue;
1672 }
1673
1674 // process section names
1675 if (*a_pData == '[') {
1676 // skip leading spaces
1677 ++a_pData;
1678 while (*a_pData && IsSpace(*a_pData)) {
1679 ++a_pData;
1680 }
1681
1682 // find the end of the section name (it may contain spaces)
1683 // and convert it to lowercase as necessary
1684 a_pSection = a_pData;
1685 while (*a_pData && *a_pData != ']' && !IsNewLineChar(*a_pData)) {
1686 ++a_pData;
1687 }
1688
1689 // if it's an invalid line, just skip it
1690 if (*a_pData != ']') {
1691 continue;
1692 }
1693
1694 // remove trailing spaces from the section
1695 pTrail = a_pData - 1;
1696 while (pTrail >= a_pSection && IsSpace(*pTrail)) {
1697 --pTrail;
1698 }
1699 ++pTrail;
1700 *pTrail = 0;
1701
1702 // skip to the end of the line
1703 ++a_pData; // safe as checked that it == ']' above
1704 while (*a_pData && !IsNewLineChar(*a_pData)) {
1705 ++a_pData;
1706 }
1707
1708 a_pKey = NULL;
1709 a_pVal = NULL;
1710 return true;
1711 }
1712
1713 // find the end of the key name (it may contain spaces)
1714 a_pKey = a_pData;
1715 while (*a_pData && *a_pData != '=' && !IsNewLineChar(*a_pData)) {
1716 ++a_pData;
1717 }
1718 // *a_pData is null, equals, or newline
1719
1720 // if no value and we don't allow no value, then invalid
1721 bHaveValue = (*a_pData == '=');
1722 if (!bHaveValue && !m_bAllowKeyOnly) {
1723 continue;
1724 }
1725
1726 // empty keys are invalid
1727 if (bHaveValue && a_pKey == a_pData) {
1728 while (*a_pData && !IsNewLineChar(*a_pData)) {
1729 ++a_pData;
1730 }
1731 continue;
1732 }
1733
1734 // remove trailing spaces from the key
1735 pTrail = a_pData - 1;
1736 while (pTrail >= a_pKey && IsSpace(*pTrail)) {
1737 --pTrail;
1738 }
1739 ++pTrail;
1740
1741 if (bHaveValue) {
1742 // process the value
1743 *pTrail = 0;
1744
1745 // skip leading whitespace on the value
1746 ++a_pData; // safe as checked that it == '=' above
1747 while (*a_pData && !IsNewLineChar(*a_pData) && IsSpace(*a_pData)) {
1748 ++a_pData;
1749 }
1750
1751 // find the end of the value which is the end of this line
1752 a_pVal = a_pData;
1753 while (*a_pData && !IsNewLineChar(*a_pData)) {
1754 ++a_pData;
1755 }
1756
1757 // remove trailing spaces from the value
1758 pTrail = a_pData - 1;
1759 if (*a_pData) { // prepare for the next round
1760 SkipNewLine(a_pData);
1761 }
1762 while (pTrail >= a_pVal && IsSpace(*pTrail)) {
1763 --pTrail;
1764 }
1765 ++pTrail;
1766 *pTrail = 0;
1767
1768 // check for multi-line entries
1769 if (m_bAllowMultiLine && IsMultiLineTag(a_pVal)) {
1770 // skip the "<<<" to get the tag that will end the multiline
1771 const SI_CHAR *pTagName = a_pVal + 3;
1772 return LoadMultiLineText(a_pData, a_pVal, pTagName);
1773 }
1774
1775 // check for quoted values, we are not supporting escapes in quoted values (yet)
1776 if (m_bParseQuotes) {
1777 --pTrail;
1778 if (pTrail > a_pVal && *a_pVal == '"' && *pTrail == '"') {
1779 ++a_pVal;
1780 *pTrail = 0;
1781 }
1782 }
1783 } else {
1784 // no value to process, just prepare for the next
1785 if (*a_pData) {
1786 SkipNewLine(a_pData);
1787 }
1788 *pTrail = 0;
1789 a_pVal = NULL;
1790 }
1791
1792 // return the standard entry
1793 return true;
1794 }
1795
1796 return false;
1797}
1798
1799template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1800bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::IsMultiLineTag(
1801 const SI_CHAR *a_pVal) const {
1802 // check for the "<<<" prefix for a multi-line entry
1803 if (*a_pVal++ != '<')
1804 return false;
1805 if (*a_pVal++ != '<')
1806 return false;
1807 if (*a_pVal++ != '<')
1808 return false;
1809 return true;
1810}
1811
1812template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1813bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::IsMultiLineData(
1814 const SI_CHAR *a_pData) const {
1815 // data is multi-line if it has any of the following features:
1816 // * whitespace prefix
1817 // * embedded newlines
1818 // * whitespace suffix
1819 // * a "<<<" prefix, which would be read back as a multi-line tag
1820
1821 // empty string
1822 if (!*a_pData) {
1823 return false;
1824 }
1825
1826 // a leading "<<<" must be written as multi-line to survive a reload
1827 if (IsMultiLineTag(a_pData)) {
1828 return true;
1829 }
1830
1831 // check for prefix
1832 if (IsSpace(*a_pData)) {
1833 return true;
1834 }
1835
1836 // embedded newlines
1837 const SI_CHAR *pStart = a_pData;
1838 while (*a_pData) {
1839 if (IsNewLineChar(*a_pData)) {
1840 return true;
1841 }
1842 ++a_pData;
1843 }
1844
1845 // check for suffix (ensure we don't go before start of string)
1846 if (a_pData > pStart && IsSpace(*(a_pData - 1))) {
1847 return true;
1848 }
1849
1850 return false;
1851}
1852
1853template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1854bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::MultiLineTextHasTag(
1855 const SI_CHAR *a_pText, const SI_CHAR *a_pTag) const {
1856 if (!a_pText || !a_pTag) {
1857 return false;
1858 }
1859 const SI_CHAR *p = a_pText;
1860 while (*p) {
1861 const SI_CHAR *pEndOfLine = p;
1862 while (*pEndOfLine && *pEndOfLine != '\n') {
1863 ++pEndOfLine;
1864 }
1865 const std::basic_string<SI_CHAR> line(p,
1866 static_cast<size_t>(pEndOfLine - p));
1867 if (!IsLess(line.c_str(), a_pTag) && !IsLess(a_pTag, line.c_str())) {
1868 return true;
1869 }
1870 p = pEndOfLine;
1871 if (*p == '\n') {
1872 ++p;
1873 }
1874 }
1875 return false;
1876}
1877
1878template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1880 IsSingleLineQuotedValue(const SI_CHAR *a_pData) const {
1881 // data needs quoting if it starts or ends with whitespace
1882 // and doesn't have embedded newlines
1883
1884 // empty string
1885 if (!*a_pData) {
1886 return false;
1887 }
1888
1889 // embedded newlines
1890 const SI_CHAR *pStart = a_pData;
1891 while (*a_pData) {
1892 if (IsNewLineChar(*a_pData)) {
1893 return false;
1894 }
1895 ++a_pData;
1896 }
1897
1898 // check for prefix only after ruling out embedded newlines
1899 if (IsSpace(*pStart)) {
1900 return true;
1901 }
1902
1903 // check for suffix (ensure we don't go before start of string)
1904 if (a_pData > pStart && IsSpace(*(a_pData - 1))) {
1905 return true;
1906 }
1907
1908 // a value that is itself quoted needs wrapping, or the loader strips its quotes
1909 if (a_pData > pStart + 1 && *pStart == '"' && *(a_pData - 1) == '"') {
1910 return true;
1911 }
1912
1913 return false;
1914}
1915
1916template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1917bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::IsNewLineChar(
1918 SI_CHAR a_c) const {
1919 return (a_c == '\n' || a_c == '\r');
1920}
1921
1922template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
1923bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::LoadMultiLineText(
1924 SI_CHAR *&a_pData, const SI_CHAR *&a_pVal, const SI_CHAR *a_pTagName,
1925 bool a_bAllowBlankLinesInComment) const {
1926 // we modify this data to strip all newlines down to a single '\n'
1927 // character. This means that on Windows we need to strip out some
1928 // characters which will make the data shorter.
1929 // i.e. LINE1-LINE1\r\nLINE2-LINE2\0 will become
1930 // LINE1-LINE1\nLINE2-LINE2\0
1931 // The pDataLine entry is the pointer to the location in memory that
1932 // the current line needs to start to run following the existing one.
1933 // This may be the same as pCurrLine in which case no move is needed.
1934 SI_CHAR *pDataLine = a_pData;
1935 SI_CHAR *pCurrLine;
1936
1937 // value starts at the current line
1938 a_pVal = a_pData;
1939
1940 // find the end tag. This tag must start in column 1 and be
1941 // followed by a newline. We ignore any whitespace after the end
1942 // tag but not whitespace before it.
1943 SI_CHAR cEndOfLineChar = *a_pData;
1944 for (;;) {
1945 // if we are loading comments then we need a comment character as
1946 // the first character on every line
1947 if (!a_pTagName && !IsComment(*a_pData)) {
1948 // if we aren't allowing blank lines then we're done
1949 if (!a_bAllowBlankLinesInComment) {
1950 break;
1951 }
1952
1953 // if we are allowing blank lines then we only include them
1954 // in this comment if another comment follows, so read ahead
1955 // to find out.
1956 SI_CHAR *pCurr = a_pData;
1957 int nNewLines = 0;
1958 while (IsSpace(*pCurr)) {
1959 if (IsNewLineChar(*pCurr)) {
1960 ++nNewLines;
1961 SkipNewLine(pCurr);
1962 } else {
1963 ++pCurr;
1964 }
1965 }
1966
1967 // we have a comment, add the blank lines to the output
1968 // and continue processing from here
1969 if (IsComment(*pCurr)) {
1970 for (; nNewLines > 0; --nNewLines)
1971 *pDataLine++ = '\n';
1972 a_pData = pCurr;
1973 continue;
1974 }
1975
1976 // the comment ends here
1977 break;
1978 }
1979
1980 // find the end of this line
1981 pCurrLine = a_pData;
1982 while (*a_pData && !IsNewLineChar(*a_pData))
1983 ++a_pData;
1984
1985 // move this line down to the location that it should be if necessary
1986 if (pDataLine < pCurrLine) {
1987 size_t nLen = (size_t)(a_pData - pCurrLine);
1988 memmove(pDataLine, pCurrLine, nLen * sizeof(SI_CHAR));
1989 pDataLine[nLen] = '\0';
1990 }
1991
1992 // end the line with a NULL
1993 cEndOfLineChar = *a_pData;
1994 *a_pData = 0;
1995
1996 // if are looking for a tag then do the check now. This is done before
1997 // checking for end of the data, so that if we have the tag at the end
1998 // of the data then the tag is removed correctly.
1999 if (a_pTagName) {
2000 // strip whitespace from the end of this tag
2001 SI_CHAR *pc = a_pData - 1;
2002 while (pc > pDataLine && IsSpace(*pc))
2003 --pc;
2004 SI_CHAR ch = *++pc;
2005 *pc = 0;
2006
2007 if (!IsLess(pDataLine, a_pTagName) && !IsLess(a_pTagName, pDataLine)) {
2008 break;
2009 }
2010
2011 *pc = ch;
2012 }
2013
2014 // if we are at the end of the data then we just automatically end
2015 // this entry and return the current data.
2016 if (!cEndOfLineChar) {
2017 return true;
2018 }
2019
2020 // otherwise we need to process this newline to ensure that it consists
2021 // of just a single \n character.
2022 pDataLine += (a_pData - pCurrLine);
2023 *a_pData = cEndOfLineChar;
2024 SkipNewLine(a_pData);
2025 *pDataLine++ = '\n';
2026 }
2027
2028 // if we didn't find a comment at all then return false
2029 if (a_pVal == a_pData) {
2030 a_pVal = NULL;
2031 return false;
2032 }
2033
2034 // the data (which ends at the end of the last line) needs to be
2035 // null-terminated BEFORE before the newline character(s). If the
2036 // user wants a new line in the multi-line data then they need to
2037 // add an empty line before the tag.
2038 *--pDataLine = '\0';
2039
2040 // if looking for a tag and if we aren't at the end of the data,
2041 // then move a_pData to the start of the next line.
2042 if (a_pTagName && cEndOfLineChar) {
2043 SI_ASSERT(IsNewLineChar(cEndOfLineChar));
2044 *a_pData = cEndOfLineChar;
2045 SkipNewLine(a_pData);
2046 }
2047
2048 return true;
2049}
2050
2051template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2052SI_Error CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::CopyString(
2053 const SI_CHAR *&a_pString) {
2054 size_t uLen = 0;
2055 if (sizeof(SI_CHAR) == sizeof(char)) {
2056 uLen = strlen((const char *)a_pString);
2057 } else if (sizeof(SI_CHAR) == sizeof(wchar_t)) {
2058 uLen = wcslen((const wchar_t *)a_pString);
2059 } else {
2060 for (; a_pString[uLen]; ++uLen) /*loop*/
2061 ;
2062 }
2063 if (uLen >= (SI_MAX_FILE_SIZE / sizeof(SI_CHAR))) {
2064 return SI_NOMEM;
2065 }
2066 ++uLen; // NULL character
2067 SI_CHAR *pCopy = new (std::nothrow) SI_CHAR[uLen];
2068 if (!pCopy) {
2069 return SI_NOMEM;
2070 }
2071 memcpy(pCopy, a_pString, sizeof(SI_CHAR) * uLen);
2072 m_strings.push_back(pCopy);
2073 a_pString = pCopy;
2074 return SI_OK;
2075}
2076
2077template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2079 UndoIncrementalLoadData(const TNamesDepend &a_oAddedSections,
2080 const TNamesDepend &a_oAddedKeys) {
2081 typename TNamesDepend::const_iterator iAddedKey = a_oAddedKeys.begin();
2082 for (; iAddedKey != a_oAddedKeys.end(); ++iAddedKey) {
2083 Delete(iAddedKey->pComment, iAddedKey->pItem, false);
2084 }
2085
2086 typename TNamesDepend::const_iterator iAddedSection =
2087 a_oAddedSections.begin();
2088 for (; iAddedSection != a_oAddedSections.end(); ++iAddedSection) {
2089 Delete(iAddedSection->pItem, NULL, false);
2090 }
2091}
2092
2093template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2094SI_Error CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::AddEntry(
2095 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pValue,
2096 const SI_CHAR *a_pComment, bool a_bForceReplace, bool a_bCopyStrings) {
2097 SI_Error rc;
2098 bool bInserted = false;
2099 bool bInsertedSection = false;
2100 bool bCopiedComment = false;
2101 bool bCopiedSection = false;
2102 bool bCopiedKey = false;
2103
2104 SI_ASSERT(!a_pComment || IsComment(*a_pComment));
2105
2106 // if we are copying strings then make a copy of the comment now
2107 // because we will need it when we add the entry.
2108 if (a_bCopyStrings && a_pComment) {
2109 rc = CopyString(a_pComment);
2110 if (rc < 0)
2111 return rc;
2112 bCopiedComment = true;
2113 }
2114
2115 // create the section entry if necessary
2116 typename TSection::iterator iSection = m_data.find(a_pSection);
2117 if (iSection == m_data.end()) {
2118 // if the section doesn't exist then we need a copy as the
2119 // string needs to last beyond the end of this function
2120 if (a_bCopyStrings) {
2121 rc = CopyString(a_pSection);
2122 if (rc < 0) {
2123 if (bCopiedComment) {
2124 DeleteString(a_pComment);
2125 }
2126 return rc;
2127 }
2128 bCopiedSection = true;
2129 }
2130
2131 // only set the comment if this is a section only entry
2132 Entry oSection(a_pSection, ++m_nOrder);
2133 if (a_pComment && !a_pKey) {
2134 oSection.pComment = a_pComment;
2135 }
2136
2137 typename TSection::value_type oEntry(oSection, TKeyVal());
2138 typedef typename TSection::iterator SectionIterator;
2139 std::pair<SectionIterator, bool> i = m_data.insert(oEntry);
2140 iSection = i.first;
2141 bInserted = true;
2142 bInsertedSection = true;
2143 }
2144 if (!a_pKey) {
2145 // section only entries are specified with pItem as NULL
2146 return bInserted ? SI_INSERTED : SI_UPDATED;
2147 }
2148
2149 // check for existence of the key
2150 TKeyVal &keyval = iSection->second;
2151 typename TKeyVal::iterator iKey = keyval.find(a_pKey);
2152 bInserted = iKey == keyval.end();
2153
2154 // remove all existing entries but save the load order and
2155 // comment of the first entry
2156 int nLoadOrder = ++m_nOrder;
2157 if (iKey != keyval.end() && m_bAllowMultiKey && a_bForceReplace) {
2158 const SI_CHAR *pComment = NULL;
2159 while (iKey != keyval.end() && !IsLess(a_pKey, iKey->first.pItem)) {
2160 if (iKey->first.nOrder < nLoadOrder) {
2161 nLoadOrder = iKey->first.nOrder;
2162 pComment = iKey->first.pComment;
2163 }
2164 ++iKey;
2165 }
2166 if (pComment) {
2167 DeleteString(a_pComment);
2168 a_pComment = pComment;
2169 rc = CopyString(a_pComment);
2170 if (rc < 0)
2171 return rc;
2172 }
2173 Delete(a_pSection, a_pKey);
2174 iKey = keyval.end();
2175 }
2176
2177 // values need to be a valid string, even if they are an empty string
2178 if (!a_pValue) {
2179 a_pValue = &m_cEmptyString;
2180 }
2181
2182 // make string copies if necessary
2183 bool bForceCreateNewKey = m_bAllowMultiKey && !a_bForceReplace;
2184 if (a_bCopyStrings) {
2185 if (bForceCreateNewKey || iKey == keyval.end()) {
2186 // if the key doesn't exist then we need a copy as the
2187 // string needs to last beyond the end of this function
2188 // because we will be inserting the key next
2189 rc = CopyString(a_pKey);
2190 if (rc < 0) {
2191 if (bInsertedSection) {
2192 m_data.erase(iSection);
2193 }
2194 if (bCopiedSection) {
2195 DeleteString(a_pSection);
2196 }
2197 if (bCopiedComment) {
2198 DeleteString(a_pComment);
2199 }
2200 return rc;
2201 }
2202 bCopiedKey = true;
2203 }
2204
2205 // we always need a copy of the value
2206 rc = CopyString(a_pValue);
2207 if (rc < 0) {
2208 if (bCopiedKey) {
2209 DeleteString(a_pKey);
2210 }
2211 if (bInsertedSection) {
2212 m_data.erase(iSection);
2213 }
2214 if (bCopiedSection) {
2215 DeleteString(a_pSection);
2216 }
2217 if (bCopiedComment) {
2218 DeleteString(a_pComment);
2219 }
2220 return rc;
2221 }
2222 }
2223
2224 // create the key entry
2225 if (iKey == keyval.end() || bForceCreateNewKey) {
2226 Entry oKey(a_pKey, nLoadOrder);
2227 if (a_pComment) {
2228 oKey.pComment = a_pComment;
2229 }
2230 typename TKeyVal::value_type oEntry(oKey,
2231 static_cast<const SI_CHAR *>(NULL));
2232 iKey = keyval.insert(oEntry);
2233 } else {
2234 DeleteString(iKey->second);
2235 }
2236
2237 iKey->second = a_pValue;
2238 return bInserted ? SI_INSERTED : SI_UPDATED;
2239}
2240
2241template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2243 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pDefault,
2244 bool *a_pHasMultiple) const {
2245 if (a_pHasMultiple) {
2246 *a_pHasMultiple = false;
2247 }
2248 if (!a_pSection || !a_pKey) {
2249 return a_pDefault;
2250 }
2251 typename TSection::const_iterator iSection = m_data.find(a_pSection);
2252 if (iSection == m_data.end()) {
2253 return a_pDefault;
2254 }
2255 typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey);
2256 if (iKeyVal == iSection->second.end()) {
2257 return a_pDefault;
2258 }
2259
2260 // check for multiple entries with the same key
2261 if (m_bAllowMultiKey && a_pHasMultiple) {
2262 typename TKeyVal::const_iterator iTemp = iKeyVal;
2263 if (++iTemp != iSection->second.end()) {
2264 if (!IsLess(a_pKey, iTemp->first.pItem)) {
2265 *a_pHasMultiple = true;
2266 }
2267 }
2268 }
2269
2270 return iKeyVal->second;
2271}
2272
2273template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2275 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, long a_nDefault,
2276 bool *a_pHasMultiple) const {
2277 // return the default if we don't have a value
2278 const SI_CHAR *pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
2279 if (!pszValue || !*pszValue)
2280 return a_nDefault;
2281
2282 // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII
2283 char szValue[64] = {0};
2284 SI_CONVERTER c(m_bStoreIsUtf8);
2285 if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) {
2286 return a_nDefault;
2287 }
2288
2289 // handle the value as hex if prefaced with "0x"
2290 long nValue = a_nDefault;
2291 char *pszSuffix = szValue;
2292 errno = 0;
2293 if (szValue[0] == '0' && (szValue[1] == 'x' || szValue[1] == 'X')) {
2294 if (!szValue[2])
2295 return a_nDefault;
2296 nValue = strtol(&szValue[2], &pszSuffix, 16);
2297 } else {
2298 nValue = strtol(szValue, &pszSuffix, 10);
2299 }
2300
2301 // any invalid strings will return the default value
2302 if (*pszSuffix || errno == ERANGE) {
2303 return a_nDefault;
2304 }
2305
2306 return nValue;
2307}
2308
2309template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2311 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, long a_nValue,
2312 const SI_CHAR *a_pComment, bool a_bUseHex, bool a_bForceReplace) {
2313 // use SetValue to create sections
2314 if (!a_pSection || !a_pKey)
2315 return SI_FAIL;
2316
2317 // convert to an ASCII string
2318 char szInput[64];
2319#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2320 sprintf_s(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue);
2321#else // !__STDC_WANT_SECURE_LIB__
2322 snprintf(szInput, sizeof(szInput), a_bUseHex ? "0x%lx" : "%ld", a_nValue);
2323#endif // __STDC_WANT_SECURE_LIB__
2324
2325 // convert to output text
2326 SI_CHAR szOutput[64];
2327 SI_CONVERTER c(m_bStoreIsUtf8);
2328 if (!c.ConvertFromStore(szInput, strlen(szInput) + 1, szOutput,
2329 sizeof(szOutput) / sizeof(SI_CHAR))) {
2330 return SI_FAIL;
2331 }
2332
2333 // actually add it
2334 return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace,
2335 true);
2336}
2337
2338template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2340 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, double a_nDefault,
2341 bool *a_pHasMultiple) const {
2342 // return the default if we don't have a value
2343 const SI_CHAR *pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
2344 if (!pszValue || !*pszValue)
2345 return a_nDefault;
2346
2347 // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII
2348 char szValue[64] = {0};
2349 SI_CONVERTER c(m_bStoreIsUtf8);
2350 if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) {
2351 return a_nDefault;
2352 }
2353
2354 char *pszSuffix = szValue;
2355 double nValue = strtod(szValue, &pszSuffix);
2356
2357 // any invalid strings will return the default value
2358 // check if no conversion was performed or if there are trailing characters
2359 if (pszSuffix == szValue || *pszSuffix) {
2360 return a_nDefault;
2361 }
2362
2363 return nValue;
2364}
2365
2366template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2368 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, double a_nValue,
2369 const SI_CHAR *a_pComment, bool a_bForceReplace) {
2370 // use SetValue to create sections
2371 if (!a_pSection || !a_pKey)
2372 return SI_FAIL;
2373
2374 // convert to an ASCII string
2375 char szInput[64];
2376#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2377 sprintf_s(szInput, "%.17g", a_nValue);
2378#else // !__STDC_WANT_SECURE_LIB__
2379 snprintf(szInput, sizeof(szInput), "%.17g", a_nValue);
2380#endif // __STDC_WANT_SECURE_LIB__
2381
2382 // convert to output text
2383 SI_CHAR szOutput[64];
2384 SI_CONVERTER c(m_bStoreIsUtf8);
2385 if (!c.ConvertFromStore(szInput, strlen(szInput) + 1, szOutput,
2386 sizeof(szOutput) / sizeof(SI_CHAR))) {
2387 return SI_FAIL;
2388 }
2389
2390 // actually add it
2391 return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace,
2392 true);
2393}
2394
2395template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2397 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bDefault,
2398 bool *a_pHasMultiple) const {
2399 // return the default if we don't have a value
2400 const SI_CHAR *pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
2401 if (!pszValue || !*pszValue)
2402 return a_bDefault;
2403
2404 // we only look at the minimum number of characters
2405 switch (pszValue[0]) {
2406 case 't':
2407 case 'T': // true
2408 case 'y':
2409 case 'Y': // yes
2410 case '1': // 1 (one)
2411 return true;
2412
2413 case 'f':
2414 case 'F': // false
2415 case 'n':
2416 case 'N': // no
2417 case '0': // 0 (zero)
2418 return false;
2419
2420 case 'o':
2421 case 'O':
2422 if (pszValue[1] == 'n' || pszValue[1] == 'N')
2423 return true; // on
2424 if (pszValue[1] == 'f' || pszValue[1] == 'F')
2425 return false; // off
2426 break;
2427 }
2428
2429 // no recognized value, return the default
2430 return a_bDefault;
2431}
2432
2433template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2435 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bValue,
2436 const SI_CHAR *a_pComment, bool a_bForceReplace) {
2437 // use SetValue to create sections
2438 if (!a_pSection || !a_pKey)
2439 return SI_FAIL;
2440
2441 // convert to an ASCII string
2442 const char *pszInput = a_bValue ? "true" : "false";
2443
2444 // convert to output text
2445 SI_CHAR szOutput[64];
2446 SI_CONVERTER c(m_bStoreIsUtf8);
2447 if (!c.ConvertFromStore(pszInput, strlen(pszInput) + 1, szOutput,
2448 sizeof(szOutput) / sizeof(SI_CHAR))) {
2449 return SI_FAIL;
2450 }
2451
2452 // actually add it
2453 return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace,
2454 true);
2455}
2456
2457template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2459 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey,
2460 TNamesDepend &a_values) const {
2461 a_values.clear();
2462
2463 if (!a_pSection || !a_pKey) {
2464 return false;
2465 }
2466 typename TSection::const_iterator iSection = m_data.find(a_pSection);
2467 if (iSection == m_data.end()) {
2468 return false;
2469 }
2470 typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey);
2471 if (iKeyVal == iSection->second.end()) {
2472 return false;
2473 }
2474
2475 // insert all values for this key
2476 a_values.push_back(
2477 Entry(iKeyVal->second, iKeyVal->first.pComment, iKeyVal->first.nOrder));
2478 if (m_bAllowMultiKey) {
2479 ++iKeyVal;
2480 while (iKeyVal != iSection->second.end() &&
2481 !IsLess(a_pKey, iKeyVal->first.pItem)) {
2482 a_values.push_back(Entry(iKeyVal->second, iKeyVal->first.pComment,
2483 iKeyVal->first.nOrder));
2484 ++iKeyVal;
2485 }
2486 }
2487
2488 return true;
2489}
2490
2491template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2493 const SI_CHAR *a_pSection) const {
2494 if (!a_pSection) {
2495 return -1;
2496 }
2497
2498 typename TSection::const_iterator iSection = m_data.find(a_pSection);
2499 if (iSection == m_data.end()) {
2500 return -1;
2501 }
2502 const TKeyVal &section = iSection->second;
2503
2504 // if multi-key isn't permitted then the section size is
2505 // the number of keys that we have.
2506 if (!m_bAllowMultiKey || section.empty()) {
2507 return (int)section.size();
2508 }
2509
2510 // otherwise we need to count them
2511 int nCount = 0;
2512 const SI_CHAR *pLastKey = NULL;
2513 typename TKeyVal::const_iterator iKeyVal = section.begin();
2514 for (; iKeyVal != section.end(); ++iKeyVal) {
2515 if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) {
2516 ++nCount;
2517 pLastKey = iKeyVal->first.pItem;
2518 }
2519 }
2520 return nCount;
2521}
2522
2523template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2526 const SI_CHAR *a_pSection) const {
2527 if (a_pSection) {
2528 typename TSection::const_iterator i = m_data.find(a_pSection);
2529 if (i != m_data.end()) {
2530 return &(i->second);
2531 }
2532 }
2533 return 0;
2534}
2535
2536template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2538 TNamesDepend &a_names) const {
2539 a_names.clear();
2540 typename TSection::const_iterator i = m_data.begin();
2541 for (; i != m_data.end(); ++i) {
2542 a_names.push_back(i->first);
2543 }
2544}
2545
2546template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2548 const SI_CHAR *a_pSection, TNamesDepend &a_names) const {
2549 a_names.clear();
2550
2551 if (!a_pSection) {
2552 return false;
2553 }
2554
2555 typename TSection::const_iterator iSection = m_data.find(a_pSection);
2556 if (iSection == m_data.end()) {
2557 return false;
2558 }
2559
2560 const TKeyVal &section = iSection->second;
2561 const SI_CHAR *pLastKey = NULL;
2562 typename TKeyVal::const_iterator iKeyVal = section.begin();
2563 for (; iKeyVal != section.end(); ++iKeyVal) {
2564 if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) {
2565 a_names.push_back(iKeyVal->first);
2566 pLastKey = iKeyVal->first.pItem;
2567 }
2568 }
2569
2570 return true;
2571}
2572
2573template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2575 const char *a_pszFile, bool a_bAddSignature) const {
2576 FILE *fp = NULL;
2577#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2578 fopen_s(&fp, a_pszFile, "wb");
2579#else // !__STDC_WANT_SECURE_LIB__
2580 fp = fopen(a_pszFile, "wb");
2581#endif // __STDC_WANT_SECURE_LIB__
2582 if (!fp)
2583 return SI_FILE;
2584 SI_Error rc = SaveFile(fp, a_bAddSignature);
2585 fclose(fp);
2586 return rc;
2587}
2588
2589#ifdef SI_HAS_WIDE_FILE
2590template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2592 const SI_WCHAR_T *a_pwszFile, bool a_bAddSignature) const {
2593#ifdef _WIN32
2594 FILE *fp = NULL;
2595#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2596 _wfopen_s(&fp, a_pwszFile, L"wb");
2597#else // !__STDC_WANT_SECURE_LIB__
2598 fp = _wfopen(a_pwszFile, L"wb");
2599#endif // __STDC_WANT_SECURE_LIB__
2600 if (!fp)
2601 return SI_FILE;
2602 SI_Error rc = SaveFile(fp, a_bAddSignature);
2603 fclose(fp);
2604 return rc;
2605#else // !_WIN32 (therefore SI_CONVERT_ICU)
2606 char *szFile = NULL;
2607 SI_Error rcPath = SI_Internal::UCharPathToUtf8(a_pwszFile, szFile);
2608 if (rcPath < 0) {
2609 return rcPath;
2610 }
2611 SI_Error rc = SaveFile(szFile, a_bAddSignature);
2612 delete[] szFile;
2613 return rc;
2614#endif // _WIN32
2615}
2616#endif // SI_HAS_WIDE_FILE
2617
2618template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2620 FILE *a_pFile, bool a_bAddSignature) const {
2621 FileWriter writer(a_pFile);
2622 return Save(writer, a_bAddSignature);
2623}
2624
2625template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2627 OutputWriter &a_oOutput, bool a_bAddSignature) const {
2628 Converter convert(m_bStoreIsUtf8);
2629
2630 // add the UTF-8 signature if it is desired
2631 if (m_bStoreIsUtf8 && a_bAddSignature) {
2632 a_oOutput.Write(SI_UTF8_SIGNATURE);
2633 }
2634
2635 // get all of the sections sorted in load order
2636 TNamesDepend oSections;
2637 GetAllSections(oSections);
2638#if defined(_MSC_VER) && _MSC_VER <= 1200
2639 oSections.sort();
2640#elif defined(__BORLANDC__)
2641 oSections.sort(Entry::LoadOrder());
2642#else
2643 oSections.sort(typename Entry::LoadOrder());
2644#endif
2645
2646 // if there is an empty section name, then it must be written out first
2647 // regardless of the load order
2648 typename TNamesDepend::iterator is = oSections.begin();
2649 for (; is != oSections.end(); ++is) {
2650 if (!*is->pItem) {
2651 // move the empty section name to the front of the section list
2652 if (is != oSections.begin()) {
2653 oSections.splice(oSections.begin(), oSections, is, std::next(is));
2654 }
2655 break;
2656 }
2657 }
2658
2659 // write the file comment if we have one
2660 bool bNeedNewLine = false;
2661 if (m_pFileComment) {
2662 if (!OutputMultiLineText(a_oOutput, convert, m_pFileComment)) {
2663 return SI_FAIL;
2664 }
2665 bNeedNewLine = true;
2666 }
2667
2668 // iterate through our sections and output the data
2669 typename TNamesDepend::const_iterator iSection = oSections.begin();
2670 for (; iSection != oSections.end(); ++iSection) {
2671 // write out the comment if there is one
2672 if (iSection->pComment) {
2673 if (bNeedNewLine) {
2674 a_oOutput.Write(SI_NEWLINE_A);
2675 a_oOutput.Write(SI_NEWLINE_A);
2676 }
2677 if (!OutputMultiLineText(a_oOutput, convert, iSection->pComment)) {
2678 return SI_FAIL;
2679 }
2680 bNeedNewLine = false;
2681 }
2682
2683 if (bNeedNewLine) {
2684 a_oOutput.Write(SI_NEWLINE_A);
2685 a_oOutput.Write(SI_NEWLINE_A);
2686 bNeedNewLine = false;
2687 }
2688
2689 // write the section (unless there is no section name)
2690 if (*iSection->pItem) {
2691 if (!convert.ConvertToStore(iSection->pItem)) {
2692 return SI_FAIL;
2693 }
2694 a_oOutput.Write("[");
2695 a_oOutput.Write(convert.Data());
2696 a_oOutput.Write("]");
2697 a_oOutput.Write(SI_NEWLINE_A);
2698 }
2699
2700 // get all of the keys sorted in load order
2701 TNamesDepend oKeys;
2702 GetAllKeys(iSection->pItem, oKeys);
2703#if defined(_MSC_VER) && _MSC_VER <= 1200
2704 oKeys.sort();
2705#elif defined(__BORLANDC__)
2706 oKeys.sort(Entry::LoadOrder());
2707#else
2708 oKeys.sort(typename Entry::LoadOrder());
2709#endif
2710
2711 // write all keys and values
2712 typename TNamesDepend::const_iterator iKey = oKeys.begin();
2713 for (; iKey != oKeys.end(); ++iKey) {
2714 // get all values for this key
2715 TNamesDepend oValues;
2716 GetAllValues(iSection->pItem, iKey->pItem, oValues);
2717
2718 typename TNamesDepend::const_iterator iValue = oValues.begin();
2719 for (; iValue != oValues.end(); ++iValue) {
2720 // write out the comment if there is one
2721 if (iValue->pComment) {
2722 a_oOutput.Write(SI_NEWLINE_A);
2723 if (!OutputMultiLineText(a_oOutput, convert, iValue->pComment)) {
2724 return SI_FAIL;
2725 }
2726 }
2727
2728 // write the key
2729 if (!convert.ConvertToStore(iKey->pItem)) {
2730 return SI_FAIL;
2731 }
2732 a_oOutput.Write(convert.Data());
2733
2734 // write the value as long
2735 if (*iValue->pItem || !m_bAllowKeyOnly) {
2736 if (!convert.ConvertToStore(iValue->pItem)) {
2737 return SI_FAIL;
2738 }
2739 a_oOutput.Write(m_bSpaces ? " = " : "=");
2740 if (m_bParseQuotes && IsSingleLineQuotedValue(iValue->pItem)) {
2741 // the only way to preserve external whitespace on a value (i.e. before or after)
2742 // is to quote it. This is simple quoting, we don't escape quotes within the data.
2743 a_oOutput.Write("\"");
2744 a_oOutput.Write(convert.Data());
2745 a_oOutput.Write("\"");
2746 } else if (m_bAllowMultiLine && IsMultiLineData(iValue->pItem)) {
2747 // multi-line data needs to be processed specially to ensure
2748 // that we use the correct newline format for the current system
2749 char szTag[64];
2750 SI_CHAR szTagSi[64];
2751 int nSuffix = 0;
2752 for (;;) {
2753 if (nSuffix == 0) {
2754#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2755 sprintf_s(szTag, "END_OF_TEXT");
2756#else
2757 snprintf(szTag, sizeof(szTag), "END_OF_TEXT");
2758#endif
2759 } else {
2760#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
2761 sprintf_s(szTag, "END_OF_TEXT_%d", nSuffix);
2762#else
2763 snprintf(szTag, sizeof(szTag), "END_OF_TEXT_%d", nSuffix);
2764#endif
2765 }
2766 size_t u = 0;
2767 for (; szTag[u] && u + 1 < sizeof(szTagSi) / sizeof(szTagSi[0]);
2768 ++u) {
2769 szTagSi[u] = static_cast<SI_CHAR>(szTag[u]);
2770 }
2771 szTagSi[u] = 0;
2772 if (!MultiLineTextHasTag(iValue->pItem, szTagSi)) {
2773 break;
2774 }
2775 ++nSuffix;
2776 }
2777 a_oOutput.Write("<<<");
2778 a_oOutput.Write(szTag);
2779 a_oOutput.Write(SI_NEWLINE_A);
2780 if (!OutputMultiLineText(a_oOutput, convert, iValue->pItem)) {
2781 return SI_FAIL;
2782 }
2783 a_oOutput.Write(szTag);
2784 } else {
2785 a_oOutput.Write(convert.Data());
2786 }
2787 }
2788 a_oOutput.Write(SI_NEWLINE_A);
2789 }
2790 }
2791
2792 bNeedNewLine = true;
2793 }
2794
2795 return SI_OK;
2796}
2797
2798template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2799bool CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::OutputMultiLineText(
2800 OutputWriter &a_oOutput, Converter &a_oConverter,
2801 const SI_CHAR *a_pText) const {
2802 const SI_CHAR *pEndOfLine;
2803 SI_CHAR cEndOfLineChar = *a_pText;
2804 while (cEndOfLineChar) {
2805 // find the end of this line
2806 pEndOfLine = a_pText;
2807 for (; *pEndOfLine && *pEndOfLine != '\n'; ++pEndOfLine) /*loop*/
2808 ;
2809 cEndOfLineChar = *pEndOfLine;
2810
2811 const std::basic_string<SI_CHAR> line(
2812 a_pText, static_cast<size_t>(pEndOfLine - a_pText));
2813 if (!a_oConverter.ConvertToStore(line.c_str())) {
2814 return false;
2815 }
2816 a_pText += (pEndOfLine - a_pText) + 1;
2817 a_oOutput.Write(a_oConverter.Data());
2818 a_oOutput.Write(SI_NEWLINE_A);
2819 }
2820 return true;
2821}
2822
2823template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2825 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bRemoveEmpty) {
2826 return DeleteValue(a_pSection, a_pKey, NULL, a_bRemoveEmpty);
2827}
2828
2829template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2831 const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pValue,
2832 bool a_bRemoveEmpty) {
2833 if (!a_pSection) {
2834 return false;
2835 }
2836
2837 typename TSection::iterator iSection = m_data.find(a_pSection);
2838 if (iSection == m_data.end()) {
2839 return false;
2840 }
2841
2842 // remove a single key if we have a keyname
2843 if (a_pKey) {
2844 typename TKeyVal::iterator iKeyVal = iSection->second.find(a_pKey);
2845 if (iKeyVal == iSection->second.end()) {
2846 return false;
2847 }
2848
2849 const static SI_STRLESS isLess = SI_STRLESS();
2850
2851 // remove any copied strings and then the key
2852 typename TKeyVal::iterator iDelete;
2853 bool bDeleted = false;
2854 do {
2855 iDelete = iKeyVal++;
2856
2857 if (a_pValue == NULL || (isLess(a_pValue, iDelete->second) == false &&
2858 isLess(iDelete->second, a_pValue) == false)) {
2859 DeleteString(iDelete->first.pItem);
2860 DeleteString(iDelete->first.pComment);
2861 DeleteString(iDelete->second);
2862 iSection->second.erase(iDelete);
2863 bDeleted = true;
2864 }
2865 } while (iKeyVal != iSection->second.end() &&
2866 !IsLess(a_pKey, iKeyVal->first.pItem));
2867
2868 if (!bDeleted) {
2869 return false;
2870 }
2871
2872 // done now if the section is not empty or we are not pruning away
2873 // the empty sections. Otherwise let it fall through into the section
2874 // deletion code
2875 if (!a_bRemoveEmpty || !iSection->second.empty()) {
2876 return true;
2877 }
2878 } else {
2879 // delete all copied strings from this section. The actual
2880 // entries will be removed when the section is removed.
2881 typename TKeyVal::iterator iKeyVal = iSection->second.begin();
2882 for (; iKeyVal != iSection->second.end(); ++iKeyVal) {
2883 DeleteString(iKeyVal->first.pItem);
2884 DeleteString(iKeyVal->first.pComment);
2885 DeleteString(iKeyVal->second);
2886 }
2887 }
2888
2889 // delete the section itself
2890 DeleteString(iSection->first.pItem);
2891 DeleteString(iSection->first.pComment);
2892 m_data.erase(iSection);
2893
2894 return true;
2895}
2896
2897template <class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
2898void CSimpleIniTempl<SI_CHAR, SI_STRLESS, SI_CONVERTER>::DeleteString(
2899 const SI_CHAR *a_pString) {
2900 if (!a_pString) {
2901 return;
2902 }
2903 // strings may exist either inside the data block, or they will be
2904 // individually allocated and stored in m_strings. We only physically
2905 // delete those stored in m_strings.
2906 if (!m_pData || a_pString < m_pData || a_pString >= m_pData + m_uDataLen) {
2907 typename TNamesDepend::iterator i = m_strings.begin();
2908 for (; i != m_strings.end(); ++i) {
2909 if (a_pString == i->pItem) {
2910 delete[] const_cast<SI_CHAR *>(i->pItem);
2911 m_strings.erase(i);
2912 break;
2913 }
2914 }
2915 }
2916}
2917
2918// ---------------------------------------------------------------------------
2919// CONVERSION FUNCTIONS
2920// ---------------------------------------------------------------------------
2921
2922// Defines the conversion classes for different libraries. Before including
2923// SimpleIni.h, set the converter that you wish you use by defining one of the
2924// following symbols.
2925//
2926// SI_NO_CONVERSION Do not make the "W" wide character version of the
2927// library available. Only CSimpleIniA etc is defined.
2928// Default on Linux/MacOS/etc.
2929// SI_CONVERT_WIN32 Use the Win32 API functions for conversion.
2930// Default on Windows.
2931// SI_CONVERT_GENERIC Use built-in locale-independent UTF-8 conversion
2932// (no additional source files required)
2933// SI_CONVERT_ICU Use the IBM ICU conversion library. Requires
2934// ICU headers on include path and icuuc.lib
2935
2936#if !defined(SI_NO_CONVERSION) && !defined(SI_CONVERT_GENERIC) && \
2937 !defined(SI_CONVERT_WIN32) && !defined(SI_CONVERT_ICU)
2938#ifdef _WIN32
2939#define SI_CONVERT_WIN32
2940#else
2941#define SI_NO_CONVERSION
2942#endif
2943#endif
2944
2950template <class SI_CHAR> struct SI_GenericCase {
2951 bool operator()(const SI_CHAR *pLeft, const SI_CHAR *pRight) const {
2952 long cmp;
2953 for (; *pLeft && *pRight; ++pLeft, ++pRight) {
2954 cmp = (long)*pLeft - (long)*pRight;
2955 if (cmp != 0) {
2956 return cmp < 0;
2957 }
2958 }
2959 return *pRight != 0;
2960 }
2961};
2962
2969template <class SI_CHAR> struct SI_GenericNoCase {
2970 inline SI_CHAR locase(SI_CHAR ch) const {
2971 return (ch < 'A' || ch > 'Z') ? ch : (ch - 'A' + 'a');
2972 }
2973 bool operator()(const SI_CHAR *pLeft, const SI_CHAR *pRight) const {
2974 long cmp;
2975 for (; *pLeft && *pRight; ++pLeft, ++pRight) {
2976 cmp = (long)locase(*pLeft) - (long)locase(*pRight);
2977 if (cmp != 0) {
2978 return cmp < 0;
2979 }
2980 }
2981 return *pRight != 0;
2982 }
2983};
2984
2988template <class SI_CHAR> class SI_ConvertA {
2989 bool m_bStoreIsUtf8;
2990
2991protected:
2992 SI_ConvertA() {}
2993
2994public:
2995 SI_ConvertA(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) {}
2996
2997 /* copy and assignment */
2998 SI_ConvertA(const SI_ConvertA &rhs) { operator=(rhs); }
2999 SI_ConvertA &operator=(const SI_ConvertA &rhs) {
3000 m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8;
3001 return *this;
3002 }
3003
3017 size_t SizeFromStore(const char *a_pInputData, size_t a_uInputDataLen) {
3018 (void)a_pInputData;
3019 SI_ASSERT(a_uInputDataLen != (size_t)-1);
3020
3021 // ASCII/MBCS/UTF-8 needs no conversion
3022 return a_uInputDataLen;
3023 }
3024
3038 bool ConvertFromStore(const char *a_pInputData, size_t a_uInputDataLen,
3039 SI_CHAR *a_pOutputData, size_t a_uOutputDataSize) {
3040 // ASCII/MBCS/UTF-8 needs no conversion
3041 if (a_uInputDataLen > a_uOutputDataSize) {
3042 return false;
3043 }
3044 memcpy(a_pOutputData, a_pInputData, a_uInputDataLen);
3045 return true;
3046 }
3047
3058 size_t SizeToStore(const SI_CHAR *a_pInputData) {
3059 // ASCII/MBCS/UTF-8 needs no conversion
3060 return strlen((const char *)a_pInputData) + 1;
3061 }
3062
3076 bool ConvertToStore(const SI_CHAR *a_pInputData, char *a_pOutputData,
3077 size_t a_uOutputDataSize) {
3078 // calc input string length (SI_CHAR type and size independent)
3079 size_t uInputLen = strlen((const char *)a_pInputData) + 1;
3080 if (uInputLen > a_uOutputDataSize) {
3081 return false;
3082 }
3083
3084 // ascii/UTF-8 needs no conversion
3085 memcpy(a_pOutputData, a_pInputData, uInputLen);
3086 return true;
3087 }
3088};
3089
3090// ---------------------------------------------------------------------------
3091// SI_CONVERT_GENERIC
3092// ---------------------------------------------------------------------------
3093#ifdef SI_CONVERT_GENERIC
3094
3095#define SI_Case SI_GenericCase
3096#define SI_NoCase SI_GenericNoCase
3097
3098#include <wchar.h>
3099
3100namespace SI_UTF8 {
3101
3102constexpr char32_t REPLACEMENT = 0xFFFD;
3103
3105inline bool Decode(const char *&a_src, const char *a_end, char32_t &a_cp) {
3106 if (a_src >= a_end) {
3107 return false;
3108 }
3109 const unsigned char c0 = (unsigned char)*a_src++;
3110 if (c0 < 0x80) {
3111 a_cp = c0;
3112 return true;
3113 }
3114 if (c0 < 0xC2) {
3115 return false;
3116 }
3117
3118 int extra = 0;
3119 char32_t ch = 0;
3120 if (c0 < 0xE0) {
3121 extra = 1;
3122 ch = c0 & 0x1F;
3123 } else if (c0 < 0xF0) {
3124 extra = 2;
3125 ch = c0 & 0x0F;
3126 } else if (c0 < 0xF5) {
3127 extra = 3;
3128 ch = c0 & 0x07;
3129 } else {
3130 return false;
3131 }
3132 if (a_src + extra > a_end) {
3133 a_src -= 1;
3134 return false;
3135 }
3136
3137 for (int i = 0; i < extra; ++i) {
3138 const unsigned char cx = (unsigned char)*a_src++;
3139 if ((cx & 0xC0) != 0x80) {
3140 a_src -= (i + 2);
3141 return false;
3142 }
3143 ch = (ch << 6) | (cx & 0x3F);
3144 }
3145
3146 if ((extra == 1 && ch < 0x80) || (extra == 2 && ch < 0x800) ||
3147 (extra == 3 && ch < 0x10000) || ch > 0x10FFFF ||
3148 (ch >= 0xD800 && ch <= 0xDFFF)) {
3149 a_src -= (extra + 1);
3150 return false;
3151 }
3152
3153 a_cp = ch;
3154 return true;
3155}
3156
3160inline int Encode(char32_t a_cp, char *a_dst, size_t a_cap) {
3161 if (a_cp >= 0x110000 || (a_cp >= 0xD800 && a_cp <= 0xDFFF)) {
3162 a_cp = REPLACEMENT;
3163 }
3164
3165 if (a_cp < 0x80) {
3166 if (a_cap < 1)
3167 return -1;
3168 a_dst[0] = (char)a_cp;
3169 return 1;
3170 }
3171 if (a_cp < 0x800) {
3172 if (a_cap < 2)
3173 return -1;
3174 a_dst[0] = (char)(0xC0 | (a_cp >> 6));
3175 a_dst[1] = (char)(0x80 | (a_cp & 0x3F));
3176 return 2;
3177 }
3178 if (a_cp < 0x10000) {
3179 if (a_cap < 3)
3180 return -1;
3181 a_dst[0] = (char)(0xE0 | (a_cp >> 12));
3182 a_dst[1] = (char)(0x80 | ((a_cp >> 6) & 0x3F));
3183 a_dst[2] = (char)(0x80 | (a_cp & 0x3F));
3184 return 3;
3185 }
3186 if (a_cap < 4)
3187 return -1;
3188 a_dst[0] = (char)(0xF0 | (a_cp >> 18));
3189 a_dst[1] = (char)(0x80 | ((a_cp >> 12) & 0x3F));
3190 a_dst[2] = (char)(0x80 | ((a_cp >> 6) & 0x3F));
3191 a_dst[3] = (char)(0x80 | (a_cp & 0x3F));
3192 return 4;
3193}
3194
3195template <class SI_CHAR>
3196inline bool AppendCodePoint(char32_t a_cp, SI_CHAR *&a_dst, SI_CHAR *a_dstEnd) {
3197 if (sizeof(SI_CHAR) >= sizeof(char32_t)) {
3198 if (a_dst >= a_dstEnd)
3199 return false;
3200 *a_dst++ = (SI_CHAR)a_cp;
3201 return true;
3202 }
3203 if (sizeof(SI_CHAR) == 2) {
3204 if (a_cp < 0x10000) {
3205 if (a_dst >= a_dstEnd)
3206 return false;
3207 *a_dst++ = (SI_CHAR)a_cp;
3208 return true;
3209 }
3210 if (a_dst + 1 >= a_dstEnd)
3211 return false;
3212 a_cp -= 0x10000;
3213 *a_dst++ = (SI_CHAR)(0xD800 + (a_cp >> 10));
3214 *a_dst++ = (SI_CHAR)(0xDC00 + (a_cp & 0x3FF));
3215 return true;
3216 }
3217 return false;
3218}
3219
3220template <class SI_CHAR>
3221inline bool ReadCodePoint(const SI_CHAR *&a_src, char32_t &a_cp) {
3222 if (sizeof(SI_CHAR) >= sizeof(char32_t)) {
3223 a_cp = (char32_t)*a_src++;
3224 return true;
3225 }
3226 if (sizeof(SI_CHAR) == 2) {
3227 const char32_t w = (char32_t)*a_src++;
3228 if (w >= 0xD800 && w <= 0xDBFF) {
3229 if (*a_src >= 0xDC00 && *a_src <= 0xDFFF) {
3230 a_cp = 0x10000 + ((w - 0xD800) << 10) + ((char32_t)*a_src++ - 0xDC00);
3231 return true;
3232 }
3233 }
3234 a_cp = w;
3235 return true;
3236 }
3237 return false;
3238}
3239
3240} // namespace SI_UTF8
3241
3246template <class SI_CHAR> class SI_ConvertW {
3247 bool m_bStoreIsUtf8;
3248
3249protected:
3250 SI_ConvertW() {}
3251
3252public:
3253 SI_ConvertW(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) {}
3254
3255 /* copy and assignment */
3256 SI_ConvertW(const SI_ConvertW &rhs) { operator=(rhs); }
3257 SI_ConvertW &operator=(const SI_ConvertW &rhs) {
3258 m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8;
3259 return *this;
3260 }
3261
3275 size_t SizeFromStore(const char *a_pInputData, size_t a_uInputDataLen) {
3276 SI_ASSERT(a_uInputDataLen != (size_t)-1);
3277
3278 if (m_bStoreIsUtf8) {
3279 // worst case scenario for UTF-8 to wchar_t is 1 char -> 1 wchar_t
3280 // so we just return the same number of characters required as for
3281 // the source text.
3282 return a_uInputDataLen;
3283 }
3284
3285 // get the required buffer size
3286#if defined(_MSC_VER)
3287 size_t uBufSiz;
3288 errno_t e = mbstowcs_s(&uBufSiz, NULL, 0, a_pInputData, a_uInputDataLen);
3289 return (e == 0) ? uBufSiz : (size_t)-1;
3290#elif !defined(SI_NO_MBSTOWCS_NULL)
3291 // mbstowcs requires a_pInputData to be NUL-terminated at or after
3292 // a_pInputData + a_uInputDataLen. A length-only buffer without a
3293 // trailing NUL may read past the supplied range.
3294 return mbstowcs(NULL, a_pInputData, a_uInputDataLen);
3295#else
3296 // fall back processing for platforms that don't support a NULL dest to mbstowcs
3297 // worst case scenario is 1:1, this will be a sufficient buffer size
3298 (void)a_pInputData;
3299 return a_uInputDataLen;
3300#endif
3301 }
3302
3316 bool ConvertFromStore(const char *a_pInputData, size_t a_uInputDataLen,
3317 SI_CHAR *a_pOutputData, size_t a_uOutputDataSize) {
3318 if (m_bStoreIsUtf8) {
3319 const char *src = a_pInputData;
3320 const char *srcEnd = a_pInputData + a_uInputDataLen;
3321 SI_CHAR *dst = a_pOutputData;
3322 SI_CHAR *dstEnd = a_pOutputData + a_uOutputDataSize;
3323 if (sizeof(SI_CHAR) != 2 && sizeof(SI_CHAR) < sizeof(char32_t)) {
3324 return false;
3325 }
3326 while (src < srcEnd) {
3327 char32_t cp;
3328 if (!SI_UTF8::Decode(src, srcEnd, cp)) {
3329 return false;
3330 }
3331 if (!SI_UTF8::AppendCodePoint(cp, dst, dstEnd)) {
3332 return false;
3333 }
3334 }
3335 return true;
3336 }
3337
3338 // convert to wchar_t
3339#if defined(_MSC_VER)
3340 size_t uBufSiz;
3341 errno_t e = mbstowcs_s(&uBufSiz, a_pOutputData, a_uOutputDataSize,
3342 a_pInputData, a_uInputDataLen);
3343 (void)uBufSiz;
3344 return (e == 0);
3345#else
3346 size_t retval = mbstowcs(a_pOutputData, a_pInputData, a_uOutputDataSize);
3347 return retval != (size_t)(-1);
3348#endif
3349 }
3350
3361 size_t SizeToStore(const SI_CHAR *a_pInputData) {
3362 if (m_bStoreIsUtf8) {
3363 // worst case scenario for wchar_t to UTF-8 is 1 wchar_t -> 6 char
3364 size_t uLen = 0;
3365 while (a_pInputData[uLen]) {
3366 ++uLen;
3367 }
3368 return (6 * uLen) + 1;
3369 } else {
3370#if defined(_MSC_VER)
3371 size_t uLen = 0;
3372 errno_t e = wcstombs_s(&uLen, NULL, 0, a_pInputData, 0);
3373 if (e != 0) {
3374 return (size_t)-1;
3375 }
3376 return uLen + 1; // include NULL terminator
3377#else
3378 size_t uLen = wcstombs(NULL, a_pInputData, 0);
3379 if (uLen == (size_t)(-1)) {
3380 return uLen;
3381 }
3382 return uLen + 1; // include NULL terminator
3383#endif
3384 }
3385 }
3386
3400 bool ConvertToStore(const SI_CHAR *a_pInputData, char *a_pOutputData,
3401 size_t a_uOutputDataSize) {
3402 if (m_bStoreIsUtf8) {
3403 const SI_CHAR *src = a_pInputData;
3404 char *dst = a_pOutputData;
3405 char *dstEnd = a_pOutputData + a_uOutputDataSize;
3406 if (sizeof(SI_CHAR) != 2 && sizeof(SI_CHAR) < sizeof(char32_t)) {
3407 return false;
3408 }
3409 while (*src) {
3410 char32_t cp;
3411 if (!SI_UTF8::ReadCodePoint(src, cp)) {
3412 return false;
3413 }
3414 char buf[4];
3415 const int n = SI_UTF8::Encode(cp, buf, sizeof(buf));
3416 if (n < 0 || (size_t)(dstEnd - dst) < (size_t)n) {
3417 return false;
3418 }
3419 memcpy(dst, buf, (size_t)n);
3420 dst += n;
3421 }
3422 if (dst >= dstEnd) {
3423 return false;
3424 }
3425 *dst = '\0';
3426 return true;
3427 } else {
3428#if defined(_MSC_VER)
3429 size_t converted = 0;
3430 errno_t e = wcstombs_s(&converted, a_pOutputData, a_uOutputDataSize,
3431 a_pInputData, 0);
3432 return (e == 0);
3433#else
3434 size_t retval = wcstombs(a_pOutputData, a_pInputData, a_uOutputDataSize);
3435 return retval != (size_t)-1;
3436#endif
3437 }
3438 }
3439};
3440
3441#endif // SI_CONVERT_GENERIC
3442
3443// ---------------------------------------------------------------------------
3444// SI_CONVERT_ICU
3445// ---------------------------------------------------------------------------
3446#ifdef SI_CONVERT_ICU
3447
3448#define SI_Case SI_GenericCase
3449#define SI_NoCase SI_GenericNoCase
3450
3451#include <unicode/ucnv.h>
3452
3456template <class SI_CHAR> class SI_ConvertW {
3457 const char *m_pEncoding;
3458 UConverter *m_pConverter;
3459
3460protected:
3461 SI_ConvertW() : m_pEncoding(NULL), m_pConverter(NULL) {}
3462
3463public:
3464 SI_ConvertW(bool a_bStoreIsUtf8) : m_pConverter(NULL) {
3465 m_pEncoding = a_bStoreIsUtf8 ? "UTF-8" : NULL;
3466 }
3467
3468 /* copy and assignment */
3469 SI_ConvertW(const SI_ConvertW &rhs) { operator=(rhs); }
3470 SI_ConvertW &operator=(const SI_ConvertW &rhs) {
3471 m_pEncoding = rhs.m_pEncoding;
3472 m_pConverter = NULL;
3473 return *this;
3474 }
3475 ~SI_ConvertW() {
3476 if (m_pConverter)
3477 ucnv_close(m_pConverter);
3478 }
3479
3493 size_t SizeFromStore(const char *a_pInputData, size_t a_uInputDataLen) {
3494 SI_ASSERT(a_uInputDataLen != (size_t)-1);
3495
3496 UErrorCode nError;
3497
3498 if (!m_pConverter) {
3499 nError = U_ZERO_ERROR;
3500 m_pConverter = ucnv_open(m_pEncoding, &nError);
3501 if (U_FAILURE(nError)) {
3502 return (size_t)-1;
3503 }
3504 }
3505
3506 nError = U_ZERO_ERROR;
3507 int32_t nLen = ucnv_toUChars(m_pConverter, NULL, 0, a_pInputData,
3508 (int32_t)a_uInputDataLen, &nError);
3509 if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) {
3510 return (size_t)-1;
3511 }
3512
3513 return (size_t)nLen;
3514 }
3515
3529 bool ConvertFromStore(const char *a_pInputData, size_t a_uInputDataLen,
3530 UChar *a_pOutputData, size_t a_uOutputDataSize) {
3531 UErrorCode nError;
3532
3533 if (!m_pConverter) {
3534 nError = U_ZERO_ERROR;
3535 m_pConverter = ucnv_open(m_pEncoding, &nError);
3536 if (U_FAILURE(nError)) {
3537 return false;
3538 }
3539 }
3540
3541 nError = U_ZERO_ERROR;
3542 ucnv_toUChars(m_pConverter, a_pOutputData, (int32_t)a_uOutputDataSize,
3543 a_pInputData, (int32_t)a_uInputDataLen, &nError);
3544 if (U_FAILURE(nError)) {
3545 return false;
3546 }
3547
3548 return true;
3549 }
3550
3561 size_t SizeToStore(const UChar *a_pInputData) {
3562 UErrorCode nError;
3563
3564 if (!m_pConverter) {
3565 nError = U_ZERO_ERROR;
3566 m_pConverter = ucnv_open(m_pEncoding, &nError);
3567 if (U_FAILURE(nError)) {
3568 return (size_t)-1;
3569 }
3570 }
3571
3572 nError = U_ZERO_ERROR;
3573 int32_t nLen =
3574 ucnv_fromUChars(m_pConverter, NULL, 0, a_pInputData, -1, &nError);
3575 if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) {
3576 return (size_t)-1;
3577 }
3578
3579 return (size_t)nLen + 1;
3580 }
3581
3595 bool ConvertToStore(const UChar *a_pInputData, char *a_pOutputData,
3596 size_t a_uOutputDataSize) {
3597 UErrorCode nError;
3598
3599 if (!m_pConverter) {
3600 nError = U_ZERO_ERROR;
3601 m_pConverter = ucnv_open(m_pEncoding, &nError);
3602 if (U_FAILURE(nError)) {
3603 return false;
3604 }
3605 }
3606
3607 nError = U_ZERO_ERROR;
3608 ucnv_fromUChars(m_pConverter, a_pOutputData, (int32_t)a_uOutputDataSize,
3609 a_pInputData, -1, &nError);
3610 if (U_FAILURE(nError)) {
3611 return false;
3612 }
3613
3614 return true;
3615 }
3616};
3617
3618#endif // SI_CONVERT_ICU
3619
3620// ---------------------------------------------------------------------------
3621// SI_CONVERT_WIN32
3622// ---------------------------------------------------------------------------
3623#ifdef SI_CONVERT_WIN32
3624
3625#define SI_Case SI_GenericCase
3626
3627// Windows CE doesn't have errno or MBCS libraries
3628#ifdef _WIN32_WCE
3629#ifndef SI_NO_MBCS
3630#define SI_NO_MBCS
3631#endif
3632#endif
3633
3634#include <windows.h>
3635#ifdef SI_NO_MBCS
3636#define SI_NoCase SI_GenericNoCase
3637#else // !SI_NO_MBCS
3646#include <mbstring.h>
3647template <class SI_CHAR> struct SI_NoCase {
3648 bool operator()(const SI_CHAR *pLeft, const SI_CHAR *pRight) const {
3649 if (sizeof(SI_CHAR) == sizeof(char)) {
3650 return _mbsicmp((const unsigned char *)pLeft,
3651 (const unsigned char *)pRight) < 0;
3652 }
3653 if (sizeof(SI_CHAR) == sizeof(wchar_t)) {
3654 return _wcsicmp((const wchar_t *)pLeft, (const wchar_t *)pRight) < 0;
3655 }
3656 return SI_GenericNoCase<SI_CHAR>()(pLeft, pRight);
3657 }
3658};
3659#endif // SI_NO_MBCS
3660
3667template <class SI_CHAR> class SI_ConvertW {
3668 UINT m_uCodePage;
3669
3670protected:
3671 SI_ConvertW() {}
3672
3673public:
3674 SI_ConvertW(bool a_bStoreIsUtf8) {
3675 m_uCodePage = a_bStoreIsUtf8 ? CP_UTF8 : CP_ACP;
3676 }
3677
3678 /* copy and assignment */
3679 SI_ConvertW(const SI_ConvertW &rhs) { operator=(rhs); }
3680 SI_ConvertW &operator=(const SI_ConvertW &rhs) {
3681 m_uCodePage = rhs.m_uCodePage;
3682 return *this;
3683 }
3684
3698 size_t SizeFromStore(const char *a_pInputData, size_t a_uInputDataLen) {
3699 SI_ASSERT(a_uInputDataLen != (size_t)-1);
3700
3701 int retval = MultiByteToWideChar(m_uCodePage, 0, a_pInputData,
3702 (int)a_uInputDataLen, 0, 0);
3703 return (size_t)(retval > 0 ? retval : -1);
3704 }
3705
3719 bool ConvertFromStore(const char *a_pInputData, size_t a_uInputDataLen,
3720 SI_CHAR *a_pOutputData, size_t a_uOutputDataSize) {
3721 int nSize =
3722 MultiByteToWideChar(m_uCodePage, 0, a_pInputData, (int)a_uInputDataLen,
3723 (wchar_t *)a_pOutputData, (int)a_uOutputDataSize);
3724 return (nSize > 0);
3725 }
3726
3737 size_t SizeToStore(const SI_CHAR *a_pInputData) {
3738 int retval = WideCharToMultiByte(
3739 m_uCodePage, 0, (const wchar_t *)a_pInputData, -1, 0, 0, 0, 0);
3740 return (size_t)(retval > 0 ? retval : -1);
3741 }
3742
3756 bool ConvertToStore(const SI_CHAR *a_pInputData, char *a_pOutputData,
3757 size_t a_uOutputDataSize) {
3758 int retval =
3759 WideCharToMultiByte(m_uCodePage, 0, (const wchar_t *)a_pInputData, -1,
3760 a_pOutputData, (int)a_uOutputDataSize, 0, 0);
3761 return retval > 0;
3762 }
3763};
3764
3765#endif // SI_CONVERT_WIN32
3766
3767// ---------------------------------------------------------------------------
3768// SI_NO_CONVERSION
3769// ---------------------------------------------------------------------------
3770#ifdef SI_NO_CONVERSION
3771
3772#define SI_Case SI_GenericCase
3773#define SI_NoCase SI_GenericNoCase
3774
3775#endif // SI_NO_CONVERSION
3776
3777// ---------------------------------------------------------------------------
3778// TYPE DEFINITIONS
3779// ---------------------------------------------------------------------------
3780
3783
3784#if defined(SI_NO_CONVERSION)
3785// if there is no wide char conversion then we don't need to define the
3786// widechar "W" versions of CSimpleIni
3787#define CSimpleIni CSimpleIniA
3788#define CSimpleIniCase CSimpleIniCaseA
3789#define SI_NEWLINE SI_NEWLINE_A
3790#else
3791#if defined(SI_CONVERT_ICU)
3792typedef CSimpleIniTempl<UChar, SI_NoCase<UChar>, SI_ConvertW<UChar>>
3793 CSimpleIniW;
3794typedef CSimpleIniTempl<UChar, SI_Case<UChar>, SI_ConvertW<UChar>>
3795 CSimpleIniCaseW;
3796#else
3797typedef CSimpleIniTempl<wchar_t, SI_NoCase<wchar_t>, SI_ConvertW<wchar_t>>
3798 CSimpleIniW;
3799typedef CSimpleIniTempl<wchar_t, SI_Case<wchar_t>, SI_ConvertW<wchar_t>>
3800 CSimpleIniCaseW;
3801#endif
3802
3803#ifdef _UNICODE
3804#define CSimpleIni CSimpleIniW
3805#define CSimpleIniCase CSimpleIniCaseW
3806#define SI_NEWLINE SI_NEWLINE_W
3807#else // !_UNICODE
3808#define CSimpleIni CSimpleIniA
3809#define CSimpleIniCase CSimpleIniCaseA
3810#define SI_NEWLINE SI_NEWLINE_A
3811#endif // _UNICODE
3812#endif
3813
3814#ifdef _MSC_VER
3815#pragma warning(pop)
3816#endif
3817
3818#endif // INCLUDED_SimpleIni_h
Definition SimpleIni.h:448
Definition SimpleIni.h:405
Definition SimpleIni.h:393
Definition SimpleIni.h:331
bool DeleteValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pValue, bool a_bRemoveEmpty=false)
Definition SimpleIni.h:2830
bool GetAllKeys(const SI_CHAR *a_pSection, TNamesDepend &a_names) const
Definition SimpleIni.h:2547
int GetSectionSize(const SI_CHAR *a_pSection) const
Definition SimpleIni.h:2492
void SetAllowKeyOnly(bool a_bAllowKeyOnly=true)
Definition SimpleIni.h:592
SI_Error LoadData(const std::string &a_strData)
Definition SimpleIni.h:652
bool UsingQuotes() const
Definition SimpleIni.h:582
std::map< Entry, TKeyVal, typename Entry::KeyOrder > TSection
Definition SimpleIni.h:383
std::list< Entry > TNamesDepend
Definition SimpleIni.h:388
SI_Error SaveFile(FILE *a_pFile, bool a_bAddSignature=false) const
Definition SimpleIni.h:2619
bool IsUnicode() const
Definition SimpleIni.h:521
SI_Error SetBoolValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bValue, const SI_CHAR *a_pComment=NULL, bool a_bForceReplace=false)
Definition SimpleIni.h:2434
bool IsMultiKey() const
Definition SimpleIni.h:546
Converter GetConverter() const
Definition SimpleIni.h:1115
bool IsEmpty() const
Definition SimpleIni.h:496
std::multimap< Entry, const SI_CHAR *, typename Entry::KeyOrder > TKeyVal
Definition SimpleIni.h:380
const TKeyVal * GetSection(const SI_CHAR *a_pSection) const
Definition SimpleIni.h:2525
SI_Error Save(OutputWriter &a_oOutput, bool a_bAddSignature=false) const
Definition SimpleIni.h:2626
const SI_CHAR * GetValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pDefault=NULL, bool *a_pHasMultiple=NULL) const
Definition SimpleIni.h:2242
bool GetBoolValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bDefault=false, bool *a_pHasMultiple=NULL) const
Definition SimpleIni.h:2396
SI_Error LoadFile(FILE *a_fpFile)
Definition SimpleIni.h:1445
long GetLongValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, long a_nDefault=0, bool *a_pHasMultiple=NULL) const
Definition SimpleIni.h:2274
void GetAllSections(TNamesDepend &a_names) const
Definition SimpleIni.h:2537
void Reset()
Definition SimpleIni.h:1280
~CSimpleIniTempl()
Definition SimpleIni.h:1275
bool UsingSpaces() const
Definition SimpleIni.h:571
SI_Error Save(std::string &a_sBuffer, bool a_bAddSignature=false) const
Definition SimpleIni.h:774
bool KeyExists(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey) const
Definition SimpleIni.h:869
SI_Error SetValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, const SI_CHAR *a_pValue, const SI_CHAR *a_pComment=NULL, bool a_bForceReplace=false)
Definition SimpleIni.h:978
bool SectionExists(const SI_CHAR *a_pSection) const
Definition SimpleIni.h:864
bool Delete(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, bool a_bRemoveEmpty=false)
Definition SimpleIni.h:2824
void SetMultiLine(bool a_bAllowMultiLine=true)
Definition SimpleIni.h:555
SI_Error LoadData(const char *a_pData, size_t a_uDataLen)
Definition SimpleIni.h:1475
void SetQuotes(bool a_bParseQuotes=true)
Definition SimpleIni.h:577
void SetUnicode(bool a_bIsUtf8=true)
Definition SimpleIni.h:515
double GetDoubleValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, double a_nDefault=0, bool *a_pHasMultiple=NULL) const
Definition SimpleIni.h:2339
SI_Error SetLongValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, long a_nValue, const SI_CHAR *a_pComment=NULL, bool a_bUseHex=false, bool a_bForceReplace=false)
Definition SimpleIni.h:2310
void SetMultiKey(bool a_bAllowMultiKey=true)
Definition SimpleIni.h:541
bool GetAllValues(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, TNamesDepend &a_values) const
Definition SimpleIni.h:2458
void SetSpaces(bool a_bSpaces=true)
Definition SimpleIni.h:568
SI_Error LoadFile(const char *a_pszFile)
Definition SimpleIni.h:1398
bool GetAllowKeyOnly() const
Definition SimpleIni.h:597
SI_Error SaveFile(const char *a_pszFile, bool a_bAddSignature=true) const
Definition SimpleIni.h:2574
CSimpleIniTempl(bool a_bIsUtf8=false, bool a_bMultiKey=false, bool a_bMultiLine=false)
Definition SimpleIni.h:1267
SI_Error SetDoubleValue(const SI_CHAR *a_pSection, const SI_CHAR *a_pKey, double a_nValue, const SI_CHAR *a_pComment=NULL, bool a_bForceReplace=false)
Definition SimpleIni.h:2367
bool IsMultiLine() const
Definition SimpleIni.h:560
Definition SimpleIni.h:2988
bool ConvertToStore(const SI_CHAR *a_pInputData, char *a_pOutputData, size_t a_uOutputDataSize)
Definition SimpleIni.h:3076
size_t SizeFromStore(const char *a_pInputData, size_t a_uInputDataLen)
Definition SimpleIni.h:3017
size_t SizeToStore(const SI_CHAR *a_pInputData)
Definition SimpleIni.h:3058
bool ConvertFromStore(const char *a_pInputData, size_t a_uInputDataLen, SI_CHAR *a_pOutputData, size_t a_uOutputDataSize)
Definition SimpleIni.h:3038
Definition SimpleIni.h:360
Definition SimpleIni.h:368
Definition SimpleIni.h:336
Definition SimpleIni.h:2950
Definition SimpleIni.h:2969