diff --git a/src/OutputFileHandler.h b/src/OutputFileHandler.h new file mode 100644 index 0000000000000000000000000000000000000000..922f8d43802e22c60c5e5a44a2ce0c0763b9f663 --- /dev/null +++ b/src/OutputFileHandler.h @@ -0,0 +1,61 @@ +#ifndef OUTPUTFILEHANDLER_H +#define OUTPUTFILEHANDLER_H + +#include <cstdint> +#include <cstdio> +#include <string> + +class OutputFileHandler { + +public: + OutputFileHandler(const std::string filename_base_path, + const std::string filename_prefix) + : filename_base_path_(filename_base_path), + filename_prefix_(filename_prefix), + working_files_basepath_(filename_base_path_ + "/" + working_dir_) { + create_output_directory_maybe(working_files_basepath_); + } + virtual ~OutputFileHandler() {} + + void open_file(uint32_t index, + uint32_t run); // 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); + } + } + +private: + void create_output_directory_maybe(std::string &output_directory); + + // name of subdir where the files are stored before they are moved to the + // final destination + static constexpr std::string working_dir_{"in_progress"}; + // name of the journal file. Note: Filename prefix is added making the final + // filename + static constexpr std::string journal_file_{"index.journal"}; + + std::string filename_base_path_; + std::string filename_prefix_; + std::string working_files_basepath_; + uint32_t current_run_number_; + FILE *current_file_; + int32_t current_index_; +}; +#endif