DRY file writing code in tools.

This commit is contained in:
Lei Zhang 2016-06-30 15:44:36 -04:00
parent 7fd27e737a
commit 23a3c2f762
4 changed files with 29 additions and 33 deletions

View 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;
}

View 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;
}

View 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_

View 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;
}