RAII for the stream and sync states

This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-09 18:00:52 -05:00
parent 07af78519b
commit 72a911c11b
3 changed files with 48 additions and 9 deletions

View File

@ -2,6 +2,28 @@
#include <cstdio>
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)

View File

@ -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);

View File

@ -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);