From 3d198d3aa5c642e010764ce5b73e68133c27e95a Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Thu, 22 May 2025 16:36:10 +0200 Subject: [PATCH 1/2] [WIP] append to sequence file output --- include/seqan3/io/sequence_file/output.hpp | 6 ++++-- include/seqan3/io/sequence_file/output_options.hpp | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/seqan3/io/sequence_file/output.hpp b/include/seqan3/io/sequence_file/output.hpp index 1b07e5ccd6..ac2a389fce 100644 --- a/include/seqan3/io/sequence_file/output.hpp +++ b/include/seqan3/io/sequence_file/output.hpp @@ -146,12 +146,14 @@ class sequence_file_output * See the section on \link io_compression compression and decompression \endlink for more information. */ sequence_file_output(std::filesystem::path filename, - selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) : + selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}, + sequence_file_output_options output_options = sequence_file_output_options{}) : + options{std::move(output_options)}, primary_stream{new std::ofstream{}, stream_deleter_default} { primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size()); static_cast *>(primary_stream.get()) - ->open(filename, std::ios_base::out | std::ios::binary); + ->open(filename, (options.append ? std::ios_base::app : std::ios_base::out) | std::ios::binary); if (!primary_stream->good()) throw file_open_error{"Could not open file " + filename.string() + " for writing."}; diff --git a/include/seqan3/io/sequence_file/output_options.hpp b/include/seqan3/io/sequence_file/output_options.hpp index 054e97175d..ff08b97bf5 100644 --- a/include/seqan3/io/sequence_file/output_options.hpp +++ b/include/seqan3/io/sequence_file/output_options.hpp @@ -40,6 +40,9 @@ struct sequence_file_output_options //!\brief Complete header given for embl or genbank bool embl_genbank_complete_header = false; + + //!\brief Whether to open the file in append mode. + bool append = false; }; } // namespace seqan3 From 599f0f5b85832d7fd7e591341b34e02a4160df27 Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Thu, 22 May 2025 17:32:33 +0200 Subject: [PATCH 2/2] [WIP] std::ios_base::openmode looks better? --- include/seqan3/io/sequence_file/output.hpp | 10 +++++++--- include/seqan3/io/sequence_file/output_options.hpp | 3 --- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/seqan3/io/sequence_file/output.hpp b/include/seqan3/io/sequence_file/output.hpp index ac2a389fce..dce4a2aecd 100644 --- a/include/seqan3/io/sequence_file/output.hpp +++ b/include/seqan3/io/sequence_file/output.hpp @@ -133,12 +133,17 @@ class sequence_file_output /*!\brief Construct from filename. * \param[in] filename Path to the file you wish to open. * \param[in] fields_tag A seqan3::fields tag. [optional] + * \param[in] openmode The open mode for the file stream. [optional] * * \details * * In addition to the file name, you may specify a custom seqan3::fields type which may be easier than * defining all the template parameters. * + * You may also specify the open mode for the file stream. `std::ios_base::out` and `std::ios::binary` will always + * be added regardless of the given open mode. + * `std::ios_base::app` can be used to open the file in append mode. + * * ### Compression * * This constructor transparently applies a compression stream on top of the file stream in case @@ -147,13 +152,12 @@ class sequence_file_output */ sequence_file_output(std::filesystem::path filename, selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}, - sequence_file_output_options output_options = sequence_file_output_options{}) : - options{std::move(output_options)}, + std::ios_base::openmode openmode = std::ios_base::out | std::ios::binary) : primary_stream{new std::ofstream{}, stream_deleter_default} { primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size()); static_cast *>(primary_stream.get()) - ->open(filename, (options.append ? std::ios_base::app : std::ios_base::out) | std::ios::binary); + ->open(filename, openmode | std::ios_base::out | std::ios::binary); if (!primary_stream->good()) throw file_open_error{"Could not open file " + filename.string() + " for writing."}; diff --git a/include/seqan3/io/sequence_file/output_options.hpp b/include/seqan3/io/sequence_file/output_options.hpp index ff08b97bf5..054e97175d 100644 --- a/include/seqan3/io/sequence_file/output_options.hpp +++ b/include/seqan3/io/sequence_file/output_options.hpp @@ -40,9 +40,6 @@ struct sequence_file_output_options //!\brief Complete header given for embl or genbank bool embl_genbank_complete_header = false; - - //!\brief Whether to open the file in append mode. - bool append = false; }; } // namespace seqan3