| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <string.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
#include <qfile.h> |
| 10 |
#include <qstring.h> |
| 11 |
#include <qtextstream.h> |
| 12 |
|
| 13 |
#include "parser.h" |
| 14 |
|
| 15 |
static QTextStream qerr(stderr, QIODevice::WriteOnly); |
| 16 |
|
| 17 |
static void usage() |
| 18 |
{ |
| 19 |
qerr << "Usage: parse [-report-whitespace-only-chardata] [-report-start-end-entity] <in-file> [<out-file>]\n"; |
| 20 |
exit(1); |
| 21 |
} |
| 22 |
|
| 23 |
int main(int argc, const char *argv[]) |
| 24 |
{ |
| 25 |
QString file_name; |
| 26 |
QString out_file_name; |
| 27 |
bool report_start_end_entity = false; |
| 28 |
bool report_whitespace_only_chardata = false; |
| 29 |
|
| 30 |
for (int i = 1 ; i < argc; ++i) { |
| 31 |
QString arg = QString::fromLocal8Bit(argv[i]); |
| 32 |
if (arg == QLatin1String("-report-whitespace-only-chardata")) |
| 33 |
report_whitespace_only_chardata = true; |
| 34 |
else if (arg == QLatin1String("-report-start-end-entity")) |
| 35 |
report_start_end_entity = true; |
| 36 |
else if (file_name.isEmpty()) |
| 37 |
file_name = arg; |
| 38 |
else if (out_file_name.isEmpty()) |
| 39 |
out_file_name = arg; |
| 40 |
else |
| 41 |
usage(); |
| 42 |
} |
| 43 |
|
| 44 |
if (file_name.isEmpty()) |
| 45 |
usage(); |
| 46 |
|
| 47 |
QFile in_file(file_name); |
| 48 |
if (!in_file.open(QIODevice::ReadOnly)) { |
| 49 |
qerr << "Could not open " << file_name << ": " << strerror(errno) << Qt::endl; |
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
if (out_file_name.isEmpty()) |
| 54 |
out_file_name = file_name + ".ref"; |
| 55 |
|
| 56 |
QFile out_file; |
| 57 |
if (out_file_name == "-") { |
| 58 |
out_file.open(stdout, QFile::WriteOnly); |
| 59 |
} else { |
| 60 |
out_file.setFileName(out_file_name); |
| 61 |
if (!out_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 62 |
qerr << "Could not open " << out_file_name << ": " << strerror(errno) << Qt::endl; |
| 63 |
return 1; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
Parser parser; |
| 68 |
if (report_start_end_entity) |
| 69 |
parser.setFeature("http://trolltech.com/xml/features/report-start-end-entity", true); |
| 70 |
if (report_whitespace_only_chardata) |
| 71 |
parser.setFeature("http://trolltech.com/xml/features/report-whitespace-only-CharData", true); |
| 72 |
|
| 73 |
parser.parseFile(&in_file); |
| 74 |
|
| 75 |
out_file.write(parser.result().toUtf8()); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |