aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/projectmacro.cpp
blob: 9dfe0e2b168385c4297ba8ca6431a8d572b8122b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "projectmacro.h"

#include <utils/algorithm.h>
#include <cctype>

namespace ProjectExplorer {

bool Macro::isValid() const
{
    return !key.isEmpty() && type != MacroType::Invalid;
}

QByteArray Macro::toByteArray() const
{
    switch (type) {
        case MacroType::Define: {
            if (value.isEmpty())
               return QByteArray("#define ") + key;
            return QByteArray("#define ") + key + ' ' + value;
        }
        case MacroType::Undefine: return QByteArray("#undef ") + key;
        case MacroType::Invalid: break;
    }
    return {};
}

QByteArray Macro::toByteArray(const Macros &macros)
{
    QByteArray text;

    for (const Macro &macro : macros) {
        const QByteArray macroText = macro.toByteArray();
        if (!macroText.isEmpty())
            text += macroText + '\n';
    }

    return  text;
}

Macros Macro::toMacros(const QByteArray &text)
{
    return tokensLinesToMacros(tokenizeLines(splitLines(text)));
}

Macro Macro::fromKeyValue(const QString &utf16text)
{
    return fromKeyValue(utf16text.toUtf8());
}

Macro Macro::fromKeyValue(const QByteArray &text)
{
    QByteArray key;
    QByteArray value;
    MacroType type = MacroType::Invalid;

    if (!text.isEmpty()) {
        type = MacroType::Define;

        int index = text.indexOf('=');

        if (index != -1) {
            key = text.left(index).trimmed();
            value = text.mid(index + 1).trimmed();
        } else {
            key = text.trimmed();
            value = "1";
        }
    }

    return Macro(key, value, type);
}

QByteArray Macro::toKeyValue(const QByteArray &prefix) const
{
    QByteArray keyValue;
    if (type != MacroType::Invalid)
        keyValue = prefix;

    if (value.isEmpty())
        keyValue += key + '=';
    else if (value == "1")
        keyValue += key;
    else
        keyValue += key + '=' + value;

    return keyValue;
}

static void removeCarriageReturn(QByteArray &line)
{
    if (line.endsWith('\r'))
        line.truncate(line.size() - 1);
}

static void removeCarriageReturns(QList<QByteArray> &lines)
{
    for (QByteArray &line : lines)
        removeCarriageReturn(line);
}

QList<QByteArray> Macro::splitLines(const QByteArray &text)
{
    QList<QByteArray> splitLines = text.split('\n');

    splitLines.removeAll("");
    removeCarriageReturns(splitLines);

    return splitLines;
}

QByteArray Macro::removeNonsemanticSpaces(QByteArray line)
{
    auto begin = line.begin();
    auto end = line.end();
    bool notInString = true;

    auto newEnd = std::unique(begin, end, [&] (char first, char second) {
        notInString = notInString && first != '\"';
        return notInString && (first == '#' || std::isspace(first)) && std::isspace(second);
    });

    line.truncate(line.size() - int(std::distance(newEnd, end)));

    return line.trimmed();
}

QList<QByteArray> Macro::tokenizeLine(const QByteArray &line)
{
    const QByteArray normalizedLine = removeNonsemanticSpaces(line);

    const auto begin = normalizedLine.begin();
    auto first = std::find(normalizedLine.begin(), normalizedLine.end(), ' ');
    const auto end = normalizedLine.end();

    QList<QByteArray> tokens;

    if (first != end) {
        auto second = std::find(std::next(first), normalizedLine.end(), ' ');
        tokens.append(QByteArray(begin, int(std::distance(begin, first))));

        std::advance(first, 1);
        tokens.append(QByteArray(first, int(std::distance(first, second))));

        if (second != end) {
            std::advance(second, 1);
            tokens.append(QByteArray(second, int(std::distance(second, end))));
        }
    }

   return tokens;
}

QList<QList<QByteArray>> Macro::tokenizeLines(const QList<QByteArray> &lines)
{
    QList<QList<QByteArray>> tokensLines = Utils::transform(lines, &Macro::tokenizeLine);

    return tokensLines;
}

Macro Macro::tokensToMacro(const QList<QByteArray> &tokens)
{
    Macro macro;

    if (tokens.size() >= 2 && tokens[0] == "#define") {
        macro.type = MacroType::Define;
        macro.key = tokens[1];

        if (tokens.size() >= 3)
            macro.value = tokens[2];
    }

    return macro;
}

Macros Macro::tokensLinesToMacros(const QList<QList<QByteArray>> &tokensLines)
{
    Macros macros;
    macros.reserve(tokensLines.size());

    for (const QList<QByteArray> &tokens : tokensLines) {
        Macro macro = tokensToMacro(tokens);

        if (macro.type != MacroType::Invalid)
            macros.push_back(std::move(macro));
    }

    return macros;
}

} // namespace ProjectExplorer