allow parsing locations

This commit is contained in:
2024-07-03 16:17:24 -06:00
committed by Celtic Minstrel
parent 945a4f14a0
commit 54ac26b4da
3 changed files with 35 additions and 0 deletions

View File

@@ -9,6 +9,7 @@
#include "location.hpp" #include "location.hpp"
#include "mathutil.hpp" #include "mathutil.hpp"
#include <iostream> #include <iostream>
#include <sstream>
eDirection& operator++ (eDirection& me, int) { eDirection& operator++ (eDirection& me, int) {
if(me == DIR_HERE) return me = DIR_N; if(me == DIR_HERE) return me = DIR_N;
@@ -237,6 +238,19 @@ std::ostream& operator<< (std::ostream& out, location l) {
return out; 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) { std::ostream& operator<< (std::ostream& out, spec_loc_t l) {
out << static_cast<location&>(l); out << static_cast<location&>(l);
out << ':' << l.spec; out << ':' << l.spec;

View File

@@ -165,6 +165,7 @@ rectangle rect(int top, int left, int bottom, int right);
std::ostream& operator << (std::ostream& out, eDirection e); std::ostream& operator << (std::ostream& out, eDirection e);
std::istream& operator >> (std::istream& in, eDirection& e); std::istream& operator >> (std::istream& in, eDirection& e);
std::ostream& operator<< (std::ostream& out, location l); 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, spec_loc_t l);
std::ostream& operator<< (std::ostream& out, sign_loc_t l); std::ostream& operator<< (std::ostream& out, sign_loc_t l);
std::ostream& operator<< (std::ostream& out, rectangle r); std::ostream& operator<< (std::ostream& out, rectangle r);

20
test/replay_parse.cpp Normal file
View 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);
}
}