Commit cb2cce51 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Adapt generated header guards to the filename

Use _HPP for example for .hpp files, for example. Based on simply
putting the chosen extension in uppercase.
parent 1f3908e0
......@@ -36,10 +36,26 @@
namespace Core {
namespace Utils {
static QString toAlphaNum(const QString &s)
{
QString rc;
const int len = s.size();
const QChar underscore = QLatin1Char('_');
for (int i = 0; i < len; i++) {
const QChar c = s.at(i);
if (c == underscore || c.isLetterOrNumber())
rc += c;
}
return rc;
}
QWORKBENCH_UTILS_EXPORT QString headerGuard(const QString &file)
{
QString rc = QFileInfo(file).baseName().toUpper();
rc += QLatin1String("_H");
const QFileInfo fi(file);
QString rc = toAlphaNum(fi.completeBaseName()).toUpper();
rc += QLatin1Char('_');
rc += toAlphaNum(fi.suffix()).toUpper();
return rc;
}
......
......@@ -183,7 +183,7 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par
return false;
const QString unqualifiedClassName = namespaceList.takeLast();
const QString guard = Core::Utils::headerGuard(unqualifiedClassName);
const QString guard = Core::Utils::headerGuard(params.headerFile);
// == Header file ==
QTextStream headerStr(header);
......
......@@ -31,6 +31,8 @@
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include <utils/codegeneration.h>
#include <QtCore/QTextStream>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
......@@ -48,20 +50,6 @@ CppFileWizard::CppFileWizard(const BaseFileWizardParameters &parameters,
{
}
QString CppFileWizard::toAlphaNum(const QString &s)
{
QString rc;
const int len = s.size();
const QChar underscore = QLatin1Char('_');
for (int i = 0; i < len; i++) {
const QChar c = s.at(i);
if (c == underscore || c.isLetterOrNumber())
rc += c;
}
return rc;
}
Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
const QString &name,
QString * /*errorMessage*/) const
......@@ -69,11 +57,11 @@ Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
{
const QString mimeType = m_type == Source ? QLatin1String(Constants::CPP_SOURCE_MIMETYPE) : QLatin1String(Constants::CPP_HEADER_MIMETYPE);
const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(mimeType));
Core::GeneratedFile file(fileName);
file.setEditorKind(QLatin1String(Constants::C_CPPEDITOR));
const QString cleanName = toAlphaNum(QFileInfo(name).baseName());
file.setContents(fileContents(m_type, fileName));
return Core::GeneratedFiles() << file;
}
......@@ -84,8 +72,7 @@ QString CppFileWizard::fileContents(FileType type, const QString &fileName) cons
QTextStream str(&contents);
switch (type) {
case Header: {
QString guard = toAlphaNum(baseName).toUpper();
guard += QLatin1String("_H");
const QString guard = Core::Utils::headerGuard(fileName);
str << QLatin1String("#ifndef ") << guard
<< QLatin1String("\n#define ") << guard << QLatin1String("\n\n#endif // ")
<< guard << QLatin1String("\n");
......
......@@ -49,7 +49,6 @@ public:
QObject *parent = 0);
protected:
static QString toAlphaNum(const QString &s);
QString fileContents(FileType type, const QString &baseName) const;
protected:
......
......@@ -73,7 +73,7 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in
const QString unqualifiedClassName = namespaceList.takeLast();
// Include guards
const QString guard = Core::Utils::headerGuard(unqualifiedClassName);
const QString guard = Core::Utils::headerGuard(headerFile);
QString uiInclude = QLatin1String("ui_");
uiInclude += QFileInfo(uiFile).baseName();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment