• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 Google Inc.
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 TOOLS_IO_H_
16 #define TOOLS_IO_H_
17 
18 #include <cstdint>
19 #include <cstdio>
20 #include <cstring>
21 #include <vector>
22 
23 #if defined(SPIRV_WINDOWS)
24 #include <fcntl.h>
25 #include <io.h>
26 
27 #define SET_STDIN_TO_BINARY_MODE() _setmode(_fileno(stdin), O_BINARY);
28 #define SET_STDIN_TO_TEXT_MODE() _setmode(_fileno(stdin), O_TEXT);
29 #else
30 #define SET_STDIN_TO_BINARY_MODE()
31 #define SET_STDIN_TO_TEXT_MODE()
32 #endif
33 
34 // Appends the contents of the |file| to |data|, assuming each element in the
35 // file is of type |T|.
36 template <typename T>
ReadFile(FILE * file,std::vector<T> * data)37 void ReadFile(FILE* file, std::vector<T>* data) {
38   if (file == nullptr) return;
39 
40   const int buf_size = 1024;
41   T buf[buf_size];
42   while (size_t len = fread(buf, sizeof(T), buf_size, file)) {
43     data->insert(data->end(), buf, buf + len);
44   }
45 }
46 
47 // Returns true if |file| has encountered an error opening the file or reading
48 // the file as a series of element of type |T|. If there was an error, writes an
49 // error message to standard error.
50 template <class T>
WasFileCorrectlyRead(FILE * file,const char * filename)51 bool WasFileCorrectlyRead(FILE* file, const char* filename) {
52   if (file == nullptr) {
53     fprintf(stderr, "error: file does not exist '%s'\n", filename);
54     return false;
55   }
56 
57   if (ftell(file) == -1L) {
58     if (ferror(file)) {
59       fprintf(stderr, "error: error reading file '%s'\n", filename);
60       return false;
61     }
62   } else {
63     if (sizeof(T) != 1 && (ftell(file) % sizeof(T))) {
64       fprintf(
65           stderr,
66           "error: file size should be a multiple of %zd; file '%s' corrupt\n",
67           sizeof(T), filename);
68       return false;
69     }
70   }
71   return true;
72 }
73 
74 // Appends the contents of the file named |filename| to |data|, assuming
75 // each element in the file is of type |T|. The file is opened as a binary file
76 // If |filename| is nullptr or "-", reads from the standard input, but
77 // reopened as a binary file. If any error occurs, writes error messages to
78 // standard error and returns false.
79 template <typename T>
ReadBinaryFile(const char * filename,std::vector<T> * data)80 bool ReadBinaryFile(const char* filename, std::vector<T>* data) {
81   const bool use_file = filename && strcmp("-", filename);
82   FILE* fp = nullptr;
83   if (use_file) {
84     fp = fopen(filename, "rb");
85   } else {
86     SET_STDIN_TO_BINARY_MODE();
87     fp = stdin;
88   }
89 
90   ReadFile(fp, data);
91   bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
92   if (use_file) fclose(fp);
93   return succeeded;
94 }
95 
96 // Appends the contents of the file named |filename| to |data|, assuming
97 // each element in the file is of type |T|. The file is opened as a text file
98 // If |filename| is nullptr or "-", reads from the standard input, but
99 // reopened as a text file. If any error occurs, writes error messages to
100 // standard error and returns false.
101 template <typename T>
ReadTextFile(const char * filename,std::vector<T> * data)102 bool ReadTextFile(const char* filename, std::vector<T>* data) {
103   const bool use_file = filename && strcmp("-", filename);
104   FILE* fp = nullptr;
105   if (use_file) {
106     fp = fopen(filename, "r");
107   } else {
108     SET_STDIN_TO_TEXT_MODE();
109     fp = stdin;
110   }
111 
112   ReadFile(fp, data);
113   bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
114   if (use_file) fclose(fp);
115   return succeeded;
116 }
117 
118 // Writes the given |data| into the file named as |filename| using the given
119 // |mode|, assuming |data| is an array of |count| elements of type |T|. If
120 // |filename| is nullptr or "-", writes to standard output. If any error occurs,
121 // returns false and outputs error message to standard error.
122 template <typename T>
WriteFile(const char * filename,const char * mode,const T * data,size_t count)123 bool WriteFile(const char* filename, const char* mode, const T* data,
124                size_t count) {
125   const bool use_stdout =
126       !filename || (filename[0] == '-' && filename[1] == '\0');
127   if (FILE* fp = (use_stdout ? stdout : fopen(filename, mode))) {
128     size_t written = fwrite(data, sizeof(T), count, fp);
129     if (count != written) {
130       fprintf(stderr, "error: could not write to file '%s'\n", filename);
131       if (!use_stdout) fclose(fp);
132       return false;
133     }
134     if (!use_stdout) fclose(fp);
135   } else {
136     fprintf(stderr, "error: could not open file '%s'\n", filename);
137     return false;
138   }
139   return true;
140 }
141 
142 #endif  // TOOLS_IO_H_
143