15#include "private/qlocking_p.h"
21#include "private/qstringconverter_p.h"
23#include "private/qtools_p.h"
25#ifndef QT_BOOTSTRAPPED
38# include <qt_windows.h>
42#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID)
46#if !defined(QT_NO_STANDARDPATHS)
47 && (defined(Q_XDG_PLATFORM) || defined(QT_PLATFORM_UIKIT) || defined(Q_OS_ANDROID))
48# define QSETTINGS_USE_QSTANDARDPATHS
55
56
57
58
59
63using namespace Qt::StringLiterals;
64using namespace QtMiscUtils;
83 Path(
const QString & p,
bool ud) : path(p), userDefined(ud) {}
85 bool userDefined =
false;
91Q_GLOBAL_STATIC(ConfFileHash, usedHashFunc)
92Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc)
93Q_GLOBAL_STATIC(PathHash, pathHashFunc)
94Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc)
96Q_CONSTINIT
static QBasicMutex settingsGlobalMutex;
98Q_CONSTINIT
static QSettings::Format globalDefaultFormat = QSettings::NativeFormat;
100QConfFile::QConfFile(
const QString &fileName,
bool _userPerms)
101 : name(fileName), size(0), ref(1),
userPerms(_userPerms)
103 usedHashFunc()->insert(name,
this);
109 usedHashFunc()->remove(name);
117 result.remove(i.key());
119 result.insert(i.key(), i.value());
127#if QT_CONFIG(temporaryfile)
128 if (fileInfo.exists()) {
131 return file.open(QFile::ReadWrite);
132#if QT_CONFIG(temporaryfile)
135 QDir dir(fileInfo.absolutePath());
137 if (!dir.mkpath(dir.absolutePath()))
142 QTemporaryFile file(name);
150 QString absPath = QFileInfo(fileName).absoluteFilePath();
156 const auto locker = qt_scoped_lock(settingsGlobalMutex);
158 if (!(confFile = usedHash->value(absPath))) {
159 if ((confFile = unusedCache->take(absPath)))
160 usedHash->insert(absPath, confFile);
166 return new QConfFile(absPath, _userPerms);
171 const auto locker = qt_scoped_lock(settingsGlobalMutex);
172 unusedCacheFunc()->clear();
178QSettingsPrivate::QSettingsPrivate(QSettings::Format format)
179 : format(format), scope(QSettings::UserScope ), fallbacks(
true),
180 pendingChanges(
false), status(QSettings::NoError)
184QSettingsPrivate::QSettingsPrivate(QSettings::Format format, QSettings::Scope scope,
185 const QString &organization,
const QString &application)
186 : format(format), scope(scope), organizationName(organization), applicationName(application),
187 fallbacks(
true), pendingChanges(
false), status(QSettings::NoError)
191QSettingsPrivate::~QSettingsPrivate()
195QString QSettingsPrivate::actualKey(QAnyStringView key)
const
197 auto n = normalizedKey(key);
198 Q_ASSERT_X(!n.isEmpty(),
"QSettings",
"empty key");
199 return groupPrefix + n;
204 QChar *write(QChar *out, QUtf8StringView v)
206 return QUtf8::convertToUnicode(out, QByteArrayView(v));
208 QChar *write(QChar *out, QLatin1StringView v)
210 return QLatin1::convertToUnicode(out, v);
212 QChar *write(QChar *out, QStringView v)
214 memcpy(out, v.data(), v.size() *
sizeof(QChar));
215 return out + v.size();
220
221
222
223
224
225
226
227QString QSettingsPrivate::normalizedKey(QAnyStringView key)
229 QString result(key.size(), Qt::Uninitialized);
230 auto out =
const_cast<QChar*>(result.constData());
232 const bool maybeEndsInSlash = key.visit([&out](
auto key) {
233 using View =
decltype(key);
235 auto it = key.begin();
236 const auto end = key.end();
239 while (*it == u'/') {
245 while (*it != u'/') {
250 out = write(out, View{mark, it});
253 Q_ASSERT(*it == u'/');
260 if (maybeEndsInSlash && out != result.constData())
262 result.truncate(out - result.constData());
268#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN) && !defined(Q_OS_WASM)
269QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format, QSettings::Scope scope,
270 const QString &organization,
const QString &application)
272 return new QConfFileSettingsPrivate(format, scope, organization, application);
276#if !defined(Q_OS_WIN)
277QSettingsPrivate *QSettingsPrivate::create(
const QString &fileName, QSettings::Format format)
279 return new QConfFileSettingsPrivate(fileName, format);
283void QSettingsPrivate::processChild(QStringView key, ChildSpec spec, QStringList &result)
285 if (spec != AllKeys) {
286 qsizetype slashPos = key.indexOf(u'/');
287 if (slashPos == -1) {
288 if (spec != ChildKeys)
291 if (spec != ChildGroups)
293 key.truncate(slashPos);
296 result.append(key.toString());
299void QSettingsPrivate::beginGroupOrArray(
const QSettingsGroup &group)
301 groupStack.push(group);
302 const QString name = group.name();
304 groupPrefix += name + u'/';
308
309
310
312void QSettingsPrivate::setStatus(QSettings::Status status)
const
314 if (status == QSettings::NoError ||
this->status == QSettings::NoError)
315 this->status = status;
318void QSettingsPrivate::update()
321 pendingChanges =
false;
324void QSettingsPrivate::requestUpdate()
326 if (!pendingChanges) {
327 pendingChanges =
true;
330 QCoreApplication::postEvent(q,
new QEvent(QEvent::UpdateRequest));
337QStringList QSettingsPrivate::variantListToStringList(
const QVariantList &l)
340 result.reserve(l.size());
342 result.append(variantToString(v));
346QVariant QSettingsPrivate::stringListToVariantList(
const QStringList &l)
348 QStringList outStringList = l;
349 for (qsizetype i = 0; i < outStringList.size(); ++i) {
350 const QString &str = outStringList.at(i);
352 if (str.startsWith(u'@')) {
353 if (str.size() < 2 || str.at(1) != u'@') {
354 QVariantList variantList;
355 variantList.reserve(l.size());
356 for (
const auto &s : l)
357 variantList.append(stringToVariant(s));
360 outStringList[i].remove(0, 1);
363 return outStringList;
366QString QSettingsPrivate::variantToString(
const QVariant &v)
370 switch (v.metaType().id()) {
371 case QMetaType::UnknownType:
372 result =
"@Invalid()"_L1;
375 case QMetaType::QByteArray: {
376 QByteArray a = v.toByteArray();
377 result =
"@ByteArray("_L1 + QLatin1StringView(a) + u')';
381#if QT_CONFIG(shortcut)
382 case QMetaType::QKeySequence:
384 case QMetaType::QString:
385 case QMetaType::LongLong:
386 case QMetaType::ULongLong:
388 case QMetaType::UInt:
389 case QMetaType::Bool:
390 case QMetaType::Float:
391 case QMetaType::Double: {
392 result = v.toString();
393 if (result.contains(QChar::Null))
394 result =
"@String("_L1 + result + u')';
395 else if (result.startsWith(u'@'))
396 result.prepend(u'@');
399 case QMetaType::QRect: {
400 QRect r = qvariant_cast<QRect>(v);
401 result = QString::asprintf(
"@Rect(%d %d %d %d)", r.x(), r.y(), r.width(), r.height());
404 case QMetaType::QSize: {
405 QSize s = qvariant_cast<QSize>(v);
406 result = QString::asprintf(
"@Size(%d %d)", s.width(), s.height());
409 case QMetaType::QPoint: {
410 QPoint p = qvariant_cast<QPoint>(v);
411 result = QString::asprintf(
"@Point(%d %d)", p.x(), p.y());
416#ifndef QT_NO_DATASTREAM
417 QDataStream::Version version;
418 const char *typeSpec;
419 if (v.userType() == QMetaType::QDateTime) {
420 version = QDataStream::Qt_5_6;
421 typeSpec =
"@DateTime(";
423 version = QDataStream::Qt_4_0;
424 typeSpec =
"@Variant(";
428 QDataStream s(&a, QIODevice::WriteOnly);
429 s.setVersion(version);
433 result = QLatin1StringView(typeSpec)
434 + QLatin1StringView(a.constData(), a.size())
437 Q_ASSERT(!
"QSettings: Cannot save custom types without QDataStream support");
447QVariant QSettingsPrivate::stringToVariant(
const QString &s)
449 if (s.startsWith(u'@')) {
450 if (s.endsWith(u')')) {
451 if (s.startsWith(
"@ByteArray("_L1)) {
452 return QVariant(QStringView{s}.sliced(11).chopped(1).toLatin1());
453 }
else if (s.startsWith(
"@String("_L1)) {
454 return QVariant(QStringView{s}.sliced(8).chopped(1).toString());
455 }
else if (s.startsWith(
"@Variant("_L1)
456 || s.startsWith(
"@DateTime("_L1)) {
457#ifndef QT_NO_DATASTREAM
458 QDataStream::Version version;
460 if (s.at(1) == u'D') {
461 version = QDataStream::Qt_5_6;
464 version = QDataStream::Qt_4_0;
467 QByteArray a = QStringView{s}.sliced(offset).toLatin1();
468 QDataStream stream(&a, QIODevice::ReadOnly);
469 stream.setVersion(version);
474 Q_ASSERT(!
"QSettings: Cannot load custom types without QDataStream support");
476 }
else if (s.startsWith(
"@Rect("_L1)) {
477 QStringList args = QSettingsPrivate::splitArgs(s, 5);
478 if (args.size() == 4)
479 return QVariant(QRect(args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3].toInt()));
480 }
else if (s.startsWith(
"@Size("_L1)) {
481 QStringList args = QSettingsPrivate::splitArgs(s, 5);
482 if (args.size() == 2)
483 return QVariant(QSize(args[0].toInt(), args[1].toInt()));
484 }
else if (s.startsWith(
"@Point("_L1)) {
485 QStringList args = QSettingsPrivate::splitArgs(s, 6);
486 if (args.size() == 2)
487 return QVariant(QPoint(args[0].toInt(), args[1].toInt()));
488 }
else if (s ==
"@Invalid()"_L1) {
493 if (s.startsWith(
"@@"_L1))
494 return QVariant(s.sliced(1));
500void QSettingsPrivate::iniEscapedKey(
const QString &key, QByteArray &result)
502 result.reserve(result.size() + key.size() * 3 / 2);
503 for (qsizetype i = 0; i < key.size(); ++i) {
504 uint ch = key.at(i).unicode();
508 }
else if (isAsciiLetterOrNumber(ch) || ch ==
'_' || ch ==
'-' || ch ==
'.') {
510 }
else if (ch <= 0xFF) {
512 result += QtMiscUtils::toHexUpper(ch / 16);
513 result += QtMiscUtils::toHexUpper(ch % 16);
517 for (
int j = 0; j < 4; ++j) {
518 hexCode.prepend(QtMiscUtils::toHexUpper(ch % 16));
526bool QSettingsPrivate::iniUnescapedKey(QByteArrayView key, QString &result)
528 const QString decoded = QString::fromUtf8(key);
529 const qsizetype size = decoded.size();
530 result.reserve(result.size() + size);
532 bool lowercaseOnly =
true;
534 char16_t ch = decoded.at(i).unicode();
542 if (ch !=
'%' || i == size - 1) {
545 lowercaseOnly =
false;
552 qsizetype firstDigitPos = i + 1;
554 ch = decoded.at(i + 1).unicode();
560 if (firstDigitPos + numDigits > size) {
567 ch = QStringView(decoded).sliced(firstDigitPos, numDigits).toUShort(&ok, 16);
576 lowercaseOnly =
false;
578 i = firstDigitPos + numDigits;
580 return lowercaseOnly;
583void QSettingsPrivate::iniEscapedString(
const QString &str, QByteArray &result)
585 bool needsQuotes =
false;
586 bool escapeNextIfDigit =
false;
587 const bool useCodec = !(str.startsWith(
"@ByteArray("_L1)
588 || str.startsWith(
"@Variant("_L1)
589 || str.startsWith(
"@DateTime("_L1));
590 const qsizetype startPos = result.size();
592 QStringEncoder toUtf8(QStringEncoder::Utf8);
594 result.reserve(startPos + str.size() * 3 / 2);
595 for (QChar qch : str) {
596 uint ch = qch.unicode();
597 if (ch ==
';' || ch ==
',' || ch ==
'=')
600 if (escapeNextIfDigit && isHexDigit(ch)) {
601 result +=
"\\x" + QByteArray::number(ch, 16);
605 escapeNextIfDigit =
false;
610 escapeNextIfDigit =
true;
639 if (ch <= 0x1F || (ch >= 0x7F && !useCodec)) {
640 result +=
"\\x" + QByteArray::number(ch, 16);
641 escapeNextIfDigit =
true;
642 }
else if (useCodec) {
644 result += toUtf8(qch);
652 || (startPos < result.size() && (result.at(startPos) ==
' '
653 || result.at(result.size() - 1) ==
' '))) {
654 result.insert(startPos,
'"');
661 qsizetype n = str.size() - 1;
663 while (n >= limit && ((ch = str.at(n)) == u' ' || ch == u'\t'))
667void QSettingsPrivate::iniEscapedStringList(
const QStringList &strs, QByteArray &result)
669 if (strs.isEmpty()) {
671
672
673
674
675
676
677
678 result +=
"@Invalid()";
680 for (qsizetype i = 0; i < strs.size(); ++i) {
683 iniEscapedString(strs.at(i), result);
688bool QSettingsPrivate::iniUnescapedStringList(QByteArrayView str,
689 QString &stringResult, QStringList &stringListResult)
691 static const char escapeCodes[][2] =
706 bool isStringList =
false;
707 bool inQuotedString =
false;
708 bool currentValueIsQuoted =
false;
709 char16_t escapeVal = 0;
712 QStringDecoder fromUtf8(QStringDecoder::Utf8);
715 while (i < str.size() && ((ch = str.at(i)) ==
' ' || ch ==
'\t'))
720 qsizetype chopLimit = stringResult.size();
721 while (i < str.size()) {
729 for (
const auto &escapeCode : escapeCodes) {
730 if (ch == escapeCode[0]) {
731 stringResult += QLatin1Char(escapeCode[1]);
745 }
else if (
const int o = fromOct(ch); o != -1) {
748 }
else if (ch ==
'\n' || ch ==
'\r') {
749 if (i < str.size()) {
750 char ch2 = str.at(i);
752 if ((ch2 ==
'\n' || ch2 ==
'\r') && ch2 != ch)
758 chopLimit = stringResult.size();
762 currentValueIsQuoted =
true;
763 inQuotedString = !inQuotedString;
768 if (!inQuotedString) {
769 if (!currentValueIsQuoted)
770 iniChopTrailingSpaces(stringResult, chopLimit);
773 stringListResult.clear();
774 stringResult.squeeze();
776 stringListResult.append(stringResult);
777 stringResult.clear();
778 currentValueIsQuoted =
false;
785 while (j < str.size()) {
787 if (ch ==
'\\' || ch ==
'"' || ch ==
',')
792 stringResult += fromUtf8(str.first(j).sliced(i));
797 if (!currentValueIsQuoted)
798 iniChopTrailingSpaces(stringResult, chopLimit);
802 if (i >= str.size()) {
803 stringResult += escapeVal;
808 if (
const int h = fromHex(ch); h != -1) {
814 stringResult += escapeVal;
819 if (i >= str.size()) {
820 stringResult += escapeVal;
825 if (
const int o = fromOct(ch); o != -1) {
831 stringResult += escapeVal;
837 stringListResult.append(stringResult);
841QStringList QSettingsPrivate::splitArgs(
const QString &s, qsizetype idx)
843 qsizetype l = s.size();
845 Q_ASSERT(s.at(idx) == u'(');
846 Q_ASSERT(s.at(l - 1) == u')');
851 for (++idx; idx < l; ++idx) {
854 Q_ASSERT(idx == l - 1);
856 }
else if (c == u' ') {
872#if defined(Q_OS_WASM)
873 extension = (format == QSettings::NativeFormat || format == QSettings::WebIndexedDBFormat)
877 extension = (format == QSettings::NativeFormat) ?
".conf"_L1 :
".ini"_L1;
881#if defined(Q_OS_DARWIN)
882 caseSensitivity = (format == QSettings::NativeFormat) ? Qt::CaseSensitive : IniCaseSensitivity;
884 caseSensitivity = IniCaseSensitivity;
888 if (format > QSettings::IniFormat && format != QSettings::WebIndexedDBFormat) {
890 if (format > QSettings::IniFormat) {
892 const auto locker = qt_scoped_lock(settingsGlobalMutex);
895 qsizetype i = qsizetype(format) - qsizetype(QSettings::CustomFormat1);
896 if (i >= 0 && i < customFormatVector->size()) {
898 extension = info.extension;
899 readFunc = info.readFunc;
900 writeFunc = info.writeFunc;
901 caseSensitivity = info.caseSensitivity;
908 if (!confFiles.isEmpty()) {
910 if (format > QSettings::IniFormat && format != QSettings::WebIndexedDBFormat) {
912 if (format > QSettings::IniFormat) {
915 setStatus(QSettings::AccessError);
923static QString windowsConfigPath(
const KNOWNFOLDERID &type)
927 PWSTR path =
nullptr;
928 if (SHGetKnownFolderPath(type, KF_FLAG_DONT_VERIFY, NULL, &path) == S_OK) {
929 result = QString::fromWCharArray(path);
933 if (result.isEmpty()) {
934 if (type == FOLDERID_ProgramData) {
935 result =
"C:\\temp\\qt-common"_L1;
936 }
else if (type == FOLDERID_RoamingAppData) {
937 result =
"C:\\temp\\qt-user"_L1;
945static inline int pathHashKey(QSettings::Format format, QSettings::Scope scope)
947 return int((uint(format) << 1) | uint(scope == QSettings::SystemScope));
953#if !defined(QSETTINGS_USE_QSTANDARDPATHS) || defined(Q_OS_ANDROID)
958 return QDir::homePath() +
"/.config/"_L1;
959 }
else if (env.startsWith(
'/')) {
960 return QFile::decodeName(env) + sep;
963 return QDir::homePath() + sep + QFile::decodeName(env) + sep;
969#ifndef QSETTINGS_USE_QSTANDARDPATHS
973 return make_user_path_without_qstandard_paths();
978 QString ret = make_user_path_without_qstandard_paths();
979 if (QFile(ret).exists())
986 return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + sep;
993 PathHash *pathHash = pathHashFunc();
998
999
1000
1001
1002 QString systemPath = QLibraryInfo::path(QLibraryInfo::SettingsPath) + u'/';
1005 if (pathHash->isEmpty()) {
1007
1008
1009
1010
1011
1013 const QString roamingAppDataFolder = windowsConfigPath(FOLDERID_RoamingAppData);
1014 const QString programDataFolder = windowsConfigPath(FOLDERID_ProgramData);
1015 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope),
1016 Path(roamingAppDataFolder + QDir::separator(),
false));
1017 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope),
1018 Path(programDataFolder + QDir::separator(),
false));
1020 const QString userPath = make_user_path();
1021 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), Path(userPath,
false));
1022 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), Path(systemPath,
false));
1024 pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), Path(userPath,
false));
1025 pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), Path(systemPath,
false));
1033static Path
getPath(QSettings::Format format, QSettings::Scope scope)
1035 Q_ASSERT(
int(QSettings::NativeFormat) == 0);
1036 Q_ASSERT(
int(QSettings::IniFormat) == 1);
1038 auto locker = qt_unique_lock(settingsGlobalMutex);
1039 PathHash *pathHash = pathHashFunc();
1040 if (pathHash->isEmpty())
1041 locker = initDefaultPaths(
std::move(locker));
1043 Path result = pathHash->value(pathHashKey(format, scope));
1044 if (!result.path.isEmpty())
1048 return pathHash->value(pathHashKey(QSettings::IniFormat, scope));
1051#if defined(QT_BUILD_INTERNAL) && defined(Q_XDG_PLATFORM) && !defined(QT_NO_STANDARDPATHS)
1053void Q_AUTOTEST_EXPORT clearDefaultPaths()
1055 const auto locker = qt_scoped_lock(settingsGlobalMutex);
1056 pathHashFunc()->clear();
1061 QSettings::Scope scope,
1062 const QString &organization,
1063 const QString &application)
1064 : QSettingsPrivate(format, scope, organization, application),
1065 nextPosition(0x40000000)
1069 QString org = organization;
1070 if (org.isEmpty()) {
1071 setStatus(QSettings::AccessError);
1072 org =
"Unknown Organization"_L1;
1075 QString appFile = org + QDir::separator() + application + extension;
1076 QString orgFile = org + extension;
1078 if (scope == QSettings::UserScope) {
1079 Path userPath = getPath(format, QSettings::UserScope);
1080 if (!application.isEmpty())
1081 confFiles.append(QConfFile::fromName(userPath.path + appFile,
true));
1082 confFiles.append(QConfFile::fromName(userPath.path + orgFile,
true));
1085 Path systemPath = getPath(format, QSettings::SystemScope);
1086#if defined(Q_XDG_PLATFORM) && !defined(QT_NO_STANDARDPATHS)
1088 if (!systemPath.userDefined) {
1092 QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation);
1094 if (!dirs.isEmpty())
1097 if (!application.isEmpty()) {
1098 paths.reserve(dirs.size() * 2);
1099 for (
const auto &dir : std::as_const(dirs))
1100 paths.append(dir + u'/' + appFile);
1102 paths.reserve(dirs.size());
1104 for (
const auto &dir : std::as_const(dirs))
1105 paths.append(dir + u'/' + orgFile);
1108 for (
const auto &path : std::as_const(paths))
1109 confFiles.append(QConfFile::fromName(path,
false));
1113 if (!application.isEmpty())
1114 confFiles.append(QConfFile::fromName(systemPath.path + appFile,
false));
1115 confFiles.append(QConfFile::fromName(systemPath.path + orgFile,
false));
1122 QSettings::Format format)
1123 : QSettingsPrivate(format),
1124 nextPosition(0x40000000)
1128 confFiles.append(QConfFile::fromName(fileName,
true));
1135 const auto locker = qt_scoped_lock(settingsGlobalMutex);
1139 for (
auto conf_file : std::as_const(confFiles)) {
1140 if (!conf_file->ref.deref()) {
1141 if (conf_file->size == 0) {
1145 usedHash->remove(conf_file->name);
1149 unusedCache->insert(conf_file->name, conf_file,
1150 10 + (conf_file->originalKeys.size() / 4));
1166 if (confFiles.isEmpty())
1174 const auto locker = qt_scoped_lock(confFile->mutex);
1176 ensureSectionParsed(confFile, theKey);
1177 ensureSectionParsed(confFile, prefix);
1180 while (i != confFile
->addedKeys.end() && i.key().startsWith(prefix))
1185 while (j != confFile
->originalKeys.constEnd() && j.key().startsWith(prefix)) {
1195 if (confFiles.isEmpty())
1201 QSettingsKey theKey(key, caseSensitivity, nextPosition++);
1202 const auto locker = qt_scoped_lock(confFile->mutex);
1210 ParsedSettingsMap::const_iterator j;
1213 for (
auto confFile : std::as_const(confFiles)) {
1214 const auto locker = qt_scoped_lock(confFile->mutex);
1216 if (!confFile->addedKeys.isEmpty()) {
1217 j = confFile->addedKeys.constFind(theKey);
1218 found = (j != confFile->addedKeys.constEnd());
1221 ensureSectionParsed(confFile, theKey);
1222 j = confFile->originalKeys.constFind(theKey);
1223 found = (j != confFile->originalKeys.constEnd()
1224 && !confFile->removedKeys.contains(theKey));
1232 return std::nullopt;
1240 qsizetype startPos = prefix.size();
1242 for (
auto confFile : std::as_const(confFiles)) {
1243 const auto locker = qt_scoped_lock(confFile->mutex);
1245 if (thePrefix.isEmpty())
1246 ensureAllSectionsParsed(confFile);
1248 ensureSectionParsed(confFile, thePrefix);
1250 const auto &originalKeys = confFile->originalKeys;
1251 auto i = originalKeys.lowerBound(thePrefix);
1252 while (i != originalKeys.end() && i.key().startsWith(thePrefix)) {
1253 if (!confFile->removedKeys.contains(i.key()))
1254 processChild(QStringView{i.key().originalCaseKey()}.sliced(startPos), spec, result);
1258 const auto &addedKeys = confFile->addedKeys;
1259 auto j = addedKeys.lowerBound(thePrefix);
1260 while (j != addedKeys.end() && j.key().startsWith(thePrefix)) {
1261 processChild(QStringView{j.key().originalCaseKey()}.sliced(startPos), spec, result);
1268 std::sort(result.begin(), result.end());
1269 result.erase(
std::unique(result.begin(), result.end()),
1276 if (confFiles.isEmpty())
1282 const auto locker = qt_scoped_lock(confFile->mutex);
1283 ensureAllSectionsParsed(confFile);
1293 for (
auto confFile : std::as_const(confFiles)) {
1294 const auto locker = qt_scoped_lock(confFile->mutex);
1295 syncConfFile(confFile);
1306 if (confFiles.isEmpty())
1310 return confFiles.at(0)->name;
1315#if defined(Q_OS_WASM)
1316 if (format > QSettings::IniFormat && format != QSettings::WebIndexedDBFormat && !writeFunc)
1318 if (format > QSettings::IniFormat && !writeFunc)
1322 if (confFiles.isEmpty())
1325 return confFiles.at(0)->isWritable();
1334
1335
1336
1337 if (readOnly && confFile->size > 0) {
1338 if (confFile->size == fileInfo.size() && confFile->timeStamp == fileInfo.lastModified(QTimeZone::UTC))
1343 setStatus(QSettings::AccessError);
1347#ifndef QT_BOOTSTRAPPED
1348 QString lockFileName = confFile->name +
".lock"_L1;
1350# if defined(Q_OS_ANDROID) && defined(QSETTINGS_USE_QSTANDARDPATHS)
1353 if (confFile->name.startsWith(
"content:"_L1))
1354 lockFileName = make_user_path() + QFileInfo(lockFileName).fileName();
1357
1358
1359
1360
1361
1362
1364 if (!readOnly && !lockFile.lock() && atomicSyncOnly) {
1365 setStatus(QSettings::AccessError);
1371
1372
1373
1375 bool mustReadFile =
true;
1376 bool createFile = !fileInfo.exists();
1379 mustReadFile = (confFile->size != fileInfo.size()
1380 || (confFile->size != 0 && confFile->timeStamp != fileInfo.lastModified(QTimeZone::UTC)));
1386 QFile file(confFile->name);
1387 if (!createFile && !file.open(QFile::ReadOnly)) {
1388 setStatus(QSettings::AccessError);
1393
1394
1395
1396 if (file.isReadable() && file.size() != 0) {
1400 if (format == QSettings::WebIndexedDBFormat) {
1401 QByteArray data = file.readAll();
1402 ok = readIniFile(data, &confFile->unparsedIniSections);
1406 if (format == QSettings::NativeFormat) {
1407 QByteArray data = file.readAll();
1408 ok = readPlistFile(data, &confFile->originalKeys);
1411 if (format <= QSettings::IniFormat) {
1413 ok = readIniFile(data, &confFile->unparsedIniSections);
1414 }
else if (readFunc) {
1415 QSettings::SettingsMap tempNewKeys;
1416 ok = readFunc(file, tempNewKeys);
1419 auto i = tempNewKeys.constBegin();
1420 while (i != tempNewKeys.constEnd()) {
1421 confFile->originalKeys.insert(QSettingsKey(i.key(), caseSensitivity),
1428 for (
const auto §ion : confFile->unparsedIniSections.keys()) {
1429 if (section.count(u'/') > 1) {
1430 setStatus(QSettings::FormatError);
1436 setStatus(QSettings::FormatError);
1439 confFile->size = fileInfo.size();
1440 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1444
1445
1446
1449 ensureAllSectionsParsed(confFile);
1452#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1453 QSaveFile sf(confFile->name);
1454 sf.setDirectWriteFallback(!atomicSyncOnly);
1457 if (confFile->name.startsWith(
"content:"_L1))
1458 sf.setDirectWriteFallback(
true);
1461 QFile sf(confFile->name);
1463 if (!sf.open(QIODevice::WriteOnly)) {
1464 setStatus(QSettings::AccessError);
1469 if (format == QSettings::WebIndexedDBFormat) {
1470 ok = writeIniFile(sf, mergedKeys);
1474 if (format == QSettings::NativeFormat) {
1475 ok = writePlistFile(sf, mergedKeys);
1478 if (format <= QSettings::IniFormat) {
1479 ok = writeIniFile(sf, mergedKeys);
1480 }
else if (writeFunc) {
1481 QSettings::SettingsMap tempOriginalKeys;
1483 auto i = mergedKeys.constBegin();
1484 while (i != mergedKeys.constEnd()) {
1485 tempOriginalKeys.insert(i.key(), i.value());
1488 ok = writeFunc(sf, tempOriginalKeys);
1491#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1503 confFile->size = fileInfo.size();
1504 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1508 QFile::Permissions perms = fileInfo.permissions() | QFile::ReadOwner | QFile::WriteOwner;
1509 if (!confFile->userPerms)
1510 perms |= QFile::ReadGroup | QFile::ReadOther;
1511 QFile(confFile->name).setPermissions(perms);
1514 setStatus(QSettings::AccessError);
1528 0, 0, 0, 0, 0, 0, 0, 0, 0,
Space,
Space |
Special, 0, 0,
Space |
Special, 0, 0,
1529 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1530 Space, 0,
Special, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1531 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Special, 0,
Special, 0, 0,
1532 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1533 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Special, 0, 0, 0,
1534 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1535 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1537 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1542 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1552 qsizetype &lineStart, qsizetype &lineLen,
1553 qsizetype &equalsPos)
1557 qsizetype dataLen = data.size();
1558 bool inQuotes =
false;
1562 lineStart = dataPos;
1563 while (lineStart < dataLen && (
charTraits[uint(uchar(data.at(lineStart)))] & Space))
1566 qsizetype i = lineStart;
1567 while (i < dataLen) {
1568 char ch = data.at(i);
1571 goto break_out_of_outer_loop;
1577 if (!inQuotes && equalsPos == -1)
1579 }
else if (ch ==
'\n' || ch ==
'\r') {
1580 if (i == lineStart + 1) {
1582 }
else if (!inQuotes) {
1584 goto break_out_of_outer_loop;
1586 }
else if (ch ==
'\\') {
1588 char ch = data.at(i++);
1590 char ch2 = data.at(i);
1592 if ((ch ==
'\n' && ch2 ==
'\r') || (ch ==
'\r' && ch2 ==
'\n'))
1596 }
else if (ch ==
'"') {
1597 inQuotes = !inQuotes;
1599 Q_ASSERT(ch ==
';');
1601 if (i == lineStart + 1) {
1602 while (i < dataLen && (((ch = data.at(i)) !=
'\n') && ch !=
'\r'))
1604 while (i < dataLen &&
charTraits[uchar(data.at(i))] & Space)
1607 }
else if (!inQuotes) {
1609 goto break_out_of_outer_loop;
1614break_out_of_outer_loop:
1616 lineLen = i - lineStart;
1621
1622
1623
1624
1628#define FLUSH_CURRENT_SECTION()
1630 QByteArray §ionData = (*unparsedIniSections)[QSettingsKey(currentSection,
1633 if (!sectionData.isEmpty())
1634 sectionData.append('\n');
1635 sectionData += data.first(lineStart).sliced(currentSectionStart);
1636 sectionPosition = ++position;
1639 QString currentSection;
1640 qsizetype currentSectionStart = 0;
1641 qsizetype dataPos = 0;
1642 qsizetype lineStart;
1644 qsizetype equalsPos;
1645 qsizetype position = 0;
1646 qsizetype sectionPosition = 0;
1650 if (data.startsWith(
"\xef\xbb\xbf"))
1651 data = data.sliced(3);
1653 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1654 QByteArrayView line = data.sliced(lineStart, lineLen);
1655 if (line.startsWith(
'[')) {
1659 qsizetype idx = line.indexOf(
']');
1660 Q_ASSERT(idx == -1 || idx > 0);
1661 Q_ASSERT(idx < lineLen);
1666 QByteArrayView iniSection = line.first(idx).sliced(1).trimmed();
1668 if (iniSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1669 currentSection.clear();
1671 if (iniSection.compare(
"%general", Qt::CaseInsensitive) == 0) {
1672 currentSection = QLatin1StringView(iniSection.constData() + 1, iniSection.size() - 1);
1674 currentSection.clear();
1675 iniUnescapedKey(iniSection, currentSection);
1677 currentSection += u'/';
1679 currentSectionStart = dataPos;
1684 Q_ASSERT(lineStart == data.size());
1689#undef FLUSH_CURRENT_SECTION
1695 QStringList strListValue;
1696 bool sectionIsLowercase = (section == section.originalCaseKey());
1697 qsizetype equalsPos;
1700 qsizetype dataPos = 0;
1701 qsizetype lineStart;
1703 qsizetype position = section.originalKeyPosition();
1705 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1706 QByteArrayView line = data.sliced(lineStart, lineLen);
1707 Q_ASSERT(!line.startsWith(
'['));
1709 if (equalsPos == -1) {
1710 if (!line.startsWith(
';'))
1715 equalsPos -= lineStart;
1717 Q_ASSERT(equalsPos >= 0 && equalsPos < lineLen);
1719 QByteArrayView key = line.first(equalsPos).trimmed();
1720 QByteArrayView value = line.sliced(equalsPos + 1);
1722 QString strKey = section.originalCaseKey();
1723 const Qt::CaseSensitivity casing = iniUnescapedKey(key, strKey) && sectionIsLowercase
1725 : IniCaseSensitivity;
1728 strValue.reserve(value.size());
1729 QVariant variant = iniUnescapedStringList(value, strValue, strListValue)
1730 ? stringListToVariantList(strListValue)
1731 : stringToVariant(strValue);
1734
1735
1736
1737
1738 settingsMap->insert(
QSettingsKey(strKey, casing, position),
std::move(variant));
1757 if (k1.position != k2.position)
1758 return k1.position < k2.position;
1759 return static_cast<
const QString &>(k1) <
static_cast<
const QString &>(k2);
1777
1778
1779
1785 const char *
const eol =
"\r\n";
1787 const char eol =
'\n';
1790 for (
auto j = map.constBegin(); j != map.constEnd(); ++j) {
1792 QSettingsIniKey key(j.key().originalCaseKey(), j.key().originalKeyPosition());
1795 if ((slashPos = key.indexOf(u'/')) != -1) {
1796 section = key.left(slashPos);
1797 key.remove(0, slashPos + 1);
1803 if (size_t(key.position) < size_t(iniSection.position))
1804 iniSection.position = key.position;
1805 iniSection.keyMap[key] = j.value();
1808 const qsizetype sectionCount = iniMap.size();
1809 QList<QSettingsIniKey> sections;
1810 sections.reserve(sectionCount);
1811 for (
auto i = iniMap.constBegin(); i != iniMap.constEnd(); ++i)
1813 std::sort(sections.begin(), sections.end());
1815 bool writeError =
false;
1816 for (qsizetype j = 0; !writeError && j < sectionCount; ++j) {
1817 auto i = iniMap.constFind(sections.at(j));
1818 Q_ASSERT(i != iniMap.constEnd());
1822 iniEscapedKey(i.key(), realSection);
1824 if (realSection.isEmpty()) {
1825 realSection =
"[General]";
1826 }
else if (realSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1827 realSection =
"[%General]";
1829 realSection.prepend(
'[');
1830 realSection.append(
']');
1834 realSection.prepend(eol);
1837 device.write(realSection);
1839 const IniKeyMap &ents = i.value().keyMap;
1840 for (
auto j = ents.constBegin(); j != ents.constEnd(); ++j) {
1842 iniEscapedKey(j.key(), block);
1845 const QVariant &value = j.value();
1848
1849
1850
1851
1852 if (value.metaType().id() == QMetaType::QStringList
1853 || (value.metaType().id() == QMetaType::QVariantList && value.toList().size() != 1)) {
1854 iniEscapedStringList(variantListToStringList(value.toList()), block);
1856 iniEscapedString(variantToString(value), block);
1859 if (device.write(block) == -1) {
1873 for (; i != end; ++i) {
1874 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1875 setStatus(QSettings::FormatError);
1886 UnparsedSettingsMap::iterator i;
1888 qsizetype indexOfSlash = key.indexOf(u'/');
1889 if (indexOfSlash != -1) {
1894 if (i.key().isEmpty() || !key.startsWith(i.key()))
1902 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1903 setStatus(QSettings::FormatError);
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2375
2376
2377
2378
2379
2380
2381
2382
2383
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2512#ifndef QT_NO_QOBJECT
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527QSettings::QSettings(
const QString &organization,
const QString &application, QObject *parent)
2528 : QObject(*QSettingsPrivate::create(NativeFormat, UserScope, organization, application),
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application,
2554 : QObject(*QSettingsPrivate::create(NativeFormat, scope, organization, application), parent)
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2577 const QString &application, QObject *parent)
2578 : QObject(*QSettingsPrivate::create(format, scope, organization, application), parent)
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612QSettings::QSettings(
const QString &fileName, Format format, QObject *parent)
2613 : QObject(*QSettingsPrivate::create(fileName, format), parent)
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652QSettings::QSettings(QObject *parent)
2653 : QSettings(UserScope, parent)
2658
2659
2660
2661
2662
2663
2664
2665QSettings::QSettings(Scope scope, QObject *parent)
2666 : QObject(*QSettingsPrivate::create(globalDefaultFormat, scope,
2668 QCoreApplication::organizationDomain().isEmpty()
2669 ? QCoreApplication::organizationName()
2670 : QCoreApplication::organizationDomain()
2672 QCoreApplication::organizationName().isEmpty()
2673 ? QCoreApplication::organizationDomain()
2674 : QCoreApplication::organizationName()
2676 , QCoreApplication::applicationName()),
2682QSettings::QSettings(
const QString &organization,
const QString &application)
2683 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, QSettings::UserScope, organization, application))
2685 d_ptr->q_ptr =
this;
2688QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application)
2689 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope, organization, application))
2691 d_ptr->q_ptr =
this;
2694QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2695 const QString &application)
2696 : d_ptr(QSettingsPrivate::create(format, scope, organization, application))
2698 d_ptr->q_ptr =
this;
2701QSettings::QSettings(
const QString &fileName, Format format)
2702 : d_ptr(QSettingsPrivate::create(fileName, format))
2704 d_ptr->q_ptr =
this;
2707QSettings::QSettings(Scope scope)
2708 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope,
2710 QCoreApplication::organizationDomain().isEmpty()
2711 ? QCoreApplication::organizationName()
2712 : QCoreApplication::organizationDomain()
2714 QCoreApplication::organizationName().isEmpty()
2715 ? QCoreApplication::organizationDomain()
2716 : QCoreApplication::organizationName()
2718 , QCoreApplication::applicationName())
2721 d_ptr->q_ptr =
this;
2726
2727
2728
2729
2730
2731
2732
2733QSettings::~QSettings()
2736 if (d->pendingChanges) {
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757void QSettings::clear()
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775void QSettings::sync()
2779 d->pendingChanges =
false;
2783
2784
2785
2786
2787
2788
2789
2790
2791QString QSettings::fileName()
const
2793 Q_D(
const QSettings);
2794 return d->fileName();
2798
2799
2800
2801
2802
2803
2804QSettings::Format QSettings::format()
const
2806 Q_D(
const QSettings);
2811
2812
2813
2814
2815
2816
2817QSettings::Scope QSettings::scope()
const
2819 Q_D(
const QSettings);
2824
2825
2826
2827
2828
2829
2830QString QSettings::organizationName()
const
2832 Q_D(
const QSettings);
2833 return d->organizationName;
2837
2838
2839
2840
2841
2842
2843QString QSettings::applicationName()
const
2845 Q_D(
const QSettings);
2846 return d->applicationName;
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859QSettings::Status QSettings::status()
const
2861 Q_D(
const QSettings);
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876bool QSettings::isAtomicSyncRequired()
const
2878 Q_D(
const QSettings);
2879 return d->atomicSyncOnly;
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903void QSettings::setAtomicSyncRequired(
bool enable)
2906 d->atomicSyncOnly = enable;
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938void QSettings::beginGroup(QAnyStringView prefix)
2941 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix)));
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954void QSettings::endGroup()
2957 if (d->groupStack.isEmpty()) {
2958 qWarning(
"QSettings::endGroup: No matching beginGroup()");
2962 QSettingsGroup group = d->groupStack.pop();
2963 qsizetype len = group.toString().size();
2965 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
2967 if (group.isArray())
2968 qWarning(
"QSettings::endGroup: Expected endArray() instead");
2972
2973
2974
2975
2976QString QSettings::group()
const
2978 Q_D(
const QSettings);
2979 return d->groupPrefix.left(d->groupPrefix.size() - 1);
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997int QSettings::beginReadArray(QAnyStringView prefix)
3000 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix),
false));
3001 return value(
"size"_L1).toInt();
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036void QSettings::beginWriteArray(QAnyStringView prefix,
int size)
3039 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), size < 0));
3044 setValue(
"size"_L1, size);
3048
3049
3050
3051
3052
3053void QSettings::endArray()
3056 if (d->groupStack.isEmpty()) {
3057 qWarning(
"QSettings::endArray: No matching beginArray()");
3061 QSettingsGroup group = d->groupStack.top();
3062 qsizetype len = group.toString().size();
3063 d->groupStack.pop();
3065 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
3067 if (group.arraySizeGuess() != -1)
3068 setValue(group.name() +
"/size"_L1, group.arraySizeGuess());
3070 if (!group.isArray())
3071 qWarning(
"QSettings::endArray: Expected endGroup() instead");
3075
3076
3077
3078
3079
3080
3081
3082void QSettings::setArrayIndex(
int i)
3085 if (d->groupStack.isEmpty() || !d->groupStack.top().isArray()) {
3086 qWarning(
"QSettings::setArrayIndex: Missing beginArray()");
3090 QSettingsGroup &top = d->groupStack.top();
3091 qsizetype len = top.toString().size();
3092 top.setArrayIndex(qMax(i, 0));
3093 d->groupPrefix.replace(d->groupPrefix.size() - len - 1, len, top.toString());
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111QStringList QSettings::allKeys()
const
3113 Q_D(
const QSettings);
3114 return d->children(d->groupPrefix, QSettingsPrivate::AllKeys);
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135QStringList QSettings::childKeys()
const
3137 Q_D(
const QSettings);
3138 return d->children(d->groupPrefix, QSettingsPrivate::ChildKeys);
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159QStringList QSettings::childGroups()
const
3161 Q_D(
const QSettings);
3162 return d->children(d->groupPrefix, QSettingsPrivate::ChildGroups);
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177bool QSettings::isWritable()
const
3179 Q_D(
const QSettings);
3180 return d->isWritable();
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203void QSettings::setValue(QAnyStringView key,
const QVariant &value)
3206 if (key.isEmpty()) {
3207 qWarning(
"QSettings::setValue: Empty key passed");
3210 d->set(d->actualKey(key), value);
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237void QSettings::remove(QAnyStringView key)
3241
3242
3243
3244 QString theKey = d->normalizedKey(key);
3245 if (theKey.isEmpty())
3248 theKey.prepend(d->groupPrefix);
3250 if (theKey.isEmpty()) {
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272bool QSettings::contains(QAnyStringView key)
const
3274 Q_D(
const QSettings);
3275 return d->get(d->actualKey(key)) != std::nullopt;
3279
3280
3281
3282
3283
3284
3285void QSettings::setFallbacksEnabled(
bool b)
3292
3293
3294
3295
3296
3297
3298bool QSettings::fallbacksEnabled()
const
3300 Q_D(
const QSettings);
3301 return d->fallbacks;
3304#ifndef QT_NO_QOBJECT
3306
3307
3308bool QSettings::event(QEvent *event)
3311 if (event->type() == QEvent::UpdateRequest) {
3315 return QObject::event(event);
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340QVariant QSettings::value(QAnyStringView key)
const
3342 Q_D(
const QSettings);
3343 return d->value(key,
nullptr);
3346QVariant QSettings::value(QAnyStringView key,
const QVariant &defaultValue)
const
3348 Q_D(
const QSettings);
3349 return d->value(key, &defaultValue);
3352QVariant QSettingsPrivate::value(QAnyStringView key,
const QVariant *defaultValue)
const
3354 if (key.isEmpty()) {
3355 qWarning(
"QSettings::value: Empty key passed");
3358 if (std::optional r = get(actualKey(key)))
3359 return std::move(*r);
3361 return *defaultValue;
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377void QSettings::setDefaultFormat(Format format)
3379 globalDefaultFormat = format;
3383
3384
3385
3386
3387
3388
3389
3390QSettings::Format QSettings::defaultFormat()
3392 return globalDefaultFormat;
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427void QSettings::setPath(Format format, Scope scope,
const QString &path)
3429 auto locker = qt_unique_lock(settingsGlobalMutex);
3430 PathHash *pathHash = pathHashFunc();
3431 if (pathHash->isEmpty())
3432 locker = initDefaultPaths(std::move(locker));
3433 pathHash->insert(pathHashKey(format, scope), Path(path + QDir::separator(),
true));
3437
3438
3439
3440
3441
3442
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505QSettings::Format QSettings::registerFormat(
const QString &extension, ReadFunc readFunc,
3506 WriteFunc writeFunc,
3507 Qt::CaseSensitivity caseSensitivity)
3510 Q_ASSERT(caseSensitivity == Qt::CaseSensitive);
3513 const auto locker = qt_scoped_lock(settingsGlobalMutex);
3514 CustomFormatVector *customFormatVector = customFormatVectorFunc();
3515 qsizetype index = customFormatVector->size();
3517 return QSettings::InvalidFormat;
3519 QConfFileCustomFormat info;
3520 info.extension = u'.' + extension;
3521 info.readFunc = readFunc;
3522 info.writeFunc = writeFunc;
3523 info.caseSensitivity = caseSensitivity;
3524 customFormatVector->append(info);
3526 return QSettings::Format(
int(QSettings::CustomFormat1) + index);
3531#ifndef QT_BOOTSTRAPPED
3532#include "moc_qsettings.cpp"
~QConfFileSettingsPrivate()
virtual void initAccess()
bool readIniFile(QByteArrayView data, UnparsedSettingsMap *unparsedIniSections)
bool isWritable() const override
QString fileName() const override
UnparsedSettingsMap unparsedIniSections
ParsedSettingsMap originalKeys
ParsedSettingsMap removedKeys
ParsedSettingsMap mergedKeyMap() const
ParsedSettingsMap addedKeys
QSettingsIniKey(const QString &str, qsizetype pos=-1)
static const char charTraits[256]
QMap< QString, QSettingsIniSection > IniMap
QList< QConfFileCustomFormat > CustomFormatVector
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
static constexpr QChar sep
static Path getPath(QSettings::Format format, QSettings::Scope scope)
QMap< QSettingsIniKey, QVariant > IniKeyMap
static int pathHashKey(QSettings::Format format, QSettings::Scope scope)
static QString make_user_path()
static std::unique_lock< QBasicMutex > initDefaultPaths(std::unique_lock< QBasicMutex > locker)
static QString make_user_path_without_qstandard_paths()
QHash< QString, QConfFile * > ConfFileHash
QHash< int, Path > PathHash
Q_DECLARE_TYPEINFO(QSettingsIniSection, Q_RELOCATABLE_TYPE)
Q_DECLARE_TYPEINFO(QConfFileCustomFormat, Q_RELOCATABLE_TYPE)
Q_DECLARE_TYPEINFO(QSettingsIniKey, Q_RELOCATABLE_TYPE)
QCache< QString, QConfFile > ConfFileCache
#define FLUSH_CURRENT_SECTION()
static void iniChopTrailingSpaces(QString &str, qsizetype limit)
QMap< QSettingsKey, QByteArray > UnparsedSettingsMap
QMap< QSettingsKey, QVariant > ParsedSettingsMap
#define QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER