1
/***************************************************************************
2
* Copyright (c) Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
29
/// Here the FreeCAD includes sorted by Base,App,Gui......
31
#include "Persistence.h"
32
#include "Exception.h"
40
using namespace zipios;
44
// ---------------------------------------------------------------------------
45
// Writer: Constructors and Destructor
46
// ---------------------------------------------------------------------------
49
: indent(0),forceXML(false)
58
void Writer::insertAsciiFile(const char* FileName)
60
std::ifstream from(FileName);
61
if (!from) throw Base::Exception("Writer::insertAsciiFile() Could not open file!");
63
Stream() << "<![CDATA[" << endl;
65
while (from.get(ch)) Stream().put(ch);
66
Stream() << endl << "]]>" << endl;
69
void Writer::insertBinFile(const char* FileName)
71
std::ifstream from(FileName);
72
if (!from) throw Base::Exception("Writer::insertAsciiFile() Could not open file!");
74
Stream() << "<![CDATA[" << endl;
76
unsigned char buf[60];
81
for(i=0 ; i<60 && from;i++)
83
Stream() << Base::base64_encode(buf,i) << endl;
86
Stream() << "]]>" << endl;
89
void Writer::setForceXML(bool on)
94
bool Writer::isForceXML(void)
99
std::string Writer::addFile(const char* Name,const Base::Persistence *Object)
101
// always check isForceXML() before requesting a file!
102
assert(isForceXML()==false);
105
temp.FileName = getUniqueFileName(Name);
106
temp.Object = Object;
108
FileList.push_back(temp);
110
FileNames.push_back( temp.FileName );
112
// return the unique file name
113
return temp.FileName;
116
std::string Writer::getUniqueFileName(const char *Name)
119
std::string CleanName = (Name ? Name : "");
120
std::vector<std::string>::const_iterator pos;
121
pos = find(FileNames.begin(),FileNames.end(),CleanName);
123
if (pos == FileNames.end()) {
124
// if not, name is OK
128
// find highest sufix
130
for (pos = FileNames.begin();pos != FileNames.end();++pos) {
131
const std::string &FilName = *pos;
132
if (FilName.substr(0, CleanName.length()) == CleanName) { // same prefix
133
std::string clSuffix(FilName.substr(CleanName.length()));
134
if (clSuffix.size() > 0){
135
std::string::size_type nPos = clSuffix.find_first_not_of("0123456789");
136
if (nPos==std::string::npos)
137
nSuff = max<int>(nSuff, atol(clSuffix.c_str()));
142
std::stringstream str;
143
str << CleanName << (nSuff + 1);
148
const std::vector<std::string>& Writer::getFilenames() const
153
void Writer::incInd(void)
156
indBuf[indent] = '\t';
157
indBuf[indent+1] = '\0';
162
void Writer::decInd(void)
166
indBuf[indent] = '\0';
170
ZipWriter::ZipWriter(const char* FileName)
171
: ZipStream(FileName)
174
ZipStream.imbue(std::locale::empty());
176
//FIXME: Check whether this is correct
177
ZipStream.imbue(std::locale::classic());
181
ZipWriter::ZipWriter(std::ostream& os)
185
ZipStream.imbue(std::locale::empty());
187
//FIXME: Check whether this is correct
188
ZipStream.imbue(std::locale::classic());
192
void ZipWriter::writeFiles(void)
194
for (std::vector<FileEntry>::const_iterator it = FileList.begin(); it != FileList.end(); ++it)
196
ZipStream.putNextEntry(it->FileName);
197
it->Object->SaveDocFile( *this );
201
ZipWriter::~ZipWriter()