fs/io/OutputStream: use C++ exceptions in Write()
This commit is contained in:
@@ -43,10 +43,7 @@ Copy(OutputStream &dest, int src)
|
||||
if (nbytes == 0)
|
||||
return true;
|
||||
|
||||
if (!dest.Write(buffer, nbytes, error)) {
|
||||
fprintf(stderr, "%s\n", error.GetMessage());
|
||||
return false;
|
||||
}
|
||||
dest.Write(buffer, nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -63,60 +63,58 @@ int main(int argc, char **argv)
|
||||
ConfigBlock block;
|
||||
block.AddBlockParam("quality", "5.0", -1);
|
||||
|
||||
Error error;
|
||||
const auto encoder = encoder_init(*plugin, block, error);
|
||||
if (encoder == NULL) {
|
||||
LogError(error, "Failed to initialize encoder");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* open the encoder */
|
||||
|
||||
AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
||||
if (argc > 2) {
|
||||
if (!audio_format_parse(audio_format, argv[2], false, error)) {
|
||||
LogError(error, "Failed to parse audio format");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!encoder->Open(audio_format, error)) {
|
||||
LogError(error, "Failed to open encoder");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
StdioOutputStream os(stdout);
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* do it */
|
||||
|
||||
ssize_t nbytes;
|
||||
while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) {
|
||||
if (!encoder_write(encoder, buffer, nbytes, error)) {
|
||||
LogError(error, "encoder_write() failed");
|
||||
try {
|
||||
Error error;
|
||||
const auto encoder = encoder_init(*plugin, block, error);
|
||||
if (encoder == NULL) {
|
||||
LogError(error, "Failed to initialize encoder");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
/* open the encoder */
|
||||
|
||||
AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
||||
if (argc > 2) {
|
||||
if (!audio_format_parse(audio_format, argv[2], false, error)) {
|
||||
LogError(error, "Failed to parse audio format");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!encoder->Open(audio_format, error)) {
|
||||
LogError(error, "Failed to open encoder");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!encoder_end(encoder, error)) {
|
||||
LogError(error, "encoder_flush() failed");
|
||||
StdioOutputStream os(stdout);
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
/* do it */
|
||||
|
||||
ssize_t nbytes;
|
||||
while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) {
|
||||
if (!encoder_write(encoder, buffer, nbytes, error)) {
|
||||
LogError(error, "encoder_write() failed");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
}
|
||||
|
||||
if (!encoder_end(encoder, error)) {
|
||||
LogError(error, "encoder_flush() failed");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
encoder->Close();
|
||||
encoder->Dispose();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
} catch (const std::exception &e) {
|
||||
LogError(e);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
encoder->Close();
|
||||
encoder->Dispose();
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@
|
||||
#include "fs/io/GunzipReader.hxx"
|
||||
#include "fs/io/FileReader.hxx"
|
||||
#include "fs/io/StdioOutputStream.hxx"
|
||||
#include "Log.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -36,8 +37,7 @@ Copy(OutputStream &dest, Reader &src, Error &error)
|
||||
if (nbytes == 0)
|
||||
return !error.IsDefined();
|
||||
|
||||
if (!dest.Write(buffer, nbytes, error))
|
||||
return false;
|
||||
dest.Write(buffer, nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +66,16 @@ main(int argc, gcc_unused char **argv)
|
||||
|
||||
Path path = Path::FromFS(argv[1]);
|
||||
|
||||
Error error;
|
||||
if (!CopyGunzip(stdout, path, error)) {
|
||||
fprintf(stderr, "%s\n", error.GetMessage());
|
||||
try {
|
||||
Error error;
|
||||
if (!CopyGunzip(stdout, path, error)) {
|
||||
fprintf(stderr, "%s\n", error.GetMessage());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
} catch (const std::exception &e) {
|
||||
LogError(e);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@@ -41,8 +41,7 @@ Copy(OutputStream &dest, int src, Error &error)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!dest.Write(buffer, nbytes, error))
|
||||
return false;
|
||||
dest.Write(buffer, nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +49,11 @@ static bool
|
||||
CopyGzip(OutputStream &_dest, int src, Error &error)
|
||||
{
|
||||
GzipOutputStream dest(_dest);
|
||||
return Copy(dest, src, error) &&
|
||||
dest.Flush(error);
|
||||
if (!Copy(dest, src, error))
|
||||
return false;
|
||||
|
||||
dest.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
|
@@ -51,72 +51,63 @@ main(gcc_unused int argc, gcc_unused char **argv)
|
||||
const auto encoder = encoder_init(*plugin, block, IgnoreError());
|
||||
assert(encoder != NULL);
|
||||
|
||||
/* open the encoder */
|
||||
try {
|
||||
/* open the encoder */
|
||||
|
||||
AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
||||
success = encoder->Open(audio_format, IgnoreError());
|
||||
assert(success);
|
||||
AudioFormat audio_format(44100, SampleFormat::S16, 2);
|
||||
success = encoder->Open(audio_format, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
StdioOutputStream os(stdout);
|
||||
StdioOutputStream os(stdout);
|
||||
|
||||
Error error;
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
/* write a block of data */
|
||||
|
||||
success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
|
||||
assert(success);
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
/* write a tag */
|
||||
|
||||
success = encoder_pre_tag(encoder, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
Tag tag;
|
||||
|
||||
{
|
||||
TagBuilder tag_builder;
|
||||
tag_builder.AddItem(TAG_ARTIST, "Foo");
|
||||
tag_builder.AddItem(TAG_TITLE, "Bar");
|
||||
tag_builder.Commit(tag);
|
||||
}
|
||||
|
||||
success = encoder_tag(encoder, tag, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
/* write another block of data */
|
||||
|
||||
success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
|
||||
assert(success);
|
||||
|
||||
/* finish */
|
||||
|
||||
success = encoder_end(encoder, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
EncoderToOutputStream(os, *encoder);
|
||||
|
||||
encoder->Close();
|
||||
encoder->Dispose();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
} catch (const std::exception &e) {
|
||||
LogError(e);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* write a block of data */
|
||||
|
||||
success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
|
||||
assert(success);
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* write a tag */
|
||||
|
||||
success = encoder_pre_tag(encoder, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Tag tag;
|
||||
|
||||
{
|
||||
TagBuilder tag_builder;
|
||||
tag_builder.AddItem(TAG_ARTIST, "Foo");
|
||||
tag_builder.AddItem(TAG_TITLE, "Bar");
|
||||
tag_builder.Commit(tag);
|
||||
}
|
||||
|
||||
success = encoder_tag(encoder, tag, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* write another block of data */
|
||||
|
||||
success = encoder_write(encoder, zero, sizeof(zero), IgnoreError());
|
||||
assert(success);
|
||||
|
||||
/* finish */
|
||||
|
||||
success = encoder_end(encoder, IgnoreError());
|
||||
assert(success);
|
||||
|
||||
if (!EncoderToOutputStream(os, *encoder, error)) {
|
||||
LogError(error);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
encoder->Close();
|
||||
encoder->Dispose();
|
||||
}
|
||||
|
Reference in New Issue
Block a user