~singpolyma/biboumi

7f580dbc0e529d200662e676119a3dcb966f67f9 — Florent Le Coz 9 years ago 4027ef8
Add irc_message.hpp
2 files changed, 93 insertions(+), 0 deletions(-)

A src/libirc/irc_message.cpp
A src/libirc/irc_message.hpp
A src/libirc/irc_message.cpp => src/libirc/irc_message.cpp +65 -0
@@ 0,0 1,65 @@
#include <libirc/irc_message.hpp>
#include <iostream>

IrcMessage::IrcMessage(std::string&& line)
{
  std::string::size_type pos;

  // optional prefix
  if (line[0] == ':')
    {
      pos = line.find(" ");
      this->prefix = line.substr(1, pos);
      line = line.substr(pos + 1, std::string::npos);
    }
  // command
  pos = line.find(" ");
  this->command = line.substr(0, pos);
  line = line.substr(pos + 1, std::string::npos);
  // arguments
  do
    {
      if (line[0] == ':')
        {
          this->arguments.emplace_back(line.substr(1, std::string::npos));
          break ;
        }
      pos = line.find(" ");
      this->arguments.emplace_back(line.substr(0, pos));
      line = line.substr(pos + 1, std::string::npos);
    } while (pos != std::string::npos);
}

IrcMessage::IrcMessage(std::string&& prefix,
                       std::string&& command,
                       std::vector<std::string>&& args):
  prefix(std::move(prefix)),
  command(std::move(command)),
  arguments(std::move(args))
{
}

IrcMessage::IrcMessage(std::string&& command,
                       std::vector<std::string>&& args):
  prefix(),
  command(std::move(command)),
  arguments(std::move(args))
{
}

IrcMessage::~IrcMessage()
{
}

std::ostream& operator<<(std::ostream& os, const IrcMessage& message)
{
  os << "IrcMessage";
  os << "[" << message.command << "]";
  for (const std::string& arg: message.arguments)
    {
      os << "{" << arg << "}";
    }
  if (!message.prefix.empty())
    os << "(from: " << message.prefix << ")";
  return os;
}

A src/libirc/irc_message.hpp => src/libirc/irc_message.hpp +28 -0
@@ 0,0 1,28 @@
#ifndef IRC_MESSAGE_INCLUDED
# define IRC_MESSAGE_INCLUDED

#include <vector>
#include <string>
#include <ostream>

class IrcMessage
{
public:
  explicit IrcMessage(std::string&& line);
  explicit IrcMessage(std::string&& prefix, std::string&& command, std::vector<std::string>&& args);
  explicit IrcMessage(std::string&& command, std::vector<std::string>&& args);
  ~IrcMessage();

  std::string prefix;
  std::string command;
  std::vector<std::string> arguments;

  IrcMessage(const IrcMessage&) = delete;
  IrcMessage(IrcMessage&&) = delete;
  IrcMessage& operator=(const IrcMessage&) = delete;
  IrcMessage& operator=(IrcMessage&&) = delete;
};

std::ostream& operator<<(std::ostream& os, const IrcMessage& message);

#endif // IRC_MESSAGE_INCLUDED