⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsettings.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        ch -= 'a' - 'A';    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {        escapeVal <<= 4;        escapeVal += strchr(hexDigits, ch) - hexDigits;        ++i;        goto StHexEscape;    } else {        stringResult += QChar(escapeVal);        goto StNormal;    }StOctEscape:    if (i >= to) {        stringResult += QChar(escapeVal);        goto end;    }    ch = str.at(i);    if (ch >= '0' && ch <= '7') {        escapeVal <<= 3;        escapeVal += ch - '0';        ++i;        goto StOctEscape;    } else {        stringResult += QChar(escapeVal);        goto StNormal;    }end:    if (!currentValueIsQuoted)        iniChopTrailingSpaces(stringResult);    if (isStringList)        stringListResult.append(stringResult);    return isStringList;}QStringList QSettingsPrivate::splitArgs(const QString &s, int idx){    int l = s.length();    Q_ASSERT(l > 0);    Q_ASSERT(s.at(idx) == QLatin1Char('('));    Q_ASSERT(s.at(l - 1) == QLatin1Char(')'));    QStringList result;    QString item;    for (++idx; idx < l; ++idx) {        QChar c = s.at(idx);        if (c == QLatin1Char(')')) {            Q_ASSERT(idx == l - 1);            result.append(item);        } else if (c == QLatin1Char(' ')) {            result.append(item);            item.clear();        } else {            item.append(c);        }    }    return result;}// ************************************************************************// QConfFileSettingsPrivate/*    If we don't have the permission to read the file, returns false.    If the file doesn't exist, returns true.*/static bool checkAccess(const QString &name){    QFileInfo fileInfo(name);    if (fileInfo.exists()) {        QFile file(name);        // if the file exists but we can't open it, report an error        return file.open(QFile::ReadOnly);    } else {        // Create the directories to the file.        QDir dir(fileInfo.absolutePath());        if (dir.exists() && dir.isReadable()) {            return true;        } else {            return dir.mkpath(dir.absolutePath());        }    }}void QConfFileSettingsPrivate::initFormat(){    extension = (format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini");    readFunc = 0;    writeFunc = 0;#if defined(Q_OS_MAC)    caseSensitivity = (format == QSettings::NativeFormat) ? Qt::CaseSensitive : Qt::CaseInsensitive;#else    caseSensitivity = IniCaseSensitivity;#endif    if (format > QSettings::IniFormat) {        QMutexLocker locker(globalMutex());        const CustomFormatVector *customFormatVector = customFormatVectorFunc();        int i = (int)format - (int)QSettings::CustomFormat1;        if (i >= 0 && i < customFormatVector->size()) {            QConfFileCustomFormat info = customFormatVector->at(i);            extension = info.extension;            readFunc = info.readFunc;            writeFunc = info.writeFunc;            caseSensitivity = info.caseSensitivity;        }    }}void QConfFileSettingsPrivate::initAccess(){    bool readAccess = false;    if (confFiles[spec]) {        readAccess = checkAccess(confFiles[spec]->name);        if (format > QSettings::IniFormat) {            if (!readFunc)                readAccess = false;        }    }    if (!readAccess)        setStatus(QSettings::AccessError);    sync();       // loads the files the first time}#ifdef Q_OS_WINstatic QString windowsConfigPath(int type){    QString result;#ifndef QT_NO_QOBJECT    // We can't use QLibrary if there is QT_NO_QOBJECT is defined    // This only happens when bootstrapping qmake.    QLibrary library(QLatin1String("shell32"));    QT_WA( {        typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPTSTR, int, BOOL);        GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");        if (SHGetSpecialFolderPath) {            TCHAR path[MAX_PATH];            SHGetSpecialFolderPath(0, path, type, FALSE);            result = QString::fromUtf16((ushort*)path);        }    } , {        typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, char*, int, BOOL);        GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathA");        if (SHGetSpecialFolderPath) {            char path[MAX_PATH];            SHGetSpecialFolderPath(0, path, type, FALSE);            result = QString::fromLocal8Bit(path);        }    } );#endif // QT_NO_QOBJECT    if (result.isEmpty()) {        switch (type) {        case CSIDL_COMMON_APPDATA:            result = QLatin1String("C:\\temp\\qt-common");            break;        case CSIDL_APPDATA:            result = QLatin1String("C:\\temp\\qt-user");            break;        default:            ;        }    }    return result;}#endif // Q_OS_WINstatic inline int pathHashKey(QSettings::Format format, QSettings::Scope scope){    return int((uint(format) << 1) | uint(scope == QSettings::SystemScope));}static QString getPath(QSettings::Format format, QSettings::Scope scope){    Q_ASSERT((int)QSettings::NativeFormat == 0);    Q_ASSERT((int)QSettings::IniFormat == 1);    QString homePath = QDir::homePath();    QString systemPath;    globalMutex()->lock();    PathHash *pathHash = pathHashFunc();    bool loadSystemPath = pathHash->isEmpty();    globalMutex()->unlock();    if (loadSystemPath) {        /*           QLibraryInfo::location() uses QSettings, so in order to           avoid a dead-lock, we can't hold the global mutex while           calling it.       */        systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath);        systemPath += QLatin1Char('/');    }    QMutexLocker locker(globalMutex());    if (pathHash->isEmpty()) {        /*           Lazy initialization of pathHash. We initialize the           IniFormat paths and (on Unix) the NativeFormat paths.           (The NativeFormat paths are not configurable for the           Windows registry and the Mac CFPreferences.)       */#ifdef Q_OS_WIN        pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope),                         windowsConfigPath(CSIDL_APPDATA) + QDir::separator());        pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope),                         windowsConfigPath(CSIDL_COMMON_APPDATA) + QDir::separator());#else        QString userPath;        char *env = getenv("XDG_CONFIG_HOME");        if (env == 0) {            userPath = homePath;            userPath += QLatin1Char('/');#ifdef Q_WS_QWS            userPath += QLatin1String("Settings");#else            userPath += QLatin1String(".config");#endif        } else if (*env == '/') {            userPath = QLatin1String(env);        } else {            userPath = homePath;            userPath += QLatin1Char('/');            userPath += QLatin1String(env);        }        userPath += QLatin1Char('/');        pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), userPath);        pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), systemPath);#ifndef Q_OS_MAC        pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), userPath);        pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), systemPath);#endif#endif    }    QString result = pathHash->value(pathHashKey(format, scope));    if (!result.isEmpty())        return result;    // fall back on INI path    return pathHash->value(pathHashKey(QSettings::IniFormat, scope));}QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format,                                                   QSettings::Scope scope,                                                   const QString &organization,                                                   const QString &application){    int i;    this->format = format;    initFormat();    for (i = 0; i < NumConfFiles; ++i)        confFiles[i] = 0;    QString org = organization;    if (org.isEmpty()) {        setStatus(QSettings::AccessError);        org = QLatin1String("Unknown Organization");    }    QString appFile = org + QDir::separator() + application + extension;    QString orgFile = org + extension;    if (scope == QSettings::UserScope) {        QString userPath = getPath(format, QSettings::UserScope);        if (!application.isEmpty())            confFiles[F_User | F_Application] = QConfFile::fromName(userPath + appFile, true);        confFiles[F_User | F_Organization] = QConfFile::fromName(userPath + orgFile, true);    }    QString systemPath = getPath(format, QSettings::SystemScope);    if (!application.isEmpty())        confFiles[F_System | F_Application] = QConfFile::fromName(systemPath + appFile, false);    confFiles[F_System | F_Organization] = QConfFile::fromName(systemPath + orgFile, false);    for (i = 0; i < NumConfFiles; ++i) {        if (confFiles[i]) {            spec = i;            break;        }    }    initAccess();}QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName,                                                   QSettings::Format format){    this->format = format;    initFormat();    confFiles[0] = QConfFile::fromName(fileName, true);    for (int i = 1; i < NumConfFiles; ++i)        confFiles[i] = 0;    initAccess();}QConfFileSettingsPrivate::~QConfFileSettingsPrivate(){    QMutexLocker locker(globalMutex());    ConfFileHash *usedHash = usedHashFunc();    ConfFileCache *unusedCache = unusedCacheFunc();    for (int i = 0; i < NumConfFiles; ++i) {        if (confFiles[i] && !confFiles[i]->ref.deref()) {            if (usedHash)                usedHash->remove(confFiles[i]->name);            if (confFiles[i]->size == 0) {                delete confFiles[i];            } else if (unusedCache) {                // ### compute a better size                unusedCache->insert(confFiles[i]->name, confFiles[i],                                    10 + (confFiles[i]->originalKeys.size() / 4));            }        }    }}void QConfFileSettingsPrivate::remove(const QString &key){    QConfFile *confFile = confFiles[spec];    if (!confFile)        return;    QSettingsKey theKey(key, caseSensitivity);    QSettingsKey prefix(key + QLatin1Char('/'), caseSensitivity);    QMutexLocker locker(&confFile->mutex);    ensureSectionParsed(confFile, theKey);    ensureSectionParsed(confFile, prefix);    ParsedSettingsMap::iterator i = confFile->addedKeys.lowerBound(prefix);    while (i != confFile->addedKeys.end() && i.key().startsWith(prefix))        i = confFile->addedKeys.erase(i);    confFile->addedKeys.remove(theKey);    ParsedSettingsMap::const_iterator j = const_cast<const ParsedSettingsMap *>(&confFile->originalKeys)->lowerBound(prefix);    while (j != confFile->originalKeys.constEnd() && j.key().startsWith(prefix)) {        confFile->removedKeys.insert(j.key(), QVariant());        ++j;    }    if (confFile->originalKeys.contains(theKey))        confFile->removedKeys.insert(theKey, QVariant());}void QConfFileSettingsPrivate::set(const QString &key, const QVariant &value){    QConfFile *confFile = confFiles[spec];    if (!confFile)        return;    QSettingsKey theKey(key, caseSensitivity);    QMutexLocker locker(&confFile->mutex);    confFile->removedKeys.remove(theKey);    confFile->addedKeys.insert(theKey, value);}bool QConfFileSettingsPrivate::get(const QString &key, QVariant *value) const{    QSettingsKey theKey(key, caseSensitivity);    ParsedSettingsMap::const_iterator j;    bool found = false;    for (int i = 0; i < NumConfFiles; ++i) {        if (QConfFile *confFile = confFiles[i]) {            QMutexLocker locker(&confFile->mutex);            if (!confFile->addedKeys.isEmpty()) {                j = confFile->addedKeys.constFind(theKey);                found = (j != confFile->addedKeys.constEnd());            }            if (!found) {                ensureSectionParsed(confFile, theKey);                j = confFile->originalKeys.constFind(theKey);                found = (j != confFile->originalKeys.constEnd()                         && !confFile->removedKeys.contains(theKey));            }            if (found && value)                *value = *j;            if (found)                return true;            if (!fallbacks)                break;        }    }    return false;}QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec spec) const{    QMap<QString, QString> result;    ParsedSettingsMap::const_iterator j;    QSettingsKey thePrefix(prefix, caseSensitivity);    int startPos = prefix.size();    for (int i = 0; i < NumConfFiles; ++i) {        if (QConfFile *confFile = confFiles[i]) {            QMutexLocker locker(&confFile->mutex);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -