File: filenode.cpp

package info (click to toggle)
qflipper 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,320 kB
  • sloc: cpp: 18,500; sh: 247; ansic: 191; xml: 38; python: 14; makefile: 5
file content (211 lines) | stat: -rw-r--r-- 4,598 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "filenode.h"

#include <QQueue>

#include "debug.h"

FileNode::FileNode():
    m_parent(nullptr)
{}

FileNode::FileNode(const QString &name, Type type, const QVariant &data):
    m_parent(nullptr),
    m_info({name, QString(), type, data})
{}

bool FileNode::operator ==(const FileNode &other) const
{
    return (m_info.type == other.m_info.type) &&
           (m_info.absolutePath == other.m_info.absolutePath) &&
           (m_info.userData == other.m_info.userData);
}

bool FileNode::operator !=(const FileNode &other) const
{
    return !(*this == other);
}

const QString &FileNode::name() const
{
    return m_info.name;
}

const QString &FileNode::absolutePath() const
{
    return m_info.absolutePath;
}

FileNode::Type FileNode::type() const
{
    return m_info.type;
}

const QVariant &FileNode::userData() const
{
    return m_info.userData;
}

const FileNode::FileInfo &FileNode::fileInfo() const
{
    return m_info;
}

void FileNode::addChild(const QSharedPointer<FileNode> &nodePtr)
{
    nodePtr->setParent(this);
    m_children.insert(nodePtr->name(), nodePtr);
}

bool FileNode::addDirectory(const QString &path)
{
    auto fragments = path.split('/');
    QSharedPointer<FileNode> node(new FileNode(fragments.takeLast(), Type::Directory));

    auto *parent = traverse(fragments);
    check_return_bool(parent, QStringLiteral("No parent node found for %1.").arg(path));

    parent->addChild(node);
    return true;
}

bool FileNode::addFile(const QString &path, const QVariant &data)
{
    auto fragments = path.split('/');
    QSharedPointer<FileNode> node(new FileNode(fragments.takeLast(), Type::RegularFile, data));

    auto *parent = traverse(fragments);
    check_return_bool(parent, QStringLiteral("No parent node found for %1.").arg(path));

    parent->addChild(node);
    return true;
}

FileNode *FileNode::traverse(const QStringList &fragments)
{
    if(fragments == QStringList({QString()})) {
        return this;
    }

    auto *current = this;

    for(const auto &fragment: fragments) {
        current = current->child(fragment);

        if(!current) {
            break;
        }
    }

    return current;
}

FileNode *FileNode::child(const QString &name) const
{
    if(!m_children.contains(name)) {
        return nullptr;
    }

    return m_children.value(name).get();
}

FileNode *FileNode::find(const QString &path)
{
    const auto fragments = path.split('/');
    return traverse(fragments);
}

FileNode *FileNode::parent() const
{
    return m_parent;
}

FileNode::FileInfoList FileNode::toPreOrderList() const
{
    FileInfoList ret;
    QQueue<const FileNode*> queue;

    queue.enqueue(this);

    while(!queue.isEmpty()) {
        const auto *current = queue.dequeue();

        for(const auto &childPtr: current->m_children) {
            queue.enqueue(childPtr.get());
        }

        ret.append(current->m_info);
    }

    return ret;
}

FileNode::FileInfoList FileNode::difference(FileNode *other)
{
    FileInfoList ret;
    QQueue<FileNode*> queue;

    queue.enqueue(other);

    while(!queue.isEmpty()) {
        auto *current = queue.dequeue();
        auto *counterpart = find(current->absolutePath());

        if(!counterpart || (counterpart->type() != current->type())) {
            ret.append(current->toPreOrderList());
            continue;
        }

        for(const auto &childPtr: qAsConst(current->m_children)) {
            queue.enqueue(childPtr.get());
        }
    }

    return ret;
}

FileNode::FileInfoList FileNode::changed(FileNode *other)
{
    FileInfoList ret;
    QQueue<FileNode*> queue;

    queue.enqueue(other);

    while(!queue.isEmpty()) {
        auto *current = queue.dequeue();
        auto *counterpart = find(current->absolutePath());

        if(!counterpart || (counterpart->type() != current->type())) {
            continue;

        } else if (*current != *counterpart) {
            ret.append(current->m_info);
            continue;
        }

        for(const auto &childPtr: qAsConst(current->m_children)) {
            queue.enqueue(childPtr.get());
        }
    }

    return ret;
}

void FileNode::setParent(FileNode *node)
{
    m_parent = node;
    QStringList fragments;

    auto *current = this;
    while(current->parent()) {
        fragments.append(current->name());
        current = current->parent();
    }

    std::reverse(fragments.begin(), fragments.end());
    m_info.absolutePath = fragments.join('/');
}

bool FileNode::FileInfo::operator <(const FileInfo &other) const
{
    return (type < other.type) || (absolutePath < other.absolutePath);
}