From 636ee0f1042b4b508562f915c06ed80e3338c012 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Tue, 9 Jul 2024 15:16:12 +1200 Subject: [PATCH] s/t/bamtools_filter.cpp: simplify GetScriptContents(). Closes: https://github.com/pezmaster31/bamtools/pull/238 --- src/toolkit/bamtools_filter.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/toolkit/bamtools_filter.cpp b/src/toolkit/bamtools_filter.cpp index bc4a3db..1809f67 100644 --- a/src/toolkit/bamtools_filter.cpp +++ b/src/toolkit/bamtools_filter.cpp @@ -540,22 +540,18 @@ const std::string FilterTool::FilterToolPrivate::GetScriptContents() // read in entire script contents char buffer[1024]; std::ostringstream docStream; - while (true) { - - // peek ahead, make sure there is data available - char ch = fgetc(inFile); - ungetc(ch, inFile); - if (feof(inFile)) { + while (!feof(inFile)) { + // read next block of data + const char* data = fgets(buffer, sizeof(buffer), inFile); + if (!data) { break; } - - // read next block of data - if (fgets(buffer, 1024, inFile) == 0) { + if (ferror(inFile)) { std::cerr << "bamtools filter ERROR: could not read script contents" << std::endl; - return std::string(); + return {}; } - docStream << buffer; + docStream << data; } // close script file