aboutsummaryrefslogtreecommitdiff
path: root/src/wanda/message.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2018-12-11 07:42:05 +0100
committerFelix Morgner <felix.morgner@gmail.com>2018-12-11 07:42:05 +0100
commited419140280553b070943b7ba539120a26ff5686 (patch)
tree3d134977a6dfae71a45c318305166d044b50320d /src/wanda/message.cpp
parentd3e691c9200b7b782c8acf17468068a699588a73 (diff)
downloadwanda-ed419140280553b070943b7ba539120a26ff5686.tar.xz
wanda-ed419140280553b070943b7ba539120a26ff5686.zip
wanda: restructure directory hierarchy
Diffstat (limited to 'src/wanda/message.cpp')
-rw-r--r--src/wanda/message.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/wanda/message.cpp b/src/wanda/message.cpp
new file mode 100644
index 0000000..978b7c4
--- /dev/null
+++ b/src/wanda/message.cpp
@@ -0,0 +1,75 @@
+#include <wanda/message.hpp>
+
+#include <ios>
+#include <iterator>
+#include <sstream>
+
+namespace wanda
+{
+ message::operator std::string() const
+ {
+ std::ostringstream buffer{};
+ buffer << source
+ << ':'
+ << command;
+ if (argument.has_value())
+ {
+ buffer << ':' << *argument;
+ }
+ return buffer.str();
+ }
+
+ std::size_t message::size() const
+ {
+ return static_cast<std::string>(*this).size();
+ }
+
+ template<typename InputIt, typename OutputIt, typename UnaryPredicate>
+ OutputIt copy_until(InputIt first, InputIt last, OutputIt out, UnaryPredicate predicate)
+ {
+ while (first != last && !predicate(*first))
+ {
+ *out++ = *first++;
+ }
+ return out;
+ }
+
+ std::istream & operator>>(std::istream & in, message & message)
+ {
+ auto pos = std::istream_iterator<char>{in};
+ auto end = std::istream_iterator<char>{};
+ auto buffer = std::string{};
+
+ copy_until(pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; });
+ if (in.eof() || buffer.size() != 1)
+ {
+ in.setstate(std::ios_base::failbit);
+ return in;
+ }
+ message.source = buffer;
+
+ buffer.clear();
+ copy_until(++pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; });
+ if (in.eof())
+ {
+ in.setstate(std::ios_base::failbit);
+ }
+ message.command = buffer;
+
+ buffer.clear();
+ copy(++pos, end, std::back_inserter(buffer));
+ if (buffer.size())
+ {
+ message.argument = std::optional{std::move(buffer)};
+ }
+
+ in.clear(in.rdstate() ^ std::ios_base::failbit);
+ return in;
+ }
+
+ std::ostream & operator<<(std::ostream & out, message const & message)
+ {
+ return out << static_cast<std::string>(message);
+ }
+
+} // namespace wanda \ No newline at end of file