2009-06-22 10:57:17 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-06-22 10:57:17 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-22 10:57:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QXmlStreamReader>
|
2010-07-07 23:59:02 +02:00
|
|
|
#include <QXmlStreamWriter>
|
2009-06-22 10:57:17 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include "projectfile.h"
|
|
|
|
|
|
|
|
static const char ProjectElementName[] = "project";
|
2010-07-07 23:59:02 +02:00
|
|
|
static const char ProjectVersionAttrib[] = "version";
|
|
|
|
static const char ProjectFileVersion[] = "1";
|
2009-07-04 00:38:47 +02:00
|
|
|
static const char IncludDirElementName[] = "includedir";
|
|
|
|
static const char DirElementName[] = "dir";
|
|
|
|
static const char DirNameAttrib[] = "name";
|
2010-05-20 07:22:19 +02:00
|
|
|
static const char DefinesElementName[] = "defines";
|
|
|
|
static const char DefineName[] = "define";
|
|
|
|
static const char DefineNameAttrib[] = "name";
|
2010-08-14 17:42:37 +02:00
|
|
|
static const char PathsElementName[] = "paths";
|
|
|
|
static const char PathName[] = "dir";
|
|
|
|
static const char PathNameAttrib[] = "name";
|
2010-08-20 22:58:00 +02:00
|
|
|
static const char RootPathName[] = "root";
|
|
|
|
static const char RootPathNameAttrib[] = "name";
|
2009-06-22 10:57:17 +02:00
|
|
|
|
|
|
|
ProjectFile::ProjectFile(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent)
|
2009-06-22 10:57:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectFile::ProjectFile(const QString &filename, QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
|
|
|
mFilename(filename)
|
2009-06-22 10:57:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProjectFile::Read(const QString &filename)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!filename.isEmpty())
|
2009-06-22 10:57:17 +02:00
|
|
|
mFilename = filename;
|
|
|
|
|
|
|
|
QFile file(mFilename);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
2009-06-22 10:57:17 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QXmlStreamReader xmlReader(&file);
|
|
|
|
bool insideProject = false;
|
2010-07-08 18:49:04 +02:00
|
|
|
bool projectTagFound = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!xmlReader.atEnd())
|
2009-06-22 10:57:17 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
switch (xmlReader.readNext())
|
2009-06-22 10:57:17 +02:00
|
|
|
{
|
|
|
|
case QXmlStreamReader::StartElement:
|
2010-04-02 07:30:58 +02:00
|
|
|
if (xmlReader.name() == ProjectElementName)
|
2010-07-08 18:49:04 +02:00
|
|
|
{
|
2009-06-22 10:57:17 +02:00
|
|
|
insideProject = true;
|
2010-07-08 18:49:04 +02:00
|
|
|
projectTagFound = true;
|
|
|
|
}
|
2010-08-20 22:58:00 +02:00
|
|
|
// Read root path from inside project element
|
|
|
|
if (insideProject && xmlReader.name() == RootPathName)
|
|
|
|
ReadRootPath(xmlReader);
|
|
|
|
|
2010-08-14 17:42:37 +02:00
|
|
|
// Find paths to check from inside project element
|
|
|
|
if (insideProject && xmlReader.name() == PathsElementName)
|
|
|
|
ReadCheckPaths(xmlReader);
|
2009-06-23 08:15:00 +02:00
|
|
|
|
2010-07-03 23:15:19 +02:00
|
|
|
// Find include directory from inside project element
|
2010-04-02 07:30:58 +02:00
|
|
|
if (insideProject && xmlReader.name() == IncludDirElementName)
|
2009-07-04 00:38:47 +02:00
|
|
|
ReadIncludeDirs(xmlReader);
|
2010-05-20 07:22:19 +02:00
|
|
|
|
2010-07-03 23:15:19 +02:00
|
|
|
// Find preprocessor define from inside project element
|
2010-05-20 07:22:19 +02:00
|
|
|
if (insideProject && xmlReader.name() == DefinesElementName)
|
|
|
|
ReadDefines(xmlReader);
|
|
|
|
|
2009-06-22 10:57:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
2010-04-02 07:30:58 +02:00
|
|
|
if (xmlReader.name() == ProjectElementName)
|
2009-06-22 10:57:17 +02:00
|
|
|
insideProject = false;
|
|
|
|
break;
|
2009-06-23 08:15:00 +02:00
|
|
|
|
|
|
|
// Not handled
|
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
2009-06-22 10:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2010-07-08 18:49:04 +02:00
|
|
|
if (projectTagFound)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2009-06-22 10:57:17 +02:00
|
|
|
}
|
|
|
|
|
2009-07-04 00:38:47 +02:00
|
|
|
QStringList ProjectFile::GetIncludeDirs() const
|
|
|
|
{
|
|
|
|
return mIncludeDirs;
|
|
|
|
}
|
|
|
|
|
2010-05-20 07:22:19 +02:00
|
|
|
QStringList ProjectFile::GetDefines() const
|
|
|
|
{
|
|
|
|
return mDefines;
|
|
|
|
}
|
|
|
|
|
2010-08-14 17:42:37 +02:00
|
|
|
QStringList ProjectFile::GetCheckPaths() const
|
|
|
|
{
|
|
|
|
return mPaths;
|
|
|
|
}
|
|
|
|
|
2010-08-20 22:58:00 +02:00
|
|
|
void ProjectFile::ReadRootPath(QXmlStreamReader &reader)
|
|
|
|
{
|
|
|
|
QXmlStreamAttributes attribs = reader.attributes();
|
|
|
|
QString name = attribs.value("", RootPathNameAttrib).toString();
|
|
|
|
if (!name.isEmpty())
|
|
|
|
mRootPath = name;
|
|
|
|
}
|
|
|
|
|
2009-07-04 00:38:47 +02:00
|
|
|
void ProjectFile::ReadIncludeDirs(QXmlStreamReader &reader)
|
|
|
|
{
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
bool allRead = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
type = reader.readNext();
|
2010-04-02 07:30:58 +02:00
|
|
|
switch (type)
|
2009-07-04 00:38:47 +02:00
|
|
|
{
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
|
|
|
// Read dir-elements
|
2010-04-02 07:30:58 +02:00
|
|
|
if (reader.name().toString() == DirElementName)
|
2009-07-04 00:38:47 +02:00
|
|
|
{
|
|
|
|
QXmlStreamAttributes attribs = reader.attributes();
|
|
|
|
QString name = attribs.value("", DirNameAttrib).toString();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!name.isEmpty())
|
2009-07-04 00:38:47 +02:00
|
|
|
mIncludeDirs << name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
2010-04-02 07:30:58 +02:00
|
|
|
if (reader.name().toString() == IncludDirElementName)
|
2009-07-04 00:38:47 +02:00
|
|
|
allRead = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Not handled
|
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!allRead);
|
2009-07-04 00:38:47 +02:00
|
|
|
}
|
2010-05-20 07:22:19 +02:00
|
|
|
|
|
|
|
void ProjectFile::ReadDefines(QXmlStreamReader &reader)
|
|
|
|
{
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
bool allRead = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
type = reader.readNext();
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
// Read define-elements
|
|
|
|
if (reader.name().toString() == DefineName)
|
|
|
|
{
|
|
|
|
QXmlStreamAttributes attribs = reader.attributes();
|
|
|
|
QString name = attribs.value("", DefineNameAttrib).toString();
|
|
|
|
if (!name.isEmpty())
|
|
|
|
mDefines << name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
if (reader.name().toString() == DefinesElementName)
|
|
|
|
allRead = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Not handled
|
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!allRead);
|
|
|
|
}
|
2010-07-07 23:59:02 +02:00
|
|
|
|
2010-08-14 17:42:37 +02:00
|
|
|
void ProjectFile::ReadCheckPaths(QXmlStreamReader &reader)
|
|
|
|
{
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
bool allRead = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
type = reader.readNext();
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
|
|
|
|
// Read dir-elements
|
|
|
|
if (reader.name().toString() == PathName)
|
|
|
|
{
|
|
|
|
QXmlStreamAttributes attribs = reader.attributes();
|
|
|
|
QString name = attribs.value("", PathNameAttrib).toString();
|
|
|
|
if (!name.isEmpty())
|
|
|
|
mPaths << name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
if (reader.name().toString() == PathsElementName)
|
|
|
|
allRead = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Not handled
|
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!allRead);
|
|
|
|
}
|
|
|
|
|
2010-07-07 23:59:02 +02:00
|
|
|
void ProjectFile::SetIncludes(QStringList includes)
|
|
|
|
{
|
|
|
|
mIncludeDirs = includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectFile::SetDefines(QStringList defines)
|
|
|
|
{
|
|
|
|
mDefines = defines;
|
|
|
|
}
|
|
|
|
|
2010-08-14 17:42:37 +02:00
|
|
|
void ProjectFile::SetCheckPaths(QStringList paths)
|
|
|
|
{
|
|
|
|
mPaths = paths;
|
|
|
|
}
|
|
|
|
|
2010-07-07 23:59:02 +02:00
|
|
|
bool ProjectFile::Write(const QString &filename)
|
|
|
|
{
|
2010-07-08 15:16:25 +02:00
|
|
|
if (!filename.isEmpty())
|
|
|
|
mFilename = filename;
|
|
|
|
|
2010-07-07 23:59:02 +02:00
|
|
|
QFile file(mFilename);
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QXmlStreamWriter xmlWriter(&file);
|
|
|
|
xmlWriter.setAutoFormatting(true);
|
|
|
|
xmlWriter.writeStartDocument("1.0");
|
|
|
|
xmlWriter.writeStartElement(ProjectElementName);
|
|
|
|
xmlWriter.writeAttribute(ProjectVersionAttrib, ProjectFileVersion);
|
|
|
|
|
2010-08-20 22:58:00 +02:00
|
|
|
if (!mRootPath.isEmpty())
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(RootPathName);
|
|
|
|
xmlWriter.writeAttribute(RootPathNameAttrib, mRootPath);
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-07-07 23:59:02 +02:00
|
|
|
if (!mIncludeDirs.isEmpty())
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(IncludDirElementName);
|
|
|
|
QString incdir;
|
|
|
|
foreach(incdir, mIncludeDirs)
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(DirElementName);
|
|
|
|
xmlWriter.writeAttribute(DirNameAttrib, incdir);
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDefines.isEmpty())
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(DefinesElementName);
|
|
|
|
QString define;
|
|
|
|
foreach(define, mDefines)
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(DefineName);
|
|
|
|
xmlWriter.writeAttribute(DefineNameAttrib, define);
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
2010-08-14 17:42:37 +02:00
|
|
|
|
|
|
|
if (!mPaths.isEmpty())
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(PathsElementName);
|
|
|
|
QString path;
|
|
|
|
foreach(path, mPaths)
|
|
|
|
{
|
|
|
|
xmlWriter.writeStartElement(PathName);
|
|
|
|
xmlWriter.writeAttribute(PathNameAttrib, path);
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
xmlWriter.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-07-07 23:59:02 +02:00
|
|
|
xmlWriter.writeEndDocument();
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|