-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add a FileWriter interface #7821
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
91cb53f
Add a FileWriter interface
Kn99HN 512c27d
Change interface
Kn99HN 70eb626
Provide 2 impl for File interface: FileManager & FileNameManager
Kn99HN 44e718d
Update
Kn99HN 845d1d8
update
Kn99HN 410b331
Merge remote-tracking branch 'upstream/master' into file-interface
Kn99HN c34f70f
Update
Kn99HN a728f0e
Add file_writer file
Kn99HN 3969d41
Update
Kn99HN 61cbbc5
Format files
Kn99HN ea0866f
Update based on review
Kn99HN f3b44eb
Update
Kn99HN 8731c4e
Merge remote-tracking branch 'upstream/master' into file-interface
Kn99HN 98e645e
Format bzl file
Kn99HN 4404ba6
Add LoadFile function
Kn99HN 095ea26
Format
Kn99HN 222c29e
Merge remote-tracking branch 'upstream/master' into file-interface
Kn99HN df786b2
Merge branch 'master' into file-interface
dbaileychess 3c1567a
Merge branch 'master' into file-interface
dbaileychess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| */ | ||
|
|
||
| #ifndef FLATBUFFERS_FILE_MANAGER_H_ | ||
| #define FLATBUFFERS_FILE_MANAGER_H_ | ||
|
|
||
| #include <set> | ||
| #include <string> | ||
|
|
||
| #include "flatbuffers/util.h" | ||
|
|
||
| namespace flatbuffers { | ||
|
|
||
| // A File interface to write data to file by default or | ||
| // 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 LoadFile(const std::string &absolute_file_name, | ||
| std::string *buf) = 0; | ||
|
|
||
| private: | ||
| // Copying is not supported. | ||
| FileManager(const FileManager &) = delete; | ||
| FileManager &operator=(const FileManager &) = delete; | ||
| }; | ||
|
|
||
| } // namespace flatbuffers | ||
|
|
||
| #endif // FLATBUFFERS_FILE_MANAGER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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 <fstream> | ||
| #include <set> | ||
| #include <string> | ||
|
|
||
| #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()); | ||
| 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_t>(size)); | ||
| ifs.seekg(0, std::ios::beg); | ||
| ifs.read(&(*output)[0], (*output).size()); | ||
| return !ifs.bad(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace flatbuffers | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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 <fstream> | ||
| #include <set> | ||
| #include <string> | ||
|
|
||
| #include "flatbuffers/file_manager.h" | ||
|
|
||
| namespace flatbuffers { | ||
|
|
||
| class FileNameSavingFileManager : public FileManager { | ||
| public: | ||
| FileNameSavingFileManager(std::set<std::string> file_names) | ||
| : file_names_(file_names) {} | ||
|
|
||
| bool SaveFile(const std::string &absolute_file_name, | ||
| const std::string &content) override { | ||
| (void)content; | ||
Kn99HN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto pair = file_names_.insert(absolute_file_name); | ||
| // pair.second indicates whether the insertion is | ||
| // successful or not. | ||
| return pair.second; | ||
Kn99HN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| bool Loadfile(const std::string &absolute_file_name, std::string *content) { | ||
| (void) absolute_file_name; | ||
Kn99HN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (void) content; | ||
| return false; | ||
| } | ||
|
|
||
| private: | ||
| std::set<std::string> file_names_; | ||
| }; | ||
|
|
||
| } // namespace flatbuffers | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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 <fstream> | ||
| #include <set> | ||
| #include <string> | ||
|
|
||
| #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()); | ||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.