From 91cb53f9f7c8c3e842c148b3a9e558af40ad7c4c Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 6 Feb 2023 22:58:58 -0800 Subject: [PATCH 01/14] Add a FileWriter interface --- include/flatbuffers/file_writer.h | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 include/flatbuffers/file_writer.h diff --git a/include/flatbuffers/file_writer.h b/include/flatbuffers/file_writer.h new file mode 100644 index 00000000000..db032d4a172 --- /dev/null +++ b/include/flatbuffers/file_writer.h @@ -0,0 +1,55 @@ +/* + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLATBUFFERS_FILE_WRITER_H_ +#define FLATBUFFERS_FILE_WRITER_H_ + +#include + +namespace flatbuffers { + +// A FileWriter interface to write data to file by default or +// save only file names +class FileWriter { + public: + virtual ~FileWriter() = default; + + enum Mode { + DEFAULT = 0, + SAVE_FILE_NAMES_ONLY = 1 + }; + + virtual bool SaveFile(); + + virtual std::string Path() const = 0; + + virtual std::string FileName() const = 0; + + protected: + FileWriter(const std::string &path, const std::string &file_name); + + private: + // Copying is not supported. + FileWriter(const FileWriter &) = delete; + FileWriter &operator=(const FileWriter &) = delete; + + std::string path_; + std::string file_name_; +}; + +} // namespace flatbuffers + +#endif // FLATBUFFERS_FILE_WRITER_H_ From 512c27d595e7e88bf91b62a67a40e68bbbd12c08 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Thu, 9 Feb 2023 07:34:15 -0800 Subject: [PATCH 02/14] Change interface --- include/flatbuffers/{file_writer.h => file.h} | 26 +++++------- src/default_file.cpp | 41 +++++++++++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) rename include/flatbuffers/{file_writer.h => file.h} (63%) create mode 100644 src/default_file.cpp diff --git a/include/flatbuffers/file_writer.h b/include/flatbuffers/file.h similarity index 63% rename from include/flatbuffers/file_writer.h rename to include/flatbuffers/file.h index db032d4a172..71c566fa905 100644 --- a/include/flatbuffers/file_writer.h +++ b/include/flatbuffers/file.h @@ -21,33 +21,27 @@ namespace flatbuffers { -// A FileWriter interface to write data to file by default or +// A File interface to write data to file by default or // save only file names -class FileWriter { +class File { public: - virtual ~FileWriter() = default; + virtual ~File() = default; - enum Mode { - DEFAULT = 0, - SAVE_FILE_NAMES_ONLY = 1 - }; + virtual bool SaveFile(std::string file_path, std::string content); - virtual bool SaveFile(); + virtual bool ReadFile(std::string file_path, bool binary, std::string *buf); - virtual std::string Path() const = 0; - - virtual std::string FileName() const = 0; + virtual std::set FileNames() const = 0; protected: - FileWriter(const std::string &path, const std::string &file_name); + File(); private: // Copying is not supported. - FileWriter(const FileWriter &) = delete; - FileWriter &operator=(const FileWriter &) = delete; + File(const File &) = delete; + File &operator=(const File &) = delete; - std::string path_; - std::string file_name_; + std::set files_; }; } // namespace flatbuffers diff --git a/src/default_file.cpp b/src/default_file.cpp new file mode 100644 index 00000000000..a4357f0dff4 --- /dev/null +++ b/src/default_file.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "include/flatbuffers/file.h" + +namespace flatbuffers { + +class DefaultFile :: File { + public: + bool SaveFile(std::string file_path, std::string buf, bool binary) override { + std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out); + if (!ofs.is_open()) return false; + ofs.write(buf.c_str(), buf.size()); + return !ofs.bad(); + } + + std::string ReadFile(std::string file_path) override { + + + } + + std::set FileNames() { return file_names_; } +}; + +} // namespace flatbuffers + +#endif // FLATBUFFERS_FILE_WRITER_H_ From 70eb626ce47649fe54f509d844b9b99c431cdb52 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 11 Feb 2023 10:27:37 -0800 Subject: [PATCH 03/14] Provide 2 impl for File interface: FileManager & FileNameManager --- include/flatbuffers/file.h | 4 +- src/file_manager.cpp | 45 +++++++++++++++++++ ...default_file.cpp => file_name_manager.cpp} | 14 +++--- 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/file_manager.cpp rename src/{default_file.cpp => file_name_manager.cpp} (73%) diff --git a/include/flatbuffers/file.h b/include/flatbuffers/file.h index 71c566fa905..f57d59be4e2 100644 --- a/include/flatbuffers/file.h +++ b/include/flatbuffers/file.h @@ -32,7 +32,7 @@ class File { virtual bool ReadFile(std::string file_path, bool binary, std::string *buf); virtual std::set FileNames() const = 0; - + protected: File(); @@ -40,7 +40,7 @@ class File { // Copying is not supported. File(const File &) = delete; File &operator=(const File &) = delete; - + std::set files_; }; diff --git a/src/file_manager.cpp b/src/file_manager.cpp new file mode 100644 index 00000000000..3f6f0a62324 --- /dev/null +++ b/src/file_manager.cpp @@ -0,0 +1,45 @@ +/* + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "include/flatbuffers/file.h" + +namespace flatbuffers { + +class FileManager :: File { + public: + bool SaveFile(std::string absolute_file_name, std::string buf, bool binary) override { + std::ofstream ofs(absolute_file_name, binary ? std::ofstream::binary : std::ofstream::out); + if (!ofs.is_open()) return false; + ofs.write(buf.c_str(), buf.size()); + if(!ofs.bad()) { + file_names_.insert(absolute_file_name); + return true; + } + return false; + } + + std::string ReadFile(std::string file_path) override { + (void) file_path; + return ""; + } + + std::set FileNames() { return file_names_; } +}; + +} // namespace flatbuffers + +#endif // FLATBUFFERS_FILE_WRITER_H_ diff --git a/src/default_file.cpp b/src/file_name_manager.cpp similarity index 73% rename from src/default_file.cpp rename to src/file_name_manager.cpp index a4357f0dff4..0219a0a1768 100644 --- a/src/default_file.cpp +++ b/src/file_name_manager.cpp @@ -19,18 +19,16 @@ namespace flatbuffers { -class DefaultFile :: File { +class FileNameManager :: File { public: - bool SaveFile(std::string file_path, std::string buf, bool binary) override { - std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out); - if (!ofs.is_open()) return false; - ofs.write(buf.c_str(), buf.size()); - return !ofs.bad(); + bool SaveFile(std::string absolute_file_name, std::string buf, bool binary) override { + auto pair = file_names_.insert(absolute_file_name); + return pair.second; } std::string ReadFile(std::string file_path) override { - - + (void) file_path; + return ""; } std::set FileNames() { return file_names_; } From 44e718d5928be3308601a74b3c67cf6d0296fe17 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sun, 12 Feb 2023 11:44:14 -0800 Subject: [PATCH 04/14] Update --- .../flatbuffers/{file.h => file_manager.h} | 24 +++++----- src/file_manager.cpp | 45 ------------------- src/file_name_manager.cpp | 17 ++++--- src/flatc.cpp | 5 +++ 4 files changed, 27 insertions(+), 64 deletions(-) rename include/flatbuffers/{file.h => file_manager.h} (59%) delete mode 100644 src/file_manager.cpp diff --git a/include/flatbuffers/file.h b/include/flatbuffers/file_manager.h similarity index 59% rename from include/flatbuffers/file.h rename to include/flatbuffers/file_manager.h index f57d59be4e2..e53d405f799 100644 --- a/include/flatbuffers/file.h +++ b/include/flatbuffers/file_manager.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef FLATBUFFERS_FILE_WRITER_H_ -#define FLATBUFFERS_FILE_WRITER_H_ +#ifndef FLATBUFFERS_FILE_MANAGER_H_ +#define FLATBUFFERS_FILE_MANAGER_H_ #include @@ -23,27 +23,27 @@ namespace flatbuffers { // A File interface to write data to file by default or // save only file names -class File { +class FileManager { public: - virtual ~File() = default; + virtual ~FileManager() = default; - virtual bool SaveFile(std::string file_path, std::string content); + virtual bool SaveFile(std::string absolute_file_path, std::string content, size_t len, bool binary) = 0; - virtual bool ReadFile(std::string file_path, bool binary, std::string *buf); + virtual bool ReadFile(std::string absolute_file_path, bool binary, std::string *buf) = 0; - virtual std::set FileNames() const = 0; + virtual std::set FileNames() const = 0; protected: - File(); + FileManager(); private: // Copying is not supported. - File(const File &) = delete; - File &operator=(const File &) = delete; + FileManager(const FileManager &) = delete; + FileManager &operator=(const FileManager &) = delete; - std::set files_; + std::set file_names_; }; } // namespace flatbuffers -#endif // FLATBUFFERS_FILE_WRITER_H_ +#endif // FLATBUFFERS_FILE_MANAGER_H_ diff --git a/src/file_manager.cpp b/src/file_manager.cpp deleted file mode 100644 index 3f6f0a62324..00000000000 --- a/src/file_manager.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2023 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "include/flatbuffers/file.h" - -namespace flatbuffers { - -class FileManager :: File { - public: - bool SaveFile(std::string absolute_file_name, std::string buf, bool binary) override { - std::ofstream ofs(absolute_file_name, binary ? std::ofstream::binary : std::ofstream::out); - if (!ofs.is_open()) return false; - ofs.write(buf.c_str(), buf.size()); - if(!ofs.bad()) { - file_names_.insert(absolute_file_name); - return true; - } - return false; - } - - std::string ReadFile(std::string file_path) override { - (void) file_path; - return ""; - } - - std::set FileNames() { return file_names_; } -}; - -} // namespace flatbuffers - -#endif // FLATBUFFERS_FILE_WRITER_H_ diff --git a/src/file_name_manager.cpp b/src/file_name_manager.cpp index 0219a0a1768..759fc7ce88e 100644 --- a/src/file_name_manager.cpp +++ b/src/file_name_manager.cpp @@ -15,25 +15,28 @@ */ #include -#include "include/flatbuffers/file.h" + +#include "include/flatbuffers/file_manager.h" namespace flatbuffers { -class FileNameManager :: File { +class FileNameManager ::FileManager { public: - bool SaveFile(std::string absolute_file_name, std::string buf, bool binary) override { + bool SaveFile(std::string absolute_file_name, std::string content, + size_t len, + bool binary) override { auto pair = file_names_.insert(absolute_file_name); return pair.second; } - std::string ReadFile(std::string file_path) override { + bool ReadFile(std::string absolute_file_name, bool binary, std::string * buf) override { (void) file_path; - return ""; + (void) binary; + (void) buf; + return false; } std::set FileNames() { return file_names_; } }; } // namespace flatbuffers - -#endif // FLATBUFFERS_FILE_WRITER_H_ diff --git a/src/flatc.cpp b/src/flatc.cpp index 2da6bc45795..39a9d5de58c 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -249,6 +249,8 @@ const static FlatCOption flatc_options[] = { { "", "no-leak-private-annotation", "", "Prevents multiple type of annotations within a Fbs SCHEMA file. " "Currently this is required to generate private types in Rust" }, + { "", "file-names-only", "", + "Print out generated file names without writing to the files"}, }; auto cmp = [](FlatCOption a, FlatCOption b) { return a.long_opt < b.long_opt; }; @@ -651,6 +653,9 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc, } else if (arg == "--annotate") { if (++argi >= argc) Error("missing path following: " + arg, true); options.annotate_schema = flatbuffers::PosixPath(argv[argi]); + } else if(arg == "--file-names-only") { + // TODO (khhn): Provide 2 implementation + options.file_names_only = true; } else { // Look up if the command line argument refers to a code generator. auto code_generator_it = code_generators_.find(arg); From 845d1d8851b4804a4807564c7213cc3d1bc6fbad Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sun, 12 Feb 2023 18:20:08 -0800 Subject: [PATCH 05/14] update --- include/flatbuffers/flatc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index e6227d64051..e98eb80d7fb 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -56,6 +56,7 @@ struct FlatCOptions { bool schema_binary = false; bool grpc_enabled = false; bool requires_bfbs = false; + bool file_names_only = false; std::vector> generators; }; From c34f70f8c25ec09064dbcecc16911b325356b2da Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 20 Feb 2023 19:10:43 -0500 Subject: [PATCH 06/14] Update --- BUILD.bazel | 1 + CMakeLists.txt | 4 ++ include/flatbuffers/file_manager.h | 12 +++-- src/BUILD.bazel | 3 ++ src/file_binary_writer.cpp | 48 +++++++++++++++++++ ....cpp => file_name_saving_file_manager.cpp} | 19 ++++---- 6 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/file_binary_writer.cpp rename src/{file_name_manager.cpp => file_name_saving_file_manager.cpp} (64%) diff --git a/BUILD.bazel b/BUILD.bazel index de910bc530b..f31104450df 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -48,6 +48,7 @@ filegroup( "include/flatbuffers/default_allocator.h", "include/flatbuffers/detached_buffer.h", "include/flatbuffers/flatbuffer_builder.h", + "include/flatbuffers/file_manager.h", "include/flatbuffers/flatbuffers.h", "include/flatbuffers/flex_flat_util.h", "include/flatbuffers/flexbuffers.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index 27d80859f70..2b65c20e505 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ set(FlatBuffers_Library_SRCS include/flatbuffers/default_allocator.h include/flatbuffers/detached_buffer.h include/flatbuffers/code_generator.h + include/flatbuffers/file_manager.h include/flatbuffers/flatbuffer_builder.h include/flatbuffers/flatbuffers.h include/flatbuffers/flexbuffers.h @@ -171,6 +172,9 @@ set(FlatBuffers_Compiler_SRCS src/idl_gen_grpc.cpp src/idl_gen_json_schema.cpp src/idl_gen_swift.cpp + src/file_name_saving_file_manager.cpp + src/file_binary_writer.cpp + src/file_writer.cpp src/idl_namer.h src/namer.h src/flatc.cpp diff --git a/include/flatbuffers/file_manager.h b/include/flatbuffers/file_manager.h index e53d405f799..7fcc4440797 100644 --- a/include/flatbuffers/file_manager.h +++ b/include/flatbuffers/file_manager.h @@ -18,6 +18,7 @@ #define FLATBUFFERS_FILE_MANAGER_H_ #include +#include namespace flatbuffers { @@ -27,21 +28,22 @@ class FileManager { public: virtual ~FileManager() = default; - virtual bool SaveFile(std::string absolute_file_path, std::string content, size_t len, bool binary) = 0; + virtual bool SaveFile(const std::string &absolute_file_name, const std::string &content) = 0; - virtual bool ReadFile(std::string absolute_file_path, bool binary, std::string *buf) = 0; + virtual bool ReadFile(const std::string &absolute_file_name, std::string *content) = 0; - virtual std::set FileNames() const = 0; + std::set FileNames() { return file_names_; } + + FileManager(); protected: - FileManager(); + std::set file_names_; private: // Copying is not supported. FileManager(const FileManager &) = delete; FileManager &operator=(const FileManager &) = delete; - std::set file_names_; }; } // namespace flatbuffers diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 3f4ba0c7f9b..ba693f00b76 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -103,6 +103,9 @@ cc_library( "idl_gen_ts.cpp", "idl_gen_ts.h", "idl_namer.h", + "file_writer.cpp", + "file_binary_writer.cpp", + "file_name_saving_file_manager.cpp", "namer.h", "util.cpp", ], diff --git a/src/file_binary_writer.cpp b/src/file_binary_writer.cpp new file mode 100644 index 00000000000..33b88ebcf28 --- /dev/null +++ b/src/file_binary_writer.cpp @@ -0,0 +1,48 @@ +/* + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "flatbuffers/file_manager.h" + +namespace flatbuffers { + +class FileBinaryWriter : public FileManager { + public: + bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { + std::ofstream ofs(absolute_file_name, + std::ofstream::binary); + if (!ofs.is_open()) return false; + ofs.write(content.c_str(), content.size()); + if (!ofs.bad()) { + file_names_.insert(absolute_file_name); + return true; + } + return false; + } + + bool ReadFile(const std::string &absolute_file_name, std::string *content) override { + (void) absolute_file_name; + (void) content; + return false; + } + + //std::set FileNames() { return file_names_; } +}; + +} // namespace flatbuffers diff --git a/src/file_name_manager.cpp b/src/file_name_saving_file_manager.cpp similarity index 64% rename from src/file_name_manager.cpp rename to src/file_name_saving_file_manager.cpp index 759fc7ce88e..5e7ea5b198e 100644 --- a/src/file_name_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -15,28 +15,27 @@ */ #include +#include +#include -#include "include/flatbuffers/file_manager.h" +#include "flatbuffers/file_manager.h" namespace flatbuffers { -class FileNameManager ::FileManager { +class FileNameSavingFileManagerManager : public FileManager { public: - bool SaveFile(std::string absolute_file_name, std::string content, - size_t len, - bool binary) override { + bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { auto pair = file_names_.insert(absolute_file_name); return pair.second; } - bool ReadFile(std::string absolute_file_name, bool binary, std::string * buf) override { - (void) file_path; - (void) binary; - (void) buf; + bool ReadFile(const std::string &absolute_file_name, std::string * content) override { + (void) absolute_file_name; + (void) content; return false; } - std::set FileNames() { return file_names_; } + //std::set FileNameSavingFileManagers() { return file_names_; } }; } // namespace flatbuffers From a728f0e17c9a9a0ae5a1e634aa5cf8dde95dd331 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 20 Feb 2023 19:15:04 -0500 Subject: [PATCH 07/14] Add file_writer file --- src/file_writer.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/file_writer.cpp diff --git a/src/file_writer.cpp b/src/file_writer.cpp new file mode 100644 index 00000000000..3557e7bde22 --- /dev/null +++ b/src/file_writer.cpp @@ -0,0 +1,48 @@ +/* + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "flatbuffers/file_manager.h" + +namespace flatbuffers { + +class FileWriter : public FileManager { + public: + bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { + std::ofstream ofs(absolute_file_name, + std::ofstream::out); + if (!ofs.is_open()) return false; + ofs.write(content.c_str(), content.size()); + if (!ofs.bad()) { + file_names_.insert(absolute_file_name); + return true; + } + return false; + } + + bool ReadFile(const std::string &absolute_file_name, std::string *content) override { + (void) absolute_file_name; + (void) content; + return false; + } + + //std::set FileNames() { return file_names_; } +}; + +} // namespace flatbuffers From 3969d416888427e05023148f6f0bc403af995c1a Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 20 Feb 2023 19:19:24 -0500 Subject: [PATCH 08/14] Update --- src/file_name_saving_file_manager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index 5e7ea5b198e..cda71569c4a 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -25,6 +25,7 @@ namespace flatbuffers { class FileNameSavingFileManagerManager : public FileManager { public: bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { + (void) content; auto pair = file_names_.insert(absolute_file_name); return pair.second; } From 61cbbc5660b110fe14e1c738a421920c92351b59 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Mon, 20 Feb 2023 19:22:37 -0500 Subject: [PATCH 09/14] Format files --- src/file_name_saving_file_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index cda71569c4a..61ee1dba7a9 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -25,7 +25,7 @@ namespace flatbuffers { class FileNameSavingFileManagerManager : public FileManager { public: bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { - (void) content; + (void)content; auto pair = file_names_.insert(absolute_file_name); return pair.second; } From ea0866f270c25d3b9c77433214d2ab024b250adf Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 18 Mar 2023 16:55:33 +0000 Subject: [PATCH 10/14] Update based on review --- include/flatbuffers/file_manager.h | 17 ++++++----------- include/flatbuffers/flatbuffer_builder.h | 3 ++- include/flatbuffers/flexbuffers.h | 10 ++++------ include/flatbuffers/minireflect.h | 2 +- include/flatbuffers/util.h | 5 +++-- src/file_binary_writer.cpp | 24 ++++++------------------ src/file_name_saving_file_manager.cpp | 23 +++++++++++------------ src/file_writer.cpp | 24 ++++++------------------ 8 files changed, 39 insertions(+), 69 deletions(-) diff --git a/include/flatbuffers/file_manager.h b/include/flatbuffers/file_manager.h index 7fcc4440797..46ebf3dcb25 100644 --- a/include/flatbuffers/file_manager.h +++ b/include/flatbuffers/file_manager.h @@ -17,8 +17,8 @@ #ifndef FLATBUFFERS_FILE_MANAGER_H_ #define FLATBUFFERS_FILE_MANAGER_H_ -#include #include +#include namespace flatbuffers { @@ -26,24 +26,19 @@ namespace flatbuffers { // save only file names class FileManager { public: + FileManager() = default; virtual ~FileManager() = default; - virtual bool SaveFile(const std::string &absolute_file_name, const std::string &content) = 0; - - virtual bool ReadFile(const std::string &absolute_file_name, std::string *content) = 0; + virtual bool SaveFile(const std::string &absolute_file_name, + const std::string &content) = 0; - std::set FileNames() { return file_names_; } - - FileManager(); - - protected: - std::set file_names_; + virtual bool LoadFile(const std::string &absolute_file_name, + std::string *content) = 0; private: // Copying is not supported. FileManager(const FileManager &) = delete; FileManager &operator=(const FileManager &) = delete; - }; } // namespace flatbuffers diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index a1d3d60a795..b9015d85020 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -1184,7 +1184,8 @@ class FlatBufferBuilder { // Allocates space for a vector of structures. // Must be completed with EndVectorOfStructs(). template T *StartVectorOfStructs(size_t vector_size) { - StartVector(vector_size * sizeof(T) / AlignOf(), sizeof(T), AlignOf()); + StartVector(vector_size * sizeof(T) / AlignOf(), sizeof(T), + AlignOf()); return reinterpret_cast(buf_.make_space(vector_size * sizeof(T))); } diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index a0ee6700358..8e8cac144ee 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.h @@ -1424,12 +1424,10 @@ class Builder FLATBUFFERS_FINAL_CLASS { template static Type GetScalarType() { static_assert(flatbuffers::is_scalar::value, "Unrelated types"); - return flatbuffers::is_floating_point::value - ? FBT_FLOAT - : flatbuffers::is_same::value - ? FBT_BOOL - : (flatbuffers::is_unsigned::value ? FBT_UINT - : FBT_INT); + return flatbuffers::is_floating_point::value ? FBT_FLOAT + : flatbuffers::is_same::value + ? FBT_BOOL + : (flatbuffers::is_unsigned::value ? FBT_UINT : FBT_INT); } public: diff --git a/include/flatbuffers/minireflect.h b/include/flatbuffers/minireflect.h index 22f43fbab92..1e04bfff02a 100644 --- a/include/flatbuffers/minireflect.h +++ b/include/flatbuffers/minireflect.h @@ -408,7 +408,7 @@ inline std::string FlatBufferToString(const uint8_t *buffer, const TypeTable *type_table, bool multi_line = false, bool vector_delimited = true, - const std::string& indent = "") { + const std::string &indent = "") { ToStringVisitor tostring_visitor(multi_line ? "\n" : " ", false, indent, vector_delimited); IterateFlatBuffer(buffer, type_table, &tostring_visitor); diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 6d0cd2c0c4f..a6bcf34b68c 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -722,9 +722,10 @@ enum class Case { kSnake2 = 9, }; -// Convert the `input` string of case `input_case` to the specified `output_case`. +// Convert the `input` string of case `input_case` to the specified +// `output_case`. std::string ConvertCase(const std::string &input, Case output_case, - Case input_case = Case::kSnake); + Case input_case = Case::kSnake); } // namespace flatbuffers diff --git a/src/file_binary_writer.cpp b/src/file_binary_writer.cpp index 33b88ebcf28..618485aeb8c 100644 --- a/src/file_binary_writer.cpp +++ b/src/file_binary_writer.cpp @@ -14,9 +14,9 @@ * limitations under the License. */ -#include -#include #include +#include +#include #include "flatbuffers/file_manager.h" @@ -24,25 +24,13 @@ namespace flatbuffers { class FileBinaryWriter : public FileManager { public: - bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { - std::ofstream ofs(absolute_file_name, - std::ofstream::binary); + bool SaveFile(const std::string &absolute_file_name, + const std::string &content) override { + std::ofstream ofs(absolute_file_name, std::ofstream::binary); if (!ofs.is_open()) return false; ofs.write(content.c_str(), content.size()); - if (!ofs.bad()) { - file_names_.insert(absolute_file_name); - return true; - } - return false; + return !ofs.bad(); } - - bool ReadFile(const std::string &absolute_file_name, std::string *content) override { - (void) absolute_file_name; - (void) content; - return false; - } - - //std::set FileNames() { return file_names_; } }; } // namespace flatbuffers diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index 61ee1dba7a9..26721cd1c8b 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -14,29 +14,28 @@ * limitations under the License. */ -#include -#include #include +#include +#include #include "flatbuffers/file_manager.h" namespace flatbuffers { -class FileNameSavingFileManagerManager : public FileManager { +class FileNameSavingFileManager : public FileManager { public: - bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { - (void)content; + FileNameSavingFileManager(std::set file_names) + : file_names_(file_names) {} + + bool SaveFile(const std::string &absolute_file_name, const std::string) { auto pair = file_names_.insert(absolute_file_name); + // pair.second indicates whether the insertion is + // successful or not. return pair.second; } - bool ReadFile(const std::string &absolute_file_name, std::string * content) override { - (void) absolute_file_name; - (void) content; - return false; - } - - //std::set FileNameSavingFileManagers() { return file_names_; } + private: + std::set file_names_; }; } // namespace flatbuffers diff --git a/src/file_writer.cpp b/src/file_writer.cpp index 3557e7bde22..793f075808b 100644 --- a/src/file_writer.cpp +++ b/src/file_writer.cpp @@ -14,9 +14,9 @@ * limitations under the License. */ -#include -#include #include +#include +#include #include "flatbuffers/file_manager.h" @@ -24,25 +24,13 @@ namespace flatbuffers { class FileWriter : public FileManager { public: - bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { - std::ofstream ofs(absolute_file_name, - std::ofstream::out); + bool SaveFile(const std::string &absolute_file_name, + const std::string &content) override { + std::ofstream ofs(absolute_file_name, std::ofstream::out); if (!ofs.is_open()) return false; ofs.write(content.c_str(), content.size()); - if (!ofs.bad()) { - file_names_.insert(absolute_file_name); - return true; - } - return false; + return !ofs.bad(); } - - bool ReadFile(const std::string &absolute_file_name, std::string *content) override { - (void) absolute_file_name; - (void) content; - return false; - } - - //std::set FileNames() { return file_names_; } }; } // namespace flatbuffers From f3b44eb64055d3611aea626fdec235ab4e4465f6 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 18 Mar 2023 17:00:48 +0000 Subject: [PATCH 11/14] Update --- src/file_name_saving_file_manager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index 26721cd1c8b..dee573c5e5f 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -27,7 +27,8 @@ class FileNameSavingFileManager : public FileManager { FileNameSavingFileManager(std::set file_names) : file_names_(file_names) {} - bool SaveFile(const std::string &absolute_file_name, const std::string) { + bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { + (void) content; auto pair = file_names_.insert(absolute_file_name); // pair.second indicates whether the insertion is // successful or not. From 98e645e51fd9e906a468ce86988dfe75b06ee42a Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 18 Mar 2023 17:08:58 +0000 Subject: [PATCH 12/14] Format bzl file --- BUILD.bazel | 2 +- src/BUILD.bazel | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index f31104450df..0ff3b234ef0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -47,8 +47,8 @@ filegroup( "include/flatbuffers/code_generators.h", "include/flatbuffers/default_allocator.h", "include/flatbuffers/detached_buffer.h", - "include/flatbuffers/flatbuffer_builder.h", "include/flatbuffers/file_manager.h", + "include/flatbuffers/flatbuffer_builder.h", "include/flatbuffers/flatbuffers.h", "include/flatbuffers/flex_flat_util.h", "include/flatbuffers/flexbuffers.h", diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 7ff6ce26e07..28d0868cede 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -69,6 +69,9 @@ cc_library( "bfbs_gen_nim.cpp", "bfbs_gen_nim.h", "bfbs_namer.h", + "file_binary_writer.cpp", + "file_name_saving_file_manager.cpp", + "file_writer.cpp", "flatc_main.cpp", "idl_gen_binary.cpp", "idl_gen_binary.h", @@ -104,9 +107,6 @@ cc_library( "idl_gen_ts.cpp", "idl_gen_ts.h", "idl_namer.h", - "file_writer.cpp", - "file_binary_writer.cpp", - "file_name_saving_file_manager.cpp", "namer.h", "util.cpp", ], From 4404ba60d3ea4b86d1928621953f288ae7632b6f Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Fri, 31 Mar 2023 03:55:13 +0000 Subject: [PATCH 13/14] Add LoadFile function --- include/flatbuffers/file_manager.h | 4 +++- src/file_binary_writer.cpp | 13 +++++++++++++ src/file_name_saving_file_manager.cpp | 11 +++++++++-- src/file_writer.cpp | 11 +++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/flatbuffers/file_manager.h b/include/flatbuffers/file_manager.h index 46ebf3dcb25..069df5b8842 100644 --- a/include/flatbuffers/file_manager.h +++ b/include/flatbuffers/file_manager.h @@ -20,6 +20,8 @@ #include #include +#include "flatbuffers/util.h" + namespace flatbuffers { // A File interface to write data to file by default or @@ -33,7 +35,7 @@ class FileManager { const std::string &content) = 0; virtual bool LoadFile(const std::string &absolute_file_name, - std::string *content) = 0; + std::string *buf) = 0; private: // Copying is not supported. diff --git a/src/file_binary_writer.cpp b/src/file_binary_writer.cpp index 618485aeb8c..69d83e77c9d 100644 --- a/src/file_binary_writer.cpp +++ b/src/file_binary_writer.cpp @@ -31,6 +31,19 @@ class FileBinaryWriter : public FileManager { ofs.write(content.c_str(), content.size()); return !ofs.bad(); } + + bool Loadfile(const std::string &absolute_file_name, std::string *output) { + if (DirExists(absolute_file_name.c_str())) return false; + std::ifstream ifs(absolute_file_name, std::ifstream::binary); + if (!ifs.is_open()) return false; + // The fastest way to read a file into a string. + ifs.seekg(0, std::ios::end); + auto size = ifs.tellg(); + (*output).resize(static_cast(size)); + ifs.seekg(0, std::ios::beg); + ifs.read(&(*output)[0], (*output).size()); + return !ifs.bad(); + } }; } // namespace flatbuffers diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index dee573c5e5f..b43c7b6eb09 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -27,14 +27,21 @@ class FileNameSavingFileManager : public FileManager { FileNameSavingFileManager(std::set file_names) : file_names_(file_names) {} - bool SaveFile(const std::string &absolute_file_name, const std::string &content) override { - (void) content; + bool SaveFile(const std::string &absolute_file_name, + const std::string &content) override { + (void)content; auto pair = file_names_.insert(absolute_file_name); // pair.second indicates whether the insertion is // successful or not. return pair.second; } + bool Loadfile(const std::string &absolute_file_name, std::string *content) { + (void)absolute_file_name; + (void)content; + return false; + } + private: std::set file_names_; }; diff --git a/src/file_writer.cpp b/src/file_writer.cpp index 793f075808b..bd34545b00d 100644 --- a/src/file_writer.cpp +++ b/src/file_writer.cpp @@ -31,6 +31,17 @@ class FileWriter : public FileManager { ofs.write(content.c_str(), content.size()); return !ofs.bad(); } + + bool Loadfile(const std::string &absolute_file_name, std::string *output) { + if (DirExists(absolute_file_name.c_str())) return false; + std::ifstream ifs(absolute_file_name, std::ifstream::in); + if (!ifs.is_open()) return false; + // This is slower, but works correctly on all platforms for text files. + std::ostringstream oss; + oss << ifs.rdbuf(); + *output = oss.str(); + return !ifs.bad(); + } }; } // namespace flatbuffers From 095ea2652c13b022ff7aae6337442e74d57df575 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Fri, 31 Mar 2023 03:57:54 +0000 Subject: [PATCH 14/14] Format --- src/file_name_saving_file_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/file_name_saving_file_manager.cpp b/src/file_name_saving_file_manager.cpp index b43c7b6eb09..fc4a4aa1ec7 100644 --- a/src/file_name_saving_file_manager.cpp +++ b/src/file_name_saving_file_manager.cpp @@ -37,8 +37,8 @@ class FileNameSavingFileManager : public FileManager { } bool Loadfile(const std::string &absolute_file_name, std::string *content) { - (void)absolute_file_name; - (void)content; + (void) absolute_file_name; + (void) content; return false; }