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 private:
60 // Escape a string, replacing certain characters with their
61 // XML encoding, i.e. '<' becomes '&lt;'
62 static wxString XMLEsc(const wxString & s);
63
64 protected:
65
66 bool mInTag;
67 int mDepth;
68 wxArrayString mTagstack;
69 std::vector<int> mHasKids;
70
71};
72
76
85class XML_API XMLFileWriter final : private wxFFile, public XMLWriter {
86
87 public:
88
92 const FilePath &outputPath, const TranslatableString &caption,
93 bool keepBackup = false );
94
95 virtual ~XMLFileWriter();
96
101 void Commit();
102
104 void PreCommit();
105
108 void PostCommit();
109
111 void Write(const wxString &data) override;
112
113 FilePath GetBackupName() const { return mBackupName; }
114
115 private:
116
118 const wxFileName &fileName, const TranslatableString &caption)
119 {
120 throw FileException{ FileException::Cause::Write, fileName, caption };
121 }
122
125 void CloseWithoutEndingTags(); // for auto-save files
126
130 const bool mKeepBackup;
131
132 wxFFile mBackupFile;
133
134 bool mCommitted{ false };
135};
136
140class XML_API XMLStringWriter final : public wxString, public XMLWriter {
141
142 public:
143
144 XMLStringWriter(size_t initialSize = 0);
145 virtual ~XMLStringWriter();
146
147 void Write(const wxString &data) override;
148
149 private:
150
151};
152
153class XML_API XMLUtf8BufferWriter final
154{
155public:
156 void StartTag(const std::string_view& name);
157 void EndTag(const std::string_view& name);
158
159 void WriteAttr(const std::string_view& name, const Identifier& value);
160 // using GET once here, permitting Identifiers in XML,
161 // so no need for it at each WriteAttr call
162
163 void WriteAttr(const std::string_view& name, const std::string_view& value);
164
165 void WriteAttr(const std::string_view& name, int value);
166 void WriteAttr(const std::string_view& name, bool value);
167 void WriteAttr(const std::string_view& name, long value);
168 void WriteAttr(const std::string_view& name, long long value);
169 void WriteAttr(const std::string_view& name, size_t value);
170 void WriteAttr(const std::string_view& name, float value, int digits = -1);
171 void WriteAttr(const std::string_view& name, double value, int digits = -1);
172
173 void WriteData(const std::string_view& value);
174
175 void WriteSubTree(const std::string_view& value);
176
177 void Write(const std::string_view& value);
178
179 MemoryStream ConsumeResult();
180private:
181 void WriteEscaped(const std::string_view& value);
182
184
185 bool mInTag { false };
186};
187
188#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.
Holds a msgid for the translation catalog; may also bind format arguments.
Wrapper to output XML data to files.
Definition: XMLWriter.h:85
wxFFile mBackupFile
Definition: XMLWriter.h:132
FilePath mBackupName
Definition: XMLWriter.h:129
const TranslatableString mCaption
Definition: XMLWriter.h:128
FilePath GetBackupName() const
Definition: XMLWriter.h:113
const bool mKeepBackup
Definition: XMLWriter.h:130
void ThrowException(const wxFileName &fileName, const TranslatableString &caption)
Definition: XMLWriter.h:117
const FilePath mOutputPath
Definition: XMLWriter.h:127
Wrapper to output XML data to strings.
Definition: XMLWriter.h:140
MemoryStream mStream
Definition: XMLWriter.h:183
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
std::vector< int > mHasKids
Definition: XMLWriter.h:69
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:37
wxArrayString mTagstack
Definition: XMLWriter.h:68
bool mInTag
Definition: XMLWriter.h:66
virtual void Write(const wxString &data)=0
int mDepth
Definition: XMLWriter.h:67