2016-06-30 18:24:04 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-06-30 18:24:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-30 18:24:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-06-30 18:24:04 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef TOOLS_IO_H_
|
|
|
|
#define TOOLS_IO_H_
|
2016-06-30 18:24:04 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
2020-09-10 07:31:41 +00:00
|
|
|
#include <cstring>
|
2016-06-30 18:24:04 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-03-01 14:38:49 +00:00
|
|
|
#if defined(SPIRV_WINDOWS)
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#define SET_STDIN_TO_BINARY_MODE() _setmode(_fileno(stdin), O_BINARY);
|
|
|
|
#define SET_STDIN_TO_TEXT_MODE() _setmode(_fileno(stdin), O_TEXT);
|
2022-06-29 19:40:29 +00:00
|
|
|
#define SET_STDOUT_TO_BINARY_MODE() _setmode(_fileno(stdout), O_BINARY);
|
|
|
|
#define SET_STDOUT_TO_TEXT_MODE() _setmode(_fileno(stdout), O_TEXT);
|
|
|
|
#define SET_STDOUT_MODE(mode) _setmode(_fileno(stdout), mode);
|
2021-03-01 14:38:49 +00:00
|
|
|
#else
|
|
|
|
#define SET_STDIN_TO_BINARY_MODE()
|
|
|
|
#define SET_STDIN_TO_TEXT_MODE()
|
2022-06-29 19:40:29 +00:00
|
|
|
#define SET_STDOUT_TO_BINARY_MODE() 0
|
|
|
|
#define SET_STDOUT_TO_TEXT_MODE() 0
|
|
|
|
#define SET_STDOUT_MODE(mode)
|
2021-03-01 14:38:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Appends the contents of the |file| to |data|, assuming each element in the
|
|
|
|
// file is of type |T|.
|
2016-06-30 18:24:04 +00:00
|
|
|
template <typename T>
|
2021-03-01 14:38:49 +00:00
|
|
|
void ReadFile(FILE* file, std::vector<T>* data) {
|
|
|
|
if (file == nullptr) return;
|
|
|
|
|
2016-06-30 18:24:04 +00:00
|
|
|
const int buf_size = 1024;
|
2021-03-01 14:38:49 +00:00
|
|
|
T buf[buf_size];
|
|
|
|
while (size_t len = fread(buf, sizeof(T), buf_size, file)) {
|
|
|
|
data->insert(data->end(), buf, buf + len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if |file| has encountered an error opening the file or reading
|
|
|
|
// the file as a series of element of type |T|. If there was an error, writes an
|
|
|
|
// error message to standard error.
|
|
|
|
template <class T>
|
|
|
|
bool WasFileCorrectlyRead(FILE* file, const char* filename) {
|
|
|
|
if (file == nullptr) {
|
2016-06-30 18:24:04 +00:00
|
|
|
fprintf(stderr, "error: file does not exist '%s'\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-01 14:38:49 +00:00
|
|
|
|
|
|
|
if (ftell(file) == -1L) {
|
|
|
|
if (ferror(file)) {
|
|
|
|
fprintf(stderr, "error: error reading file '%s'\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sizeof(T) != 1 && (ftell(file) % sizeof(T))) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"error: file size should be a multiple of %zd; file '%s' corrupt\n",
|
|
|
|
sizeof(T), filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 18:24:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:38:49 +00:00
|
|
|
// Appends the contents of the file named |filename| to |data|, assuming
|
|
|
|
// each element in the file is of type |T|. The file is opened as a binary file
|
|
|
|
// If |filename| is nullptr or "-", reads from the standard input, but
|
|
|
|
// reopened as a binary file. If any error occurs, writes error messages to
|
|
|
|
// standard error and returns false.
|
|
|
|
template <typename T>
|
|
|
|
bool ReadBinaryFile(const char* filename, std::vector<T>* data) {
|
|
|
|
const bool use_file = filename && strcmp("-", filename);
|
|
|
|
FILE* fp = nullptr;
|
|
|
|
if (use_file) {
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
} else {
|
|
|
|
SET_STDIN_TO_BINARY_MODE();
|
|
|
|
fp = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadFile(fp, data);
|
|
|
|
bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
|
2021-08-16 13:44:22 +00:00
|
|
|
if (use_file && fp) fclose(fp);
|
2021-03-01 14:38:49 +00:00
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appends the contents of the file named |filename| to |data|, assuming
|
|
|
|
// each element in the file is of type |T|. The file is opened as a text file
|
|
|
|
// If |filename| is nullptr or "-", reads from the standard input, but
|
|
|
|
// reopened as a text file. If any error occurs, writes error messages to
|
|
|
|
// standard error and returns false.
|
|
|
|
template <typename T>
|
|
|
|
bool ReadTextFile(const char* filename, std::vector<T>* data) {
|
|
|
|
const bool use_file = filename && strcmp("-", filename);
|
|
|
|
FILE* fp = nullptr;
|
|
|
|
if (use_file) {
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
} else {
|
|
|
|
SET_STDIN_TO_TEXT_MODE();
|
|
|
|
fp = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadFile(fp, data);
|
|
|
|
bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
|
2021-08-16 13:44:22 +00:00
|
|
|
if (use_file && fp) fclose(fp);
|
2021-03-01 14:38:49 +00:00
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
2022-06-29 19:40:29 +00:00
|
|
|
namespace {
|
|
|
|
// A class to create and manage a file for outputting data.
|
|
|
|
class OutputFile {
|
|
|
|
public:
|
|
|
|
// Opens |filename| in the given mode. If |filename| is nullptr, the empty
|
|
|
|
// string or "-", stdout will be set to the given mode.
|
2023-03-03 15:53:08 +00:00
|
|
|
OutputFile(const char* filename, const char* mode) : old_mode_(0) {
|
2022-06-29 19:40:29 +00:00
|
|
|
const bool use_stdout =
|
|
|
|
!filename || (filename[0] == '-' && filename[1] == '\0');
|
|
|
|
if (use_stdout) {
|
|
|
|
if (strchr(mode, 'b')) {
|
|
|
|
old_mode_ = SET_STDOUT_TO_BINARY_MODE();
|
|
|
|
} else {
|
|
|
|
old_mode_ = SET_STDOUT_TO_TEXT_MODE();
|
|
|
|
}
|
|
|
|
fp_ = stdout;
|
|
|
|
} else {
|
|
|
|
fp_ = fopen(filename, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~OutputFile() {
|
|
|
|
if (fp_ == stdout) {
|
|
|
|
SET_STDOUT_MODE(old_mode_);
|
|
|
|
} else if (fp_ != nullptr) {
|
|
|
|
fclose(fp_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a file handle to the file.
|
|
|
|
FILE* GetFileHandle() const { return fp_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE* fp_;
|
|
|
|
int old_mode_;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2016-06-30 19:44:36 +00:00
|
|
|
// Writes the given |data| into the file named as |filename| using the given
|
|
|
|
// |mode|, assuming |data| is an array of |count| elements of type |T|. If
|
|
|
|
// |filename| is nullptr or "-", writes to standard output. If any error occurs,
|
|
|
|
// returns false and outputs error message to standard error.
|
|
|
|
template <typename T>
|
|
|
|
bool WriteFile(const char* filename, const char* mode, const T* data,
|
|
|
|
size_t count) {
|
2022-06-29 19:40:29 +00:00
|
|
|
OutputFile file(filename, mode);
|
|
|
|
FILE* fp = file.GetFileHandle();
|
|
|
|
if (fp == nullptr) {
|
2016-06-30 19:44:36 +00:00
|
|
|
fprintf(stderr, "error: could not open file '%s'\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-29 19:40:29 +00:00
|
|
|
|
|
|
|
size_t written = fwrite(data, sizeof(T), count, fp);
|
|
|
|
if (count != written) {
|
|
|
|
fprintf(stderr, "error: could not write to file '%s'\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:44:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // TOOLS_IO_H_
|