From ac4deac1873ca1bb570ffd479ed2cc1010762f89 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 28 Feb 2013 16:45:11 -0500 Subject: Call QUtil::safe_fopen in place of fopen fopen was previuosly called wrapped by QUtil::fopen_wrapper, but QUtil::safe_fopen does this itself, which is less cumbersome. --- libtests/aes.cc | 17 +++-------------- libtests/flate.cc | 23 ++++++----------------- libtests/lzw.cc | 6 ++---- libtests/md5.cc | 42 ++++++++++++++++++++---------------------- libtests/png_filter.cc | 17 +++-------------- libtests/qtest/qutil/qutil.out | 2 +- libtests/qutil.cc | 4 +--- libtests/rc4.cc | 16 +++------------- 8 files changed, 39 insertions(+), 88 deletions(-) (limited to 'libtests') diff --git a/libtests/aes.cc b/libtests/aes.cc index 4b11b5f9..b5a63d9a 100644 --- a/libtests/aes.cc +++ b/libtests/aes.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -89,20 +90,8 @@ int main(int argc, char* argv[]) unsigned int hexkeylen = strlen(hexkey); unsigned int keylen = hexkeylen / 2; - FILE* infile = fopen(infilename, "rb"); // XXXX - if (infile == 0) - { - std::cerr << "can't open " << infilename << std::endl; - exit(2); - } - - FILE* outfile = fopen(outfilename, "wb"); // XXXX - if (outfile == 0) - { - std::cerr << "can't open " << outfilename << std::endl; - exit(2); - } - + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); unsigned char* key = new unsigned char[keylen]; for (unsigned int i = 0; i < strlen(hexkey); i += 2) { diff --git a/libtests/flate.cc b/libtests/flate.cc index d5f77162..a6d4eeb6 100644 --- a/libtests/flate.cc +++ b/libtests/flate.cc @@ -2,33 +2,22 @@ #include #include #include +#include #include #include #include #include -FILE* safe_fopen(char const* filename, char const* mode) -{ - FILE* result = fopen(filename, mode); // XXXX - if (result == 0) - { - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX - << std::endl; - exit(2); - } - return result; -} - void run(char const* filename) { std::string n1 = std::string(filename) + ".1"; std::string n2 = std::string(filename) + ".2"; std::string n3 = std::string(filename) + ".3"; - FILE* o1 = safe_fopen(n1.c_str(), "wb"); - FILE* o2 = safe_fopen(n2.c_str(), "wb"); - FILE* o3 = safe_fopen(n3.c_str(), "wb"); + FILE* o1 = QUtil::safe_fopen(n1.c_str(), "wb"); + FILE* o2 = QUtil::safe_fopen(n2.c_str(), "wb"); + FILE* o3 = QUtil::safe_fopen(n3.c_str(), "wb"); Pipeline* out1 = new Pl_StdioFile("o1", o1); Pipeline* out2 = new Pl_StdioFile("o2", o2); Pipeline* out3 = new Pl_StdioFile("o3", o3); @@ -46,7 +35,7 @@ void run(char const* filename) Pipeline* inf3 = new Pl_Flate("inf3", count3, Pl_Flate::a_inflate); Pipeline* def3 = new Pl_Flate("def3", inf3, Pl_Flate::a_deflate); - FILE* in1 = safe_fopen(filename, "rb"); + FILE* in1 = QUtil::safe_fopen(filename, "rb"); unsigned char buf[1024]; size_t len; while ((len = fread(buf, 1, sizeof(buf), in1)) > 0) @@ -75,7 +64,7 @@ void run(char const* filename) fclose(o3); // Now read the compressed data and write to the output uncompress pipeline - FILE* in2 = safe_fopen(n1.c_str(), "rb"); + FILE* in2 = QUtil::safe_fopen(n1.c_str(), "rb"); while ((len = fread(buf, 1, sizeof(buf), in2)) > 0) { inf2->write(buf, len); diff --git a/libtests/lzw.cc b/libtests/lzw.cc index 1007ae24..003c9217 100644 --- a/libtests/lzw.cc +++ b/libtests/lzw.cc @@ -26,10 +26,8 @@ int main(int argc, char* argv[]) char* infilename = argv[1]; char* outfilename = argv[2]; - FILE* infile = QUtil::fopen_wrapper("open input file", - fopen(infilename, "rb")); // XXXX - FILE* outfile = QUtil::fopen_wrapper("open output file", - fopen(outfilename, "wb")); // XXXX + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); Pl_StdioFile out("output", outfile); Pl_LZWDecoder decode("decode", &out, early_code_change); diff --git a/libtests/md5.cc b/libtests/md5.cc index 71d83f31..9c9c8cf1 100644 --- a/libtests/md5.cc +++ b/libtests/md5.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -46,28 +47,25 @@ int main(int, char*[]) Pl_MD5 p("MD5", &d); for (int i = 0; i < 2; ++i) { - FILE* f = fopen("md5.in", "rb"); // XXXX - if (f) - { - // buffer size < size of md5.in - unsigned char buf[50]; - bool done = false; - while (! done) - { - size_t len = fread(buf, 1, sizeof(buf), f); - if (len <= 0) - { - done = true; - } - else - { - p.write(buf, len); - } - } - fclose(f); - p.finish(); - std::cout << p.getHexDigest() << std::endl; - } + FILE* f = QUtil::safe_fopen("md5.in", "rb"); + // buffer size < size of md5.in + unsigned char buf[50]; + bool done = false; + while (! done) + { + size_t len = fread(buf, 1, sizeof(buf), f); + if (len <= 0) + { + done = true; + } + else + { + p.write(buf, len); + } + } + fclose(f); + p.finish(); + std::cout << p.getHexDigest() << std::endl; } return 0; diff --git a/libtests/png_filter.cc b/libtests/png_filter.cc index 68f50aee..c3d4f646 100644 --- a/libtests/png_filter.cc +++ b/libtests/png_filter.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -7,23 +8,11 @@ #include #include -FILE* safe_fopen(char const* filename, char const* mode) -{ - FILE* result = fopen(filename, mode); // XXXX - if (result == 0) - { - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX - << std::endl; - exit(2); - } - return result; -} - void run(char const* filename, bool encode, unsigned int columns) { // Decode the file - FILE* in = safe_fopen(filename, "rb"); - FILE* o1 = safe_fopen("out", "wb"); + FILE* in = QUtil::safe_fopen(filename, "rb"); + FILE* o1 = QUtil::safe_fopen("out", "wb"); Pipeline* out = new Pl_StdioFile("out", o1); Pipeline* pl = new Pl_PNGFilter( "png", out, diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index ebbf97b5..0e996dec 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -19,7 +19,7 @@ before remove exception: remove file: No such file or directory ---- before fopen -exception: fopen file: No such file or directory +exception: open /this/file/does/not/exist: No such file or directory ---- IN_TESTSUITE: 1: 1 HAGOOGAMAGOOGLE: 0 diff --git a/libtests/qutil.cc b/libtests/qutil.cc index dedd73d1..5e4dd196 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -62,12 +62,10 @@ void os_wrapper_test() void fopen_wrapper_test() { - FILE* f = 0; try { std::cout << "before fopen" << std::endl; - f = QUtil::fopen_wrapper("fopen file", - fopen("/this/file/does/not/exist", "r")); // XXXX + FILE* f = QUtil::safe_fopen("/this/file/does/not/exist", "r"); std::cout << "after fopen" << std::endl; (void) fclose(f); } diff --git a/libtests/rc4.cc b/libtests/rc4.cc index e71e0d01..7a4d8bd9 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -22,13 +23,7 @@ int main(int argc, char* argv[]) unsigned char* key = new unsigned char[keylen + 1]; key[keylen] = '\0'; - FILE* infile = fopen(infilename, "rb"); // XXXX - if (infile == 0) - { - std::cerr << "can't open " << infilename << std::endl; - exit(2); - } - + FILE* infile = QUtil::safe_fopen(infilename, "rb"); for (unsigned int i = 0; i < strlen(hexkey); i += 2) { char t[3]; @@ -40,12 +35,7 @@ int main(int argc, char* argv[]) key[i/2] = static_cast(val); } - FILE* outfile = fopen(outfilename, "wb"); // XXXX - if (outfile == 0) - { - std::cerr << "can't open " << outfilename << std::endl; - exit(2); - } + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); // Use a small buffer size (64) for testing Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64); -- cgit v1.2.3-54-g00ecf