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 FileWriter : 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::out); 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::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 } 45 }; 46 47 } // namespace flatbuffers 48