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