2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-06-17 00:01:27 +10:00
|
|
|
** contact the sales department at http://www.qtsoftware.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "proitems.h"
|
|
|
|
|
#include "prowriter.h"
|
|
|
|
|
|
2008-12-02 12:57:59 +01:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
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();
|
2008-12-09 11:07:24 +01:00
|
|
|
switch (v->variableOperator()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
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();
|
2008-12-09 11:07:24 +01:00
|
|
|
for (int i = 0; i < items.count(); ++i) {
|
2008-12-02 12:01:29 +01:00
|
|
|
m_writeState &= ~LastItem;
|
|
|
|
|
m_writeState &= ~FirstItem;
|
|
|
|
|
if (i == 0)
|
|
|
|
|
m_writeState |= FirstItem;
|
2008-12-09 11:07:24 +01:00
|
|
|
if (i == items.count() - 1)
|
2008-12-02 12:01:29 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|