Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
filedocumentwriter.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4
#
include
"filedocumentwriter.h"
5
6
#
include
"location.h"
7
8
#
include
<
QtCore
/
qdir
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
using
namespace
Qt::StringLiterals;
13
14
/*!
15
\class FileDocumentWriter
16
\internal
17
\brief Production implementation of DocumentWriter that writes to files.
18
19
FileDocumentWriter manages the lifecycle of output files, replacing the
20
Generator's beginSubPage()/endSubPage() pattern with an explicit interface.
21
22
\section1 Usage Pattern
23
24
\code
25
FileDocumentWriter writer(context);
26
writer.beginDocument("output.html");
27
writer.write("<html>");
28
writer.writeLine("<body>Content</body>");
29
writer.write("</html>");
30
writer.endDocument();
31
\endcode
32
33
\section1 Error Handling
34
35
If the file cannot be opened, beginDocument() reports an error and
36
subsequent write calls become no-ops.
37
38
\sa DocumentWriter, StringDocumentWriter, OutputContext
39
*/
40
41
/*!
42
\fn const OutputContext &FileDocumentWriter::context() const
43
44
Returns the output context used by this writer.
45
*/
46
47
/*!
48
The constructor takes a \a context.
49
50
\sa OutputContext
51
*/
52
FileDocumentWriter::FileDocumentWriter(
OutputContext
context)
53
: m_context(std::move(context))
54
{
55
}
56
57
/*!
58
The destructor ensures a call to endDocument before the object is destroyed.
59
60
\sa endDocument()
61
*/
62
FileDocumentWriter::~FileDocumentWriter()
63
{
64
endDocument();
65
}
66
67
/*!
68
Opens a new document with the given \a fileName for writing.
69
70
If a document is already open, it will be closed first.
71
*/
72
void
FileDocumentWriter::beginDocument(
const
QString &fileName)
73
{
74
// Close any currently open document
75
endDocument();
76
77
const
QString &fullPath = m_context.outputDir.absoluteFilePath(fileName);
78
m_currentFileName = fileName;
79
80
// Ensure parent directory exists
81
QFileInfo fileInfo(fullPath);
82
QDir parentDir = fileInfo.dir();
83
if
(!parentDir.exists()) {
84
if
(!parentDir.mkpath(u"."_s)) {
85
Location
{
}
.error(u"Cannot create output directory '%1'"_s.arg(parentDir.path()));
86
return
;
87
}
88
}
89
90
// Open the file
91
m_file = std::make_unique<QFile>(fullPath);
92
if
(!m_file->open(QIODevice::WriteOnly | QIODevice::Text)) {
93
Location
{
}
.error(u"Cannot open file '%1' for writing: %2"_s
94
.arg(fullPath, m_file->errorString()));
95
m_file.reset();
96
m_currentFileName.clear();
97
return
;
98
}
99
100
m_stream = std::make_unique<QTextStream>(m_file.get());
101
}
102
103
/*!
104
Closes the current document, flushing any buffered content.
105
Safe to call even if no document is open.
106
*/
107
void
FileDocumentWriter::endDocument()
108
{
109
if
(m_stream) {
110
m_stream->flush();
111
m_stream.reset();
112
}
113
if
(m_file) {
114
m_file->close();
115
m_file.reset();
116
}
117
m_currentFileName.clear();
118
}
119
120
/*!
121
Closes and removes the current document after its render fails.
122
Safe to call when no document is open.
123
*/
124
void
FileDocumentWriter::discardDocument()
125
{
126
m_stream.reset();
127
if
(m_file) {
128
m_file->close();
129
if
(!m_file->remove())
130
Location
{
}
.error(u"Cannot remove failed output file '%1': %2"_s
131
.arg(m_file->fileName(), m_file->errorString()));
132
m_file.reset();
133
}
134
m_currentFileName.clear();
135
}
136
137
void
FileDocumentWriter::write(QStringView content)
138
{
139
if
(m_stream)
140
*m_stream << content;
141
}
142
143
void
FileDocumentWriter::writeLine(QStringView content)
144
{
145
if
(m_stream)
146
*m_stream << content <<
'\n'
;
147
}
148
149
bool
FileDocumentWriter::isOpen()
const
150
{
151
return
m_file && m_file->isOpen();
152
}
153
154
QString FileDocumentWriter::currentFileName()
const
155
{
156
return
m_currentFileName;
157
}
158
159
QT_END_NAMESPACE
Location
The Location class provides a way to mark a location in a file.
Definition
location.h:20
Location::Location
Location()
Constructs an empty location.
Definition
location.cpp:50
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
OutputContext
Bundles output configuration without static variables.
Definition
outputcontext.h:18
qttools
src
qdoc
qdoc
src
qdoc
filedocumentwriter.cpp
Generated on
for Qt by
1.18.0