File: zipnode.cpp

package info (click to toggle)
fife 0.4.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,204 kB
  • sloc: cpp: 42,642; xml: 18,881; python: 13,521; makefile: 23
file content (270 lines) | stat: -rw-r--r-- 9,336 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/***************************************************************************
*   Copyright (C) 2005-2019 by the FIFE team                              *
*   http://www.fifengine.net                                              *
*   This file is part of FIFE.                                            *
*                                                                         *
*   FIFE is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Lesser General Public            *
*   License as published by the Free Software Foundation; either          *
*   version 2.1 of the License, or (at your option) any later version.    *
*                                                                         *
*   This library is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
*   Lesser General Public License for more details.                       *
*                                                                         *
*   You should have received a copy of the GNU Lesser General Public      *
*   License along with this library; if not, write to the                 *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
***************************************************************************/

// Standard C++ library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "vfs/fife_boost_filesystem.h"

#include "zipnode.h"
#include <algorithm>

namespace {
    /** helper function to find a value in a ZipNodeContainer
     *  @param container the ZipNodeContainer to search through
        @param the name to search for
        @return the ZipNode pointer, or NULL if not found
     */
    FIFE::ZipNode* FindNameInContainer(const FIFE::ZipNodeContainer& container, 
                                       const std::string& name) {
        for (FIFE::ZipNodeContainer::const_iterator iter = container.begin(); iter != container.end(); ++iter) {
            if ((*iter)->getName() == name) {
                return *iter;
            }
        }

        return 0;
    }

    FIFE::ZipNodeContainer::iterator FindNameInContainer(FIFE::ZipNodeContainer& container, 
                                                         const std::string& name)
    {
        for (FIFE::ZipNodeContainer::iterator iter = container.begin(); iter != container.end(); ++iter) {
            if ((*iter)->getName() == name) {
                return iter;
            }
        }

        return container.end();
    }
}

namespace FIFE {
    ZipEntryData::ZipEntryData() 
    : comp(0), crc32(0), size_comp(0), size_real(0), offset(0) { 
    }

    ZipNode::ZipNode(const std::string& name, ZipNode* parent/*=0*/)
    : m_name(name), m_parent(parent) {

        // set the content type based on whether
        // the name has an extension
        if (HasExtension(m_name))
        {
            m_contentType = ZipContentType::File;
        }
        else
        {
            m_contentType = ZipContentType::Directory;
        }
    }

    ZipNode::~ZipNode() {
        ZipNodeContainer::iterator iter;
        for (iter = m_fileChildren.begin(); iter != m_fileChildren.end(); ++iter) {
            delete *iter;
        }
        m_fileChildren.clear();

        for (iter = m_directoryChildren.begin(); iter != m_directoryChildren.end(); ++iter) {
            delete *iter;
        }
        m_directoryChildren.clear();
    }

    std::string ZipNode::getName() const {
        return m_name;
    }

    std::string ZipNode::getFullName() const {
        // traverse up the hierarchy of parents
        // to build the full path
        if (m_parent)
        {
            bfs::path path(m_parent->getFullName());
            path /= m_name;
            return (path.string());
        }
        else
        {
            return m_name;
        }
    }

    ZipContentType::Enum ZipNode::getContentType() const {
        return m_contentType;
    }

    ZipNode* ZipNode::getParent() const {
        return m_parent;
    }

    std::vector<ZipNode*> ZipNode::getChildren(ZipContentType::Enum contentType/*=ZipContentType::All*/) const {
        switch (contentType) {
            default:                    // fall through on purpose
            case ZipContentType::All: {
                // concatenate directory and file children
                // putting all directories before files
                // reserve space in destination vector to avoid
                // tons of vector resizing overhead
                ZipNodeContainer allNodes;
                allNodes.reserve(m_directoryChildren.size() + m_fileChildren.size());
                allNodes.insert(allNodes.end(), m_directoryChildren.begin(), m_directoryChildren.end());
                allNodes.insert(allNodes.end(), m_fileChildren.begin(), m_fileChildren.end());

                return allNodes;
            }
            case ZipContentType::File: {
                return m_fileChildren;
            }
            case ZipContentType::Directory: {
                return m_directoryChildren;
            }
        }
    }

    ZipNode* ZipNode::getChild(const std::string& name, 
                               ZipContentType::Enum contentType/*=ZipContentType::All*/) const {
        bool hasExtension = HasExtension(name);
        
        switch (contentType) {
            default:                    // fall through on purpose
            case ZipContentType::All: {
                ZipNode* node = 0;
                if (hasExtension) {
                    node = FindNameInContainer(m_fileChildren, name);
                }
                else {
                    node = FindNameInContainer(m_directoryChildren, name);
                }

                return node;
            }
            case ZipContentType::File: {
                if (!hasExtension) {
                    return 0;
                }

                return FindNameInContainer(m_fileChildren, name);
            }
            case ZipContentType::Directory: {
                if (hasExtension) {
                    return 0;
                }

                return FindNameInContainer(m_directoryChildren, name);
            }
        }
    }

    ZipNode* ZipNode::addChild(const std::string& name) {
        ZipNode* child = new ZipNode(name, this);
        if (child) {
            if (child->getContentType() == ZipContentType::File) {
                m_fileChildren.push_back(child);
            }
            else if (child->getContentType() == ZipContentType::Directory) {
                m_directoryChildren.push_back(child);
            }
            else {
                // TODO - vtchill - error case here, maybe exception
            }
        }

        return child;
    }

    void ZipNode::removeChild(ZipNode* child) {
        if (child) {
            if (child->getContentType() == ZipContentType::File) {
                ZipNodeContainer::iterator iter;
                iter = std::find(m_fileChildren.begin(), m_fileChildren.end(), child);
                
                if (iter != m_fileChildren.end()) {
                    delete *iter;
                    m_fileChildren.erase(iter);
                }
            }
        }
    }

    void ZipNode::removeChild(const std::string& name) {
        if (HasExtension(name)) {
            ZipNodeContainer::iterator iter = FindNameInContainer(m_fileChildren, name);

            if (iter != m_fileChildren.end()) {
                delete *iter;
                m_fileChildren.erase(iter);
            }
        }
        else {
            ZipNodeContainer::iterator iter = FindNameInContainer(m_directoryChildren, name);

            if (iter != m_directoryChildren.end()) {
                delete *iter;
                m_directoryChildren.erase(iter);
            }
        }
    }

    bool ZipNode::isLeaf() const
    {
        return (m_fileChildren.empty() && m_directoryChildren.empty());
    }

    bool ZipNode::isBranch() const
    {
        return (!isLeaf());
    }

    void ZipNode::setZipEntryData(const ZipEntryData& entryData)
    {
        m_entryData = entryData;
    }

    const ZipEntryData& ZipNode::getZipEntryData() const
    {
        return m_entryData;
    }
}

std::ostream& operator<<(std::ostream& os, const FIFE::ZipNode& node) {
    // print node name first
    os << node.getName() << std::endl;

    // print all file children
    FIFE::ZipNodeContainer fileChildren = node.getChildren(FIFE::ZipContentType::File);
    FIFE::ZipNodeContainer::iterator iter;
    for (iter = fileChildren.begin(); iter != fileChildren.end(); ++iter) {
        os << *(*iter) << std::endl;
    }

    // print all directory children (recursively)
    FIFE::ZipNodeContainer directoryChildren = node.getChildren(FIFE::ZipContentType::Directory);
    for (iter = directoryChildren.begin(); iter != directoryChildren.end(); ++iter) {
        os << *(*iter) << std::endl;
    }

    return os;
}