Path: blob/main/libshaderc_util/src/io_shaderc.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/io_shaderc.h"1516#include "libshaderc_util/universal_unistd.h"1718#if _WIN3219// Need _fileno from stdio.h20// Need _O_BINARY and _O_TEXT from fcntl.h21#include <fcntl.h>22#include <stdio.h>23#endif2425#include <errno.h>2627#include <cstdio>28#include <cstring>29#include <fstream>30#include <iostream>3132namespace {3334// Outputs a descriptive message for errno_value to cerr.35// This may be truncated to 1023 bytes on certain platforms.36void OutputFileErrorMessage(int errno_value) {37#ifdef _MSC_VER38// If the error message is more than 1023 bytes it will be truncated.39char buffer[1024];40strerror_s(buffer, errno_value);41std::cerr << ": " << buffer << std::endl;42#else43std::cerr << ": " << strerror(errno_value) << std::endl;44#endif45}4647} // anonymous namespace4849namespace shaderc_util {5051bool IsAbsolutePath(const std::string& path) {52if (path.empty()) return false;53// Unix-like OS: /path/to/file54if (path.front() == '/') return true;55// Windows: \\server\user\file56if (path.size() > 1 && path[0] == '\\' && path[1] == '\\') {57return true;58}59// Windows: X:\path\to\file60if (path.size() > 2 && ::isalpha(path[0]) && path[1] == ':' &&61path[2] == '\\') {62return true;63}64return false;65}6667std::string GetBaseFileName(const std::string& file_path) {68size_t loc_slash = file_path.find_last_of("/\\");69std::string base_name =70file_path.substr((loc_slash == std::string::npos ? -1 : loc_slash) + 1);71if (base_name == ".." || base_name == ".") {72base_name = "";73}74return base_name;75}7677bool ReadFile(const std::string& input_file_name,78std::vector<char>* input_data) {79std::istream* stream = &std::cin;80std::ifstream input_file;81if (input_file_name != "-") {82input_file.open(input_file_name, std::ios_base::binary);83stream = &input_file;84if (input_file.fail()) {85std::cerr << "glslc: error: cannot open input file: '" << input_file_name86<< "'";87if (access(input_file_name.c_str(), R_OK) != 0) {88OutputFileErrorMessage(errno);89return false;90}91std::cerr << std::endl;92return false;93}94}95*input_data = std::vector<char>((std::istreambuf_iterator<char>(*stream)),96std::istreambuf_iterator<char>());97return true;98}99100std::ostream* GetOutputStream(const string_piece& output_filename,101std::ofstream* file_stream, std::ostream* err) {102std::ostream* stream = &std::cout;103if (output_filename != "-") {104file_stream->open(output_filename.str(), std::ios_base::binary);105stream = file_stream;106if (file_stream->fail()) {107*err << "glslc: error: cannot open output file: '" << output_filename108<< "'";109if (access(output_filename.str().c_str(), W_OK) != 0) {110OutputFileErrorMessage(errno);111return nullptr;112}113std::cerr << std::endl;114return nullptr;115}116}117return stream;118}119120bool WriteFile(std::ostream* stream, const string_piece& output_data) {121if (output_data.size() > 0) {122stream->write(output_data.data(), output_data.size());123if (!stream->good()) {124return false;125}126}127stream->flush();128return true;129}130131void FlushAndSetBinaryModeOnStdout() {132std::fflush(stdout);133#if _WIN32134_setmode(_fileno(stdout), _O_BINARY);135#endif136}137138void FlushAndSetTextModeOnStdout() {139std::fflush(stdout);140#if _WIN32141_setmode(_fileno(stdout), _O_TEXT);142#endif143}144145} // namespace shaderc_util146147148