summaryrefslogtreecommitdiff
path: root/shared/proparser/prowriter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'shared/proparser/prowriter.cpp')
-rw-r--r--shared/proparser/prowriter.cpp207
1 files changed, 207 insertions, 0 deletions
diff --git a/shared/proparser/prowriter.cpp b/shared/proparser/prowriter.cpp
new file mode 100644
index 0000000000..aa00600e1e
--- /dev/null
+++ b/shared/proparser/prowriter.cpp
@@ -0,0 +1,207 @@
+/***************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+**
+** Non-Open Source Usage
+**
+** Licensees may use this file in accordance with the Qt Beta Version
+** License Agreement, Agreement version 2.2 provided with the Software or,
+** alternatively, in accordance with the terms contained in a written
+** agreement between you and Nokia.
+**
+** GNU General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License versions 2.0 or 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the packaging
+** of this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+**
+** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt GPL Exception version
+** 1.2, included in the file GPL_EXCEPTION.txt in this package.
+**
+***************************************************************************/
+#include <QtCore/QFile>
+
+#include "proitems.h"
+#include "prowriter.h"
+
+using namespace Qt4ProjectManager::Internal;
+
+bool ProWriter::write(ProFile *profile, const QString &fileName)
+{
+ QFile data(fileName);
+ if (!data.open(QFile::WriteOnly|QFile::Text))
+ return false;
+
+ m_writeState = 0;
+ m_comment.clear();
+ m_out.setDevice(&data);
+ writeItem(profile, QString());
+ data.close();
+
+ return true;
+}
+
+QString ProWriter::contents(ProFile *profile)
+{
+ QString result;
+
+ m_writeState = 0;
+ m_comment.clear();
+ m_out.setString(&result, QIODevice::WriteOnly);
+ writeItem(profile, QString());
+
+ return result;
+}
+
+QString ProWriter::fixComment(const QString &comment, const QString &indent) const
+{
+ QString result = comment;
+ result = result.replace(QLatin1Char('\n'),
+ QLatin1Char('\n') + indent + QLatin1String("# "));
+ return QLatin1String("# ") + result;
+}
+
+void ProWriter::writeValue(ProValue *value, const QString &indent)
+{
+ if (m_writeState & NewLine) {
+ m_out << indent << QLatin1String(" ");
+ m_writeState &= ~NewLine;
+ }
+
+ m_out << value->value();
+
+ if (!(m_writeState & LastItem))
+ m_out << QLatin1String(" \\");
+
+ if (!value->comment().isEmpty())
+ m_out << QLatin1Char(' ') << fixComment(value->comment(), indent);
+
+ m_out << endl;
+ m_writeState |= NewLine;
+}
+
+void ProWriter::writeOther(ProItem *item, const QString &indent)
+{
+ if (m_writeState & NewLine) {
+ m_out << indent;
+ m_writeState &= ~NewLine;
+ }
+
+ if (item->kind() == ProItem::FunctionKind) {
+ ProFunction *v = static_cast<ProFunction*>(item);
+ m_out << v->text();
+ } else if (item->kind() == ProItem::ConditionKind) {
+ ProCondition *v = static_cast<ProCondition*>(item);
+ m_out << v->text();
+ } else if (item->kind() == ProItem::OperatorKind) {
+ ProOperator *v = static_cast<ProOperator*>(item);
+ if (v->operatorKind() == ProOperator::OrOperator)
+ m_out << QLatin1Char('|');
+ else
+ m_out << QLatin1Char('!');
+ }
+
+ if (!item->comment().isEmpty()) {
+ if (!m_comment.isEmpty())
+ m_comment += QLatin1Char('\n');
+ m_comment += item->comment();
+ }
+}
+
+void ProWriter::writeBlock(ProBlock *block, const QString &indent)
+{
+ if (m_writeState & NewLine) {
+ m_out << indent;
+ m_writeState &= ~NewLine;
+ }
+
+ if (!block->comment().isEmpty()) {
+ if (!(m_writeState & FirstItem))
+ m_out << endl << indent;
+ m_out << fixComment(block->comment(), indent) << endl << indent;
+ }
+
+ QString newindent = indent;
+ if (block->blockKind() & ProBlock::VariableKind) {
+ ProVariable *v = static_cast<ProVariable*>(block);
+ m_out << v->variable();
+ switch(v->variableOperator()) {
+ case ProVariable::AddOperator:
+ m_out << QLatin1String(" += "); break;
+ case ProVariable::RemoveOperator:
+ m_out << QLatin1String(" -= "); break;
+ case ProVariable::ReplaceOperator:
+ m_out << QLatin1String(" ~= "); break;
+ case ProVariable::SetOperator:
+ m_out << QLatin1String(" = "); break;
+ case ProVariable::UniqueAddOperator:
+ m_out << QLatin1String(" *= "); break;
+ }
+ } else if (block->blockKind() & ProBlock::ScopeContentsKind) {
+ if (block->items().count() > 1) {
+ newindent = indent + QLatin1String(" ");
+ m_out << QLatin1String(" { ");
+ if (!m_comment.isEmpty()) {
+ m_out << fixComment(m_comment, indent);
+ m_comment.clear();
+ }
+ m_out << endl;
+ m_writeState |= NewLine;
+ } else {
+ m_out << QLatin1Char(':');
+ }
+ }
+
+ QList<ProItem*> items = block->items();
+ for (int i=0; i<items.count(); ++i) {
+ m_writeState &= ~LastItem;
+ m_writeState &= ~FirstItem;
+ if (i == 0)
+ m_writeState |= FirstItem;
+ if (i == (items.count()-1))
+ m_writeState |= LastItem;
+ writeItem(items.at(i), newindent);
+ }
+
+ if ((block->blockKind() & ProBlock::ScopeContentsKind) && (block->items().count() > 1)) {
+ if (m_writeState & NewLine) {
+ m_out << indent;
+ m_writeState &= ~NewLine;
+ }
+ m_out << QLatin1Char('}');
+ }
+
+ if (!m_comment.isEmpty()) {
+ m_out << fixComment(m_comment, indent);
+ m_out << endl;
+ m_writeState |= NewLine;
+ m_comment.clear();
+ }
+
+ if (!(m_writeState & NewLine)) {
+ m_out << endl;
+ m_writeState |= NewLine;
+ }
+}
+
+void ProWriter::writeItem(ProItem *item, const QString &indent)
+{
+ if (item->kind() == ProItem::ValueKind) {
+ writeValue(static_cast<ProValue*>(item), indent);
+ } else if (item->kind() == ProItem::BlockKind) {
+ writeBlock(static_cast<ProBlock*>(item), indent);
+ } else {
+ writeOther(item, indent);
+ }
+}