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