1 // Copyright 2015 The Shaderc Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef LIBSHADERC_UTIL_IO_H_ 16 #define LIBSHADERC_UTIL_IO_H_ 17 18 #include <string> 19 #include <vector> 20 21 #include "string_piece.h" 22 23 namespace shaderc_util { 24 25 // Returns true if the given path is an absolute path. 26 bool IsAbsolutePath(const std::string& path); 27 28 // A helper function to return the base file name from either absolute path or 29 // relative path representation of a file. It keeps the component from the last 30 // '/' or '\' to the end of the given string. If the component is '..' or '.', 31 // returns an empty string. If '/' or '\' is the last char of the given string, 32 // also returns an empty string. 33 // e.g.: dir_a/dir_b/file_c.vert => file_c.vert 34 // dir_a/dir_b/.. => <empty string> 35 // dir_a/dir_b/. => <empty string> 36 // dir_a/dirb/c/ => <empty string> 37 // Note that this method doesn't check whether the given path is a valid one or 38 // not. 39 std::string GetBaseFileName(const std::string& file_path); 40 41 // Reads all of the characters in a given file into input_data. Outputs an 42 // error message to std::cerr if the file could not be read and returns false if 43 // there was an error. If the input_file is "-", then input is read from 44 // std::cin. 45 bool ReadFile(const std::string& input_file_name, 46 std::vector<char>* input_data); 47 48 // Returns and initializes the file_stream parameter if the output_filename 49 // refers to a file, or returns &std::cout if the output_filename is "-". 50 // Returns nullptr and emits an error message to err if the file could 51 // not be opened for writing. If the output refers to a file, and the open 52 // failed for writing, file_stream is left with its fail_bit set. 53 std::ostream* GetOutputStream(const string_piece& output_filename, 54 std::ofstream* file_stream, std::ostream* err); 55 56 // Writes output_data to a file, overwriting if it exists. If output_file_name 57 // is "-", writes to std::cout. 58 bool WriteFile(std::ostream* output_stream, const string_piece& output_data); 59 60 } // namespace shaderc_util 61 62 #endif // LIBSHADERC_UTIL_IO_H_ 63