diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6098e1f --- /dev/null +++ b/.clang-format @@ -0,0 +1,41 @@ +--- +# We'll use defaults from the LLVM style, but with 4 columns indentation. +BasedOnStyle: LLVM +IndentWidth: 2 +--- +Language: Cpp +DeriveLineEnding: false +UseCRLF: true +DerivePointerAlignment: false +PointerAlignment: Left +AlignConsecutiveAssignments: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AlwaysBreakTemplateDeclarations: Yes +AccessModifierOffset: -2 +AlignTrailingComments: true +SpacesBeforeTrailingComments: 2 +NamespaceIndentation: Inner +MaxEmptyLinesToKeep: 1 +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: false + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: true +ColumnLimit: 88 +ForEachMacros: ['Q_FOREACH', 'foreach'] diff --git a/src/espexceptions.h b/src/espexceptions.h index c197b49..368ba11 100644 --- a/src/espexceptions.h +++ b/src/espexceptions.h @@ -3,21 +3,21 @@ #include +namespace ESP +{ -namespace ESP { - - -class InvalidRecordException : public std::runtime_error { +class InvalidRecordException : public std::runtime_error +{ public: - InvalidRecordException(const std::string &message) : std::runtime_error(message) {} + InvalidRecordException(const std::string& message) : std::runtime_error(message) {} }; - -class InvalidFileException : public std::runtime_error { +class InvalidFileException : public std::runtime_error +{ public: - InvalidFileException(const std::string &message) : std::runtime_error(message) {} + InvalidFileException(const std::string& message) : std::runtime_error(message) {} }; -} +} // namespace ESP -#endif // ESPEXCEPTIONS_H +#endif // ESPEXCEPTIONS_H diff --git a/src/espfile.cpp b/src/espfile.cpp index e32bba0..ca759ae 100644 --- a/src/espfile.cpp +++ b/src/espfile.cpp @@ -1,35 +1,33 @@ #include "espfile.h" +#include "espexceptions.h" #include "subrecord.h" #include "tes3subrecord.h" -#include "espexceptions.h" -#include #include +#include - -ESP::File::File(const std::string &fileName) +ESP::File::File(const std::string& fileName) { m_File.open(fileName, std::fstream::in | std::fstream::binary); init(); } -ESP::File::File(const std::wstring &fileName) +ESP::File::File(const std::wstring& fileName) { m_File.open(fileName, std::fstream::in | std::fstream::binary); init(); } - class membuf : public std::basic_streambuf { public: - membuf(const char *start, size_t size) { + membuf(const char* start, size_t size) + { // baad me! this is intended for an istream only so we're not modifying - char *startMod = const_cast(start); + char* startMod = const_cast(start); setg(startMod, startMod, startMod + size); } }; - void ESP::File::init() { if (!m_File.is_open()) { @@ -42,97 +40,102 @@ void ESP::File::init() throw ESP::InvalidFileException("file incomplete"); } if (memcmp(type, "TES3", 4) == 0) { - ESP::TES3Record rec; - rec.readFrom(m_File); + ESP::TES3Record rec; + rec.readFrom(m_File); - while (!m_File.eof() && !m_File.fail()) { - ESP::TES3SubRecord subRec; - bool success = subRec.readFrom(m_File); - int headerSize = sizeof(m_TES3Header); - if (success) { - if (subRec.type() != TES3SubRecord::TYPE_UNKNOWN) { - switch (subRec.type()) { - case TES3SubRecord::TYPE_HEDR: - if (subRec.data().size() != sizeof(m_TES3Header)) { - printf("invalid header size\n"); - m_Header.version = 0.0f; - m_Header.numRecords = 1; // prevent this esp appear like a dummy - } - else { - memcpy(&m_TES3Header, &subRec.data()[0], sizeof(m_TES3Header)); - } - m_Header.version = m_TES3Header.version; - //m_Header.numRecords = m_TES3Header.numRecords; - m_Author = reinterpret_cast(m_TES3Header.author); - m_Description = reinterpret_cast(m_TES3Header.description); - break; - case TES3SubRecord::TYPE_MAST: - if (subRec.data().size() > 0) - m_Masters.insert(reinterpret_cast(&subRec.data()[0])); - break; - } - } - } - } + while (!m_File.eof() && !m_File.fail()) { + ESP::TES3SubRecord subRec; + bool success = subRec.readFrom(m_File); + int headerSize = sizeof(m_TES3Header); + if (success) { + if (subRec.type() != TES3SubRecord::TYPE_UNKNOWN) { + switch (subRec.type()) { + case TES3SubRecord::TYPE_HEDR: + if (subRec.data().size() != sizeof(m_TES3Header)) { + printf("invalid header size\n"); + m_Header.version = 0.0f; + m_Header.numRecords = 1; // prevent this esp appear like a dummy + } else { + memcpy(&m_TES3Header, &subRec.data()[0], sizeof(m_TES3Header)); + } + m_Header.version = m_TES3Header.version; + // m_Header.numRecords = m_TES3Header.numRecords; + m_Author = reinterpret_cast(m_TES3Header.author); + m_Description = reinterpret_cast(m_TES3Header.description); + break; + case TES3SubRecord::TYPE_MAST: + if (subRec.data().size() > 0) + m_Masters.insert(reinterpret_cast(&subRec.data()[0])); + break; + } + } + } + } } else if (memcmp(type, "TES4", 4) == 0) { - m_File.seekg(0); + m_File.seekg(0); - m_MainRecord = readRecord(); + m_MainRecord = readRecord(); - const std::vector &data = m_MainRecord.data(); - membuf buf(reinterpret_cast(&data[0]), data.size()); + const std::vector& data = m_MainRecord.data(); + membuf buf(reinterpret_cast(&data[0]), data.size()); - std::istream stream(&buf); - while (!stream.eof() && !stream.fail()) { - SubRecord rec; - bool success = rec.readFrom(stream); - if (success) { - if (rec.type() != SubRecord::TYPE_UNKNOWN) { - switch (rec.type()) { - case SubRecord::TYPE_HEDR: onHEDR(rec); break; - case SubRecord::TYPE_MAST: onMAST(rec); break; - case SubRecord::TYPE_CNAM: onCNAM(rec); break; - case SubRecord::TYPE_SNAM: onSNAM(rec); break; - } - } - } - } + std::istream stream(&buf); + while (!stream.eof() && !stream.fail()) { + SubRecord rec; + bool success = rec.readFrom(stream); + if (success) { + if (rec.type() != SubRecord::TYPE_UNKNOWN) { + switch (rec.type()) { + case SubRecord::TYPE_HEDR: + onHEDR(rec); + break; + case SubRecord::TYPE_MAST: + onMAST(rec); + break; + case SubRecord::TYPE_CNAM: + onCNAM(rec); + break; + case SubRecord::TYPE_SNAM: + onSNAM(rec); + break; + } + } + } + } } else { - throw ESP::InvalidFileException("invalid file type"); + throw ESP::InvalidFileException("invalid file type"); } } -void ESP::File::onHEDR(const SubRecord &rec) +void ESP::File::onHEDR(const SubRecord& rec) { if (rec.data().size() != sizeof(m_Header)) { printf("invalid header size\n"); - m_Header.version = 0.0f; - m_Header.numRecords = 1; // prevent this esp appear like a dummy + m_Header.version = 0.0f; + m_Header.numRecords = 1; // prevent this esp appear like a dummy } else { memcpy(&m_Header, &rec.data()[0], sizeof(m_Header)); } } -void ESP::File::onMAST(const SubRecord &rec) +void ESP::File::onMAST(const SubRecord& rec) { if (rec.data().size() > 0) m_Masters.insert(reinterpret_cast(&rec.data()[0])); - } -void ESP::File::onCNAM(const SubRecord &rec) +void ESP::File::onCNAM(const SubRecord& rec) { if (rec.data().size() > 0) m_Author = reinterpret_cast(&rec.data()[0]); } -void ESP::File::onSNAM(const SubRecord &rec) +void ESP::File::onSNAM(const SubRecord& rec) { if (rec.data().size() > 0) m_Description = reinterpret_cast(&rec.data()[0]); } - ESP::Record ESP::File::readRecord() { ESP::Record rec; @@ -147,16 +150,16 @@ bool ESP::File::isMaster() const bool ESP::File::isLight(bool overrideSupport) const { - if (overrideSupport) { - return m_MainRecord.flagSet(Record::FLAG_LIGHT_ALTERNATE); - } else { - return m_MainRecord.flagSet(Record::FLAG_LIGHT); - } + if (overrideSupport) { + return m_MainRecord.flagSet(Record::FLAG_LIGHT_ALTERNATE); + } else { + return m_MainRecord.flagSet(Record::FLAG_LIGHT); + } } bool ESP::File::isOverride() const { - return m_MainRecord.flagSet(Record::FLAG_OVERRIDE); + return m_MainRecord.flagSet(Record::FLAG_OVERRIDE); } bool ESP::File::isDummy() const diff --git a/src/espfile.h b/src/espfile.h index 41f17f5..cf9dd5c 100644 --- a/src/espfile.h +++ b/src/espfile.h @@ -1,24 +1,22 @@ #ifndef ESPFILE_H #define ESPFILE_H - -#include -#include -#include #include "record.h" #include "tes3record.h" +#include +#include +#include - -namespace ESP { +namespace ESP +{ class SubRecord; class File { public: - - File(const std::string &fileName); - File(const std::wstring &fileName); + File(const std::string& fileName); + File(const std::wstring& fileName); Record readRecord(); @@ -31,30 +29,30 @@ public: std::set masters() const { return m_Masters; } private: - void init(); - void onHEDR(const SubRecord &rec); - void onMAST(const SubRecord &rec); - void onCNAM(const SubRecord &rec); - void onSNAM(const SubRecord &rec); + void onHEDR(const SubRecord& rec); + void onMAST(const SubRecord& rec); + void onCNAM(const SubRecord& rec); + void onSNAM(const SubRecord& rec); private: - std::ifstream m_File; - struct { + struct + { float version; int32_t numRecords; uint32_t nextObjectId; } m_Header; - struct { - float version; - uint32_t unknown; - char author[32]; - char description[256]; - uint32_t numRecords; + struct + { + float version; + uint32_t unknown; + char author[32]; + char description[256]; + uint32_t numRecords; } m_TES3Header; Record m_MainRecord; @@ -63,9 +61,8 @@ private: std::string m_Description; std::set m_Masters; - }; -} +} // namespace ESP -#endif // ESPFILE_H +#endif // ESPFILE_H diff --git a/src/esptypes.h b/src/esptypes.h index 116221a..8019b63 100644 --- a/src/esptypes.h +++ b/src/esptypes.h @@ -1,13 +1,13 @@ #ifndef ESPTYPES_H #define ESPTYPES_H - #include - -template static T readType(std::istream &stream) +template +static T readType(std::istream& stream) { - union { + union + { char buffer[sizeof(T)]; T value; }; @@ -16,4 +16,4 @@ template static T readType(std::istream &stream) return value; } -#endif // ESPTYPES_H +#endif // ESPTYPES_H diff --git a/src/record.cpp b/src/record.cpp index 5ea45d1..b7297bb 100644 --- a/src/record.cpp +++ b/src/record.cpp @@ -1,21 +1,14 @@ #include "record.h" #include "espexceptions.h" - -ESP::Record::Record() - : m_Header() - , m_Data() - , m_OblivionStyle(false) -{ - -} +ESP::Record::Record() : m_Header(), m_Data(), m_OblivionStyle(false) {} bool ESP::Record::flagSet(ESP::Record::EFlag flag) const { return (m_Header.flags & flag) != 0; } -bool ESP::Record::readFrom(std::istream &stream) +bool ESP::Record::readFrom(std::istream& stream) { if (!stream.read(reinterpret_cast(&m_Header), sizeof(Header))) { if (stream.gcount() == 0) { @@ -30,7 +23,7 @@ bool ESP::Record::readFrom(std::istream &stream) if (memcmp(buf, "HEDR", 4) == 0) { m_OblivionStyle = true; stream.seekg(-4, std::istream::cur); - } // skyrim has some version data here I don't know how to interpret anyway + } // skyrim has some version data here I don't know how to interpret anyway m_Data.resize(m_Header.dataSize); stream.read(reinterpret_cast(&m_Data[0]), m_Header.dataSize); diff --git a/src/record.h b/src/record.h index 539a223..890692f 100644 --- a/src/record.h +++ b/src/record.h @@ -2,50 +2,52 @@ #define RECORD_H #include -#include #include +#include +namespace ESP +{ -namespace ESP { - - /** - * @brief record storage class without record-specific information - */ - class Record +/** + * @brief record storage class without record-specific information + */ +class Record +{ +public: + enum EFlag { - public: - enum EFlag { - FLAG_MASTER = 0x00000001, - FLAG_LIGHT_ALTERNATE = 0x00000100, // SF light flag (FE/FF memory space) - FLAG_LIGHT = 0x00000200, // SSE & FO4 light flag (FE/FF memory space) - FLAG_OVERRIDE = 0x00000200, // SF override flag (does not claim new memory space, overrules light flag) - FLAG_COMPRESSED = 0x00040000 - }; - - public: - Record(); - - bool readFrom(std::istream& stream); - - bool flagSet(EFlag flag) const; - - const std::vector& data() const { return m_Data; } - - private: - struct Header { - char type[4]; - uint32_t dataSize; - uint32_t flags; - uint32_t id; - uint32_t revision; - } m_Header; - - std::vector m_Data; - - bool m_OblivionStyle; - + FLAG_MASTER = 0x00000001, + FLAG_LIGHT_ALTERNATE = 0x00000100, // SF light flag (FE/FF memory space) + FLAG_LIGHT = 0x00000200, // SSE & FO4 light flag (FE/FF memory space) + FLAG_OVERRIDE = 0x00000200, // SF override flag (does not claim new memory space, + // overrules light flag) + FLAG_COMPRESSED = 0x00040000 }; -} +public: + Record(); -#endif // RECORD_H + bool readFrom(std::istream& stream); + + bool flagSet(EFlag flag) const; + + const std::vector& data() const { return m_Data; } + +private: + struct Header + { + char type[4]; + uint32_t dataSize; + uint32_t flags; + uint32_t id; + uint32_t revision; + } m_Header; + + std::vector m_Data; + + bool m_OblivionStyle; +}; + +} // namespace ESP + +#endif // RECORD_H diff --git a/src/subrecord.cpp b/src/subrecord.cpp index a66daad..aa3b2fb 100644 --- a/src/subrecord.cpp +++ b/src/subrecord.cpp @@ -1,29 +1,20 @@ #include "subrecord.h" -#include "esptypes.h" #include "espexceptions.h" +#include "esptypes.h" +#include #include #include #include -#include - using namespace boost::assign; +ESP::SubRecord::SubRecord() : m_Type(TYPE_UNKNOWN), m_Data() {} -ESP::SubRecord::SubRecord() - : m_Type(TYPE_UNKNOWN) - , m_Data() +bool ESP::SubRecord::readFrom(std::istream& stream, uint32_t sizeOverride) { -} - - -bool ESP::SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) -{ - static std::unordered_map s_TypeMap = map_list_of("HEDR", TYPE_HEDR) - ("CNAM", TYPE_CNAM) - ("MAST", TYPE_MAST) - ("ONAM", TYPE_ONAM) - ("SNAM", TYPE_SNAM); + static std::unordered_map s_TypeMap = + map_list_of("HEDR", TYPE_HEDR)("CNAM", TYPE_CNAM)("MAST", TYPE_MAST)( + "ONAM", TYPE_ONAM)("SNAM", TYPE_SNAM); char typeString[5]; if (!stream.read(typeString, 4)) { @@ -34,15 +25,17 @@ bool ESP::SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) } } if (stream.gcount() != 4) { - throw ESP::InvalidRecordException(std::string("sub-record type incomplete (invalid type ") + typeString + ")"); + throw ESP::InvalidRecordException( + std::string("sub-record type incomplete (invalid type ") + typeString + ")"); } - typeString[4] = '\0'; // not sure if this is required, shouldn't be - auto iter = s_TypeMap.find(std::string(typeString)); + typeString[4] = '\0'; // not sure if this is required, shouldn't be + auto iter = s_TypeMap.find(std::string(typeString)); if (iter != s_TypeMap.end()) { m_Type = iter->second; } else if (strncmp(typeString, "XXXX", 4) == 0) { if (readType(stream) != 4) { - throw ESP::InvalidRecordException("XXXX record is supposed to be 4 bytes in size"); + throw ESP::InvalidRecordException( + "XXXX record is supposed to be 4 bytes in size"); } return readFrom(stream, readType(stream)); } else { @@ -57,7 +50,8 @@ bool ESP::SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) stream.read(reinterpret_cast(&m_Data[0]), dataSize); if (!stream) { - throw ESP::InvalidRecordException(std::string("sub-record incomplete: ") + typeString); + throw ESP::InvalidRecordException(std::string("sub-record incomplete: ") + + typeString); } return true; } diff --git a/src/subrecord.h b/src/subrecord.h index 68265f9..e425a59 100644 --- a/src/subrecord.h +++ b/src/subrecord.h @@ -1,14 +1,12 @@ #ifndef SUBRECORD_H #define SUBRECORD_H - #include #include #include - -namespace ESP { - +namespace ESP +{ /** * @brief sub-record storage class without record-specific information @@ -16,8 +14,8 @@ namespace ESP { class SubRecord { public: - - enum EType { + enum EType + { TYPE_UNKNOWN, TYPE_HEDR, TYPE_CNAM, @@ -31,17 +29,16 @@ public: public: SubRecord(); - bool readFrom(std::istream &stream, uint32_t sizeOverride = 0UL); + bool readFrom(std::istream& stream, uint32_t sizeOverride = 0UL); EType type() const { return m_Type; } - const std::vector &data() const { return m_Data; } + const std::vector& data() const { return m_Data; } private: - EType m_Type; std::vector m_Data; }; -} +} // namespace ESP -#endif // SUBRECORD_H +#endif // SUBRECORD_H diff --git a/src/tes3record.cpp b/src/tes3record.cpp index e58d065..6f0fe60 100644 --- a/src/tes3record.cpp +++ b/src/tes3record.cpp @@ -1,20 +1,16 @@ #include "tes3record.h" #include "espexceptions.h" +ESP::TES3Record::TES3Record() : m_Header() {} -ESP::TES3Record::TES3Record() - : m_Header() -{ } - -bool ESP::TES3Record::readFrom(std::istream &stream) +bool ESP::TES3Record::readFrom(std::istream& stream) { - if (!stream.read(reinterpret_cast(&m_Header), sizeof(Header))) { - if (stream.gcount() == 0) { - return false; - } - else { - throw ESP::InvalidRecordException("record incomplete"); - } - } - return true; + if (!stream.read(reinterpret_cast(&m_Header), sizeof(Header))) { + if (stream.gcount() == 0) { + return false; + } else { + throw ESP::InvalidRecordException("record incomplete"); + } + } + return true; } diff --git a/src/tes3record.h b/src/tes3record.h index 73bf119..19fa040 100644 --- a/src/tes3record.h +++ b/src/tes3record.h @@ -2,30 +2,31 @@ #define TES3_RECORD_H #include -#include #include +#include +namespace ESP +{ -namespace ESP { +/** + * @brief record storage class without record-specific information + */ +class TES3Record +{ +public: + TES3Record(); - /** - * @brief record storage class without record-specific information - */ - class TES3Record - { - public: - TES3Record(); + bool readFrom(std::istream& stream); - bool readFrom(std::istream &stream); +private: + struct Header + { + char type[4]; + float version; + long unknown; + } m_Header; +}; - private: - struct Header { - char type[4]; - float version; - long unknown; - } m_Header; - }; +} // namespace ESP -} - -#endif // TES3_RECORD_H +#endif // TES3_RECORD_H diff --git a/src/tes3subrecord.cpp b/src/tes3subrecord.cpp index 625c240..94ac31c 100644 --- a/src/tes3subrecord.cpp +++ b/src/tes3subrecord.cpp @@ -1,27 +1,19 @@ #include "tes3subrecord.h" -#include "esptypes.h" #include "espexceptions.h" +#include "esptypes.h" +#include #include #include #include -#include - using namespace boost::assign; +ESP::TES3SubRecord::TES3SubRecord() : m_Type(TYPE_UNKNOWN), m_Data() {} -ESP::TES3SubRecord::TES3SubRecord() - : m_Type(TYPE_UNKNOWN) - , m_Data() +bool ESP::TES3SubRecord::readFrom(std::istream& stream, uint32_t sizeOverride) { -} - - -bool ESP::TES3SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) -{ - static std::unordered_map s_TypeMap = map_list_of("HEDR", TYPE_HEDR) - ("MAST", TYPE_MAST) - ("DATA", TYPE_DATA); + static std::unordered_map s_TypeMap = + map_list_of("HEDR", TYPE_HEDR)("MAST", TYPE_MAST)("DATA", TYPE_DATA); char typeString[5]; if (!stream.read(typeString, 4)) { @@ -32,18 +24,19 @@ bool ESP::TES3SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) } } if (stream.gcount() != 4) { - throw ESP::InvalidRecordException(std::string("sub-record type incomplete (invalid type ") + typeString + ")"); + throw ESP::InvalidRecordException( + std::string("sub-record type incomplete (invalid type ") + typeString + ")"); } - typeString[4] = '\0'; // not sure if this is required, shouldn't be - auto iter = s_TypeMap.find(std::string(typeString)); + typeString[4] = '\0'; // not sure if this is required, shouldn't be + auto iter = s_TypeMap.find(std::string(typeString)); uint32_t dataSize; if (iter != s_TypeMap.end()) { - m_Type = iter->second; - dataSize = readType(stream); + m_Type = iter->second; + dataSize = readType(stream); } else { - m_Type = TYPE_UNKNOWN; - dataSize = readType(stream); - stream.seekg(8, std::istream::cur); + m_Type = TYPE_UNKNOWN; + dataSize = readType(stream); + stream.seekg(8, std::istream::cur); } if (sizeOverride != 0UL) { @@ -53,7 +46,8 @@ bool ESP::TES3SubRecord::readFrom(std::istream &stream, uint32_t sizeOverride) stream.read(reinterpret_cast(&m_Data[0]), dataSize); if (!stream) { - throw ESP::InvalidRecordException(std::string("sub-record incomplete: ") + typeString); + throw ESP::InvalidRecordException(std::string("sub-record incomplete: ") + + typeString); } return true; } diff --git a/src/tes3subrecord.h b/src/tes3subrecord.h index 4059530..966afcb 100644 --- a/src/tes3subrecord.h +++ b/src/tes3subrecord.h @@ -1,14 +1,12 @@ #ifndef TES3_SUBRECORD_H #define TES3_SUBRECORD_H - #include #include #include - -namespace ESP { - +namespace ESP +{ /** * @brief sub-record storage class without record-specific information @@ -16,30 +14,29 @@ namespace ESP { class TES3SubRecord { public: - - enum EType { + enum EType + { TYPE_UNKNOWN, TYPE_HEDR, - TYPE_MAST, - TYPE_DATA + TYPE_MAST, + TYPE_DATA }; static const int NUM_TYPES = TYPE_DATA; public: - TES3SubRecord(); + TES3SubRecord(); - bool readFrom(std::istream &stream, uint32_t sizeOverride = 0UL); + bool readFrom(std::istream& stream, uint32_t sizeOverride = 0UL); EType type() const { return m_Type; } - const std::vector &data() const { return m_Data; } + const std::vector& data() const { return m_Data; } private: - EType m_Type; std::vector m_Data; }; -} +} // namespace ESP -#endif // TES3_SUBRECORD_H +#endif // TES3_SUBRECORD_H