qt5base-lts/examples/xml/htmlinfo/main.cpp
Marc Mutz aa37e67ef7 Port from qAsConst() to std::as_const()
We've been requiring C++17 since Qt 6.0, and our qAsConst use finally
starts to bother us (QTBUG-99313), so time to port away from it
now.

Since qAsConst has exactly the same semantics as std::as_const (down
to rvalue treatment, constexpr'ness and noexcept'ness), there's really
nothing more to it than a global search-and-replace, with manual
unstaging of the actual definition and documentation in dist/,
src/corelib/doc/ and src/corelib/global/.

Task-number: QTBUG-99313
Change-Id: I4c7114444a325ad4e62d0fcbfd347d2bbfb21541
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
2022-10-11 23:17:18 +02:00

83 lines
2.3 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtCore>
void parseHtmlFile(QTextStream &out, const QString &fileName)
{
QFile file(fileName);
out << "Analysis of HTML file: " << fileName << Qt::endl;
if (!file.open(QIODevice::ReadOnly)) {
out << " Couldn't open the file." << Qt::endl << Qt::endl << Qt::endl;
return;
}
//! [0]
QXmlStreamReader reader(&file);
//! [0]
//! [1]
int paragraphCount = 0;
QStringList links;
QString title;
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
if (reader.name() == QLatin1String("title"))
title = reader.readElementText();
else if (reader.name() == QLatin1String("a"))
links.append(reader.attributes().value(QLatin1String("href")).toString());
else if (reader.name() == QLatin1String("p"))
++paragraphCount;
}
}
//! [1]
//! [2]
if (reader.hasError()) {
out << " The HTML file isn't well-formed: " << reader.errorString()
<< Qt::endl << Qt::endl << Qt::endl;
return;
}
//! [2]
out << " Title: \"" << title << '"' << Qt::endl
<< " Number of paragraphs: " << paragraphCount << Qt::endl
<< " Number of links: " << links.size() << Qt::endl
<< " Showing first few links:" << Qt::endl;
while (links.size() > 5)
links.removeLast();
for (const QString &link : std::as_const(links))
out << " " << link << Qt::endl;
out << Qt::endl << Qt::endl;
}
int main(int argc, char **argv)
{
// initialize Qt Core application
QCoreApplication app(argc, argv);
// get a list of all html files in the current directory
const QStringList filter = { QStringLiteral("*.htm"),
QStringLiteral("*.html") };
const QStringList htmlFiles = QDir(QStringLiteral(":/")).entryList(filter, QDir::Files);
QTextStream out(stdout);
if (htmlFiles.isEmpty()) {
out << "No html files available.";
return 1;
}
// parse each html file and write the result to file/stream
for (const QString &file : htmlFiles)
parseHtmlFile(out, QStringLiteral(":/") + file);
return 0;
}