[PATCH rtems-tools v1 3/7] TraceWriter: Convert to C++

Ryan Long ryan.long at oarcorp.com
Tue Sep 21 16:44:59 UTC 2021


---
 tester/covoar/TraceWriterBase.h  |  3 ++-
 tester/covoar/TraceWriterQEMU.cc | 36 ++++++++++++------------------------
 tester/covoar/TraceWriterQEMU.h  |  2 +-
 3 files changed, 15 insertions(+), 26 deletions(-)

diff --git a/tester/covoar/TraceWriterBase.h b/tester/covoar/TraceWriterBase.h
index 9e80dbc..a18c387 100644
--- a/tester/covoar/TraceWriterBase.h
+++ b/tester/covoar/TraceWriterBase.h
@@ -8,6 +8,7 @@
 #define __TRACE_WRITER_BASE_H__
 
 #include <stdint.h>
+#include <string>
 #include "TraceReaderBase.h"
 
 namespace Trace {
@@ -41,7 +42,7 @@ namespace Trace {
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
      virtual bool writeFile(
-       const char* const          file,
+       const std::string&         file,
        Trace::TraceReaderBase    *log,
        bool                       verbose
      ) =  0;
diff --git a/tester/covoar/TraceWriterQEMU.cc b/tester/covoar/TraceWriterQEMU.cc
index 177b77f..577acab 100644
--- a/tester/covoar/TraceWriterQEMU.cc
+++ b/tester/covoar/TraceWriterQEMU.cc
@@ -38,6 +38,7 @@
 #include <cstdio>
 
 #include <iostream>
+#include <fstream>
 #include <iomanip>
 
 #include <rld-process.h>
@@ -47,18 +48,6 @@
 #include "CoverageMap.h"
 #include "qemu-traces.h"
 
-#if HAVE_STAT64
-#define STAT stat64
-#else
-#define STAT stat
-#endif
-
-#if HAVE_OPEN64
-#define OPEN fopen64
-#else
-#define OPEN fopen
-#endif
-
 namespace Trace {
 
   TraceWriterQEMU::TraceWriterQEMU():
@@ -71,14 +60,14 @@ namespace Trace {
   }
 
   bool TraceWriterQEMU::writeFile(
-    const char* const          file,
+    const std::string&         file,
     Trace::TraceReaderBase    *log,
     bool                       verbose
   )
   {
     struct trace_header header;
     int                 status;
-    FILE*               traceFile;
+    std::ofstream       traceFile;
     uint8_t             taken;
     uint8_t             notTaken;
 
@@ -89,15 +78,16 @@ namespace Trace {
     // Verify that the TraceList has a non-zero size.
     //
     if ( log->Trace.set.begin() == log->Trace.set.end() ){
-      fprintf( stderr, "ERROR: Empty TraceList\n" );
+      std::cerr << "ERROR: Empty TraceList" << std::endl;
       return false;
     }
 
     //
     // Open the trace file.
     //
-    traceFile = ::OPEN( file, "w" );
-    if (!traceFile) {
+    traceFile.open( file );
+
+    if ( !traceFile.is_open() ) {
       std::ostringstream what;
       std::cerr << "Unable to open " << file << std::endl;
       return false;
@@ -113,10 +103,10 @@ namespace Trace {
     header.big_endian = false;
     header.machine[0] = 0; // XXX ??
     header.machine[1] = 0; // XXX ??
-    status = ::fwrite( &header, sizeof(trace_header), 1, traceFile );
-    if (status != 1) {
+
+    traceFile.write( (char *) &header, sizeof( trace_header ) );
+    if ( traceFile.fail() ) {
       std::cerr << "Unable to write header to " << file << std::endl;
-      ::fclose( traceFile );
       return false;
     }
 
@@ -163,15 +153,13 @@ namespace Trace {
                   << std::dec << std::setfill(' ')
                   << std::endl;
 
-      status = ::fwrite( &entry, sizeof(entry), 1, traceFile );
-      if (status != 1) {
-        ::fclose( traceFile );
+      traceFile.write( (char *) &entry, sizeof( entry ) );
+      if ( traceFile.fail() ) {
         std::cerr << "Unable to write entry to " << file << std::endl;
         return false;
       }
     }
 
-    ::fclose( traceFile );
     return true;
   }
 }
diff --git a/tester/covoar/TraceWriterQEMU.h b/tester/covoar/TraceWriterQEMU.h
index 09c2ddf..aa89860 100644
--- a/tester/covoar/TraceWriterQEMU.h
+++ b/tester/covoar/TraceWriterQEMU.h
@@ -44,7 +44,7 @@ namespace Trace {
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
      bool writeFile(
-       const char* const          file,
+       const std::string&         file,
        Trace::TraceReaderBase    *log,
        bool                       verbose
      );
-- 
1.8.3.1



More information about the devel mailing list