Audacity 3.2.0
XMLWriter.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 XMLWriter.h
6
7 Leland Lucius, Dmitry Vedenko
8
9**********************************************************************/
10#ifndef __AUDACITY_XML_XML_FILE_WRITER__
11#define __AUDACITY_XML_XML_FILE_WRITER__
12
13#include <vector>
14#include <wx/ffile.h> // to inherit
15
16#include <string_view> // For UTF8 writer
17#include "MemoryStream.h"
18
19#include "FileException.h"
20
21#include "Identifier.h"
22
26class XML_API XMLWriter /* not final */ {
27
28 public:
29
30 XMLWriter();
31 virtual ~XMLWriter();
32
33 virtual void StartTag(const wxString &name);
34 virtual void EndTag(const wxString &name);
35
36 // nonvirtual pass-through
37 void WriteAttr(const wxString &name, const Identifier &value)
38 // using GET once here, permitting Identifiers in XML,
39 // so no need for it at each WriteAttr call
40 { WriteAttr( name, value.GET() ); }
41
42 virtual void WriteAttr(const wxString &name, const wxString &value);
43 virtual void WriteAttr(const wxString &name, const wxChar *value);
44
45 virtual void WriteAttr(const wxString &name, int value);
46 virtual void WriteAttr(const wxString &name, bool value);
47 virtual void WriteAttr(const wxString &name, long value);
48 virtual void WriteAttr(const wxString &name, long long value);
49 virtual void WriteAttr(const wxString &name, size_t value);
50 virtual void WriteAttr(const wxString &name, float value, int digits = -1);
51 virtual void WriteAttr(const wxString &name, double value, int digits = -1);
52
53 virtual void WriteData(const wxString &value);
54
55 virtual void WriteSubTree(const wxString &value);
56
57 virtual void Write(const wxString &data) = 0;
58
59 // Escape a string, replacing certain characters with their
60 // XML encoding, i.e. '<' becomes '&lt;'
61 static wxString XMLEsc(const wxString & s);
62
63 protected:
64
65 bool mInTag;
66 int mDepth;
67 wxArrayString mTagstack;
68 std::vector<int> mHasKids;
69
70};
71
75
84class XML_API XMLFileWriter final : private wxFFile, public XMLWriter {
85
86 public:
87
91 const FilePath &outputPath, const TranslatableString &caption,
92 bool keepBackup = false );
93
94 virtual ~XMLFileWriter();
95
100 void Commit();
101
103 void PreCommit();
104
107 void PostCommit();
108
110 void Write(const wxString &data) override;
111
112 FilePath GetBackupName() const { return mBackupName; }
113
114 private:
115
117 const wxFileName &fileName, const TranslatableString &caption)
118 {
119 throw FileException{ FileException::Cause::Write, fileName, caption };
120 }
121
124 void CloseWithoutEndingTags(); // for auto-save files
125
129 const bool mKeepBackup;
130
131 wxFFile mBackupFile;
132
133 bool mCommitted{ false };
134};
135
139class XML_API XMLStringWriter final : public wxString, public XMLWriter {
140
141 public:
142
143 XMLStringWriter(size_t initialSize = 0);
144 virtual ~XMLStringWriter();
145
146 void Write(const wxString &data) override;
147
148 private:
149
150};
151
152class XML_API XMLUtf8BufferWriter final
153{
154public:
155 void StartTag(const std::string_view& name);
156 void EndTag(const std::string_view& name);
157
158 void WriteAttr(const std::string_view& name, const Identifier& value);
159 // using GET once here, permitting Identifiers in XML,
160 // so no need for it at each WriteAttr call
161
162 void WriteAttr(const std::string_view& name, const std::string_view& value);
163
164 void WriteAttr(const std::string_view& name, int value);
165 void WriteAttr(const std::string_view& name, bool value);
166 void WriteAttr(const std::string_view& name, long value);
167 void WriteAttr(const std::string_view& name, long long value);
168 void WriteAttr(const std::string_view& name, size_t value);
169 void WriteAttr(const std::string_view& name, float value, int digits = -1);
170 void WriteAttr(const std::string_view& name, double value, int digits = -1);
171
172 void WriteData(const std::string_view& value);
173
174 void WriteSubTree(const std::string_view& value);
175
176 void Write(const std::string_view& value);
177
178 MemoryStream ConsumeResult();
179private:
180 void WriteEscaped(const std::string_view& value);
181
183
184 bool mInTag { false };
185};
186
187#endif
const TranslatableString name
Definition: Distortion.cpp:82
MessageBoxException for failures of file operations.
wxString FilePath
Definition: Project.h:20
Thrown for failure of file or database operations in deeply nested places.
Definition: FileException.h:19
@ Write
most important to detect when storage space is exhausted
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
const wxString & GET() const
Explicit conversion to wxString, meant to be ugly-looking and demanding of a comment why it's correct...
Definition: Identifier.h:66
A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view.
Definition: MemoryStream.h:31
Holds a msgid for the translation catalog; may also bind format arguments.
Wrapper to output XML data to files.
Definition: XMLWriter.h:84
wxFFile mBackupFile
Definition: XMLWriter.h:131
FilePath mBackupName
Definition: XMLWriter.h:128
const TranslatableString mCaption
Definition: XMLWriter.h:127
FilePath GetBackupName() const
Definition: XMLWriter.h:112
const bool mKeepBackup
Definition: XMLWriter.h:129
void ThrowException(const wxFileName &fileName, const TranslatableString &caption)
Definition: XMLWriter.h:116
const FilePath mOutputPath
Definition: XMLWriter.h:126
Wrapper to output XML data to strings.
Definition: XMLWriter.h:139
MemoryStream mStream
Definition: XMLWriter.h:182
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
std::vector< int > mHasKids
Definition: XMLWriter.h:68
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:37
wxArrayString mTagstack
Definition: XMLWriter.h:67
bool mInTag
Definition: XMLWriter.h:65
virtual void Write(const wxString &data)=0
int mDepth
Definition: XMLWriter.h:66