FreeCAD

Форк
0
/
Persistence.cpp 
146 строк · 4.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 Jürgen 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
#include <cassert>
28
#endif
29

30
#include "Exception.h"
31
#include "Reader.h"
32
#include "Writer.h"
33

34
/// Here the FreeCAD includes sorted by Base,App,Gui......
35
#include "Persistence.h"
36

37

38
using namespace Base;
39

40
TYPESYSTEM_SOURCE_ABSTRACT(Base::Persistence, Base::BaseClass)
41

42

43
//**************************************************************************
44
// Construction/Destruction
45

46

47
//**************************************************************************
48
// separator for other implementation aspects
49

50
unsigned int Persistence::getMemSize() const
51
{
52
    // you have to implement this method in all descending classes!
53
    assert(0);
54
    return 0;
55
}
56

57
void Persistence::Save(Writer& /*writer*/) const
58
{
59
    // you have to implement this method in all descending classes!
60
    assert(0);
61
}
62

63
void Persistence::Restore(XMLReader& /*reader*/)
64
{
65
    // you have to implement this method in all descending classes!
66
    assert(0);
67
}
68

69
void Persistence::SaveDocFile(Writer& /*writer*/) const
70
{}
71

72
void Persistence::RestoreDocFile(Reader& /*reader*/)
73
{}
74

75
std::string Persistence::encodeAttribute(const std::string& str)
76
{
77
    std::string tmp;
78
    for (char it : str) {
79
        switch (it) {
80
            case '<':
81
                tmp += "&lt;";
82
                break;
83
            case '\"':
84
                tmp += "&quot;";
85
                break;
86
            case '\'':
87
                tmp += "&apos;";
88
                break;
89
            case '&':
90
                tmp += "&amp;";
91
                break;
92
            case '>':
93
                tmp += "&gt;";
94
                break;
95
            case '\r':
96
                tmp += "&#13;";
97
                break;
98
            case '\n':
99
                tmp += "&#10;";
100
                break;
101
            case '\t':
102
                tmp += "&#9;";
103
                break;
104
            default:
105
                tmp += it;
106
                break;
107
        }
108
    }
109

110
    return tmp;
111
}
112

113
void Persistence::dumpToStream(std::ostream& stream, int compression)
114
{
115
    // we need to close the zipstream to get a good result, the only way to do this is to delete the
116
    // ZipWriter. Hence the scope...
117
    {
118
        // create the writer
119
        Base::ZipWriter writer(stream);
120
        writer.setLevel(compression);
121
        writer.putNextEntry("Persistence.xml");
122
        writer.setMode("BinaryBrep");
123

124
        // save the content (we need to encapsulate it with xml tags to be able to read single
125
        // element xmls like happen for properties)
126
        writer.Stream() << "<Content>" << std::endl;
127
        Save(writer);
128
        writer.Stream() << "</Content>";
129
        writer.writeFiles();
130
    }
131
}
132

133
void Persistence::restoreFromStream(std::istream& stream)
134
{
135
    zipios::ZipInputStream zipstream(stream);
136
    Base::XMLReader reader("", zipstream);
137

138
    if (!reader.isValid()) {
139
        throw Base::ValueError("Unable to construct reader");
140
    }
141

142
    reader.readElement("Content");
143
    Restore(reader);
144
    reader.readFiles(zipstream);
145
    restoreFinished();
146
}
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.