aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coco/modificationfile.cpp
blob: b2c717f706be1a4707d799cdca4cf63020b8c1c7 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "modificationfile.h"
#include "projectexplorer/project.h"

#include <projectexplorer/buildconfiguration.h>

namespace Coco::Internal {

static void cutTail(QStringList &list)
{
    while (!list.isEmpty() && list.last().trimmed().isEmpty())
        list.removeLast();
}

ModificationFile::ModificationFile(const QString &fileName, const Utils::FilePath &defaultModificationFile)
    : m_fileName{fileName}
    , m_defaultModificationFile{defaultModificationFile}
{}

void ModificationFile::setFilePath(ProjectExplorer::BuildConfiguration *buildConfig)
{
    Utils::FilePath projectDirectory = buildConfig->project()->projectDirectory();
    m_filePath = projectDirectory.pathAppended(fileName());
}

QString ModificationFile::fileName() const
{
    return m_fileName;
}

bool ModificationFile::exists() const
{
    return m_filePath.exists();
}

void ModificationFile::clear()
{
    m_options.clear();
    m_tweaks.clear();
}

QStringList ModificationFile::defaultModificationFile() const
{
    return contentOf(m_defaultModificationFile);
}

QStringList ModificationFile::contentOf(const Utils::FilePath &filePath) const
{
    QFile resource(filePath.nativePath());
    QTC_CHECK(resource.open(QIODevice::ReadOnly | QIODevice::Text));
    QTextStream inStream(&resource);

    QStringList result;
    QString line;
    while (inStream.readLineInto(&line))
        result << line + '\n';
    return result;
}

QStringList ModificationFile::currentModificationFile() const
{
    QStringList lines;
    if (m_filePath.exists())
        lines = contentOf(m_filePath);
    else
        lines = defaultModificationFile();

    return lines;
}

void ModificationFile::setOptions(const QString &options)
{
    m_options = options.split('\n', Qt::SkipEmptyParts);
}

void ModificationFile::setOptions(const QStringList &options)
{
    m_options = options;
}

void ModificationFile::setTweaks(const QString &tweaks)
{
    m_tweaks = tweaks.split('\n', Qt::KeepEmptyParts);
    cutTail(m_tweaks);
}

void ModificationFile::setTweaks(const QStringList &tweaks)
{
    m_tweaks = tweaks;
    cutTail(m_tweaks);
}

} // namespace Coco::Internal