Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
5
6#include "location.h"
7
8#include <QtCore/qdir.h>
9
11
12using 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*/
52FileDocumentWriter::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*/
62FileDocumentWriter::~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*/
72void 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*/
107void 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
120void FileDocumentWriter::write(QStringView content)
121{
122 if (m_stream)
123 *m_stream << content;
124}
125
126void FileDocumentWriter::writeLine(QStringView content)
127{
128 if (m_stream)
129 *m_stream << content << '\n';
130}
131
132bool FileDocumentWriter::isOpen() const
133{
134 return m_file && m_file->isOpen();
135}
136
137QString FileDocumentWriter::currentFileName() const
138{
139 return m_currentFileName;
140}
141
142QT_END_NAMESPACE
The Location class provides a way to mark a location in a file.
Definition location.h:20
Location()
Constructs an empty location.
Definition location.cpp:48
Combined button and popup list for selecting options.
Bundles output configuration without static variables.