-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirection.hpp
51 lines (39 loc) · 1003 Bytes
/
Direction.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2016 Anton Erholt <aerholt@kth.se>
#ifndef LAB3_DIRECTION_HPP_
#define LAB3_DIRECTION_HPP_
#include <string>
#include <set>
#include <sstream>
namespace lab3 {
typedef std::string Direction;
const Direction EAST = "E";
const Direction WEST = "W";
const Direction NORTH = "N";
const Direction SOUTH = "S";
const Direction UP = "U";
const Direction DOWN = "D";
const std::set<Direction> DIRECTIONS() {
std::set<Direction> dirs;
dirs.insert(EAST);
dirs.insert(WEST);
dirs.insert(NORTH);
dirs.insert(SOUTH);
dirs.insert(UP);
dirs.insert(DOWN);
return dirs;
}
Direction parseDirection(std::string dir) {
if (dir.empty())
throw std::invalid_argument("No such direction");
char c = *(dir.begin());
std::locale loc;
c = std::toupper(c, loc);
std::string s;
s += c;
if (DIRECTIONS().find(s) != DIRECTIONS().end()) {
return (Direction) s;
}
throw std::invalid_argument("No such direction.");
}
} // namespace lab3
#endif // LAB3_DIRECTION_HPP_