From 72a911c11bee8a7b267814d8fc1c918acde1ea57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Fri, 9 Nov 2018 18:00:52 -0500 Subject: [PATCH] RAII for the stream and sync states --- src/ogg.cc | 22 ++++++++++++++++++++++ src/opustags.cc | 7 ------- src/opustags.h | 28 ++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/ogg.cc b/src/ogg.cc index 2b67e4e..76e6cf5 100644 --- a/src/ogg.cc +++ b/src/ogg.cc @@ -2,6 +2,28 @@ #include +ot::ogg_reader::ogg_reader() +{ + ogg_sync_init(&sync); + memset(&stream, 0, sizeof(stream)); +} + +ot::ogg_reader::~ogg_reader() +{ + ogg_sync_clear(&sync); + ogg_stream_clear(&stream); +} + +ot::ogg_writer::ogg_writer() +{ + memset(&stream, 0, sizeof(stream)); +} + +ot::ogg_writer::~ogg_writer() +{ + ogg_stream_clear(&stream); +} + int ot::write_page(ogg_page *og, FILE *stream) { if((ssize_t) fwrite(og->header, 1, og->header_len, stream) < og->header_len) diff --git a/src/opustags.cc b/src/opustags.cc index fa0bde8..5e8baab 100644 --- a/src/opustags.cc +++ b/src/opustags.cc @@ -193,7 +193,6 @@ int main(int argc, char **argv){ } } ot::opus_tags tags; - ogg_sync_init(&reader.sync); char *buf; size_t len; const char *error = NULL; @@ -346,12 +345,6 @@ int main(int argc, char **argv){ else if(packet_count >= 2) // Read-only mode break; } - if(packet_count >= 0){ - ogg_stream_clear(&reader.stream); - if(writer.file) - ogg_stream_clear(&writer.stream); - } - ogg_sync_clear(&reader.sync); fclose(reader.file); if(writer.file) fclose(writer.file); diff --git a/src/opustags.h b/src/opustags.h index ae4edca..f949be2 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -42,13 +42,27 @@ private: */ struct ogg_reader { + /** + * Initialize the sync state and zero-initialize the stream. You'll need to initialize the + * stream yourself once you have the serialno. + */ + ogg_reader(); + /** + * Clear all the internal memory allocated by libogg for the sync and stream state. The + * page and the packet are owned by these states, so nothing to do with them. + * + * The input file is not closed. + */ + ~ogg_reader(); /** * The file is our source of binary data. It is not integrated to libogg, so we need to * handle it ourselves. * + * The file is not owned by the reader, you need to close it yourself when you're done. + * * In the future, we should use an std::istream or something. */ - FILE* file; + FILE* file = nullptr; /** * The sync layer gets binary data and yields a sequence of pages. * @@ -84,6 +98,14 @@ struct ogg_reader { }; struct ogg_writer { + /** + * Zeroes the stream state. You need to initialize it with the serialno. + */ + ogg_writer(); + /** + * Clears the stream state and any internal memory. Does not close the output file. + */ + ~ogg_writer(); /** * The stream state receives packets and generates pages. * @@ -94,8 +116,10 @@ struct ogg_writer { /** * Output file. It should be opened in binary mode. We use it to write whole pages, * represented as a block of data and a length. + * + * The file is not owner by the writer. You need to close it yourself. */ - FILE* file; + FILE* file = nullptr; }; int write_page(ogg_page *og, FILE *stream);