From 7fd27e737ae1aae6ba2152a64eadc8a463d93cea Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 30 Jun 2016 14:24:04 -0400 Subject: [PATCH] DRY file reading code in tools. --- source/spirv_target_env.h | 2 ++ tools/as/as.cpp | 14 +++------- tools/dis/dis.cpp | 20 ++------------ tools/io.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++ tools/opt/opt.cpp | 31 ++++------------------ tools/val/val.cpp | 16 +++--------- 6 files changed, 81 insertions(+), 68 deletions(-) create mode 100644 tools/io.h diff --git a/source/spirv_target_env.h b/source/spirv_target_env.h index 3b0975d..e66c356 100644 --- a/source/spirv_target_env.h +++ b/source/spirv_target_env.h @@ -27,6 +27,8 @@ #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); diff --git a/tools/as/as.cpp b/tools/as/as.cpp index c894cfc..2855c70 100644 --- a/tools/as/as.cpp +++ b/tools/as/as.cpp @@ -28,8 +28,9 @@ #include #include +#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( @@ -127,16 +128,7 @@ int main(int argc, char** argv) { } std::vector 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(inFile, "r", &contents)) return 1; spv_binary binary; spv_diagnostic diagnostic = nullptr; diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp index 103d9ba..bc79ab3 100644 --- a/tools/dis/dis.cpp +++ b/tools/dis/dis.cpp @@ -30,6 +30,7 @@ #include #include "spirv-tools/libspirv.h" +#include "tools/io.h" static void print_usage(char* argv0) { printf( @@ -151,24 +152,7 @@ int main(int argc, char** argv) { // Read the input binary. std::vector 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(inFile, "rb", &contents)) return 1; // If printing to standard output, then spvBinaryToText should // do the printing. In particular, colour printing on Windows is diff --git a/tools/io.h b/tools/io.h new file mode 100644 index 0000000..79bce3c --- /dev/null +++ b/tools/io.h @@ -0,0 +1,66 @@ +// 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 +#include +#include + +// 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 +bool ReadFile(const char* filename, const char* mode, std::vector* 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_ diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index d4d4b87..ed22751 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -28,9 +28,10 @@ #include #include -#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; @@ -110,29 +111,7 @@ int main(int argc, char** argv) { } std::vector 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(in_file, "rb", &source)) return 1; // Let's do validation first. spv_context context = spvContextCreate(target_env); diff --git a/tools/val/val.cpp b/tools/val/val.cpp index 9a636af..c74fa10 100644 --- a/tools/val/val.cpp +++ b/tools/val/val.cpp @@ -29,8 +29,9 @@ #include #include +#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( @@ -103,18 +104,7 @@ int main(int argc, char** argv) { } std::vector 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(inFile, "rb", &contents)) return 1; spv_const_binary_t binary = {contents.data(), contents.size()}; -- 2.7.4