Skip to content

Commit 4404ba6

Browse files
committed
Add LoadFile function
1 parent 98e645e commit 4404ba6

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

include/flatbuffers/file_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <set>
2121
#include <string>
2222

23+
#include "flatbuffers/util.h"
24+
2325
namespace flatbuffers {
2426

2527
// A File interface to write data to file by default or
@@ -33,7 +35,7 @@ class FileManager {
3335
const std::string &content) = 0;
3436

3537
virtual bool LoadFile(const std::string &absolute_file_name,
36-
std::string *content) = 0;
38+
std::string *buf) = 0;
3739

3840
private:
3941
// Copying is not supported.

src/file_binary_writer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ class FileBinaryWriter : public FileManager {
3131
ofs.write(content.c_str(), content.size());
3232
return !ofs.bad();
3333
}
34+
35+
bool Loadfile(const std::string &absolute_file_name, std::string *output) {
36+
if (DirExists(absolute_file_name.c_str())) return false;
37+
std::ifstream ifs(absolute_file_name, std::ifstream::binary);
38+
if (!ifs.is_open()) return false;
39+
// The fastest way to read a file into a string.
40+
ifs.seekg(0, std::ios::end);
41+
auto size = ifs.tellg();
42+
(*output).resize(static_cast<size_t>(size));
43+
ifs.seekg(0, std::ios::beg);
44+
ifs.read(&(*output)[0], (*output).size());
45+
return !ifs.bad();
46+
}
3447
};
3548

3649
} // namespace flatbuffers

src/file_name_saving_file_manager.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ class FileNameSavingFileManager : public FileManager {
2727
FileNameSavingFileManager(std::set<std::string> file_names)
2828
: file_names_(file_names) {}
2929

30-
bool SaveFile(const std::string &absolute_file_name, const std::string &content) override {
31-
(void) content;
30+
bool SaveFile(const std::string &absolute_file_name,
31+
const std::string &content) override {
32+
(void)content;
3233
auto pair = file_names_.insert(absolute_file_name);
3334
// pair.second indicates whether the insertion is
3435
// successful or not.
3536
return pair.second;
3637
}
3738

39+
bool Loadfile(const std::string &absolute_file_name, std::string *content) {
40+
(void)absolute_file_name;
41+
(void)content;
42+
return false;
43+
}
44+
3845
private:
3946
std::set<std::string> file_names_;
4047
};

src/file_writer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ class FileWriter : public FileManager {
3131
ofs.write(content.c_str(), content.size());
3232
return !ofs.bad();
3333
}
34+
35+
bool Loadfile(const std::string &absolute_file_name, std::string *output) {
36+
if (DirExists(absolute_file_name.c_str())) return false;
37+
std::ifstream ifs(absolute_file_name, std::ifstream::in);
38+
if (!ifs.is_open()) return false;
39+
// This is slower, but works correctly on all platforms for text files.
40+
std::ostringstream oss;
41+
oss << ifs.rdbuf();
42+
*output = oss.str();
43+
return !ifs.bad();
44+
}
3445
};
3546

3647
} // namespace flatbuffers

0 commit comments

Comments
 (0)