summaryrefslogtreecommitdiff
path: root/src/lib/util.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-10-16 01:46:18 +0100
committerCarl Hetherington <cth@carlh.net>2012-10-16 01:46:18 +0100
commit763acf87c7c80d508f6c10af0f3f46045d5c23df (patch)
tree72cf7b3da0f300bbf2f6dfb45e5b53ce3803d368 /src/lib/util.cc
parent5428006e97b37d757a03c14024d7a0fb363bdcc6 (diff)
Try to make client/server communications a little more robust.
Diffstat (limited to 'src/lib/util.cc')
-rw-r--r--src/lib/util.cc58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc
index a14db810f..c2b24944d 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -35,6 +35,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
+#include <boost/lexical_cast.hpp>
#include <openjpeg.h>
#include <openssl/md5.h>
#include <magick/MagickCore.h>
@@ -609,8 +610,8 @@ Rectangle::intersection (Rectangle const & other) const
return Rectangle (
tx, ty,
- min (x + w, other.x + other.w) - tx,
- min (y + h, other.y + other.h) - ty
+ min (x + width, other.x + other.width) - tx,
+ min (y + height, other.y + other.height) - ty
);
}
@@ -650,3 +651,56 @@ read_key_value (istream &s)
return kv;
}
+
+string
+get_required_string (multimap<string, string> const & kv, string k)
+{
+ if (kv.count (k) > 1) {
+ throw StringError ("unexpected multiple keys in key-value set");
+ }
+
+ multimap<string, string>::const_iterator i = kv.find (k);
+
+ if (i == kv.end ()) {
+ throw StringError (String::compose ("missing key %1 in key-value set", k));
+ }
+
+ return i->second;
+}
+
+int
+get_required_int (multimap<string, string> const & kv, string k)
+{
+ string const v = get_required_string (kv, k);
+ return lexical_cast<int> (v);
+}
+
+string
+get_optional_string (multimap<string, string> const & kv, string k)
+{
+ if (kv.count (k) > 1) {
+ throw StringError ("unexpected multiple keys in key-value set");
+ }
+
+ multimap<string, string>::const_iterator i = kv.find (k);
+ if (i == kv.end ()) {
+ return "";
+ }
+
+ return i->second;
+}
+
+int
+get_optional_int (multimap<string, string> const & kv, string k)
+{
+ if (kv.count (k) > 1) {
+ throw StringError ("unexpected multiple keys in key-value set");
+ }
+
+ multimap<string, string>::const_iterator i = kv.find (k);
+ if (i == kv.end ()) {
+ return 0;
+ }
+
+ return lexical_cast<int> (i->second);
+}