1// Copyright (C) 2021 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 "qmltccommandlineutils.h"
5
6#include <QtCore/qstring.h>
7#include <QtCore/qfileinfo.h>
8#include <QtCore/qdir.h>
9#include <QtCore/qurl.h>
10#include <QtCore/qlibraryinfo.h>
11
12QT_BEGIN_NAMESPACE
13
14QString parseUrlArgument(const QString &arg)
15{
16 const QUrl url = QUrl::fromUserInput(userInput: arg, workingDirectory: QDir::currentPath(), options: QUrl::AssumeLocalFile);
17 if (!url.isValid()) {
18 fprintf(stderr, format: "Invalid URL: \"%s\"\n", qPrintable(arg));
19 return QString();
20 }
21 if (!url.isLocalFile()) {
22 fprintf(stderr, format: "\"%s\" is not a local file\n", qPrintable(arg));
23 return QString();
24 }
25 return url.toLocalFile();
26}
27
28QString loadUrl(const QString &url)
29{
30 const QFileInfo fi(url);
31 if (!fi.exists()) {
32 fprintf(stderr, format: "\"%s\" does not exist.\n",
33 qPrintable(QDir::toNativeSeparators(fi.absoluteFilePath())));
34 return QString();
35 }
36
37 QFile f(fi.absoluteFilePath());
38 if (!f.open(flags: QIODevice::ReadOnly)) {
39 fprintf(stderr, format: "Unable to read \"%s\": %s.\n",
40 qPrintable(QDir::toNativeSeparators(fi.absoluteFilePath())),
41 qPrintable(f.errorString()));
42 return QString();
43 }
44
45 QByteArray data(fi.size(), Qt::Uninitialized);
46 if (f.read(data: data.data(), maxlen: data.size()) != data.size()) {
47 fprintf(stderr, format: "Unable to read \"%s\": %s.\n",
48 qPrintable(QDir::toNativeSeparators(fi.absoluteFilePath())),
49 qPrintable(f.errorString()));
50 return QString();
51 }
52 return QString::fromUtf8(ba: data);
53}
54
55QString getImplicitImportDirectory(const QString &url)
56{
57 const QFileInfo fi(url);
58 Q_ASSERT(fi.exists());
59 QDir dir = fi.dir();
60 QString implicitImport = dir.canonicalPath(); // resolves symlinks, etc.
61 if (implicitImport.isEmpty()) {
62 fprintf(stderr, format: "Cannot resolve implicit import directory of \"%s\"",
63 qPrintable(QDir::toNativeSeparators(fi.absoluteFilePath())));
64 return QString();
65 }
66 return implicitImport;
67}
68
69QT_END_NAMESPACE
70

source code of qtdeclarative/tools/qmltc/qmltccommandlineutils.cpp