| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#ifndef SRC_BASE_WRITER_H_ |
| 26 |
#define SRC_BASE_WRITER_H_ |
| 27 |
|
| 28 |
|
| 29 |
#include <set> |
| 30 |
#include <string> |
| 31 |
#include <sstream> |
| 32 |
#include <vector> |
| 33 |
#include <memory> |
| 34 |
|
| 35 |
#include <zipios++/zipoutputstream.h> |
| 36 |
|
| 37 |
#include <Base/UniqueNameManager.h> |
| 38 |
|
| 39 |
#include "FileInfo.h" |
| 40 |
|
| 41 |
|
| 42 |
namespace Base |
| 43 |
{ |
| 44 |
|
| 45 |
class Persistence; |
| 46 |
|
| 47 |
|
| 48 |
/** The Writer class |
| 49 |
* This is an important helper class for the store and retrieval system |
| 50 |
* of persistent objects in FreeCAD. |
| 51 |
* \see Base::Persistence |
| 52 |
* \author Juergen Riegel |
| 53 |
*/ |
| 54 |
class BaseExport Writer |
| 55 |
{ |
| 56 |
private: |
| 57 |
// This overrides UniqueNameManager's suffix-locating function so that the last '.' and |
| 58 |
// everything after it is considered suffix. |
| 59 |
class UniqueFileNameManager: public UniqueNameManager |
| 60 |
{ |
| 61 |
protected: |
| 62 |
std::string::const_reverse_iterator getNameSuffixStartPosition( |
| 63 |
const std::string& name |
| 64 |
) const override |
| 65 |
{ |
| 66 |
// This is an awkward way to do this, because the FileInfo class only yields pieces of |
| 67 |
// the path, not delimiter positions. We can't just use fi.extension().size() because |
| 68 |
// both "xyz" and "xyz." would yield three; we need the length of the extension |
| 69 |
// *including its delimiter* so we use the length difference between the fileName and |
| 70 |
// fileNamePure. |
| 71 |
FileInfo fi(name); |
| 72 |
return name.rbegin() + (fi.fileName().size() - fi.fileNamePure().size()); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
public: |
| 77 |
Writer(); |
| 78 |
virtual ~Writer(); |
| 79 |
|
| 80 |
/// switch the writer in XML only mode (no files allowed) |
| 81 |
void setForceXML(bool on); |
| 82 |
/// check on state |
| 83 |
bool isForceXML() const; |
| 84 |
void setFileVersion(int); |
| 85 |
int getFileVersion() const; |
| 86 |
|
| 87 |
/// put the next entry with a give name |
| 88 |
virtual void putNextEntry(const char* filename, const char* objName = nullptr); |
| 89 |
|
| 90 |
/// insert a file as CDATA section in the XML file |
| 91 |
void insertAsciiFile(const char* FileName); |
| 92 |
/// insert a binary file BASE64 coded as CDATA section in the XML file |
| 93 |
void insertBinFile(const char* FileName); |
| 94 |
/// insert text string as CDATA |
| 95 |
void insertText(const std::string& str); |
| 96 |
|
| 97 |
/** @name additional file writing */ |
| 98 |
//@{ |
| 99 |
/// add a write request of a persistent object |
| 100 |
std::string addFile(const char* Name, const Base::Persistence* Object); |
| 101 |
/// process the requested file storing |
| 102 |
virtual void writeFiles() = 0; |
| 103 |
/// Set mode |
| 104 |
void setMode(const std::string& mode); |
| 105 |
/// Set modes |
| 106 |
void setModes(const std::set<std::string>& modes); |
| 107 |
/// Get mode |
| 108 |
bool getMode(const std::string& mode) const; |
| 109 |
/// Get modes |
| 110 |
std::set<std::string> getModes() const; |
| 111 |
/// Clear mode |
| 112 |
void clearMode(const std::string& mode); |
| 113 |
/// Clear modes |
| 114 |
void clearModes(); |
| 115 |
//@} |
| 116 |
|
| 117 |
/** @name Error handling */ |
| 118 |
//@{ |
| 119 |
void addError(const std::string&); |
| 120 |
void checkErrNo(); |
| 121 |
bool hasErrors() const; |
| 122 |
void clearErrors(); |
| 123 |
std::vector<std::string> getErrors() const; |
| 124 |
//@} |
| 125 |
|
| 126 |
/** @name pretty formatting for XML */ |
| 127 |
//@{ |
| 128 |
/// get the current indentation |
| 129 |
const char* ind() const |
| 130 |
{ |
| 131 |
return indBuf; |
| 132 |
} |
| 133 |
/// increase indentation by one tab |
| 134 |
void incInd(); |
| 135 |
/// decrease indentation by one tab |
| 136 |
void decInd(); |
| 137 |
//@} |
| 138 |
|
| 139 |
virtual std::ostream& Stream() = 0; |
| 140 |
|
| 141 |
/** Create an output stream for storing character content |
| 142 |
* The input is assumed to be valid character with |
| 143 |
* the current XML encoding, and will be enclosed inside |
| 144 |
* CDATA section. The stream will scan the input and |
| 145 |
* properly escape any CDATA ending inside. |
| 146 |
* |
| 147 |
* @param format: If Base64Encoded, the input will be base64 encoded before storing. |
| 148 |
* If Raw, the input is assumed to be valid character with |
| 149 |
* the current XML encoding, and will be enclosed inside |
| 150 |
* CDATA section. The stream will scan the input and |
| 151 |
* properly escape any CDATA ending inside. |
| 152 |
* @return Returns an output stream. |
| 153 |
* |
| 154 |
* You must call endCharStream() to end the current character stream. |
| 155 |
*/ |
| 156 |
std::ostream& beginCharStream(CharStreamFormat format = CharStreamFormat::Raw); |
| 157 |
/** End the current character output stream |
| 158 |
* @return Returns the normal writer stream for convenience |
| 159 |
*/ |
| 160 |
std::ostream& endCharStream(); |
| 161 |
/// Return the current character output stream |
| 162 |
std::ostream& charStream(); |
| 163 |
|
| 164 |
// NOLINTBEGIN |
| 165 |
/// name for underlying file saves |
| 166 |
std::string ObjectName; |
| 167 |
|
| 168 |
protected: |
| 169 |
struct FileEntry |
| 170 |
{ |
| 171 |
std::string FileName; |
| 172 |
const Base::Persistence* Object; |
| 173 |
}; |
| 174 |
std::vector<FileEntry> FileList; |
| 175 |
UniqueFileNameManager FileNameManager; |
| 176 |
std::vector<std::string> Errors; |
| 177 |
std::set<std::string> Modes; |
| 178 |
|
| 179 |
short indent {0}; |
| 180 |
char indBuf[1024] {}; |
| 181 |
|
| 182 |
bool forceXML {false}; |
| 183 |
int fileVersion {1}; |
| 184 |
// NOLINTEND |
| 185 |
|
| 186 |
public: |
| 187 |
Writer(const Writer&) = delete; |
| 188 |
Writer(Writer&&) = delete; |
| 189 |
Writer& operator=(const Writer&) = delete; |
| 190 |
Writer& operator=(Writer&&) = delete; |
| 191 |
|
| 192 |
private: |
| 193 |
std::unique_ptr<std::ostream> CharStream; |
| 194 |
CharStreamFormat charStreamFormat; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** The ZipWriter class |
| 199 |
* This is an important helper class implementation for the store and retrieval system |
| 200 |
* of persistent objects in FreeCAD. |
| 201 |
* \see Base::Persistence |
| 202 |
* \author Juergen Riegel |
| 203 |
*/ |
| 204 |
class BaseExport ZipWriter: public Writer |
| 205 |
{ |
| 206 |
public: |
| 207 |
explicit ZipWriter(const char* FileName); |
| 208 |
explicit ZipWriter(std::ostream&); |
| 209 |
~ZipWriter() override; |
| 210 |
|
| 211 |
void writeFiles() override; |
| 212 |
|
| 213 |
std::ostream& Stream() override |
| 214 |
{ |
| 215 |
return ZipStream; |
| 216 |
} |
| 217 |
|
| 218 |
void setComment(const char* str) |
| 219 |
{ |
| 220 |
ZipStream.setComment(str); |
| 221 |
} |
| 222 |
void setLevel(int level) |
| 223 |
{ |
| 224 |
ZipStream.setLevel(level); |
| 225 |
} |
| 226 |
void putNextEntry(const char* filename, const char* objName = nullptr) override; |
| 227 |
|
| 228 |
ZipWriter(const ZipWriter&) = delete; |
| 229 |
ZipWriter(ZipWriter&&) = delete; |
| 230 |
ZipWriter& operator=(const ZipWriter&) = delete; |
| 231 |
ZipWriter& operator=(ZipWriter&&) = delete; |
| 232 |
|
| 233 |
private: |
| 234 |
zipios::ZipOutputStream ZipStream; |
| 235 |
}; |
| 236 |
|
| 237 |
/** The StringWriter class |
| 238 |
* This is an important helper class implementation for the store and retrieval system |
| 239 |
* of objects in FreeCAD. |
| 240 |
* \see Base::Persistence |
| 241 |
* \author Juergen Riegel |
| 242 |
*/ |
| 243 |
class BaseExport StringWriter: public Writer |
| 244 |
{ |
| 245 |
|
| 246 |
public: |
| 247 |
std::ostream& Stream() override |
| 248 |
{ |
| 249 |
return StrStream; |
| 250 |
} |
| 251 |
std::string getString() const |
| 252 |
{ |
| 253 |
return StrStream.str(); |
| 254 |
} |
| 255 |
void writeFiles() override |
| 256 |
{} |
| 257 |
|
| 258 |
private: |
| 259 |
std::stringstream StrStream; |
| 260 |
}; |
| 261 |
|
| 262 |
/*! The FileWriter class |
| 263 |
This class writes out the data into files into a given directory name. |
| 264 |
\see Base::Persistence |
| 265 |
\author Werner Mayer |
| 266 |
*/ |
| 267 |
class BaseExport FileWriter: public Writer |
| 268 |
{ |
| 269 |
public: |
| 270 |
explicit FileWriter(const char* DirName); |
| 271 |
~FileWriter() override; |
| 272 |
|
| 273 |
void putNextEntry(const char* filename, const char* objName = nullptr) override; |
| 274 |
void writeFiles() override; |
| 275 |
|
| 276 |
std::ostream& Stream() override |
| 277 |
{ |
| 278 |
return FileStream; |
| 279 |
} |
| 280 |
void close() |
| 281 |
{ |
| 282 |
FileStream.close(); |
| 283 |
} |
| 284 |
/*! |
| 285 |
This method can be re-implemented in sub-classes to avoid |
| 286 |
to write out certain objects. The default implementation |
| 287 |
always returns true. |
| 288 |
*/ |
| 289 |
virtual bool shouldWrite(const std::string& name, const Base::Persistence* Object) const; |
| 290 |
|
| 291 |
FileWriter(const FileWriter&) = delete; |
| 292 |
FileWriter(FileWriter&&) = delete; |
| 293 |
FileWriter& operator=(const FileWriter&) = delete; |
| 294 |
FileWriter& operator=(FileWriter&&) = delete; |
| 295 |
|
| 296 |
protected: |
| 297 |
// NOLINTBEGIN |
| 298 |
std::string DirName; |
| 299 |
std::ofstream FileStream; |
| 300 |
// NOLINTEND |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
} // namespace Base |
| 305 |
|
| 306 |
|
| 307 |
#endif // SRC_BASE_WRITER_H_ |