/*
 * Work.cpp
 *
 *  Created on: Apr 12, 2018
 *      Author: armin
 */

#include <Work.h>
#include <stddef.h>
#include <cassert>
#include <cstdlib>
#include <sstream>

namespace comm {

using namespace std;

Work::Work(string work_s) {
  vector<string> wrk_v = split(work_s, " ");
  assert(wrk_v.size() == 3);

  from = wrk_v.at(0);
  to = wrk_v.at(1);
  total = strtol(wrk_v.at(2).c_str(), nullptr, 10);
}

vector<string> Work::split(const string s, const string delimiter) {
  vector<string> split_str;

  size_t start_pos = 0;
  size_t end_pos = 0;
  while ((end_pos = s.find(delimiter, start_pos)) != string::npos) {
    split_str.push_back(s.substr(start_pos, end_pos - start_pos));
    start_pos = end_pos + delimiter.size();
  }

  split_str.push_back(s.substr(start_pos, string::npos));

  return (split_str);
}

string Work::repr(void) {
  ostringstream buffer;
  buffer << "<" << from << " " << to << " " << total << ">";
  return (buffer.str());
}

Work::~Work() {
  // TODO Auto-generated destructor stub
}

}  // namespace comm
