-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGDocument.cpp
More file actions
executable file
·127 lines (112 loc) · 3.23 KB
/
SVGDocument.cpp
File metadata and controls
executable file
·127 lines (112 loc) · 3.23 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//------------------------------------------------------------------------------
/// Filename: SVGDocument.cpp
/// Description: creates and writes the SVGDocument
/// Authors:
/// Robin Ankele(0931951)
/// Tutor: Manuel Weber
/// Group: 24
/// Created: 08.09.2011
/// Last change: 08.09.2011
//------------------------------------------------------------------------------
#include <sstream>
#include <iostream>
#include <fstream>
#include "SVGDocument.h"
#include "Error.h"
#include "Database.h"
#include "UserInterface.h"
#include "SVGObject.h"
#include "SVGHandler.h"
#include "ErrorCodes.h"
//------------------------------------------------------------------------------
SVGDocument::SVGDocument(Database *db,
SVGHandler *svgh) : db_(db),
svgh_(svgh)
{
try
{
error_ = new Error();
}
catch(std::bad_alloc& exception)
{
delete error_;
svgh_->setErrors(OUT_OF_MEMORY);
}
}
//------------------------------------------------------------------------------
SVGDocument::~SVGDocument() throw()
{
if(error_)
delete error_;
}
//------------------------------------------------------------------------------
std::string SVGDocument::getSVGHeader()
{
std::stringstream header;
header<<"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
header<<" <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
<<"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
header<<" <svg width=\""<<db_->getWidth()<<"\" height=\""<<db_->getHeight()
<<"\" xmlns=\"http://www.w3.org/2000/svg\" "
<<"xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
return header.str();
}
//------------------------------------------------------------------------------
std::string SVGDocument::getSVGFooter()
{
return std::string(" </svg>\n");
}
//------------------------------------------------------------------------------
bool SVGDocument::writeSVG()
{
std::string filename, answer;
signed int value = 0;
ui_->getParam(" file? ", false, false, value, filename, false);
if(checkOpen(filename))
{
while(true)
{
error_->filenameExists(filename);
ui_->getParam("", false, false, value, answer, false);
if(!answer.compare("n"))
return false;
if(!answer.compare("y"))
break;
}
}
std::ofstream svg_file;
svg_file.open(filename.c_str(), std::fstream::out | std::fstream::trunc);
if(svg_file.fail())
{
while(true)
{
error_->cannotWriteToFile(filename);
ui_->getParam("", false, false, value, answer, false);
if(!answer.compare("n"))
return false;
if(!answer.compare("y"))
return true;
}
}
svg_file<<getSVGHeader();
for(std::vector<SVGObject*>::iterator it = db_->getAllSVGObjects().begin();
it != db_->getAllSVGObjects().end(); ++it)
{
svg_file<<(*it)->getSVGTag()<<"\n";
}
svg_file<<getSVGFooter();
svg_file.close();
return true;
}
//------------------------------------------------------------------------------
bool SVGDocument::checkOpen(std::string filename)
{
std::fstream svg_file;
svg_file.open (filename.c_str());
if(svg_file.is_open())
{
svg_file.close();
return true;
}
return false;
}