allow parsing locations
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "location.hpp"
|
||||
#include "mathutil.hpp"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
eDirection& operator++ (eDirection& me, int) {
|
||||
if(me == DIR_HERE) return me = DIR_N;
|
||||
@@ -237,6 +238,19 @@ std::ostream& operator<< (std::ostream& out, location l) {
|
||||
return out;
|
||||
}
|
||||
|
||||
std::istream& operator>> (std::istream& in, location& l) {
|
||||
in.get(); // (
|
||||
std::stringbuf sstr;
|
||||
in.get(sstr, ',');
|
||||
l.x = atoi(sstr.str().c_str());
|
||||
in.get(); // ,
|
||||
sstr.str("");
|
||||
in.get(sstr, ')');
|
||||
l.y = atoi(sstr.str().c_str());
|
||||
in.get(); // )
|
||||
return in;
|
||||
}
|
||||
|
||||
std::ostream& operator<< (std::ostream& out, spec_loc_t l) {
|
||||
out << static_cast<location&>(l);
|
||||
out << ':' << l.spec;
|
||||
|
@@ -165,6 +165,7 @@ rectangle rect(int top, int left, int bottom, int right);
|
||||
std::ostream& operator << (std::ostream& out, eDirection e);
|
||||
std::istream& operator >> (std::istream& in, eDirection& e);
|
||||
std::ostream& operator<< (std::ostream& out, location l);
|
||||
std::istream& operator>> (std::istream& in, location& l);
|
||||
std::ostream& operator<< (std::ostream& out, spec_loc_t l);
|
||||
std::ostream& operator<< (std::ostream& out, sign_loc_t l);
|
||||
std::ostream& operator<< (std::ostream& out, rectangle r);
|
||||
|
20
test/replay_parse.cpp
Normal file
20
test/replay_parse.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include <sstream>
|
||||
#include "location.hpp"
|
||||
|
||||
TEST_CASE("Parsing and stringifying locations") {
|
||||
SECTION("Stringifying") {
|
||||
location l(27, 923);
|
||||
std::stringstream out;
|
||||
out << l;
|
||||
CHECK(out.str() == "(27,923)");
|
||||
}
|
||||
SECTION("Parsing") {
|
||||
location l;
|
||||
std::stringstream in("(27,923)");
|
||||
in >> l;
|
||||
CHECK(l.x == 27);
|
||||
CHECK(l.y == 923);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user