FreeCAD

Форк
0
124 строки · 4.3 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2

3
/***************************************************************************
4
 *   Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net>     *
5
 *                                                                         *
6
 *   This file is part of FreeCAD.                                         *
7
 *                                                                         *
8
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
9
 *   under the terms of the GNU Lesser General Public License as           *
10
 *   published by the Free Software Foundation, either version 2.1 of the  *
11
 *   License, or (at your option) any later version.                       *
12
 *                                                                         *
13
 *   FreeCAD is distributed in the hope that it will be useful, but        *
14
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
16
 *   Lesser General Public License for more details.                       *
17
 *                                                                         *
18
 *   You should have received a copy of the GNU Lesser General Public      *
19
 *   License along with FreeCAD. If not, see                               *
20
 *   <https://www.gnu.org/licenses/>.                                      *
21
 *                                                                         *
22
 **************************************************************************/
23

24
#include <QCoreApplication>
25
#include <QFile>
26
#include <QDomDocument>
27
#include <QXmlStreamReader>
28
#include <QXmlStreamWriter>
29
#include <iostream>
30

31
int main(int argc, char* argv[])
32
{
33
    QCoreApplication app(argc, argv);
34
    QStringList args = QCoreApplication::arguments();
35
    if (args.size() != 2) {
36
        std::cerr << "Requires input file\n";
37
        return -1;
38
    }
39

40
    QFile file(args[1]);
41
    if (!file.open(QFile::ReadOnly)) {
42
        std::cerr << "Failed to read file\n";
43
        return -1;
44
    }
45

46
    QXmlStreamReader reader(file.readAll());
47
    file.close();
48

49
    if (!file.open(QFile::WriteOnly)) {
50
        std::cerr << "Failed to write file\n";
51
        return -1;
52
    }
53

54
    QXmlStreamWriter writer(&file);
55
    writer.setAutoFormatting(true);
56
    writer.setAutoFormattingIndent(4);
57

58
    auto findAttr = [](const QXmlStreamAttributes& attr, const QString& name) {
59
        for (int i = 0; i < attr.size(); ++i) {
60
            if (attr.at(i).name() == name) {
61
                return i;
62
            }
63
        }
64

65
        return -1;
66
    };
67

68
    // ----------------------
69

70
    auto sortAttr = [&findAttr](QXmlStreamAttributes& attr) {
71
        QStringList list = {"Name",
72
                            "Namespace",
73
                            "Twin",
74
                            "TwinPointer",
75
                            "PythonName",
76
                            "FatherInclude",
77
                            "Include",
78
                            "Father",
79
                            "FatherNamespace"};
80
        QXmlStreamAttributes sorted;
81
        for (const auto& it : list) {
82
            int index = findAttr(attr, it);
83
            if (index > -1) {
84
                sorted.append(attr.at(index));
85
                attr.remove(index);
86
            }
87
        }
88

89
        // add the rest
90
        for (const auto& it : qAsConst(attr)) {
91
            sorted.append(it);
92
        }
93

94
        return sorted;
95
    };
96

97
    // ----------------------
98

99
    while (!reader.atEnd()) {
100
        reader.readNext();
101
        if (reader.isStartElement() && reader.name() == QLatin1String("PythonExport")) {
102
            QXmlStreamAttributes attr = reader.attributes();
103
            attr = sortAttr(attr);
104

105
            writer.writeStartElement(QString("PythonExport"));
106
            for (int i = 0; i < attr.size(); ++i) {
107
                file.write("\n       ");
108
                writer.writeAttribute(attr.at(i));
109
            }
110
        }
111
        else if (reader.isStartElement() && reader.name() == QLatin1String("UserDocu")) {
112
            QString text = reader.readElementText().trimmed();
113
            text.replace("\t", "    ");
114
            writer.writeTextElement(QString("UserDocu"), text);
115
        }
116
        else if (!reader.isWhitespace()) {
117
            writer.writeCurrentToken(reader);
118
        }
119
    }
120

121
    file.close();
122

123
    return 0;
124
}
125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.