#ifndef LIBSPIRV_SPIRV_TARGET_ENV_H_
#define LIBSPIRV_SPIRV_TARGET_ENV_H_
+#include "spirv-tools/libspirv.h"
+
// Parses s into *env and returns true if successful. If unparsable, returns
// false and sets *env to SPV_ENV_UNIVERSAL_1_0.
bool spvParseTargetEnv(const char* s, spv_target_env* env);
#include <cstring>
#include <vector>
+#include "source/spirv_target_env.h"
#include "spirv-tools/libspirv.h"
-#include "spirv_target_env.h"
+#include "tools/io.h"
void print_usage(char* argv0) {
printf(
}
std::vector<char> contents;
- const bool use_file = inFile && strcmp("-", inFile);
- if (FILE* fp = (use_file ? fopen(inFile, "r") : stdin)) {
- char buf[1024];
- while (size_t len = fread(buf, 1, sizeof(buf), fp))
- contents.insert(contents.end(), buf, buf + len);
- if (use_file) fclose(fp);
- } else {
- fprintf(stderr, "error: file does not exist '%s'\n", inFile);
- return 1;
- }
+ if (!ReadFile<char>(inFile, "r", &contents)) return 1;
spv_binary binary;
spv_diagnostic diagnostic = nullptr;
#include <vector>
#include "spirv-tools/libspirv.h"
+#include "tools/io.h"
static void print_usage(char* argv0) {
printf(
// Read the input binary.
std::vector<uint32_t> contents;
- {
- FILE* input = stdin;
- const bool use_file = inFile && strcmp("-", inFile);
- if (use_file) {
- input = fopen(inFile, "rb");
- if (!input) {
- auto msg =
- std::string("error: Can't open file ") + inFile + " for reading";
- perror(msg.c_str());
- return 1;
- }
- }
- uint32_t buf[1024];
- while (size_t len = fread(buf, sizeof(uint32_t), 1024, input)) {
- contents.insert(contents.end(), buf, buf + len);
- }
- if (use_file) fclose(input);
- }
+ if (!ReadFile<uint32_t>(inFile, "rb", &contents)) return 1;
// If printing to standard output, then spvBinaryToText should
// do the printing. In particular, colour printing on Windows is
--- /dev/null
+// Copyright (c) 2016 Google Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and/or associated documentation files (the
+// "Materials"), to deal in the Materials without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Materials, and to
+// permit persons to whom the Materials are furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Materials.
+//
+// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
+// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
+// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
+// https://www.khronos.org/registry/
+//
+// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+
+#ifndef LIBSPIRV_TOOLS_IO_H_
+#define LIBSPIRV_TOOLS_IO_H_
+
+#include <cstdint>
+#include <cstdio>
+#include <vector>
+
+// Appends the content from the file named as |filename| to |data|, assuming
+// each element in the file is of type |T|. The file is opened with the given
+// |mode|. If |filename| is nullptr or "-", reads from the standard input. If
+// any error occurs, writes error messages to standard error and returns false.
+template <typename T>
+bool ReadFile(const char* filename, const char* mode, std::vector<T>* data) {
+ const int buf_size = 1024;
+ const bool use_file = filename && strcmp("-", filename);
+ if (FILE* fp = (use_file ? fopen(filename, mode) : stdin)) {
+ T buf[buf_size];
+ while (size_t len = fread(buf, sizeof(T), buf_size, fp)) {
+ data->insert(data->end(), buf, buf + len);
+ }
+ if (ftell(fp) == -1L) {
+ if (ferror(fp)) {
+ fprintf(stderr, "error: error reading file '%s'\n", filename);
+ return false;
+ }
+ } else {
+ if (ftell(fp) % sizeof(T)) {
+ fprintf(stderr, "error: corrupted word found in file '%s'\n", filename);
+ return false;
+ }
+ }
+ if (use_file) fclose(fp);
+ } else {
+ fprintf(stderr, "error: file does not exist '%s'\n", filename);
+ return false;
+ }
+ return true;
+}
+
+#endif // LIBSPIRV_TOOLS_IO_H_
#include <iostream>
#include <vector>
-#include "opt/ir_loader.h"
-#include "opt/libspirv.hpp"
-#include "opt/pass_manager.h"
+#include "source/opt/ir_loader.h"
+#include "source/opt/libspirv.hpp"
+#include "source/opt/pass_manager.h"
+#include "tools/io.h"
using namespace spvtools;
}
std::vector<uint32_t> source;
- const bool use_file = in_file && strcmp("-", in_file);
- if (FILE* fp = (use_file ? fopen(in_file, "rb") : stdin)) {
- uint32_t buf[1024];
- while (size_t len = fread(buf, sizeof(uint32_t),
- sizeof(buf) / sizeof(uint32_t), fp)) {
- source.insert(source.end(), buf, buf + len);
- }
- if (ftell(fp) == -1L) {
- if (ferror(fp)) {
- fprintf(stderr, "error: error reading file '%s'\n", in_file);
- return 1;
- }
- } else {
- if (ftell(fp) % sizeof(uint32_t)) {
- fprintf(stderr, "error: corrupted word found in file '%s'\n", in_file);
- return 1;
- }
- }
- if (use_file) fclose(fp);
- } else {
- fprintf(stderr, "error: file does not exist '%s'\n", in_file);
- return 1;
- }
+ if (!ReadFile<uint32_t>(in_file, "rb", &source)) return 1;
// Let's do validation first.
spv_context context = spvContextCreate(target_env);
#include <cstring>
#include <vector>
+#include "source/spirv_target_env.h"
#include "spirv-tools/libspirv.h"
-#include "spirv_target_env.h"
+#include "tools/io.h"
void print_usage(char* argv0) {
printf(
}
std::vector<uint32_t> contents;
- const bool use_file = inFile && strcmp("-", inFile);
- if (FILE* fp = (use_file ? fopen(inFile, "rb") : stdin)) {
- uint32_t buf[1024];
- while (size_t len = fread(buf, sizeof(uint32_t),
- sizeof(buf) / sizeof(uint32_t), fp)) {
- contents.insert(contents.end(), buf, buf + len);
- }
- if (use_file) fclose(fp);
- } else {
- fprintf(stderr, "error: file does not exist '%s'\n", inFile);
- return 1;
- }
+ if (!ReadFile<uint32_t>(inFile, "rb", &contents)) return 1;
spv_const_binary_t binary = {contents.data(), contents.size()};