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());
341 for (
const auto &v : l)
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
1003 QString systemPath = QLibraryInfo::paths(QLibraryInfo::SettingsPath).value(0, QString()) + u'/';
1006 if (pathHash->isEmpty()) {
1008
1009
1010
1011
1012
1014 const QString roamingAppDataFolder = windowsConfigPath(FOLDERID_RoamingAppData);
1015 const QString programDataFolder = windowsConfigPath(FOLDERID_ProgramData);
1016 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope),
1017 Path(roamingAppDataFolder + QDir::separator(),
false));
1018 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope),
1019 Path(programDataFolder + QDir::separator(),
false));
1021 const QString userPath = make_user_path();
1022 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), Path(userPath,
false));
1023 pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), Path(systemPath,
false));
1025 pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), Path(userPath,
false));
1026 pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), Path(systemPath,
false));
1034static Path
getPath(QSettings::Format format, QSettings::Scope scope)
1036 Q_ASSERT(
int(QSettings::NativeFormat) == 0);
1037 Q_ASSERT(
int(QSettings::IniFormat) == 1);
1039 auto locker = qt_unique_lock(settingsGlobalMutex);
1040 PathHash *pathHash = pathHashFunc();
1041 if (pathHash->isEmpty())
1042 locker = initDefaultPaths(
std::move(locker));
1044 Path result = pathHash->value(pathHashKey(format, scope));
1045 if (!result.path.isEmpty())
1049 return pathHash->value(pathHashKey(QSettings::IniFormat, scope));
1052#if defined(QT_BUILD_INTERNAL) && defined(Q_XDG_PLATFORM) && !defined(QT_NO_STANDARDPATHS)
1054void Q_AUTOTEST_EXPORT clearDefaultPaths()
1056 const auto locker = qt_scoped_lock(settingsGlobalMutex);
1057 pathHashFunc()->clear();
1062 QSettings::Scope scope,
1063 const QString &organization,
1064 const QString &application)
1065 : QSettingsPrivate(format, scope, organization, application),
1066 nextPosition(0x40000000)
1070 QString org = organization;
1071 if (org.isEmpty()) {
1072 setStatus(QSettings::AccessError);
1073 org =
"Unknown Organization"_L1;
1076 QString appFile = org + QDir::separator() + application + extension;
1077 QString orgFile = org + extension;
1079 if (scope == QSettings::UserScope) {
1080 Path userPath = getPath(format, QSettings::UserScope);
1081 if (!application.isEmpty())
1082 confFiles.append(QConfFile::fromName(userPath.path + appFile,
true));
1083 confFiles.append(QConfFile::fromName(userPath.path + orgFile,
true));
1086 Path systemPath = getPath(format, QSettings::SystemScope);
1087#if defined(Q_XDG_PLATFORM) && !defined(QT_NO_STANDARDPATHS)
1089 if (!systemPath.userDefined) {
1093 QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation);
1095 if (!dirs.isEmpty())
1098 if (!application.isEmpty()) {
1099 paths.reserve(dirs.size() * 2);
1100 for (
const auto &dir : std::as_const(dirs))
1101 paths.append(dir + u'/' + appFile);
1103 paths.reserve(dirs.size());
1105 for (
const auto &dir : std::as_const(dirs))
1106 paths.append(dir + u'/' + orgFile);
1109 for (
const auto &path : std::as_const(paths))
1110 confFiles.append(QConfFile::fromName(path,
false));
1114 if (!application.isEmpty())
1115 confFiles.append(QConfFile::fromName(systemPath.path + appFile,
false));
1116 confFiles.append(QConfFile::fromName(systemPath.path + orgFile,
false));
1123 QSettings::Format format)
1124 : QSettingsPrivate(format),
1125 nextPosition(0x40000000)
1129 confFiles.append(QConfFile::fromName(fileName,
true));
1136 const auto locker = qt_scoped_lock(settingsGlobalMutex);
1140 for (
auto conf_file : std::as_const(confFiles)) {
1141 if (!conf_file->ref.deref()) {
1142 if (conf_file->size == 0) {
1146 usedHash->remove(conf_file->name);
1150 unusedCache->insert(conf_file->name, conf_file,
1151 10 + (conf_file->originalKeys.size() / 4));
1167 if (confFiles.isEmpty())
1175 const auto locker = qt_scoped_lock(confFile->mutex);
1177 ensureSectionParsed(confFile, theKey);
1178 ensureSectionParsed(confFile, prefix);
1181 while (i != confFile
->addedKeys.end() && i.key().startsWith(prefix))
1186 while (j != confFile
->originalKeys.constEnd() && j.key().startsWith(prefix)) {
1196 if (confFiles.isEmpty())
1202 QSettingsKey theKey(key, caseSensitivity, nextPosition++);
1203 const auto locker = qt_scoped_lock(confFile->mutex);
1211 ParsedSettingsMap::const_iterator j;
1214 for (
auto confFile : std::as_const(confFiles)) {
1215 const auto locker = qt_scoped_lock(confFile->mutex);
1217 if (!confFile->addedKeys.isEmpty()) {
1218 j = confFile->addedKeys.constFind(theKey);
1219 found = (j != confFile->addedKeys.constEnd());
1222 ensureSectionParsed(confFile, theKey);
1223 j = confFile->originalKeys.constFind(theKey);
1224 found = (j != confFile->originalKeys.constEnd()
1225 && !confFile->removedKeys.contains(theKey));
1233 return std::nullopt;
1241 qsizetype startPos = prefix.size();
1243 for (
auto confFile : std::as_const(confFiles)) {
1244 const auto locker = qt_scoped_lock(confFile->mutex);
1246 if (thePrefix.isEmpty())
1247 ensureAllSectionsParsed(confFile);
1249 ensureSectionParsed(confFile, thePrefix);
1251 const auto &originalKeys = confFile->originalKeys;
1252 auto i = originalKeys.lowerBound(thePrefix);
1253 while (i != originalKeys.end() && i.key().startsWith(thePrefix)) {
1254 if (!confFile->removedKeys.contains(i.key()))
1255 processChild(QStringView{i.key().originalCaseKey()}.sliced(startPos), spec, result);
1259 const auto &addedKeys = confFile->addedKeys;
1260 auto j = addedKeys.lowerBound(thePrefix);
1261 while (j != addedKeys.end() && j.key().startsWith(thePrefix)) {
1262 processChild(QStringView{j.key().originalCaseKey()}.sliced(startPos), spec, result);
1269 std::sort(result.begin(), result.end());
1270 result.erase(
std::unique(result.begin(), result.end()),
1277 if (confFiles.isEmpty())
1283 const auto locker = qt_scoped_lock(confFile->mutex);
1284 ensureAllSectionsParsed(confFile);
1294 for (
auto confFile : std::as_const(confFiles)) {
1295 const auto locker = qt_scoped_lock(confFile->mutex);
1296 syncConfFile(confFile);
1307 if (confFiles.isEmpty())
1311 return confFiles.at(0)->name;
1316#if defined(Q_OS_WASM)
1317 if (format > QSettings::IniFormat && format != QSettings::WebIndexedDBFormat && !writeFunc)
1319 if (format > QSettings::IniFormat && !writeFunc)
1323 if (confFiles.isEmpty())
1326 return confFiles.at(0)->isWritable();
1335
1336
1337
1338 if (readOnly && confFile->size > 0) {
1339 if (confFile->size == fileInfo.size() && confFile->timeStamp == fileInfo.lastModified(QTimeZone::UTC))
1344 setStatus(QSettings::AccessError);
1348#ifndef QT_BOOTSTRAPPED
1349 QString lockFileName = confFile->name +
".lock"_L1;
1351# if defined(Q_OS_ANDROID) && defined(QSETTINGS_USE_QSTANDARDPATHS)
1354 if (confFile->name.startsWith(
"content:"_L1))
1355 lockFileName = make_user_path() + QFileInfo(lockFileName).fileName();
1358
1359
1360
1361
1362
1363
1365 if (!readOnly && !lockFile.lock() && atomicSyncOnly) {
1366 setStatus(QSettings::AccessError);
1372
1373
1374
1376 bool mustReadFile =
true;
1377 bool createFile = !fileInfo.exists();
1380 mustReadFile = (confFile->size != fileInfo.size()
1381 || (confFile->size != 0 && confFile->timeStamp != fileInfo.lastModified(QTimeZone::UTC)));
1387 QFile file(confFile->name);
1388 if (!createFile && !file.open(QFile::ReadOnly)) {
1389 setStatus(QSettings::AccessError);
1394
1395
1396
1397 if (file.isReadable() && file.size() != 0) {
1401 if (format == QSettings::WebIndexedDBFormat) {
1402 QByteArray data = file.readAll();
1403 ok = readIniFile(data, &confFile->unparsedIniSections);
1407 if (format == QSettings::NativeFormat) {
1408 QByteArray data = file.readAll();
1409 ok = readPlistFile(data, &confFile->originalKeys);
1412 if (format <= QSettings::IniFormat) {
1414 ok = readIniFile(data, &confFile->unparsedIniSections);
1415 }
else if (readFunc) {
1416 QSettings::SettingsMap tempNewKeys;
1417 ok = readFunc(file, tempNewKeys);
1420 auto i = tempNewKeys.constBegin();
1421 while (i != tempNewKeys.constEnd()) {
1422 confFile
->originalKeys.insert(QSettingsKey(i.key(), caseSensitivity),
1431 if (it->count(u'/') > 1) {
1432 setStatus(QSettings::FormatError);
1438 setStatus(QSettings::FormatError);
1441 confFile->size = fileInfo.size();
1442 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1446
1447
1448
1451 ensureAllSectionsParsed(confFile);
1454#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1455 QSaveFile sf(confFile->name);
1456 sf.setDirectWriteFallback(!atomicSyncOnly);
1459 if (confFile->name.startsWith(
"content:"_L1))
1460 sf.setDirectWriteFallback(
true);
1463 QFile sf(confFile->name);
1465 if (!sf.open(QIODevice::WriteOnly)) {
1466 setStatus(QSettings::AccessError);
1471 if (format == QSettings::WebIndexedDBFormat) {
1472 ok = writeIniFile(sf, mergedKeys);
1476 if (format == QSettings::NativeFormat) {
1477 ok = writePlistFile(sf, mergedKeys);
1480 if (format <= QSettings::IniFormat) {
1481 ok = writeIniFile(sf, mergedKeys);
1482 }
else if (writeFunc) {
1483 QSettings::SettingsMap tempOriginalKeys;
1485 auto i = mergedKeys.constBegin();
1486 while (i != mergedKeys.constEnd()) {
1487 tempOriginalKeys.insert(i.key(), i.value());
1490 ok = writeFunc(sf, tempOriginalKeys);
1493#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1505 confFile->size = fileInfo.size();
1506 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1510 QFile::Permissions perms = fileInfo.permissions() | QFile::ReadOwner | QFile::WriteOwner;
1511 if (!confFile->userPerms)
1512 perms |= QFile::ReadGroup | QFile::ReadOther;
1513 QFile(confFile->name).setPermissions(perms);
1516 setStatus(QSettings::AccessError);
1530 0, 0, 0, 0, 0, 0, 0, 0, 0,
Space,
Space |
Special, 0, 0,
Space |
Special, 0, 0,
1531 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1532 Space, 0,
Special, 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,
Special, 0,
Special, 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,
Special, 0, 0, 0,
1536 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,
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,
1545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1546 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1554 qsizetype &lineStart, qsizetype &lineLen,
1555 qsizetype &equalsPos)
1559 qsizetype dataLen = data.size();
1560 bool inQuotes =
false;
1564 lineStart = dataPos;
1565 while (lineStart < dataLen && (
charTraits[uint(uchar(data.at(lineStart)))] & Space))
1568 qsizetype i = lineStart;
1569 while (i < dataLen) {
1570 char ch = data.at(i);
1573 goto break_out_of_outer_loop;
1579 if (!inQuotes && equalsPos == -1)
1581 }
else if (ch ==
'\n' || ch ==
'\r') {
1582 if (i == lineStart + 1) {
1584 }
else if (!inQuotes) {
1586 goto break_out_of_outer_loop;
1588 }
else if (ch ==
'\\') {
1590 char ch = data.at(i++);
1592 char ch2 = data.at(i);
1594 if ((ch ==
'\n' && ch2 ==
'\r') || (ch ==
'\r' && ch2 ==
'\n'))
1598 }
else if (ch ==
'"') {
1599 inQuotes = !inQuotes;
1601 Q_ASSERT(ch ==
';');
1603 if (i == lineStart + 1) {
1604 while (i < dataLen && (((ch = data.at(i)) !=
'\n') && ch !=
'\r'))
1606 while (i < dataLen &&
charTraits[uchar(data.at(i))] & Space)
1609 }
else if (!inQuotes) {
1611 goto break_out_of_outer_loop;
1616break_out_of_outer_loop:
1618 lineLen = i - lineStart;
1623
1624
1625
1626
1630#define FLUSH_CURRENT_SECTION()
1632 QByteArray §ionData = (*unparsedIniSections)[QSettingsKey(currentSection,
1635 if (!sectionData.isEmpty())
1636 sectionData.append('\n');
1637 sectionData += data.first(lineStart).sliced(currentSectionStart);
1638 sectionPosition = ++position;
1641 QString currentSection;
1642 qsizetype currentSectionStart = 0;
1643 qsizetype dataPos = 0;
1644 qsizetype lineStart;
1646 qsizetype equalsPos;
1647 qsizetype position = 0;
1648 qsizetype sectionPosition = 0;
1652 if (data.startsWith(
"\xef\xbb\xbf"))
1653 data = data.sliced(3);
1655 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1656 QByteArrayView line = data.sliced(lineStart, lineLen);
1657 if (line.startsWith(
'[')) {
1661 qsizetype idx = line.indexOf(
']');
1662 Q_ASSERT(idx == -1 || idx > 0);
1663 Q_ASSERT(idx < lineLen);
1668 QByteArrayView iniSection = line.first(idx).sliced(1).trimmed();
1670 if (iniSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1671 currentSection.clear();
1673 if (iniSection.compare(
"%general", Qt::CaseInsensitive) == 0) {
1674 currentSection = QLatin1StringView(iniSection.constData() + 1, iniSection.size() - 1);
1676 currentSection.clear();
1677 iniUnescapedKey(iniSection, currentSection);
1679 currentSection += u'/';
1681 currentSectionStart = dataPos;
1686 Q_ASSERT(lineStart == data.size());
1691#undef FLUSH_CURRENT_SECTION
1697 QStringList strListValue;
1698 bool sectionIsLowercase = (section == section.originalCaseKey());
1699 qsizetype equalsPos;
1702 qsizetype dataPos = 0;
1703 qsizetype lineStart;
1705 qsizetype position = section.originalKeyPosition();
1707 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1708 QByteArrayView line = data.sliced(lineStart, lineLen);
1709 Q_ASSERT(!line.startsWith(
'['));
1711 if (equalsPos == -1) {
1712 if (!line.startsWith(
';'))
1717 equalsPos -= lineStart;
1719 Q_ASSERT(equalsPos >= 0 && equalsPos < lineLen);
1721 QByteArrayView key = line.first(equalsPos).trimmed();
1722 QByteArrayView value = line.sliced(equalsPos + 1);
1724 QString strKey = section.originalCaseKey();
1725 const Qt::CaseSensitivity casing = iniUnescapedKey(key, strKey) && sectionIsLowercase
1727 : IniCaseSensitivity;
1730 strValue.reserve(value.size());
1731 QVariant variant = iniUnescapedStringList(value, strValue, strListValue)
1732 ? stringListToVariantList(strListValue)
1733 : stringToVariant(strValue);
1736
1737
1738
1739
1740 settingsMap->insert(
QSettingsKey(strKey, casing, position),
std::move(variant));
1759 if (k1.position != k2.position)
1760 return k1.position < k2.position;
1761 return static_cast<
const QString &>(k1) <
static_cast<
const QString &>(k2);
1779
1780
1781
1787 const char *
const eol =
"\r\n";
1789 const char eol =
'\n';
1792 for (
auto j = map.constBegin(); j != map.constEnd(); ++j) {
1794 QSettingsIniKey key(j.key().originalCaseKey(), j.key().originalKeyPosition());
1797 if ((slashPos = key.indexOf(u'/')) != -1) {
1798 section = key.left(slashPos);
1799 key.remove(0, slashPos + 1);
1805 if (size_t(key.position) < size_t(iniSection.position))
1806 iniSection.position = key.position;
1807 iniSection.keyMap[key] = j.value();
1810 const qsizetype sectionCount = iniMap.size();
1811 QList<QSettingsIniKey> sections;
1812 sections.reserve(sectionCount);
1813 for (
auto i = iniMap.constBegin(); i != iniMap.constEnd(); ++i)
1815 std::sort(sections.begin(), sections.end());
1817 bool writeError =
false;
1818 for (qsizetype j = 0; !writeError && j < sectionCount; ++j) {
1819 auto i = iniMap.constFind(sections.at(j));
1820 Q_ASSERT(i != iniMap.constEnd());
1824 iniEscapedKey(i.key(), realSection);
1826 if (realSection.isEmpty()) {
1827 realSection =
"[General]";
1828 }
else if (realSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1829 realSection =
"[%General]";
1831 realSection.prepend(
'[');
1832 realSection.append(
']');
1836 realSection.prepend(eol);
1839 device.write(realSection);
1841 const IniKeyMap &ents = i.value().keyMap;
1842 for (
auto j = ents.constBegin(); j != ents.constEnd(); ++j) {
1844 iniEscapedKey(j.key(), block);
1847 const QVariant &value = j.value();
1850
1851
1852
1853
1854 if (value.metaType().id() == QMetaType::QStringList
1855 || (value.metaType().id() == QMetaType::QVariantList && value.toList().size() != 1)) {
1856 iniEscapedStringList(variantListToStringList(value.toList()), block);
1858 iniEscapedString(variantToString(value), block);
1861 if (device.write(block) == -1) {
1875 for (; i != end; ++i) {
1876 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1877 setStatus(QSettings::FormatError);
1888 UnparsedSettingsMap::iterator i;
1890 qsizetype indexOfSlash = key.indexOf(u'/');
1891 if (indexOfSlash != -1) {
1896 if (i.key().isEmpty() || !key.startsWith(i.key()))
1904 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1905 setStatus(QSettings::FormatError);
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
2373
2374
2375
2376
2377
2378
2379
2380
2381
2384
2385
2386
2387
2388
2389
2390
2391
2392
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
2496
2497
2498
2499
2500
2501
2502
2503
2504
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2521#ifndef QT_NO_QOBJECT
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536QSettings::QSettings(
const QString &organization,
const QString &application, QObject *parent)
2537 : QObject(*QSettingsPrivate::create(NativeFormat, UserScope, organization, application),
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application,
2563 : QObject(*QSettingsPrivate::create(NativeFormat, scope, organization, application), parent)
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2586 const QString &application, QObject *parent)
2587 : QObject(*QSettingsPrivate::create(format, scope, organization, application), parent)
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621QSettings::QSettings(
const QString &fileName, Format format, QObject *parent)
2622 : QObject(*QSettingsPrivate::create(fileName, format), parent)
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
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661QSettings::QSettings(QObject *parent)
2662 : QSettings(UserScope, parent)
2667
2668
2669
2670
2671
2672
2673
2674QSettings::QSettings(Scope scope, QObject *parent)
2675 : QObject(*QSettingsPrivate::create(globalDefaultFormat, scope,
2677 QCoreApplication::organizationDomain().isEmpty()
2678 ? QCoreApplication::organizationName()
2679 : QCoreApplication::organizationDomain()
2681 QCoreApplication::organizationName().isEmpty()
2682 ? QCoreApplication::organizationDomain()
2683 : QCoreApplication::organizationName()
2685 , QCoreApplication::applicationName()),
2691QSettings::QSettings(
const QString &organization,
const QString &application)
2692 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, QSettings::UserScope, organization, application))
2694 d_ptr->q_ptr =
this;
2697QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application)
2698 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope, organization, application))
2700 d_ptr->q_ptr =
this;
2703QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2704 const QString &application)
2705 : d_ptr(QSettingsPrivate::create(format, scope, organization, application))
2707 d_ptr->q_ptr =
this;
2710QSettings::QSettings(
const QString &fileName, Format format)
2711 : d_ptr(QSettingsPrivate::create(fileName, format))
2713 d_ptr->q_ptr =
this;
2716QSettings::QSettings(Scope scope)
2717 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope,
2719 QCoreApplication::organizationDomain().isEmpty()
2720 ? QCoreApplication::organizationName()
2721 : QCoreApplication::organizationDomain()
2723 QCoreApplication::organizationName().isEmpty()
2724 ? QCoreApplication::organizationDomain()
2725 : QCoreApplication::organizationName()
2727 , QCoreApplication::applicationName())
2730 d_ptr->q_ptr =
this;
2735
2736
2737
2738
2739
2740
2741
2742QSettings::~QSettings()
2745 if (d->pendingChanges) {
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766void QSettings::clear()
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784void QSettings::sync()
2788 d->pendingChanges =
false;
2792
2793
2794
2795
2796
2797
2798
2799
2800QString QSettings::fileName()
const
2802 Q_D(
const QSettings);
2803 return d->fileName();
2807
2808
2809
2810
2811
2812
2813QSettings::Format QSettings::format()
const
2815 Q_D(
const QSettings);
2820
2821
2822
2823
2824
2825
2826QSettings::Scope QSettings::scope()
const
2828 Q_D(
const QSettings);
2833
2834
2835
2836
2837
2838
2839QString QSettings::organizationName()
const
2841 Q_D(
const QSettings);
2842 return d->organizationName;
2846
2847
2848
2849
2850
2851
2852QString QSettings::applicationName()
const
2854 Q_D(
const QSettings);
2855 return d->applicationName;
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868QSettings::Status QSettings::status()
const
2870 Q_D(
const QSettings);
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885bool QSettings::isAtomicSyncRequired()
const
2887 Q_D(
const QSettings);
2888 return d->atomicSyncOnly;
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912void QSettings::setAtomicSyncRequired(
bool enable)
2915 d->atomicSyncOnly = enable;
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947void QSettings::beginGroup(QAnyStringView prefix)
2950 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix)));
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963void QSettings::endGroup()
2966 if (d->groupStack.isEmpty()) {
2967 qWarning(
"QSettings::endGroup: No matching beginGroup()");
2971 QSettingsGroup group = d->groupStack.pop();
2972 qsizetype len = group.toString().size();
2974 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
2976 if (group.isArray())
2977 qWarning(
"QSettings::endGroup: Expected endArray() instead");
2981
2982
2983
2984
2985QString QSettings::group()
const
2987 Q_D(
const QSettings);
2988 return d->groupPrefix.left(d->groupPrefix.size() - 1);
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006int QSettings::beginReadArray(QAnyStringView prefix)
3009 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix),
false));
3010 return value(
"size"_L1).toInt();
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045void QSettings::beginWriteArray(QAnyStringView prefix,
int size)
3048 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), size < 0));
3053 setValue(
"size"_L1, size);
3057
3058
3059
3060
3061
3062void QSettings::endArray()
3065 if (d->groupStack.isEmpty()) {
3066 qWarning(
"QSettings::endArray: No matching beginArray()");
3070 QSettingsGroup group = d->groupStack.top();
3071 qsizetype len = group.toString().size();
3072 d->groupStack.pop();
3074 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
3076 if (group.arraySizeGuess() != -1)
3077 setValue(group.name() +
"/size"_L1, group.arraySizeGuess());
3079 if (!group.isArray())
3080 qWarning(
"QSettings::endArray: Expected endGroup() instead");
3084
3085
3086
3087
3088
3089
3090
3091void QSettings::setArrayIndex(
int i)
3094 if (d->groupStack.isEmpty() || !d->groupStack.top().isArray()) {
3095 qWarning(
"QSettings::setArrayIndex: Missing beginArray()");
3099 QSettingsGroup &top = d->groupStack.top();
3100 qsizetype len = top.toString().size();
3101 top.setArrayIndex(qMax(i, 0));
3102 d->groupPrefix.replace(d->groupPrefix.size() - len - 1, len, top.toString());
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120QStringList QSettings::allKeys()
const
3122 Q_D(
const QSettings);
3123 return d->children(d->groupPrefix, QSettingsPrivate::AllKeys);
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144QStringList QSettings::childKeys()
const
3146 Q_D(
const QSettings);
3147 return d->children(d->groupPrefix, QSettingsPrivate::ChildKeys);
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168QStringList QSettings::childGroups()
const
3170 Q_D(
const QSettings);
3171 return d->children(d->groupPrefix, QSettingsPrivate::ChildGroups);
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186bool QSettings::isWritable()
const
3188 Q_D(
const QSettings);
3189 return d->isWritable();
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212void QSettings::setValue(QAnyStringView key,
const QVariant &value)
3215 if (key.isEmpty()) {
3216 qWarning(
"QSettings::setValue: Empty key passed");
3219 d->set(d->actualKey(key), value);
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246void QSettings::remove(QAnyStringView key)
3250
3251
3252
3253 QString theKey = d->normalizedKey(key);
3254 if (theKey.isEmpty())
3257 theKey.prepend(d->groupPrefix);
3259 if (theKey.isEmpty()) {
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281bool QSettings::contains(QAnyStringView key)
const
3283 Q_D(
const QSettings);
3284 return d->get(d->actualKey(key)) != std::nullopt;
3288
3289
3290
3291
3292
3293
3294void QSettings::setFallbacksEnabled(
bool b)
3301
3302
3303
3304
3305
3306
3307bool QSettings::fallbacksEnabled()
const
3309 Q_D(
const QSettings);
3310 return d->fallbacks;
3313#ifndef QT_NO_QOBJECT
3315
3316
3317bool QSettings::event(QEvent *event)
3320 if (event->type() == QEvent::UpdateRequest) {
3324 return QObject::event(event);
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349QVariant QSettings::value(QAnyStringView key)
const
3351 Q_D(
const QSettings);
3352 return d->value(key,
nullptr);
3355QVariant QSettings::value(QAnyStringView key,
const QVariant &defaultValue)
const
3357 Q_D(
const QSettings);
3358 return d->value(key, &defaultValue);
3361QVariant QSettingsPrivate::value(QAnyStringView key,
const QVariant *defaultValue)
const
3363 if (key.isEmpty()) {
3364 qWarning(
"QSettings::value: Empty key passed");
3367 if (std::optional r = get(actualKey(key)))
3368 return std::move(*r);
3370 return *defaultValue;
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386void QSettings::setDefaultFormat(Format format)
3388 globalDefaultFormat = format;
3392
3393
3394
3395
3396
3397
3398
3399QSettings::Format QSettings::defaultFormat()
3401 return globalDefaultFormat;
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437void QSettings::setPath(Format format, Scope scope,
const QString &path)
3439 auto locker = qt_unique_lock(settingsGlobalMutex);
3440 PathHash *pathHash = pathHashFunc();
3441 if (pathHash->isEmpty())
3442 locker = initDefaultPaths(std::move(locker));
3443 pathHash->insert(pathHashKey(format, scope), Path(path + QDir::separator(),
true));
3447
3448
3449
3450
3451
3452
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515QSettings::Format QSettings::registerFormat(
const QString &extension, ReadFunc readFunc,
3516 WriteFunc writeFunc,
3517 Qt::CaseSensitivity caseSensitivity)
3520 Q_ASSERT(caseSensitivity == Qt::CaseSensitive);
3523 const auto locker = qt_scoped_lock(settingsGlobalMutex);
3524 CustomFormatVector *customFormatVector = customFormatVectorFunc();
3525 qsizetype index = customFormatVector->size();
3527 return QSettings::InvalidFormat;
3529 QConfFileCustomFormat info;
3530 info.extension = u'.' + extension;
3531 info.readFunc = readFunc;
3532 info.writeFunc = writeFunc;
3533 info.caseSensitivity = caseSensitivity;
3534 customFormatVector->append(info);
3536 return QSettings::Format(
int(QSettings::CustomFormat1) + index);
3541#ifndef QT_BOOTSTRAPPED
3542#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