[Spice-devel] [PATCH spice-streaming-agent 4/4] mjpeg-fallback: a more C++ way of handling options
Lukáš Hrázký
lhrazky at redhat.com
Wed Jan 31 11:58:00 UTC 2018
Use C++ standard library:
- std::string
- std::stoi() to parse the numbers (also note atoi() behavior is undefined in
case of errors)
- exceptions for errors, makes testing and potential future changes to
error handling easier.
Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
---
src/mjpeg-fallback.cpp | 42 +++++++++++++++++++++---------------------
1 file changed, 21 insertions(+), 21 deletions(-)
diff --git a/src/mjpeg-fallback.cpp b/src/mjpeg-fallback.cpp
index d6dcf74..c4b8ebf 100644
--- a/src/mjpeg-fallback.cpp
+++ b/src/mjpeg-fallback.cpp
@@ -11,6 +11,7 @@
#include <exception>
#include <stdexcept>
#include <sstream>
+#include <iostream>
#include <memory>
#include <syslog.h>
#include <X11/Xlib.h>
@@ -159,29 +160,24 @@ unsigned MjpegPlugin::Rank()
void MjpegPlugin::ParseOptions(const ConfigureOption *options)
{
-#define arg_error(...) syslog(LOG_ERR, ## __VA_ARGS__);
-
for (; options->name; ++options) {
- const char *name = options->name;
- const char *value = options->value;
-
- if (strcmp(name, "framerate") == 0) {
- int val = atoi(value);
- if (val > 0) {
- settings.fps = val;
+ const string name = options->name;
+ const string value = options->value;
+
+ if (name == "framerate") {
+ try {
+ settings.fps = stoi(value);
+ } catch (const exception &e) {
+ throw runtime_error("Invalid value '" + value + "' for option 'framerate'.");
}
- else {
- arg_error("wrong framerate arg %s\n", value);
- }
- }
- if (strcmp(name, "mjpeg.quality") == 0) {
- int val = atoi(value);
- if (val > 0) {
- settings.quality = val;
- }
- else {
- arg_error("wrong mjpeg.quality arg %s\n", value);
+ } else if (name == "mjpeg.quality") {
+ try {
+ settings.quality = stoi(value);
+ } catch (const exception &e) {
+ throw runtime_error("Invalid value '" + value + "' for option 'mjpeg.quality'.");
}
+ } else {
+ throw runtime_error("Invalid option '" + name + "'.");
}
}
}
@@ -202,7 +198,11 @@ bool MjpegPlugin::Register(Agent* agent)
std::unique_ptr<MjpegPlugin> plugin(new MjpegPlugin());
- plugin->ParseOptions(agent->Options());
+ try {
+ plugin->ParseOptions(agent->Options());
+ } catch (const exception &e) {
+ cerr << e.what() << endl;
+ }
agent->Register(*plugin.release());
--
2.16.1
More information about the Spice-devel
mailing list