-
Notifications
You must be signed in to change notification settings - Fork 1
/
CanvasCollection.cpp
96 lines (77 loc) · 2.31 KB
/
CanvasCollection.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
#include "CanvasCollection.h"
#include "global.h"
//the line that identifies a file as a valid canvas config file
#define MAGIC_LINE ";-*-EVENT_BROWSER_CANVASES-*-"
#ifdef DEBUG
#include <iostream>
#endif
using namespace std;
CanvasCollection::CanvasCollection() {};
CanvasCollection::~CanvasCollection()
{
for (int i = 0; i < (int) canvases.size(); ++i)
{
delete canvases[i];
}
}
void CanvasCollection::removeCanvas(int index)
{
delete canvases[index];
canvases.erase(canvases.begin() + index);
}
void CanvasCollection::print()
{
cout << "-----------------------------------" << endl;
cout << "There are " << size() << " canvases: " << endl;
for (int i = 0; i <(int) canvases.size(); ++i)
{
cout << canvases[i]->getName() << endl;
}
}
void CanvasCollection::saveToFile(string filename)
{
ofstream file;
file.open (filename.c_str(), ios::trunc);
if (file.is_open())
{
file << MAGIC_LINE << endl;
file <<
";************************************************ \n"
";This file is generated by browser and contains \n"
";definitions of canvases created during a program\n"
";session. This file can be parsed later by the\n"
";program.\n"
";\n"
";You can edit this file by hand; in which case, be\n"
";aware of the syntax:\n"
";each \"canvas\" is headed by a header of the format:\n"
";[Canvas:_canvas type_]\n"
";where canvas type corresponds to the enum defined\n"
";in Canvas.h. The rest of the variables are defined\n"
";in the format:\n"
";name = value\n"
";\"name\" conforms to the rules regarding the names\n"
";of c-style variables.\n"
";************************************************ \n"
<< endl;
for (int i = 0; i < (int) canvases.size(); ++i)
canvases[i]->streamToFile(file);
file.close();
}
}
CanvasCollection* CanvasCollection::readFromFile(string filename)
{
ifstream file(filename.c_str());
string line;
getline(file,line,END_OF_LINE);
// if MAGIC_LINE not found then not a valid file to parse
if (line.find(MAGIC_LINE) == string::npos) return NULL;
CanvasCollection* cc = new CanvasCollection();
Canvas *c = new Canvas();
while (c->parseFromFile(file))
{
cc->push_back(c);
c = new Canvas();
}
return cc;
}