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 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),
1430 if (it->count(u'/') > 1) {
1431 setStatus(QSettings::FormatError);
1437 setStatus(QSettings::FormatError);
1440 confFile->size = fileInfo.size();
1441 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1445
1446
1447
1450 ensureAllSectionsParsed(confFile);
1453#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1454 QSaveFile sf(confFile->name);
1455 sf.setDirectWriteFallback(!atomicSyncOnly);
1458 if (confFile->name.startsWith(
"content:"_L1))
1459 sf.setDirectWriteFallback(
true);
1462 QFile sf(confFile->name);
1464 if (!sf.open(QIODevice::WriteOnly)) {
1465 setStatus(QSettings::AccessError);
1470 if (format == QSettings::WebIndexedDBFormat) {
1471 ok = writeIniFile(sf, mergedKeys);
1475 if (format == QSettings::NativeFormat) {
1476 ok = writePlistFile(sf, mergedKeys);
1479 if (format <= QSettings::IniFormat) {
1480 ok = writeIniFile(sf, mergedKeys);
1481 }
else if (writeFunc) {
1482 QSettings::SettingsMap tempOriginalKeys;
1484 auto i = mergedKeys.constBegin();
1485 while (i != mergedKeys.constEnd()) {
1486 tempOriginalKeys.insert(i.key(), i.value());
1489 ok = writeFunc(sf, tempOriginalKeys);
1492#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(temporaryfile)
1504 confFile->size = fileInfo.size();
1505 confFile->timeStamp = fileInfo.lastModified(QTimeZone::UTC);
1509 QFile::Permissions perms = fileInfo.permissions() | QFile::ReadOwner | QFile::WriteOwner;
1510 if (!confFile->userPerms)
1511 perms |= QFile::ReadGroup | QFile::ReadOther;
1512 QFile(confFile->name).setPermissions(perms);
1515 setStatus(QSettings::AccessError);
1529 0, 0, 0, 0, 0, 0, 0, 0, 0,
Space,
Space |
Special, 0, 0,
Space |
Special, 0, 0,
1530 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1531 Space, 0,
Special, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1532 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Special, 0,
Special, 0, 0,
1533 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1534 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Special, 0, 0, 0,
1535 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1536 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,
1545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1553 qsizetype &lineStart, qsizetype &lineLen,
1554 qsizetype &equalsPos)
1558 qsizetype dataLen = data.size();
1559 bool inQuotes =
false;
1563 lineStart = dataPos;
1564 while (lineStart < dataLen && (
charTraits[uint(uchar(data.at(lineStart)))] & Space))
1567 qsizetype i = lineStart;
1568 while (i < dataLen) {
1569 char ch = data.at(i);
1572 goto break_out_of_outer_loop;
1578 if (!inQuotes && equalsPos == -1)
1580 }
else if (ch ==
'\n' || ch ==
'\r') {
1581 if (i == lineStart + 1) {
1583 }
else if (!inQuotes) {
1585 goto break_out_of_outer_loop;
1587 }
else if (ch ==
'\\') {
1589 char ch = data.at(i++);
1591 char ch2 = data.at(i);
1593 if ((ch ==
'\n' && ch2 ==
'\r') || (ch ==
'\r' && ch2 ==
'\n'))
1597 }
else if (ch ==
'"') {
1598 inQuotes = !inQuotes;
1600 Q_ASSERT(ch ==
';');
1602 if (i == lineStart + 1) {
1603 while (i < dataLen && (((ch = data.at(i)) !=
'\n') && ch !=
'\r'))
1605 while (i < dataLen &&
charTraits[uchar(data.at(i))] & Space)
1608 }
else if (!inQuotes) {
1610 goto break_out_of_outer_loop;
1615break_out_of_outer_loop:
1617 lineLen = i - lineStart;
1622
1623
1624
1625
1629#define FLUSH_CURRENT_SECTION()
1631 QByteArray §ionData = (*unparsedIniSections)[QSettingsKey(currentSection,
1634 if (!sectionData.isEmpty())
1635 sectionData.append('\n');
1636 sectionData += data.first(lineStart).sliced(currentSectionStart);
1637 sectionPosition = ++position;
1640 QString currentSection;
1641 qsizetype currentSectionStart = 0;
1642 qsizetype dataPos = 0;
1643 qsizetype lineStart;
1645 qsizetype equalsPos;
1646 qsizetype position = 0;
1647 qsizetype sectionPosition = 0;
1651 if (data.startsWith(
"\xef\xbb\xbf"))
1652 data = data.sliced(3);
1654 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1655 QByteArrayView line = data.sliced(lineStart, lineLen);
1656 if (line.startsWith(
'[')) {
1660 qsizetype idx = line.indexOf(
']');
1661 Q_ASSERT(idx == -1 || idx > 0);
1662 Q_ASSERT(idx < lineLen);
1667 QByteArrayView iniSection = line.first(idx).sliced(1).trimmed();
1669 if (iniSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1670 currentSection.clear();
1672 if (iniSection.compare(
"%general", Qt::CaseInsensitive) == 0) {
1673 currentSection = QLatin1StringView(iniSection.constData() + 1, iniSection.size() - 1);
1675 currentSection.clear();
1676 iniUnescapedKey(iniSection, currentSection);
1678 currentSection += u'/';
1680 currentSectionStart = dataPos;
1685 Q_ASSERT(lineStart == data.size());
1690#undef FLUSH_CURRENT_SECTION
1696 QStringList strListValue;
1697 bool sectionIsLowercase = (section == section.originalCaseKey());
1698 qsizetype equalsPos;
1701 qsizetype dataPos = 0;
1702 qsizetype lineStart;
1704 qsizetype position = section.originalKeyPosition();
1706 while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
1707 QByteArrayView line = data.sliced(lineStart, lineLen);
1708 Q_ASSERT(!line.startsWith(
'['));
1710 if (equalsPos == -1) {
1711 if (!line.startsWith(
';'))
1716 equalsPos -= lineStart;
1718 Q_ASSERT(equalsPos >= 0 && equalsPos < lineLen);
1720 QByteArrayView key = line.first(equalsPos).trimmed();
1721 QByteArrayView value = line.sliced(equalsPos + 1);
1723 QString strKey = section.originalCaseKey();
1724 const Qt::CaseSensitivity casing = iniUnescapedKey(key, strKey) && sectionIsLowercase
1726 : IniCaseSensitivity;
1729 strValue.reserve(value.size());
1730 QVariant variant = iniUnescapedStringList(value, strValue, strListValue)
1731 ? stringListToVariantList(strListValue)
1732 : stringToVariant(strValue);
1735
1736
1737
1738
1739 settingsMap->insert(
QSettingsKey(strKey, casing, position),
std::move(variant));
1758 if (k1.position != k2.position)
1759 return k1.position < k2.position;
1760 return static_cast<
const QString &>(k1) <
static_cast<
const QString &>(k2);
1778
1779
1780
1786 const char *
const eol =
"\r\n";
1788 const char eol =
'\n';
1791 for (
auto j = map.constBegin(); j != map.constEnd(); ++j) {
1793 QSettingsIniKey key(j.key().originalCaseKey(), j.key().originalKeyPosition());
1796 if ((slashPos = key.indexOf(u'/')) != -1) {
1797 section = key.left(slashPos);
1798 key.remove(0, slashPos + 1);
1804 if (size_t(key.position) < size_t(iniSection.position))
1805 iniSection.position = key.position;
1806 iniSection.keyMap[key] = j.value();
1809 const qsizetype sectionCount = iniMap.size();
1810 QList<QSettingsIniKey> sections;
1811 sections.reserve(sectionCount);
1812 for (
auto i = iniMap.constBegin(); i != iniMap.constEnd(); ++i)
1814 std::sort(sections.begin(), sections.end());
1816 bool writeError =
false;
1817 for (qsizetype j = 0; !writeError && j < sectionCount; ++j) {
1818 auto i = iniMap.constFind(sections.at(j));
1819 Q_ASSERT(i != iniMap.constEnd());
1823 iniEscapedKey(i.key(), realSection);
1825 if (realSection.isEmpty()) {
1826 realSection =
"[General]";
1827 }
else if (realSection.compare(
"general", Qt::CaseInsensitive) == 0) {
1828 realSection =
"[%General]";
1830 realSection.prepend(
'[');
1831 realSection.append(
']');
1835 realSection.prepend(eol);
1838 device.write(realSection);
1840 const IniKeyMap &ents = i.value().keyMap;
1841 for (
auto j = ents.constBegin(); j != ents.constEnd(); ++j) {
1843 iniEscapedKey(j.key(), block);
1846 const QVariant &value = j.value();
1849
1850
1851
1852
1853 if (value.metaType().id() == QMetaType::QStringList
1854 || (value.metaType().id() == QMetaType::QVariantList && value.toList().size() != 1)) {
1855 iniEscapedStringList(variantListToStringList(value.toList()), block);
1857 iniEscapedString(variantToString(value), block);
1860 if (device.write(block) == -1) {
1874 for (; i != end; ++i) {
1875 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1876 setStatus(QSettings::FormatError);
1887 UnparsedSettingsMap::iterator i;
1889 qsizetype indexOfSlash = key.indexOf(u'/');
1890 if (indexOfSlash != -1) {
1895 if (i.key().isEmpty() || !key.startsWith(i.key()))
1903 if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys))
1904 setStatus(QSettings::FormatError);
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
2373
2376
2377
2378
2379
2380
2381
2382
2383
2384
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
2496
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2513#ifndef QT_NO_QOBJECT
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528QSettings::QSettings(
const QString &organization,
const QString &application, QObject *parent)
2529 : QObject(*QSettingsPrivate::create(NativeFormat, UserScope, organization, application),
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application,
2555 : QObject(*QSettingsPrivate::create(NativeFormat, scope, organization, application), parent)
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2578 const QString &application, QObject *parent)
2579 : QObject(*QSettingsPrivate::create(format, scope, organization, application), parent)
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
2612
2613QSettings::QSettings(
const QString &fileName, Format format, QObject *parent)
2614 : QObject(*QSettingsPrivate::create(fileName, format), parent)
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
2652
2653QSettings::QSettings(QObject *parent)
2654 : QSettings(UserScope, parent)
2659
2660
2661
2662
2663
2664
2665
2666QSettings::QSettings(Scope scope, QObject *parent)
2667 : QObject(*QSettingsPrivate::create(globalDefaultFormat, scope,
2669 QCoreApplication::organizationDomain().isEmpty()
2670 ? QCoreApplication::organizationName()
2671 : QCoreApplication::organizationDomain()
2673 QCoreApplication::organizationName().isEmpty()
2674 ? QCoreApplication::organizationDomain()
2675 : QCoreApplication::organizationName()
2677 , QCoreApplication::applicationName()),
2683QSettings::QSettings(
const QString &organization,
const QString &application)
2684 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, QSettings::UserScope, organization, application))
2686 d_ptr->q_ptr =
this;
2689QSettings::QSettings(Scope scope,
const QString &organization,
const QString &application)
2690 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope, organization, application))
2692 d_ptr->q_ptr =
this;
2695QSettings::QSettings(Format format, Scope scope,
const QString &organization,
2696 const QString &application)
2697 : d_ptr(QSettingsPrivate::create(format, scope, organization, application))
2699 d_ptr->q_ptr =
this;
2702QSettings::QSettings(
const QString &fileName, Format format)
2703 : d_ptr(QSettingsPrivate::create(fileName, format))
2705 d_ptr->q_ptr =
this;
2708QSettings::QSettings(Scope scope)
2709 : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope,
2711 QCoreApplication::organizationDomain().isEmpty()
2712 ? QCoreApplication::organizationName()
2713 : QCoreApplication::organizationDomain()
2715 QCoreApplication::organizationName().isEmpty()
2716 ? QCoreApplication::organizationDomain()
2717 : QCoreApplication::organizationName()
2719 , QCoreApplication::applicationName())
2722 d_ptr->q_ptr =
this;
2727
2728
2729
2730
2731
2732
2733
2734QSettings::~QSettings()
2737 if (d->pendingChanges) {
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758void QSettings::clear()
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776void QSettings::sync()
2780 d->pendingChanges =
false;
2784
2785
2786
2787
2788
2789
2790
2791
2792QString QSettings::fileName()
const
2794 Q_D(
const QSettings);
2795 return d->fileName();
2799
2800
2801
2802
2803
2804
2805QSettings::Format QSettings::format()
const
2807 Q_D(
const QSettings);
2812
2813
2814
2815
2816
2817
2818QSettings::Scope QSettings::scope()
const
2820 Q_D(
const QSettings);
2825
2826
2827
2828
2829
2830
2831QString QSettings::organizationName()
const
2833 Q_D(
const QSettings);
2834 return d->organizationName;
2838
2839
2840
2841
2842
2843
2844QString QSettings::applicationName()
const
2846 Q_D(
const QSettings);
2847 return d->applicationName;
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860QSettings::Status QSettings::status()
const
2862 Q_D(
const QSettings);
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877bool QSettings::isAtomicSyncRequired()
const
2879 Q_D(
const QSettings);
2880 return d->atomicSyncOnly;
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904void QSettings::setAtomicSyncRequired(
bool enable)
2907 d->atomicSyncOnly = enable;
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
2938
2939void QSettings::beginGroup(QAnyStringView prefix)
2942 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix)));
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955void QSettings::endGroup()
2958 if (d->groupStack.isEmpty()) {
2959 qWarning(
"QSettings::endGroup: No matching beginGroup()");
2963 QSettingsGroup group = d->groupStack.pop();
2964 qsizetype len = group.toString().size();
2966 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
2968 if (group.isArray())
2969 qWarning(
"QSettings::endGroup: Expected endArray() instead");
2973
2974
2975
2976
2977QString QSettings::group()
const
2979 Q_D(
const QSettings);
2980 return d->groupPrefix.left(d->groupPrefix.size() - 1);
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998int QSettings::beginReadArray(QAnyStringView prefix)
3001 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix),
false));
3002 return value(
"size"_L1).toInt();
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
3036
3037void QSettings::beginWriteArray(QAnyStringView prefix,
int size)
3040 d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), size < 0));
3045 setValue(
"size"_L1, size);
3049
3050
3051
3052
3053
3054void QSettings::endArray()
3057 if (d->groupStack.isEmpty()) {
3058 qWarning(
"QSettings::endArray: No matching beginArray()");
3062 QSettingsGroup group = d->groupStack.top();
3063 qsizetype len = group.toString().size();
3064 d->groupStack.pop();
3066 d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1));
3068 if (group.arraySizeGuess() != -1)
3069 setValue(group.name() +
"/size"_L1, group.arraySizeGuess());
3071 if (!group.isArray())
3072 qWarning(
"QSettings::endArray: Expected endGroup() instead");
3076
3077
3078
3079
3080
3081
3082
3083void QSettings::setArrayIndex(
int i)
3086 if (d->groupStack.isEmpty() || !d->groupStack.top().isArray()) {
3087 qWarning(
"QSettings::setArrayIndex: Missing beginArray()");
3091 QSettingsGroup &top = d->groupStack.top();
3092 qsizetype len = top.toString().size();
3093 top.setArrayIndex(qMax(i, 0));
3094 d->groupPrefix.replace(d->groupPrefix.size() - len - 1, len, top.toString());
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112QStringList QSettings::allKeys()
const
3114 Q_D(
const QSettings);
3115 return d->children(d->groupPrefix, QSettingsPrivate::AllKeys);
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136QStringList QSettings::childKeys()
const
3138 Q_D(
const QSettings);
3139 return d->children(d->groupPrefix, QSettingsPrivate::ChildKeys);
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160QStringList QSettings::childGroups()
const
3162 Q_D(
const QSettings);
3163 return d->children(d->groupPrefix, QSettingsPrivate::ChildGroups);
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178bool QSettings::isWritable()
const
3180 Q_D(
const QSettings);
3181 return d->isWritable();
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204void QSettings::setValue(QAnyStringView key,
const QVariant &value)
3207 if (key.isEmpty()) {
3208 qWarning(
"QSettings::setValue: Empty key passed");
3211 d->set(d->actualKey(key), value);
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238void QSettings::remove(QAnyStringView key)
3242
3243
3244
3245 QString theKey = d->normalizedKey(key);
3246 if (theKey.isEmpty())
3249 theKey.prepend(d->groupPrefix);
3251 if (theKey.isEmpty()) {
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273bool QSettings::contains(QAnyStringView key)
const
3275 Q_D(
const QSettings);
3276 return d->get(d->actualKey(key)) != std::nullopt;
3280
3281
3282
3283
3284
3285
3286void QSettings::setFallbacksEnabled(
bool b)
3293
3294
3295
3296
3297
3298
3299bool QSettings::fallbacksEnabled()
const
3301 Q_D(
const QSettings);
3302 return d->fallbacks;
3305#ifndef QT_NO_QOBJECT
3307
3308
3309bool QSettings::event(QEvent *event)
3312 if (event->type() == QEvent::UpdateRequest) {
3316 return QObject::event(event);
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341QVariant QSettings::value(QAnyStringView key)
const
3343 Q_D(
const QSettings);
3344 return d->value(key,
nullptr);
3347QVariant QSettings::value(QAnyStringView key,
const QVariant &defaultValue)
const
3349 Q_D(
const QSettings);
3350 return d->value(key, &defaultValue);
3353QVariant QSettingsPrivate::value(QAnyStringView key,
const QVariant *defaultValue)
const
3355 if (key.isEmpty()) {
3356 qWarning(
"QSettings::value: Empty key passed");
3359 if (std::optional r = get(actualKey(key)))
3360 return std::move(*r);
3362 return *defaultValue;
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378void QSettings::setDefaultFormat(Format format)
3380 globalDefaultFormat = format;
3384
3385
3386
3387
3388
3389
3390
3391QSettings::Format QSettings::defaultFormat()
3393 return globalDefaultFormat;
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
3427
3428
3429void QSettings::setPath(Format format, Scope scope,
const QString &path)
3431 auto locker = qt_unique_lock(settingsGlobalMutex);
3432 PathHash *pathHash = pathHashFunc();
3433 if (pathHash->isEmpty())
3434 locker = initDefaultPaths(std::move(locker));
3435 pathHash->insert(pathHashKey(format, scope), Path(path + QDir::separator(),
true));
3439
3440
3441
3442
3443
3444
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
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
3505
3506
3507QSettings::Format QSettings::registerFormat(
const QString &extension, ReadFunc readFunc,
3508 WriteFunc writeFunc,
3509 Qt::CaseSensitivity caseSensitivity)
3512 Q_ASSERT(caseSensitivity == Qt::CaseSensitive);
3515 const auto locker = qt_scoped_lock(settingsGlobalMutex);
3516 CustomFormatVector *customFormatVector = customFormatVectorFunc();
3517 qsizetype index = customFormatVector->size();
3519 return QSettings::InvalidFormat;
3521 QConfFileCustomFormat info;
3522 info.extension = u'.' + extension;
3523 info.readFunc = readFunc;
3524 info.writeFunc = writeFunc;
3525 info.caseSensitivity = caseSensitivity;
3526 customFormatVector->append(info);
3528 return QSettings::Format(
int(QSettings::CustomFormat1) + index);
3533#ifndef QT_BOOTSTRAPPED
3534#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