DRY file writing code in tools.
authorLei Zhang <antiagainst@google.com>
Thu, 30 Jun 2016 19:44:36 +0000 (15:44 -0400)
committerLei Zhang <antiagainst@google.com>
Thu, 30 Jun 2016 19:56:47 +0000 (15:56 -0400)
tools/as/as.cpp
tools/dis/dis.cpp
tools/io.h
tools/opt/opt.cpp

index 2855c70..8b54e12 100644 (file)
@@ -142,17 +142,8 @@ int main(int argc, char** argv) {
     return error;
   }
 
-  const bool use_stdout = outFile[0] == '-' && outFile[1] == 0;
-  if (FILE* fp = (use_stdout ? stdout : fopen(outFile, "wb"))) {
-    size_t written =
-        fwrite(binary->code, sizeof(uint32_t), (size_t)binary->wordCount, fp);
-    if (binary->wordCount != written) {
-      fprintf(stderr, "error: could not write to file '%s'\n", outFile);
-      return 1;
-    }
-    if (!use_stdout) fclose(fp);
-  } else {
-    fprintf(stderr, "error: could not open file '%s'\n", outFile);
+  if (!WriteFile<uint32_t>(outFile, "wb", binary->code, binary->wordCount)) {
+    spvBinaryDestroy(binary);
     return 1;
   }
 
index bc79ab3..8db2840 100644 (file)
@@ -162,7 +162,7 @@ int main(int argc, char** argv) {
   // If the printing option is off, then save the text in memory, so
   // it can be emitted later in this function.
   const bool print_to_stdout = SPV_BINARY_TO_TEXT_OPTION_PRINT & options;
-  spv_text text;
+  spv_text text = nullptr;
   spv_text* textOrNull = print_to_stdout ? nullptr : &text;
   spv_diagnostic diagnostic = nullptr;
   spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
@@ -176,22 +176,13 @@ int main(int argc, char** argv) {
     return error;
   }
 
-  // Output the result.
   if (!print_to_stdout) {
-    if (FILE* fp = fopen(outFile, "w")) {
-      size_t written =
-          fwrite(text->str, sizeof(char), (size_t)text->length, fp);
-      if (text->length != written) {
-        spvTextDestroy(text);
-        fprintf(stderr, "error: Could not write to file '%s'\n", outFile);
-        return 1;
-      }
-    } else {
+    if (!WriteFile<char>(outFile, "w", text->str, text->length)) {
       spvTextDestroy(text);
-      fprintf(stderr, "error: Could not open file '%s'\n", outFile);
       return 1;
     }
   }
+  spvTextDestroy(text);
 
   return 0;
 }
index 79bce3c..a6f6c47 100644 (file)
@@ -63,4 +63,27 @@ bool ReadFile(const char* filename, const char* mode, std::vector<T>* data) {
   return true;
 }
 
+// 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) {
+  const bool use_stdout =
+      !filename || (filename[0] == '-' && filename[1] == '\0');
+  if (FILE* fp = (use_stdout ? stdout : fopen(filename, mode))) {
+    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;
+    }
+    if (!use_stdout) fclose(fp);
+  } else {
+    fprintf(stderr, "error: could not open file '%s'\n", filename);
+    return false;
+  }
+  return true;
+}
+
 #endif  // LIBSPIRV_TOOLS_IO_H_
index ed22751..e3246a1 100644 (file)
@@ -133,16 +133,7 @@ int main(int argc, char** argv) {
   std::vector<uint32_t> target;
   module->ToBinary(&target, /* skip_nop = */ true);
 
-  const bool use_stdout = out_file[0] == '-' && out_file[1] == 0;
-  if (FILE* fp = (use_stdout ? stdout : fopen(out_file, "wb"))) {
-    size_t written = fwrite(target.data(), sizeof(uint32_t), target.size(), fp);
-    if (target.size() != written) {
-      fprintf(stderr, "error: could not write to file '%s'\n", out_file);
-      return 1;
-    }
-    if (!use_stdout) fclose(fp);
-  } else {
-    fprintf(stderr, "error: could not open file '%s'\n", out_file);
+  if (!WriteFile<uint32_t>(out_file, "wb", target.data(), target.size())) {
     return 1;
   }