/
Writer.h
205 lines (167 loc) · 6.19 KB
/
Writer.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef BASE_WRITER_H
#define BASE_WRITER_H
#include <set>
#include <string>
#include <sstream>
#include <vector>
#include <cassert>
#ifdef _MSC_VER
#include <zipios++/zipios-config.h>
#endif
#include <zipios++/zipfile.h>
#include <zipios++/zipinputstream.h>
#include <zipios++/zipoutputstream.h>
#include <zipios++/meta-iostreams.h>
#include "FileInfo.h"
namespace Base
{
class Persistence;
/** The Writer class
* This is an important helper class for the store and retrieval system
* of persistent objects in FreeCAD.
* \see Base::Persistence
* \author Juergen Riegel
*/
class BaseExport Writer
{
public:
Writer(void);
virtual ~Writer();
/// switch the writer in XML only mode (no files allowed)
void setForceXML(bool on);
/// check on state
bool isForceXML(void);
void setFileVersion(int);
int getFileVersion() const;
/// insert a file as CDATA section in the XML file
void insertAsciiFile(const char* FileName);
/// insert a binary file BASE64 coded as CDATA section in the XML file
void insertBinFile(const char* FileName);
/** @name additional file writing */
//@{
/// add a write request of a persistent object
std::string addFile(const char* Name, const Base::Persistence *Object);
/// process the requested file storing
virtual void writeFiles(void)=0;
/// get all registered file names
const std::vector<std::string>& getFilenames() const;
/// Set mode
void setMode(const std::string& mode);
/// Set modes
void setModes(const std::set<std::string>& modes);
/// Get mode
bool getMode(const std::string& mode) const;
/// Get modes
std::set<std::string> getModes() const;
/// Clear mode
void clearMode(const std::string& mode);
/// Clear modes
void clearModes();
//@}
/** @name pretty formating for XML */
//@{
/// get the current indentation
const char* ind(void) const {return indBuf;}
/// increase indentation by one tab
void incInd(void);
/// decrease indentation by one tab
void decInd(void);
//@}
virtual std::ostream &Stream(void)=0;
/// name for underlying file saves
std::string ObjectName;
protected:
std::string getUniqueFileName(const char *Name);
struct FileEntry {
std::string FileName;
const Base::Persistence *Object;
};
std::vector<FileEntry> FileList;
std::vector<std::string> FileNames;
std::set<std::string> Modes;
short indent;
char indBuf[1024];
bool forceXML;
int fileVersion;
};
/** The ZipWriter class
* This is an important helper class implementation for the store and retrieval system
* of persistent objects in FreeCAD.
* \see Base::Persistence
* \author Juergen Riegel
*/
class BaseExport ZipWriter : public Writer
{
public:
ZipWriter(const char* FileName);
ZipWriter(std::ostream&);
virtual ~ZipWriter();
virtual void writeFiles(void);
virtual std::ostream &Stream(void){return ZipStream;}
void setComment(const char* str){ZipStream.setComment(str);}
void setLevel(int level){ZipStream.setLevel( level );}
void putNextEntry(const char* str){ZipStream.putNextEntry(str);}
private:
zipios::ZipOutputStream ZipStream;
};
/** The StringWriter class
* This is an important helper class implementation for the store and retrieval system
* of objects in FreeCAD.
* \see Base::Persistence
* \author Juergen Riegel
*/
class BaseExport StringWriter : public Writer
{
public:
virtual std::ostream &Stream(void){return StrStream;}
std::string getString(void) const {return StrStream.str();}
virtual void writeFiles(void){}
private:
std::stringstream StrStream;
};
/*! The FileWriter class
This class writes out the data into files into a given directory name.
\see Base::Persistence
\author Werner Mayer
*/
class BaseExport FileWriter : public Writer
{
public:
FileWriter(const char* DirName);
virtual ~FileWriter();
void putNextEntry(const char* file);
virtual void writeFiles(void);
virtual std::ostream &Stream(void){return FileStream;}
/*!
This method can be re-implemented in sub-classes to avoid
to write out certain objects. The default implementation
always returns true.
*/
virtual bool shouldWrite(const std::string& name, const Base::Persistence *Object) const;
protected:
std::string DirName;
std::ofstream FileStream;
};
} //namespace Base
#endif // BASE_WRITER_H