Home | History | Annotate | Download | only in src
      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 #include "libshaderc_util/io.h"
     16 
     17 #include "libshaderc_util/universal_unistd.h"
     18 
     19 #include <errno.h>
     20 #include <cstring>
     21 #include <fstream>
     22 #include <iostream>
     23 
     24 namespace {
     25 
     26 // Outputs a descriptive message for errno_value to cerr.
     27 // This may be truncated to 1023 bytes on certain platforms.
     28 void OutputFileErrorMessage(int errno_value) {
     29 #ifdef _MSC_VER
     30   // If the error message is more than 1023 bytes it will be truncated.
     31   char buffer[1024];
     32   strerror_s(buffer, errno_value);
     33   std::cerr << ": " << buffer << std::endl;
     34 #else
     35   std::cerr << ": " << strerror(errno_value) << std::endl;
     36 #endif
     37 }
     38 
     39 }  // anonymous namespace
     40 
     41 namespace shaderc_util {
     42 
     43 bool IsAbsolutePath(const std::string& path) {
     44   if (path.empty()) return false;
     45   // Unix-like OS: /path/to/file
     46   if (path.front() == '/') return true;
     47   // Windows: \\server\user\file
     48   if (path.size() > 1 && path[0] == '\\' && path[1] == '\\') {
     49     return true;
     50   }
     51   // Windows: X:\path\to\file
     52   if (path.size() > 2 && ::isalpha(path[0]) && path[1] == ':' &&
     53       path[2] == '\\') {
     54     return true;
     55   }
     56   return false;
     57 }
     58 
     59 std::string GetBaseFileName(const std::string& file_path) {
     60   size_t loc_slash = file_path.find_last_of("/\\");
     61   std::string base_name =
     62       file_path.substr((loc_slash == std::string::npos ? -1 : loc_slash) + 1);
     63   if (base_name == ".." || base_name == ".") {
     64     base_name = "";
     65   }
     66   return base_name;
     67 }
     68 
     69 bool ReadFile(const std::string& input_file_name,
     70               std::vector<char>* input_data) {
     71   std::istream* stream = &std::cin;
     72   std::ifstream input_file;
     73   if (input_file_name != "-") {
     74     input_file.open(input_file_name, std::ios_base::binary);
     75     stream = &input_file;
     76     if (input_file.fail()) {
     77       std::cerr << "glslc: error: cannot open input file: '" << input_file_name
     78                 << "'";
     79       if (access(input_file_name.c_str(), R_OK) != 0) {
     80         OutputFileErrorMessage(errno);
     81         return false;
     82       }
     83       std::cerr << std::endl;
     84       return false;
     85     }
     86   }
     87   *input_data = std::vector<char>((std::istreambuf_iterator<char>(*stream)),
     88                                   std::istreambuf_iterator<char>());
     89   return true;
     90 }
     91 
     92 std::ostream* GetOutputStream(const string_piece& output_filename,
     93                               std::ofstream* file_stream, std::ostream* err) {
     94   std::ostream* stream = &std::cout;
     95   if (output_filename != "-") {
     96     file_stream->open(output_filename.str(), std::ios_base::binary);
     97     stream = file_stream;
     98     if (file_stream->fail()) {
     99       *err << "glslc: error: cannot open output file: '" << output_filename
    100                 << "'";
    101       if (access(output_filename.str().c_str(), W_OK) != 0) {
    102         OutputFileErrorMessage(errno);
    103         return nullptr;
    104       }
    105       std::cerr << std::endl;
    106       return nullptr;
    107     }
    108   }
    109   return stream;
    110 }
    111 
    112 bool WriteFile(std::ostream* stream, const string_piece& output_data) {
    113   if (output_data.size() > 0) {
    114     stream->write(output_data.data(), output_data.size());
    115     if (!stream->good()) {
    116       return false;
    117     }
    118   }
    119   stream->flush();
    120   return true;
    121 }
    122 
    123 }  // namespace shaderc_util
    124