output/pipe: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-11-05 15:24:10 +01:00
parent 543c5034af
commit b45ea66175

View File

@ -21,10 +21,10 @@
#include "PipeOutputPlugin.hxx" #include "PipeOutputPlugin.hxx"
#include "../OutputAPI.hxx" #include "../OutputAPI.hxx"
#include "../Wrapper.hxx" #include "../Wrapper.hxx"
#include "config/ConfigError.hxx" #include "system/Error.hxx"
#include "util/Error.hxx"
#include <string> #include <string>
#include <stdexcept>
#include <stdio.h> #include <stdio.h>
@ -33,13 +33,10 @@ class PipeOutput {
AudioOutput base; AudioOutput base;
std::string cmd; const std::string cmd;
FILE *fh; FILE *fh;
PipeOutput() PipeOutput(const ConfigBlock &block);
:base(pipe_output_plugin) {}
bool Configure(const ConfigBlock &block, Error &error);
public: public:
static PipeOutput *Create(const ConfigBlock &block, Error &error); static PipeOutput *Create(const ConfigBlock &block, Error &error);
@ -53,54 +50,36 @@ public:
size_t Play(const void *chunk, size_t size, Error &error); size_t Play(const void *chunk, size_t size, Error &error);
}; };
inline bool PipeOutput::PipeOutput(const ConfigBlock &block)
PipeOutput::Configure(const ConfigBlock &block, Error &error) :base(pipe_output_plugin, block),
cmd(block.GetBlockValue("command", ""))
{ {
if (!base.Configure(block, error)) if (cmd.empty())
return false; throw std::runtime_error("No \"command\" parameter specified");
cmd = block.GetBlockValue("command", "");
if (cmd.empty()) {
error.Set(config_domain,
"No \"command\" parameter specified");
return false;
}
return true;
} }
inline PipeOutput * inline PipeOutput *
PipeOutput::Create(const ConfigBlock &block, Error &error) PipeOutput::Create(const ConfigBlock &block, Error &)
{ {
PipeOutput *po = new PipeOutput(); return new PipeOutput(block);
if (!po->Configure(block, error)) {
delete po;
return nullptr;
}
return po;
} }
inline bool inline bool
PipeOutput::Open(gcc_unused AudioFormat &audio_format, Error &error) PipeOutput::Open(gcc_unused AudioFormat &audio_format, Error &)
{ {
fh = popen(cmd.c_str(), "w"); fh = popen(cmd.c_str(), "w");
if (fh == nullptr) { if (fh == nullptr)
error.FormatErrno("Error opening pipe \"%s\"", throw FormatErrno("Error opening pipe \"%s\"", cmd.c_str());
cmd.c_str());
return false;
}
return true; return true;
} }
inline size_t inline size_t
PipeOutput::Play(const void *chunk, size_t size, Error &error) PipeOutput::Play(const void *chunk, size_t size, Error &)
{ {
size_t nbytes = fwrite(chunk, 1, size, fh); size_t nbytes = fwrite(chunk, 1, size, fh);
if (nbytes == 0) if (nbytes == 0)
error.SetErrno("Write error on pipe"); throw MakeErrno("Write error on pipe");
return nbytes; return nbytes;
} }