~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/Writer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) Riegel         <juergen.riegel@web.de>                  *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
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.      *
 
10
 *                                                                         *
 
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.                  *
 
15
 *                                                                         *
 
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                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#ifndef _PreComp_
 
27
#endif
 
28
 
 
29
/// Here the FreeCAD includes sorted by Base,App,Gui......
 
30
#include "Writer.h"
 
31
#include "Persistence.h"
 
32
#include "Exception.h"
 
33
#include "Base64.h"
 
34
 
 
35
#include <algorithm>
 
36
#include <locale>
 
37
 
 
38
using namespace Base;
 
39
using namespace std;
 
40
using namespace zipios;
 
41
 
 
42
 
 
43
 
 
44
// ---------------------------------------------------------------------------
 
45
//  Writer: Constructors and Destructor
 
46
// ---------------------------------------------------------------------------
 
47
 
 
48
Writer::Writer(void)
 
49
  : indent(0),forceXML(false)
 
50
{
 
51
    indBuf[0] = '\0';
 
52
}
 
53
 
 
54
Writer::~Writer()
 
55
{
 
56
}
 
57
 
 
58
void Writer::insertAsciiFile(const char* FileName)
 
59
{
 
60
    std::ifstream from(FileName);
 
61
    if (!from) throw Base::Exception("Writer::insertAsciiFile() Could not open file!");
 
62
 
 
63
    Stream() << "<![CDATA[" << endl;
 
64
    char ch;
 
65
    while (from.get(ch)) Stream().put(ch);
 
66
    Stream() << endl << "]]>" << endl;
 
67
}
 
68
 
 
69
void Writer::insertBinFile(const char* FileName)
 
70
{
 
71
    std::ifstream from(FileName);
 
72
    if (!from) throw Base::Exception("Writer::insertAsciiFile() Could not open file!");
 
73
 
 
74
    Stream() << "<![CDATA[" << endl;
 
75
 
 
76
    unsigned char buf[60];
 
77
    std::string encoded;
 
78
    unsigned int i;
 
79
 
 
80
    while (from){
 
81
        for(i=0 ; i<60 && from;i++)
 
82
            buf[i] = from.get();
 
83
        Stream() << Base::base64_encode(buf,i) << endl;
 
84
    }
 
85
 
 
86
    Stream() << "]]>" << endl;
 
87
}
 
88
 
 
89
void Writer::setForceXML(bool on)
 
90
{
 
91
    forceXML = on;
 
92
}
 
93
 
 
94
bool Writer::isForceXML(void)
 
95
{
 
96
    return forceXML;
 
97
}
 
98
 
 
99
std::string Writer::addFile(const char* Name,const Base::Persistence *Object)
 
100
{
 
101
    // always check isForceXML() before requesting a file!
 
102
    assert(isForceXML()==false);
 
103
 
 
104
    FileEntry temp;
 
105
    temp.FileName = getUniqueFileName(Name);
 
106
    temp.Object = Object;
 
107
  
 
108
    FileList.push_back(temp);
 
109
 
 
110
    FileNames.push_back( temp.FileName );
 
111
 
 
112
    // return the unique file name
 
113
    return temp.FileName;
 
114
}
 
115
 
 
116
std::string Writer::getUniqueFileName(const char *Name)
 
117
{
 
118
    // name in use?
 
119
    std::string CleanName = (Name ? Name : "");
 
120
    std::vector<std::string>::const_iterator pos;
 
121
    pos = find(FileNames.begin(),FileNames.end(),CleanName);
 
122
 
 
123
    if (pos == FileNames.end()) {
 
124
        // if not, name is OK
 
125
        return CleanName;
 
126
    }
 
127
    else {
 
128
        // find highest sufix
 
129
        int nSuff = 0;  
 
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()));
 
138
                }
 
139
            }
 
140
        }
 
141
 
 
142
        std::stringstream str;
 
143
        str << CleanName << (nSuff + 1);
 
144
        return str.str();
 
145
    }
 
146
}
 
147
 
 
148
const std::vector<std::string>& Writer::getFilenames() const
 
149
{
 
150
    return FileNames;
 
151
}
 
152
 
 
153
void Writer::incInd(void)
 
154
{
 
155
    if (indent < 255) {
 
156
        indBuf[indent] = '\t';
 
157
        indBuf[indent+1] = '\0';
 
158
        indent++;
 
159
    }
 
160
}
 
161
 
 
162
void Writer::decInd(void)
 
163
{
 
164
    if (indent > 0) {
 
165
        indent--;
 
166
        indBuf[indent] = '\0';
 
167
    }
 
168
}
 
169
 
 
170
ZipWriter::ZipWriter(const char* FileName) 
 
171
  : ZipStream(FileName)
 
172
{
 
173
#ifdef _MSC_VER
 
174
    ZipStream.imbue(std::locale::empty());
 
175
#else
 
176
    //FIXME: Check whether this is correct
 
177
    ZipStream.imbue(std::locale::classic());
 
178
#endif
 
179
}
 
180
 
 
181
ZipWriter::ZipWriter(std::ostream& os) 
 
182
  : ZipStream(os)
 
183
{
 
184
#ifdef _MSC_VER
 
185
    ZipStream.imbue(std::locale::empty());
 
186
#else
 
187
    //FIXME: Check whether this is correct
 
188
    ZipStream.imbue(std::locale::classic());
 
189
#endif
 
190
}
 
191
 
 
192
void ZipWriter::writeFiles(void)
 
193
{
 
194
    for (std::vector<FileEntry>::const_iterator it = FileList.begin(); it != FileList.end(); ++it)
 
195
    {
 
196
        ZipStream.putNextEntry(it->FileName);
 
197
        it->Object->SaveDocFile( *this );
 
198
    }
 
199
}
 
200
 
 
201
ZipWriter::~ZipWriter()
 
202
{
 
203
    ZipStream.close();
 
204
}