-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaploader.h
More file actions
92 lines (67 loc) · 2.06 KB
/
maploader.h
File metadata and controls
92 lines (67 loc) · 2.06 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MAPLOADER_H
#define MAPLOADER_H
#include "robotmap.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <QDebug>
#include <QFile>
#define LABEL_MAP "[map]"
#define LABEL_ROBOT "[robot]"
#define STATE_NONE 0
#define STATE_MAP 2
#define STATE_ROBOT 4
class MapLoader{
public:
static RobotMap* load(QString fileName) {
QFile file(fileName);
unsigned state = STATE_NONE;
RobotMap* rm = new RobotMap();
Robot* robot = nullptr;
std::vector<std::vector<int>> mapVector;
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "File " << fileName << " cannot be open for loading map.";
return rm;
}
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
if(line == LABEL_MAP) { state = STATE_MAP; continue; }
if(line == LABEL_ROBOT) { state = STATE_ROBOT; continue; }
if(line.isEmpty() || line.isNull()) { continue; }
switch(state) {
case STATE_NONE:
qWarning() << "Non label line at none statement: \"" << line << "\"" << "\n\r";
break;
case STATE_MAP: {
std::vector<int> tiles = splitMapLine(line);
mapVector.push_back(tiles);
break;
}
case STATE_ROBOT:
robot = getRobotFromParams(line);
break;
}
}
rm->setMap(mapVector);
rm->setRobot(robot);
file.close();
return rm;
}
static std::vector<int> splitMapLine(QString line) {
std::vector<int> ints;
QStringList list = line.split(" ");
for(QString s: list) {
if(s.isEmpty()) continue;
ints.push_back(s.toInt());
}
return ints;
}
static Robot* getRobotFromParams(QString line) {
QStringList list = line.split(" ");
size_t i = list[0].toUInt();
size_t j = list[1].toUInt();
return new Robot(i, j);
}
};
#endif