[PATCH v1] Explanations.cc: Convert to c++ file handling

Ryan Long ryan.long at oarcorp.com
Fri Jun 4 18:17:41 UTC 2021


---
 tester/covoar/Explanations.cc | 45 ++++++++++++++++---------------------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/tester/covoar/Explanations.cc b/tester/covoar/Explanations.cc
index d94cd2e..1449fb2 100644
--- a/tester/covoar/Explanations.cc
+++ b/tester/covoar/Explanations.cc
@@ -10,6 +10,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <fstream>
+
 #include <rld.h>
 
 #include "Explanations.h"
@@ -30,15 +32,14 @@ namespace Coverage {
   )
   {
     #define MAX_LINE_LENGTH 512
-    FILE       *explain;
-    char        *cStatus;
-    Explanation  e;
-    int          line = 1;
+    std::ifstream  explain;
+    Explanation    e;
+    int            line = 1;
 
     if (!explanations)
       return;
 
-    explain = ::fopen( explanations, "r" );
+    explain.open( explanations );
     if (!explain) {
       std::ostringstream what;
       what << "Unable to open " << explanations;
@@ -50,9 +51,9 @@ namespace Coverage {
       // skip blank lines between
       do {
         inputBuffer[0] = '\0';
-        cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
-        if (cStatus == NULL) {
-          goto done;
+        explain.getline( inputBuffer, MAX_LINE_LENGTH );
+        if (explain.fail()) {
+          return;
         }
         inputBuffer[ strlen(inputBuffer) - 1] = '\0';
         line++;
@@ -64,7 +65,6 @@ namespace Coverage {
         what << "line " << line
              << "contains a duplicate explanation ("
              << inputBuffer << ")";
-        fclose( explain );
         throw rld::error( what, "Explanations::load" );
       }
 
@@ -73,12 +73,11 @@ namespace Coverage {
       e.found = false;
 
       // Get the classification
-      cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
-      if (cStatus == NULL) {
+      explain.getline( inputBuffer, MAX_LINE_LENGTH );
+      if (explain.fail()) {
         std::ostringstream what;
         what << "line " << line
              << "out of sync at the classification";
-        fclose( explain );
         throw rld::error( what, "Explanations::load" );
       }
       inputBuffer[ strlen(inputBuffer) - 1] = '\0';
@@ -87,13 +86,12 @@ namespace Coverage {
 
       // Get the explanation
       while (1) {
-        cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
+        explain.getline( inputBuffer, MAX_LINE_LENGTH );
         // fprintf( stderr, "%d - %s\n", line, inputBuffer );
-        if (cStatus == NULL) {
+        if (explain.fail()) {
           std::ostringstream what;
           what << "line " << line
                << "out of sync at the explanation";
-          fclose( explain );
           throw rld::error( what, "Explanations::load" );
         }
         inputBuffer[ strlen(inputBuffer) - 1] = '\0';
@@ -110,10 +108,6 @@ namespace Coverage {
       // Add this to the set of Explanations
       set[ e.startingPoint ] = e;
     }
-done:
-    ;
-
-    fclose( explain );
 
     #undef MAX_LINE_LENGTH
   }
@@ -137,14 +131,14 @@ done:
     const char* const fileName
   )
   {
-    FILE* notFoundFile;
+    std::ofstream notFoundFile;
     bool  notFoundOccurred = false;
 
     if (!fileName)
       return;
 
-    notFoundFile = fopen( fileName, "w" );
-    if (notFoundFile == nullptr) {
+    notFoundFile.open( fileName );
+    if (!notFoundFile) {
       std::ostringstream what;
       what << "Unable to open " << fileName
            << " out of sync at the explanation";
@@ -159,14 +153,9 @@ done:
 
       if (!e.found) {
         notFoundOccurred = true;
-        fprintf(
-          notFoundFile,
-          "%s\n",
-          e.startingPoint.c_str()
-        );
+        notFoundFile << e.startingPoint << std::endl;
       }
     }
-    fclose( notFoundFile );
 
     if (!notFoundOccurred) {
       if (!unlink( fileName )) {
-- 
1.8.3.1



More information about the devel mailing list