Debian Codegraph @

src/App/ProjectFile.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24
25
26 #include <cassert>
27 #include <xercesc/util/PlatformUtils.hpp>
28 #include <xercesc/util/XercesVersion.hpp>
29 #include <xercesc/dom/DOM.hpp>
30 #include <xercesc/dom/DOMImplementation.hpp>
31 #include <xercesc/dom/DOMImplementationLS.hpp>
32 #include <xercesc/framework/StdOutFormatTarget.hpp>
33 #include <xercesc/framework/LocalFileFormatTarget.hpp>
34 #include <xercesc/framework/LocalFileInputSource.hpp>
35 #include <xercesc/parsers/XercesDOMParser.hpp>
36 #include <xercesc/util/XMLUni.hpp>
37 #include <xercesc/util/XMLUniDefs.hpp>
38 #include <xercesc/util/XMLString.hpp>
39 #include <xercesc/sax/ErrorHandler.hpp>
40 #include <xercesc/sax/SAXParseException.hpp>
41 #include <sstream>
42
43 #include <zipios++/zipios-config.h>
44 #include <zipios++/zipfile.h>
45 #include <zipios++/zipinputstream.h>
46 #include <zipios++/zipoutputstream.h>
47 #include <zipios++/meta-iostreams.h>
48
49 #include "ProjectFile.h"
50 #include "DocumentObject.h"
51 #include <Base/FileInfo.h>
52 #include <Base/InputSource.h>
53 #include <Base/Reader.h>
54 #include <Base/Writer.h>
55 #include <Base/Stream.h>
56 #include <Base/XMLTools.h>
57
58 #ifndef XERCES_CPP_NAMESPACE_BEGIN
59 #define XERCES_CPP_NAMESPACE_QUALIFIER
60 using namespace XERCES_CPP_NAMESPACE;
61 #else
62 XERCES_CPP_NAMESPACE_USE
63 #endif
64 using namespace App;
65
66 namespace
67 {
68
69 class ZipTools
70 {
71 public:
72 static std::unique_ptr<zipios::ZipFile> open(const std::string& file)
73 {
74 std::unique_ptr<zipios::ZipFile> project;
75 try {
76 project = std::make_unique<zipios::ZipFile>(file);
77 if (!project->isValid()) {
78 project.reset();
79 }
80 }
81 catch (const std::exception&) {
82 }
83
84 return project;
85 }
86 };
87
88 class DocumentMetadata
89 {
90 public:
91 explicit DocumentMetadata(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* xmlDocument)
92 : xmlDocument {xmlDocument}
93 {}
94
95 ProjectFile::Metadata getMetadata() const
96 {
97 return metadata;
98 }
99
100 void readXML()
101 {
102 readProgramVersion();
103
104 std::map<std::string, std::string> propMap = initMap();
105
106 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Properties").unicodeForm());
107 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
108 DOMNode* node = nodes->item(i);
109 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
110 auto elem = static_cast<DOMElement*>(node); // NOLINT
111 DOMNodeList* propList = elem->getElementsByTagName(XStrLiteral("Property").unicodeForm());
112 for (XMLSize_t j = 0; j < propList->getLength(); j++) {
113 DOMNode* propNode = propList->item(j);
114 readProperty(propNode, propMap);
115 }
116 }
117 break;
118 }
119
120 setMetadata(propMap);
121 }
122
123 private:
124 void readProgramVersion()
125 {
126 if (DOMNodeList* nodes =
127 xmlDocument->getElementsByTagName(XStrLiteral("Document").unicodeForm())) {
128 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
129 DOMNode* node = nodes->item(i);
130 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
131 DOMNode* nameAttr =
132 node->getAttributes()->getNamedItem(XStrLiteral("ProgramVersion").unicodeForm());
133 if (nameAttr) {
134 std::string value = StrX(nameAttr->getNodeValue()).c_str();
135 metadata.programVersion = value;
136 break;
137 }
138 }
139 }
140 }
141 }
142 static std::map<std::string, std::string> initMap()
143 {
144 // clang-format off
145 std::map<std::string, std::string> propMap = {{"Comment", ""},
146 {"Company", ""},
147 {"CreatedBy", ""},
148 {"CreationDate", ""},
149 {"Label", ""},
150 {"LastModifiedBy", ""},
151 {"LastModifiedDate", ""},
152 {"License", ""},
153 {"LicenseURL", ""},
154 {"Uid", ""}};
155 return propMap;
156 // clang-format on
157 }
158
159 void setMetadata(const std::map<std::string, std::string>& propMap)
160 {
161 metadata.comment = propMap.at("Comment");
162 metadata.company = propMap.at("Company");
163 metadata.createdBy = propMap.at("CreatedBy");
164 metadata.creationDate = propMap.at("CreationDate");
165 metadata.label = propMap.at("Label");
166 metadata.lastModifiedBy = propMap.at("LastModifiedBy");
167 metadata.lastModifiedDate = propMap.at("LastModifiedDate");
168 metadata.license = propMap.at("License");
169 metadata.licenseURL = propMap.at("LicenseURL");
170 metadata.uuid = propMap.at("Uid");
171 }
172
173 static void readProperty(DOMNode* propNode, std::map<std::string, std::string>& propMap)
174 {
175 DOMNode* nameAttr = propNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
176 if (nameAttr) {
177 std::string name = StrX(nameAttr->getNodeValue()).c_str();
178 auto it = propMap.find(name);
179 if (it != propMap.end()) {
180 it->second = readValue(propNode);
181 }
182 }
183 }
184
185 static std::string readValue(DOMNode* node)
186 {
187 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
188 if (DOMElement* child =
189 static_cast<DOMElement*>(node)->getFirstElementChild()) { // NOLINT
190 if (DOMNode* nameAttr =
191 child->getAttributes()->getNamedItem(XStrLiteral("value").unicodeForm())) {
192 std::string value = StrX(nameAttr->getNodeValue()).c_str();
193 return value;
194 }
195 }
196 }
197
198 return {};
199 }
200
201 private:
202 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* xmlDocument;
203 ProjectFile::Metadata metadata;
204 };
205 } // namespace
206
207 ProjectFile::ProjectFile()
208 : xmlDocument(nullptr)
209 {}
210
211 ProjectFile::ProjectFile(std::string zipArchive)
212 : stdFile(std::move(zipArchive))
213 , xmlDocument(nullptr)
214 {}
215
216 ProjectFile::~ProjectFile()
217 {
218 delete xmlDocument;
219 }
220
221 void ProjectFile::setProjectFile(const std::string& zipArchive)
222 {
223 stdFile = zipArchive;
224 delete xmlDocument;
225 xmlDocument = nullptr;
226 }
227
228 bool ProjectFile::loadDocument()
229 {
230 if (xmlDocument) {
231 return true; // already loaded
232 }
233
234 auto project = ZipTools::open(stdFile);
235 if (!project) {
236 return false;
237 }
238
239 std::unique_ptr<std::istream> str(project->getInputStream("Document.xml"));
240 if (str) {
241 std::unique_ptr<XercesDOMParser> parser(new XercesDOMParser);
242 parser->setValidationScheme(XercesDOMParser::Val_Auto);
243 parser->setDoNamespaces(false);
244 parser->setDoSchema(false);
245 parser->setValidationSchemaFullChecking(false);
246 parser->setCreateEntityReferenceNodes(false);
247
248 try {
249 Base::StdInputSource inputSource(*str, stdFile.c_str());
250 parser->parse(inputSource);
251 if (parser->getErrorCount() > 0) {
252 return false;
253 }
254 xmlDocument = parser->adoptDocument();
255 return true;
256 }
257 catch (const XMLException&) {
258 return false;
259 }
260 catch (const DOMException&) {
261 return false;
262 }
263 }
264
265 return false;
266 }
267
268 ProjectFile::Metadata ProjectFile::getMetadata() const
269 {
270 if (!xmlDocument) {
271 return {};
272 }
273
274 DocumentMetadata reader(xmlDocument);
275 reader.readXML();
276 return reader.getMetadata();
277 }
278
279 std::list<ProjectFile::Object> ProjectFile::getObjects() const
280 {
281 std::list<Object> names;
282 if (!xmlDocument) {
283 return names;
284 }
285
286 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
287 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
288 DOMNode* node = nodes->item(i);
289 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
290 DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
291 XStrLiteral("Object").unicodeForm()); // NOLINT
292 for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
293 DOMNode* objectNode = objectList->item(j);
294 DOMNode* typeAttr =
295 objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
296 DOMNode* nameAttr =
297 objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
298 if (typeAttr && nameAttr) {
299 Object obj;
300 obj.name = StrX(nameAttr->getNodeValue()).c_str();
301 obj.type = Base::Type::fromName(StrX(typeAttr->getNodeValue()).c_str());
302 names.push_back(obj);
303 }
304 }
305 }
306 }
307
308 return names;
309 }
310
311 std::list<std::string> ProjectFile::getObjectsOfType(const Base::Type& typeId) const
312 {
313 std::list<std::string> names;
314 if (!xmlDocument) {
315 return names;
316 }
317
318 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
319 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
320 DOMNode* node = nodes->item(i);
321 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
322 DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
323 XStrLiteral("Object").unicodeForm()); // NOLINT
324 for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
325 DOMNode* objectNode = objectList->item(j);
326 DOMNode* typeAttr =
327 objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
328 DOMNode* nameAttr =
329 objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
330 if (typeAttr && nameAttr) {
331 if (Base::Type::fromName(StrX(typeAttr->getNodeValue()).c_str()) == typeId) {
332 names.emplace_back(StrX(nameAttr->getNodeValue()).c_str());
333 }
334 }
335 }
336 }
337 }
338
339 return names;
340 }
341
342 bool ProjectFile::restoreObject(const std::string& name, App::PropertyContainer* obj, bool verbose)
343 {
344 Base::FileInfo fi(stdFile);
345 Base::ifstream file(fi, std::ios::in | std::ios::binary);
346
347 zipios::ZipInputStream zipstream(file);
348 Base::XMLReader reader(stdFile.c_str(), zipstream);
349 reader.setVerbose(verbose);
350
351 if (!reader.isValid()) {
352 return false;
353 }
354
355 // skip document properties
356 reader.readElement("Properties");
357 reader.readEndElement("Properties");
358
359 // skip objects
360 reader.readElement("Objects");
361 reader.readEndElement("Objects");
362
363 reader.readElement("ObjectData");
364 long Cnt = reader.getAttribute<long>("Count");
365 for (long i = 0; i < Cnt; i++) {
366 reader.readElement("Object");
367 std::string nameAttr = reader.getAttribute<const char*>("name");
368
369 if (nameAttr == name) {
370 // obj->StatusBits.set(4);
371 obj->Restore(reader);
372 // obj->StatusBits.reset(4);
373 }
374 reader.readEndElement("Object");
375 }
376 reader.readEndElement("ObjectData");
377
378 reader.readFiles(zipstream);
379
380 return true;
381 }
382
383 Base::Type ProjectFile::getTypeId(const std::string& name) const
384 {
385 // <Objects Count="1">
386 // <Object type="Mesh::MeshFeature" name="Mesh" />
387 // <Objects/>
388 if (!xmlDocument) {
389 return Base::Type::BadType;
390 }
391
392 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
393 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
394 DOMNode* node = nodes->item(i);
395 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
396 DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
397 XStrLiteral("Object").unicodeForm()); // NOLINT
398 for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
399 DOMNode* objectNode = objectList->item(j);
400 DOMNode* typeAttr =
401 objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
402 DOMNode* nameAttr =
403 objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
404 if (typeAttr && nameAttr) {
405 if (strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
406 std::string typeId = StrX(typeAttr->getNodeValue()).c_str();
407 return Base::Type::fromName(typeId.c_str());
408 }
409 }
410 }
411 }
412 }
413
414 return Base::Type::BadType;
415 }
416
417 std::list<ProjectFile::PropertyFile> ProjectFile::getPropertyFiles(const std::string& name) const
418 {
419 // <ObjectData Count="1">
420 // <Object name="Mesh">
421 // <Properties Count="1">
422 // <Property name="Mesh" type="Mesh::PropertyMeshKernel">
423 // <Mesh file="MeshKernel.bms"/>
424 // <Property/>
425 // <Properties/>
426 // <Object/>
427 // <ObjectData/>
428 if (!xmlDocument) {
429 return {};
430 }
431
432 std::list<PropertyFile> files;
433 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("ObjectData").unicodeForm());
434 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
435 DOMNode* node = nodes->item(i);
436 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
437 DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
438 XStrLiteral("Object").unicodeForm()); // NOLINT
439 for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
440 DOMNode* objectNode = objectList->item(j);
441 DOMNode* nameAttr =
442 objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
443 if (nameAttr && strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
444 // now go recursively through the sub-tree (i.e. the properties) and collect
445 // every file attribute
446 findFiles(objectNode, files);
447 break;
448 }
449 }
450 }
451 }
452 return files;
453 }
454
455 void ProjectFile::findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node,
456 std::list<ProjectFile::PropertyFile>& files) const
457 {
458 if (node->hasAttributes()) {
459 ProjectFile::PropertyFile prop;
460 DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStrLiteral("file").unicodeForm());
461 if (fileAttr) {
462 DOMNode* parentNode = node->getParentNode();
463 if (parentNode) {
464 DOMNode* nameAttr =
465 parentNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
466 if (nameAttr) {
467 prop.name = StrX(nameAttr->getNodeValue()).c_str();
468 }
469
470 DOMNode* typeAttr =
471 parentNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
472 if (typeAttr) {
473 prop.type = Base::Type::fromName(StrX(typeAttr->getNodeValue()).c_str());
474 }
475 }
476
477 prop.file = StrX(fileAttr->getNodeValue()).c_str();
478 files.push_back(prop);
479 }
480 }
481
482 DOMNodeList* subNodes = node->getChildNodes();
483 for (XMLSize_t i = 0; i < subNodes->getLength(); i++) {
484 DOMNode* child = subNodes->item(i);
485 findFiles(child, files);
486 }
487 }
488
489 bool ProjectFile::containsFile(const std::string& name) const
490 {
491 zipios::ZipFile project(stdFile);
492 auto entry = project.getEntry(name);
493 return entry != nullptr;
494 }
495
496 std::list<std::string> ProjectFile::getInputFiles(const std::string& name) const
497 {
498 // <ObjectData Count="1">
499 // <Object name="Mesh">
500 // <Properties Count="1">
501 // <Property name="Mesh" type="Mesh::PropertyMeshKernel">
502 // <Mesh file="MeshKernel.bms"/>
503 // <Property/>
504 // <Properties/>
505 // <Object/>
506 // <ObjectData/>
507 if (!xmlDocument) {
508 return {};
509 }
510
511 std::list<std::string> files;
512 DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("ObjectData").unicodeForm());
513 for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
514 DOMNode* node = nodes->item(i);
515 if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
516 DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
517 XStrLiteral("Object").unicodeForm()); // NOLINT
518 for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
519 DOMNode* objectNode = objectList->item(j);
520 DOMNode* nameAttr =
521 objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
522 if (nameAttr && strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
523 // now go recursively through the sub-tree (i.e. the properties) and collect
524 // every file attribute
525 findFiles(objectNode, files);
526 break;
527 }
528 }
529 }
530 }
531 return files;
532 }
533
534 void ProjectFile::findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node,
535 std::list<std::string>& files) const
536 {
537 if (node->hasAttributes()) {
538 DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStrLiteral("file").unicodeForm());
539 if (fileAttr) {
540 files.emplace_back(StrX(fileAttr->getNodeValue()).c_str());
541 }
542 }
543
544 DOMNodeList* subNodes = node->getChildNodes();
545 for (XMLSize_t i = 0; i < subNodes->getLength(); i++) {
546 DOMNode* child = subNodes->item(i);
547 findFiles(child, files);
548 }
549 }
550
551 std::string ProjectFile::extractInputFile(const std::string& name)
552 {
553 zipios::ZipFile project(stdFile);
554 std::unique_ptr<std::istream> str(project.getInputStream(name));
555 if (str) {
556 // write it to a tmp. file as writing to the string stream
557 // might take too long
558 Base::FileInfo fi(Base::FileInfo::getTempFileName());
559 Base::ofstream file(fi, std::ios::out | std::ios::binary);
560 std::streambuf* buf = file.rdbuf();
561 (*str) >> buf;
562 file.flush();
563 file.close();
564 return fi.filePath();
565 }
566
567 return {};
568 }
569
570 void ProjectFile::readInputFile(const std::string& name, std::ostream& str)
571 {
572 Base::FileInfo fi(extractInputFile(name));
573 if (fi.exists()) {
574 Base::ifstream file(fi, std::ios::in | std::ios::binary);
575 file >> str.rdbuf();
576 file.close();
577 fi.deleteFile();
578 }
579 }
580
581 // Read the given input file from the zip directly into the given stream (not using a temporary
582 // file)
583 void ProjectFile::readInputFileDirect(const std::string& name, std::ostream& str)
584 {
585 zipios::ZipFile project(stdFile);
586 std::unique_ptr<std::istream> istr(project.getInputStream(name));
587 if (istr) {
588 *istr >> str.rdbuf();
589 }
590 }
591
592 std::string ProjectFile::replaceInputFile(const std::string& name, std::istream& inp)
593 {
594 // create a new zip file with the name '<zipfile>.<uuid>'
595 std::string uuid = Base::Uuid::createUuid();
596 std::string fn = stdFile;
597 fn += ".";
598 fn += uuid;
599 Base::FileInfo tmp(fn);
600 Base::ofstream newZip(tmp, std::ios::out | std::ios::binary);
601
602 // standard compression
603 const int compressionLevel = 6;
604 zipios::ZipOutputStream outZip(newZip);
605 outZip.setComment("FreeCAD Document");
606 outZip.setLevel(compressionLevel);
607
608 // open the original zip file
609 zipios::ZipFile project(stdFile);
610 zipios::ConstEntries files = project.entries();
611 for (const auto& it : files) {
612 std::string file = it->getFileName();
613 outZip.putNextEntry(file);
614 if (file == name) {
615 inp >> outZip.rdbuf();
616 }
617 else {
618 std::unique_ptr<std::istream> str(project.getInputStream(file));
619 if (str) {
620 *str >> outZip.rdbuf();
621 }
622 }
623 }
624
625 project.close();
626 outZip.close();
627 newZip.close();
628
629 return fn;
630 }
631
632 std::string ProjectFile::replaceInputFiles(const std::map<std::string, std::istream*>& inp)
633 {
634 // create a new zip file with the name '<zipfile>.<uuid>'
635 std::string uuid = Base::Uuid::createUuid();
636 std::string fn = stdFile;
637 fn += ".";
638 fn += uuid;
639 Base::FileInfo tmp(fn);
640 Base::ofstream newZip(tmp, std::ios::out | std::ios::binary);
641
642 // standard compression
643 const int compressionLevel = 6;
644 zipios::ZipOutputStream outZip(newZip);
645 outZip.setComment("FreeCAD Document");
646 outZip.setLevel(compressionLevel);
647
648 // open the original zip file
649 zipios::ZipFile project(stdFile);
650 zipios::ConstEntries files = project.entries();
651 for (const auto& it : files) {
652 std::string file = it->getFileName();
653 outZip.putNextEntry(file);
654
655 auto jt = inp.find(file);
656 if (jt != inp.end()) {
657 *jt->second >> outZip.rdbuf();
658 }
659 else {
660 std::unique_ptr<std::istream> str(project.getInputStream(file));
661 if (str) {
662 *str >> outZip.rdbuf();
663 }
664 }
665 }
666
667 project.close();
668 outZip.close();
669 newZip.close();
670
671 return fn;
672 }
673
674 std::string ProjectFile::replacePropertyFiles(const std::map<std::string, App::Property*>& props)
675 {
676 // create a new zip file with the name '<zipfile>.<uuid>'
677 std::string uuid = Base::Uuid::createUuid();
678 std::string fn = stdFile;
679 fn += ".";
680 fn += uuid;
681 Base::FileInfo tmp(fn);
682 Base::ofstream newZip(tmp, std::ios::out | std::ios::binary);
683
684 // open extra scope
685 {
686 // standard compression
687 const int compressionLevel = 6;
688 Base::ZipWriter writer(newZip);
689 writer.setComment("FreeCAD Document");
690 writer.setLevel(compressionLevel);
691
692 // open the original zip file
693 zipios::ZipFile project(stdFile);
694 zipios::ConstEntries files = project.entries();
695 for (const auto& it : files) {
696 std::string file = it->getFileName();
697 writer.putNextEntry(file.c_str());
698
699 auto jt = props.find(file);
700 if (jt != props.end()) {
701 jt->second->SaveDocFile(writer);
702 }
703 else {
704 std::unique_ptr<std::istream> str(project.getInputStream(file));
705 if (str) {
706 *str >> writer.Stream().rdbuf();
707 }
708 }
709 }
710 project.close();
711 }
712
713 return fn;
714 }
715
716 bool ProjectFile::replaceProjectFile(const std::string& name, bool keepfile)
717 {
718 std::string uuid = Base::Uuid::createUuid();
719 std::string fn = stdFile;
720 fn += ".";
721 fn += uuid;
722
723 // Now rename the original file to something unique
724 Base::FileInfo orig(stdFile);
725 if (!orig.renameFile(fn.c_str())) {
726 return false;
727 }
728 orig.setFile(fn);
729
730 // rename the tmp.file to the original file name
731 Base::FileInfo other(name);
732 if (!other.renameFile(stdFile.c_str())) {
733 return false;
734 }
735
736 // and delete the renamed original file.
737 if (!keepfile) {
738 if (!orig.deleteFile()) {
739 return false;
740 }
741 }
742
743 return true;
744 }