From 6995fe89380fdcc8f79d059bb7e4a45f637d6f64 Mon Sep 17 00:00:00 2001
From: Emilio <Emilio.Meschi@cern.ch>
Date: Mon, 24 Oct 2022 01:00:46 +0200
Subject: [PATCH]  remove all file actions and rationalize passing of arguments

---
 src/OutputByOrbit.cc | 79 +++-----------------------------------------
 1 file changed, 4 insertions(+), 75 deletions(-)

diff --git a/src/OutputByOrbit.cc b/src/OutputByOrbit.cc
index 76561e89..ca1b2b4e 100644
--- a/src/OutputByOrbit.cc
+++ b/src/OutputByOrbit.cc
@@ -7,60 +7,22 @@
 #include <string>
 #include <system_error>
 
+#include "config.h"
+#include "controls.h"
 #include "log.h"
 #include "slice.h"
 #include "tools.h"
 
-/* Defines the journal file. Note: Filename prefix is added making the final
- * filename */
-static const std::string journal_file{"index.journal"};
-
-/* Defined where are the files stored before they are moved to the final
- * destination */
-static const std::string working_dir{"in_progress"};
-
-static void create_output_directory(std::string &output_directory) {
-  struct stat sb;
-
-  /* check if path exists and is a directory */
-  if (stat(output_directory.c_str(), &sb) == 0) {
-    if (S_ISDIR(sb.st_mode)) {  // output directory already exists
-      return;
-    }
-    std::string err = "ERROR The output directory path '" + output_directory +
-                      "' exists, but the path is not a directory!";
-    LOG(ERROR) << err;
-    throw std::runtime_error(err);
-  }
-
-  if (!tools::filesystem::create_directories(output_directory)) {
-    std::string err =
-        tools::strerror("ERROR when creating the output directory '" + output_directory + "'");
-    LOG(ERROR) << err;
-    throw std::runtime_error(err);
-  }
-  LOG(TRACE) << "Created output directory: " << output_directory << "'.";
-}
-
-OutputByOrbitStream::OutputByOrbitStream(const std::string output_filename_base,
-                                         const std::string output_filename_prefix, ctrl &c,
-                                         config &conf_)
+OutputByOrbitStream::OutputByOrbitStream(ctrl &c, config &conf_)
     : tbb::filter(serial_in_order),
-      my_output_filename_base(output_filename_base),
-      my_output_filename_prefix(output_filename_prefix),
       totcounts(0),
       current_file_size(0),
       file_count(-1),
       control(c),
       current_run_number(0),
       conf(conf_),
-      fixedOrbitsPerFile(bool(conf.getNOrbitsPerFile())),
-      outFile() {
+      output_file_handler_(conf.getOutputFilenameBase(), conf.getOutputFilenamePrefix()) {
   LOG(TRACE) << "Created output filter at " << static_cast<void *>(this);
-
-  // Create the ouput directory
-  std::string output_directory = my_output_filename_base + "/" + working_dir;
-  create_output_directory(output_directory);
 }
 
 void *OutputByOrbitStream::OutputFixedOrbits(Slice &out) {
@@ -107,18 +69,6 @@ void *OutputByOrbitStream::operator()(void *item) {
   return NULL;
 }
 
-/*
- * Create a properly formatted file name
- * TODO: Change to C++
- */
-static std::string format_run_file_stem(std::string &filename_prefix, uint32_t run_number,
-                                        int32_t file_count) {
-  char run_order_stem[PATH_MAX];
-  snprintf(run_order_stem, sizeof(run_order_stem), "%s_%06d_%06d.dat", filename_prefix.c_str(),
-           run_number, file_count);
-  return std::string(run_order_stem);
-}
-
 void OutputByOrbitStream::close_and_move_file()  // Used for fixedNorbits per
                                                  // file option
 {
@@ -137,24 +87,3 @@ void OutputByOrbitStream::close_and_move_file()  // Used for fixedNorbits per
   }
   current_run_number = control.run_number;
 }
-
-void OutputByOrbitStream::open_file(uint32_t index_)  // Used for fixedNorbits per file option
-{
-  // Create a new file
-  std::string output_directory = my_output_filename_base + "/" + working_dir;
-  create_output_directory(output_directory);
-  std::string current_filename_a =
-      output_directory + "/" +
-      format_run_file_stem(my_output_filename_prefix, current_run_number, index_);
-  std::string filename =
-      output_directory + "/" +
-      format_run_file_stem(my_output_filename_prefix, control.run_number, index_);
-  LOG(INFO) << "opening file with index " << index_;
-  outFile.setFile(fopen(filename.c_str(), "w"));
-  outFile.setIndex(index_);
-  if (outFile.getFile() == NULL) {
-    std::string err = tools::strerror("ERROR when creating file '" + current_filename_a + "'");
-    LOG(ERROR) << err;
-    throw std::runtime_error(err);
-  }
-}
-- 
GitLab