New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AURORA: GFF3Writer #300
AURORA: GFF3Writer #300
Conversation
c7adebe
to
29f5a1e
Compare
29f5a1e
to
3fb0982
Compare
The memory leaks should now be fixed. |
src/aurora/gff3writer.cpp
Outdated
} else { | ||
// If the values are complex (greater then 4 bytes) write the index to the field data. | ||
stream.writeUint32LE(fieldDataIndex); | ||
switch (field.type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that a repeat of lines 61ff?
Move that into a method getFieldSize()
or something like that.
src/aurora/gff3writer.cpp
Outdated
|
||
// Write labels. | ||
for (size_t i = 0; i < _labels.size(); ++i) { | ||
Common::UString label = _labels[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that copy there necessary?
src/aurora/gff3writer.cpp
Outdated
namespace Aurora { | ||
|
||
GFF3Writer::GFF3Writer(uint32 id, uint32 version) : _id(id), _version(version) { | ||
_structs.push_back(GFF3WriterStructPtr(new GFF3WriterStruct(this))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use boost::make_shared instead of new
'ing the struct into the shared_ptr (here and everywhere else as well)?
I.e. boost::make_shared<GFF3WriterStruct>(this)
should be equivalent to boost::shared_ptr<GFF3WriterStruct>(new GFF3WriterStruct(this))
, and gets rid of the explicit new
.
src/aurora/gff3writer.cpp
Outdated
* Determine if this field has simple values (less equal 32 bit) which are written in the field | ||
* or complex values, bigger than 32bit like strings written in the field data section. | ||
*/ | ||
bool simple = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a const.
src/aurora/gff3writer.cpp
Outdated
break; | ||
case GFF3Struct::kFieldTypeResRef: | ||
stream.writeByte(MIN(static_cast<byte>(16), static_cast<byte>(field.stringValue.size()))); | ||
stream.write(field.stringValue.c_str(), field.stringValue.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still might write a longer string than 16 bytes here
src/aurora/gff3writer.cpp
Outdated
field.type = GFF3Struct::kFieldTypeByte; | ||
field.labelIndex = _parent->addLabel(label); | ||
field.uint32Value = value; | ||
_parent->_fields.push_back(field); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of code duplication here in these methods. Creating the field index and adding the field, i.e. everything except setting the actual value, can be abstracted into a method.
3fb0982
to
9d15b0f
Compare
The issues should now be fixed |
src/aurora/locstring.h
Outdated
/** Write the LocString to a write stream. */ | ||
void writeLocString(Common::WriteStream &stream, bool withNullTerminate = false); | ||
void writeLocString(Common::WriteStream &stream, bool withNullTerminate = false) const ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's now an extra space between the const
and the ;
src/aurora/gff3writer.cpp
Outdated
|
||
// Insert the struct to the field vector. | ||
_parent->_fields.push_back(field); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/aurora/gff3writer.cpp
Outdated
void GFF3WriterStruct::addByte(const Common::UString &label, byte value) { | ||
size_t index = _parent->createField(GFF3Struct::kFieldTypeByte, label); | ||
_parent->_fields[index].uint32Value = value; | ||
_fieldIndices.push_back(index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd go even further. GFF3Writer::Field &GFF3WriterStruct::createField(GFF3Struct::FieldType type, const Common::UString &label)
. Calls _parent->createField()
, put the index into _fieldIndices
and returns a direct reference to the field.
Then you only need to do
createField(GFF3Struct::kFieldTypeByte, label).uint32Value = value;
etc. in each method.
src/aurora/gff3writer.h
Outdated
|
||
class GFF3Writer { | ||
public: | ||
//TODO: Add a constructor consuming a GFF3File object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO:
src/aurora/gff3writer.h
Outdated
/** Get the toplevel struct. */ | ||
GFF3WriterStructPtr getTopLevelStruct(); | ||
|
||
/** Write the gff3 to stream. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, capitalize GFF and GFF3.
src/aurora/gff3writer.h
Outdated
stringValue = field.stringValue; | ||
locStringValue = field.locStringValue; | ||
|
||
if (field.voidData.get() != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should still do an unconditional field.voidData.reset()
before that. Otherwise, "a = b", with a.voidData filled and b.voidData empty will not empty a.voidData.
src/aurora/gff3writer.cpp
Outdated
} | ||
|
||
// Write fields. | ||
unsigned int fieldDataIndex = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the move to unsigned int instead of size_t here?
src/aurora/gff3writer.cpp
Outdated
} | ||
} | ||
|
||
uint32 GFF3Writer::getFieldDataSize(const Aurora::GFF3Writer::Field &field) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can even be a static function in this file's scope. It doesn't need to be a method of GFF3Writer.
9d15b0f
to
061090d
Compare
061090d
to
c947e90
Compare
The issues should now be fixed |
Merged as 4971a26...da35c58, thanks! :) |
I thought long about my first attempt on a gff3 writer, but ultimately decided that it would be better to write an external class for exporting gff3, since the internal structure of the GFF3File class makes it very difficult to modify values. Also it is difficult to get the necessary information for exporting in an easy way. This class is only for generating a gff3 tree and export it. I have made many unit tests and also tested it against the original kotor executable by replacing a simple file by one created by the writer, and it behaved as i expected.