当前位置: 首页>>代码示例>>C++>>正文


C++ QTextStream::codec方法代码示例

本文整理汇总了C++中QTextStream::codec方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextStream::codec方法的具体用法?C++ QTextStream::codec怎么用?C++ QTextStream::codec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QTextStream的用法示例。


在下文中一共展示了QTextStream::codec方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: sectionRe

bool Wizard::IniSettings::writeFile(const QString &path, QTextStream &stream)
{
    // Look for a square bracket, "'\\["
    // that has one or more "not nothing" in it, "([^]]+)"
    // and is closed with a square bracket, "\\]"
    QRegExp sectionRe(QLatin1String("^\\[([^]]+)\\]"));

    // Find any character(s) that is/are not equal sign(s), "[^=]+"
    // followed by an optional whitespace, an equal sign, and another optional whitespace, "\\s*=\\s*"
    // and one or more periods, "(.+)"
    QRegExp keyRe(QLatin1String("^([^=]+)\\s*=\\s*(.+)$"));

    const QStringList keys(mSettings.keys());

    QString currentSection;
    QString buffer;

    while (!stream.atEnd()) {

        const QString line(stream.readLine());

        if (line.isEmpty() || line.startsWith(QLatin1Char(';'))) {
            buffer.append(line + QLatin1String("\n"));
            continue;
        }

        if (sectionRe.exactMatch(line)) {
            buffer.append(line + QLatin1String("\n"));
            currentSection = sectionRe.cap(1);
        } else  if (keyRe.indexIn(line) != -1) {
            QString key(keyRe.cap(1).trimmed());
            QString lookupKey(key);

            // Append the section, but only if there is one
            if (!currentSection.isEmpty())
                lookupKey = currentSection + QLatin1Char('/') + key;

            buffer.append(key + QLatin1Char('=') + mSettings[lookupKey].toString() + QLatin1String("\n"));
            mSettings.remove(lookupKey);
        }
    }

    // Add the new settings to the buffer
    QHashIterator<QString, QVariant> i(mSettings);
    while (i.hasNext()) {
        i.next();

        QStringList fullKey(i.key().split(QLatin1Char('/')));
        QString section(fullKey.at(0));
        section.prepend(QLatin1Char('['));
        section.append(QLatin1Char(']'));
        QString key(fullKey.at(1));

        int index = buffer.lastIndexOf(section);
        if (index != -1) {
            // Look for the next section
            index = buffer.indexOf(QLatin1Char('['), index + 1);

            if (index == -1 ) {
                // We are at the last section, append it to the bottom of the file
                buffer.append(QString("\n%1=%2").arg(key, i.value().toString()));
                mSettings.remove(i.key());
                continue;
            } else {
                // Not at last section, add the key at the index
                buffer.insert(index - 1, QString("\n%1=%2").arg(key, i.value().toString()));
                mSettings.remove(i.key());
            }

        } else {
            // Add the section to the end of the file, because it's not found
            buffer.append(QString("\n%1\n").arg(section));
            i.previous();
        }
    }

    // Now we reopen the file, this time we write
    QFile file(path);

    if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
        QTextStream in(&file);
        in.setCodec(stream.codec());

        // Write the updated buffer to an empty file
        in << buffer;
        file.flush();
        file.close();
    } else {
        return false;
    }

    return true;
}
开发者ID:AAlderman,项目名称:openmw,代码行数:93,代码来源:inisettings.cpp


注:本文中的QTextStream::codec方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。