/* * Copyright 2017, The Android Open Source Project * * 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 FILE_UTILS_H #define FILE_UTILS_H #include #include #include #include namespace android { namespace spirit { template std::vector readFile(const char *filename) { std::ifstream ifs; std::filebuf *fb = ifs.rdbuf(); fb->open(filename, std::ios::in); if (!fb->is_open()) { std::cerr << "failed opening " << filename << std::endl; return std::vector(); } ifs.seekg(0, ifs.end); int length = ifs.tellg(); ifs.seekg(0, ifs.beg); std::vector ret(length / sizeof(T)); ifs.read((char *)ret.data(), length); fb->close(); return ret; } template std::vector readFile(const std::string &filename) { return readFile(filename.c_str()); } template void writeFile(const char *filename, const std::vector &data) { std::ofstream ofs(filename, std::ios::out); ofs.write(reinterpret_cast(data.data()), sizeof(T) * data.size()); } } // namespace spirit } // namespace android #endif // FILE_UTILS_H