• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fstream>
18 #include <set>
19 #include <string>
20 
21 #include "flatbuffers/file_manager.h"
22 
23 namespace flatbuffers {
24 
25 class FileBinaryWriter : public FileManager {
26  public:
SaveFile(const std::string & absolute_file_name,const std::string & content)27   bool SaveFile(const std::string &absolute_file_name,
28                 const std::string &content) override {
29     std::ofstream ofs(absolute_file_name, std::ofstream::binary);
30     if (!ofs.is_open()) return false;
31     ofs.write(content.c_str(), content.size());
32     return !ofs.bad();
33   }
34 
Loadfile(const std::string & absolute_file_name,std::string * output)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   }
47 };
48 
49 }  // namespace flatbuffers
50