-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatternManager.cpp
97 lines (90 loc) · 2.58 KB
/
PatternManager.cpp
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
93
94
95
96
97
#include "PatternManager.h"
PatternManager::PatternManager() {
this->patterns.clear();
}
PatternManager::PatternManager(string baseFile) {
string filename;
ifstream fin;
try {
fin.open(baseFile);
Pattern temp;
while(fin.good()) {
getline(fin, filename);
temp = Pattern(filename);
if(temp.me() != "Error pattern") {
this->patterns.push_back(temp);
cout << "read: "<< temp.me() <<endl;
}
}
} catch(ifstream::failure &readError) {
cout << "could not create pattern managaer from: " << baseFile << endl;
this->patterns.clear();
}
}
PatternManager::~PatternManager() {
//do nothing
}
size_t PatternManager::size() {
return this->patterns.size();
}
void PatternManager::writePatternFile(int index, string filename) {
if (index >= 0 && index < this->patterns.size()){
this->patterns[index].save(filename);
}
}
void PatternManager::writeAllPatterns() {
for (int i = 0; i < this->patterns.size(); i++) {
this->patterns[i].save(this->patterns[i].me());
}
}
void PatternManager::pushPattern(Pattern pattern) {
this->patterns.push_back(pattern);
}
void PatternManager::pushPattern(string name, vector<pair<int, int> > coords) {
this->patterns.push_back(Pattern(name,coords));
}
Pattern PatternManager::getPattern(int index) {
if (index >=0 && index < this->patterns.size()) {
return this->patterns[index];
} else {
return Pattern();
}
}
int PatternManager::getPatternIndex(string name) {
for (int i = 0; i < this->patterns.size(); i++) {
if(this->patterns[i].me() == name){
return i;
}
}
return -1;
}
void PatternManager::setPattern(int index, Pattern &p) {
if(index >=0 && index < this->patterns.size()) {
this->patterns[index] = p;
}
}
void PatternManager::popPattern() {
if(this->patterns.size()){
this->patterns.pop_back();
}
}
void PatternManager::popPattern(int index) {
if(this->patterns.size()) {
int count = 0;
for( vector<Pattern>::iterator it = this->patterns.begin(); it != this->patterns.end(); it++){
if(count == index){
this->patterns.erase(it);
}
count++;
}
}
}
void PatternManager::popPattern(string name) {
if(this->patterns.size()) {
for( vector<Pattern>::iterator it = this->patterns.begin(); it != this->patterns.end(); it++){
if(it->me() == name){
this->patterns.erase(it);
}
}
}
}