Skip to content

Commit

Permalink
Fixed incorrect file path handling for global enumerations that defin…
Browse files Browse the repository at this point in the history
…e files with local path data. Added a warning if a non-global enumeration has a file attribute. Prettied up the main protocol file a bit. 2.14.b
  • Loading branch information
billvaglienti committed Jan 27, 2018
1 parent 7cce6eb commit 6fa22d7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 66 deletions.
36 changes: 27 additions & 9 deletions enumcreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ EnumCreator::~EnumCreator(void)
void EnumCreator::clear(void)
{
file.clear();
filepath.clear();
sourceOutput.clear();
minbitwidth = 0;
hidden = false;
Expand Down Expand Up @@ -148,18 +149,13 @@ void EnumCreator::clear(void)
* Parse the DOM to fill out the enumeration list for a global enum. This sill
* also set the header reference file name that others need to include to use
* this enum.
* \param filename is the reference file if none is specified in the xml
*/
void EnumCreator::parseGlobal(QString filename)
void EnumCreator::parseGlobal(void)
{
isglobal = true;

parse();

// Global enums must have a reference file
if(file.isEmpty())
file = filename.left(filename.indexOf(".")); // remove any extension

isglobal = false;
}

Expand Down Expand Up @@ -199,12 +195,34 @@ void EnumCreator::parse(void)
hidden = ProtocolParser::isFieldSet("hidden", map);
lookup = ProtocolParser::isFieldSet("lookup", map);
lookupTitle = ProtocolParser::isFieldSet("lookupTitle", map);
file = ProtocolParser::getAttribute("file", map);

// The file attribute is only supported on global enumerations
if(isglobal)
{
// Remove the extension if any
file = ProtocolParser::getAttribute("file", map);
file = file.left(file.indexOf("."));
QString extension;
filepath = support.outputpath;

// If no file information is provided we use the global header name
if(file.isEmpty())
file = support.protoName + "Protocol";

// This will separate all the path information
ProtocolFile::separateModuleNameAndPath(file, filepath);

// Make sure the extension is correct (.h, .hpp, .hxx, etc)
ProtocolFile::extractExtension(file, extension);
if(!extension.contains(".h"))
extension = ".h";

// Now put the (corrected) extension back
file += extension;

}
else if(!file.isEmpty())
{
file.clear();
emitWarning("Enumeration must be global to support file attribute");
}

QDomNodeList list = e.elementsByTagName("Value");
Expand Down
10 changes: 8 additions & 2 deletions enumcreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class EnumCreator : public ProtocolDocumentation
virtual void parse() Q_DECL_OVERRIDE;

//! Parse the DOM to fill out the enumeration list for a global enum
void parseGlobal(QString filename);
void parseGlobal(void);

//! Check names against the list of C keywords
virtual void checkAgainstKeywords(void) Q_DECL_OVERRIDE;
Expand Down Expand Up @@ -118,9 +118,12 @@ class EnumCreator : public ProtocolDocumentation
//! The hierarchical name of this object
virtual QString getHierarchicalName(void) const Q_DECL_OVERRIDE {return parent + ":" + name;}

//! Return the header file name (if any) of the file holding this enumeration
//! Get the name of the header file (if any) holding this enumeration
QString getHeaderFileName(void) const {return file;}

//! Get the path of the header file (if any) holding this enumeration
QString getHeaderFilePath(void) const {return filepath;}

protected:

//! Parse the enumeration values to build the number list
Expand All @@ -135,6 +138,9 @@ class EnumCreator : public ProtocolDocumentation
//! Output file for global enumerations
QString file;

//! Output file path for global enumerations
QString filepath;

//! Output file for source code file (may not be used)
QString sourceOutput;

Expand Down
11 changes: 1 addition & 10 deletions exampleprotocol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ that the ProtoGen application can do regarding automatic protocol packing/upacki
code generation.
ProtoGen can handle bitfields, enumerations, arrays, and can
efficiently encode data for little or big endian architectures.
\verbatim
Text between the verbatim escapes will appear without
reflow
-or-
interpretation
\verbatim">
efficiently encode data for little or big endian architectures.">

<Include name="string.h" comment="C string manipulation function header" global="true"/>
<Include name="indices.h" comment="indices.h is included to for array length enumerations"/>
Expand Down
97 changes: 64 additions & 33 deletions protocolfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ ProtocolFile::ProtocolFile() :
{
}


void ProtocolFile::setModuleNameAndPath(QString name, QString filepath)
{
setModuleNameAndPath(QString(), name, filepath);
Expand All @@ -52,6 +51,68 @@ void ProtocolFile::setModuleNameAndPath(QString prefix, QString name, QString fi
// Remove any contents we currently have
clear();

// Clean it all up
separateModuleNameAndPath(name, filepath);

// Extract the extension
extractExtension(name);

// Remember the module name
module = prefix + name;

// And the path
path = filepath;

// This will see if the file already exists and will setup the initial output
prepareToAppend();
}


/*!
* Get the extension information for this name, and remove it from the name.
* \param name has its extension (if any) recorded and removed
*/
void ProtocolFile::extractExtension(QString& name)
{
extractExtension(name, extension);
}


/*!
* Get the extension information for this name, and remove it from the name.
* \param name has its extension (if any) removed
* \param extension receives the extension
*/
void ProtocolFile::extractExtension(QString& name, QString& extension)
{
// Note that "." as the first character is not an extension, it indicates a hidden file
int index = name.lastIndexOf(".");
if(index >= 1)
{
// The extension, including the "."
extension = name.right(name.size() - index);

// The name without the extension
name = name.left(index);
}
else
extension.clear();

}// ProtocolFile::extractExtension


/*!
* Given a module name and path adjust the name and path so that all the path
* information is in the path, the base name is in the name
* \param name contains the name with may include some path information. The
* path and extension will be removed
* \param filepath contains the path information, which will be augmented
* with any path information from the name, unless the name contains
* absolute path information, in which case the filepath will be
* replaced with the name path
*/
void ProtocolFile::separateModuleNameAndPath(QString& name, QString& filepath)
{
// Handle the case where the file includes "./" to reference the current working
// directory. We just remove this as its not needed.
if(name.startsWith("./"))
Expand Down Expand Up @@ -87,40 +148,10 @@ void ProtocolFile::setModuleNameAndPath(QString prefix, QString name, QString fi

}// If name contains a path separator

// Remove and log the extension
extractExtension(name);

// Remember the module name
module = prefix + name;

// Make sure the path uses native separators and ends with a separator (unless its empty)
path = sanitizePath(filepath);
filepath = sanitizePath(filepath);

// This will see if the file already exists and will setup the initial output
prepareToAppend();
}


/*!
* Get the extension information for this name, and remove it from the name.
* \param name has its extension (if any) logged and removed
*/
void ProtocolFile::extractExtension(QString& name)
{
// Note that "." as the first character is not an extension, it indicates a hidden file
int index = name.lastIndexOf(".");
if(index >= 1)
{
// The extension, including the "."
extension = name.right(name.size() - index);

// The name without the extension
name = name.left(index);
}
else
extension.clear();

}// ProtocolFile::extractExtension
}// ProtocolFile::separateModuleNameAndPath


/*!
Expand Down
16 changes: 11 additions & 5 deletions protocolfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class ProtocolFile
//! Make sure one blank line at end
void makeLineSeparator(void);

//! Adjust the name and path so that all the path information is in the path
static void separateModuleNameAndPath(QString& name, QString& filepath);

//! Extract the extension from a name
static void extractExtension(QString& name, QString& extension);

//! Make a nice, native, relative path
static QString sanitizePath(const QString& path);

Expand Down Expand Up @@ -88,7 +94,7 @@ class ProtocolFile
protected:

//! Prepare to do an append operation
virtual void prepareToAppend(void){}
virtual void prepareToAppend(void) {}

//! Get the extension information for this name
virtual void extractExtension(QString& name);
Expand Down Expand Up @@ -120,10 +126,10 @@ class ProtocolHeaderFile : public ProtocolFile
protected:

//! Prepare to do an append operation
virtual void prepareToAppend(void);
virtual void prepareToAppend(void) Q_DECL_OVERRIDE;

//! Get the extension information for this name
virtual void extractExtension(QString& name);
virtual void extractExtension(QString& name) Q_DECL_OVERRIDE;

//! \return the text that is appended to close a header file
QString getClosingStatement(void);
Expand All @@ -140,10 +146,10 @@ class ProtocolSourceFile : public ProtocolFile
protected:

//! Prepare to do an append operation
virtual void prepareToAppend(void);
virtual void prepareToAppend(void) Q_DECL_OVERRIDE;

//! Get the extension information for this name
virtual void extractExtension(QString& name);
virtual void extractExtension(QString& name) Q_DECL_OVERRIDE;

//! \return the text that is appended to close a source file
QString getClosingStatement(void);
Expand Down
22 changes: 15 additions & 7 deletions protocolparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <iostream>

// The version of the protocol generator is set here
const QString ProtocolParser::genVersion = "2.14.a";
const QString ProtocolParser::genVersion = "2.14.b";

/*!
* \brief ProtocolParser::ProtocolParser
Expand Down Expand Up @@ -205,9 +205,9 @@ bool ProtocolParser::parse(QString filename, QString path, QStringList otherfile
{
EnumCreator* module = globalEnums.at(i);

module->parseGlobal(name + "Protocol");
module->parseGlobal();
enumfile.setLicenseText(support.licenseText);
enumfile.setModuleNameAndPath(module->getHeaderFileName(), support.outputpath);
enumfile.setModuleNameAndPath(module->getHeaderFileName(), module->getHeaderFilePath());
enumfile.write(module->getOutput());
enumfile.makeLineSeparator();
enumfile.flush();
Expand Down Expand Up @@ -635,31 +635,39 @@ void ProtocolParser::createProtocolHeader(const QDomElement& docElem)
header.write("\n");
}

if(version.isEmpty() && api.isEmpty())
header.write("\n");

header.write(" */\n");
header.write("\n");

header.makeLineSeparator();

// Includes
header.write("#include <stdint.h>\n");
header.writeIncludeDirective("stdint.h", QString(), true);

// Add other includes
outputIncludes(name, header, docElem);

header.makeLineSeparator();

// API functions
if(!api.isEmpty())
{
header.write("\n");
header.makeLineSeparator();
header.write("//! \\return the protocol API enumeration\n");
header.write("#define get" + name + "Api() " + api + "\n");
}

// Version functions
if(!version.isEmpty())
{
header.write("\n");
header.makeLineSeparator();
header.write("//! \\return the protocol version string\n");
header.write("#define get" + name + "Version() \"" + version + "\"\n");
}

header.makeLineSeparator();

header.flush();

}// ProtocolParser::createProtocolFiles
Expand Down

0 comments on commit 6fa22d7

Please sign in to comment.