mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 22:56:27 +00:00
File identifier feature.
Allows you to add, and test for the presence of a magic 4-char string in a FlatBuffer. Tested: on OS X. Change-Id: I090692a9e4fb53bed3543279a28563e67132cba0
This commit is contained in:
@@ -27,9 +27,10 @@ bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + ".bin").c_str(),
|
||||
(path + file_name + "." + ext).c_str(),
|
||||
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
|
||||
parser.builder_.GetSize(),
|
||||
true);
|
||||
|
||||
@@ -466,18 +466,36 @@ std::string GenerateCPP(const Parser &parser, const std::string &include_guard_i
|
||||
code += decl_code;
|
||||
code += enum_code_post;
|
||||
|
||||
// Generate convenient root datatype accessor, and root verifier.
|
||||
// Generate convenient global helper functions:
|
||||
if (parser.root_struct_def) {
|
||||
// The root datatype accessor:
|
||||
code += "inline const " + parser.root_struct_def->name + " *Get";
|
||||
code += parser.root_struct_def->name;
|
||||
code += "(const void *buf) { return flatbuffers::GetRoot<";
|
||||
code += parser.root_struct_def->name + ">(buf); }\n\n";
|
||||
|
||||
// The root verifier:
|
||||
code += "inline bool Verify";
|
||||
code += parser.root_struct_def->name;
|
||||
code += "Buffer(const flatbuffers::Verifier &verifier) { "
|
||||
"return verifier.VerifyBuffer<";
|
||||
code += parser.root_struct_def->name + ">(); }\n\n";
|
||||
|
||||
// Finish a buffer with a given root object:
|
||||
code += "inline void Finish" + parser.root_struct_def->name;
|
||||
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
|
||||
code += parser.root_struct_def->name + "> root) { fbb.Finish(root";
|
||||
if (parser.file_identifier_.length())
|
||||
code += ", \"" + parser.file_identifier_ + "\"";
|
||||
code += "); }\n\n";
|
||||
|
||||
if (parser.file_identifier_.length()) {
|
||||
// Check if a buffer has the identifier.
|
||||
code += "inline bool " + parser.root_struct_def->name;
|
||||
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
|
||||
code += "BufferHasIdentifier(buf, \"" + parser.file_identifier_;
|
||||
code += "\"); }\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Close the namespaces.
|
||||
|
||||
@@ -81,7 +81,9 @@ template<> inline Offset<void> atot<Offset<void>>(const char *s) {
|
||||
TD(Enum, 263, "enum") \
|
||||
TD(Union, 264, "union") \
|
||||
TD(NameSpace, 265, "namespace") \
|
||||
TD(RootType, 266, "root_type")
|
||||
TD(RootType, 266, "root_type") \
|
||||
TD(FileIdentifier, 267, "file_identifier") \
|
||||
TD(FileExtension, 268, "file_extension")
|
||||
#ifdef __GNUC__
|
||||
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
|
||||
#endif
|
||||
@@ -194,6 +196,14 @@ void Parser::Next() {
|
||||
if (attribute_ == "union") { token_ = kTokenUnion; return; }
|
||||
if (attribute_ == "namespace") { token_ = kTokenNameSpace; return; }
|
||||
if (attribute_ == "root_type") { token_ = kTokenRootType; return; }
|
||||
if (attribute_ == "file_identifier") {
|
||||
token_ = kTokenFileIdentifier;
|
||||
return;
|
||||
}
|
||||
if (attribute_ == "file_extension") {
|
||||
token_ = kTokenFileExtension;
|
||||
return;
|
||||
}
|
||||
// If not, it is a user-defined identifier:
|
||||
token_ = kTokenIdentifier;
|
||||
return;
|
||||
@@ -802,11 +812,26 @@ bool Parser::Parse(const char *source) {
|
||||
Next();
|
||||
auto root_type = attribute_;
|
||||
Expect(kTokenIdentifier);
|
||||
Expect(';');
|
||||
if (!SetRootType(root_type.c_str()))
|
||||
Error("unknown root type: " + root_type);
|
||||
if (root_struct_def->fixed)
|
||||
Error("root type must be a table");
|
||||
Expect(';');
|
||||
} else if (token_ == kTokenFileIdentifier) {
|
||||
Next();
|
||||
file_identifier_ = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
if (file_identifier_.length() !=
|
||||
FlatBufferBuilder::kFileIdentifierLength)
|
||||
Error("file_identifier must be exactly " +
|
||||
NumToString(FlatBufferBuilder::kFileIdentifierLength) +
|
||||
" characters");
|
||||
Expect(';');
|
||||
} else if (token_ == kTokenFileExtension) {
|
||||
Next();
|
||||
file_extension_ = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
Expect(';');
|
||||
} else {
|
||||
ParseDecl();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user