Debian Codegraph @

engine/core/vfs/zip/ziptree.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <string>
24 #include <vector>
25
26 // 3rd party library includes
27
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "vfs/fife_boost_filesystem.h"
33
34 #include "ziptree.h"
35 #include "zipnode.h"
36
37 namespace FIFE {
38
39 ZipTree::ZipTree()
40 : m_rootNode(new ZipNode("/")) {
41
42 }
43
44 ZipTree::~ZipTree() {
45 delete m_rootNode;
46 }
47
48 ZipNode* ZipTree::addNode(const std::string& nodePath) {
49 bfs::path filePath(nodePath);
50 std::string filename = filePath.string();
51
52 // remove the trailing '/' if it exists because the
53 // boost::filesystem::path objects will report extra
54 // path parts otherwise
55 if (filename[filename.length()-1] == '/') {
56 filename = filename.erase(filename.length()-1);
57 }
58
59 filePath = bfs::path(filename);
60
61 ZipNode* node = m_rootNode;
62 ZipNode* tempNode = 0;
63 ZipNode* returnNode = 0;
64 for (bfs::path::iterator iter = filePath.begin(); iter != filePath.end(); ++iter) {
65 std::string pathString = GetPathIteratorAsString(iter);
66 tempNode = node->getChild(pathString);
67
68 if (!tempNode) {
69 // attempt to add the child since it was not found
70 // and reset the node to the new child node
71 node = node->addChild(pathString);
72 }
73 else {
74 // node found so reset node
75 node = tempNode;
76 }
77
78 if (node) {
79 // node was found or added, set the return node properly
80 returnNode = node;
81 }
82 else {
83 // node was not found and could not be added
84 return 0;
85 }
86 }
87
88 return returnNode;
89 }
90
91 ZipNode* ZipTree::getNode(const std::string& name) const {
92 bfs::path filePath(name);
93 ZipNode* returnNode = 0;
94 ZipNode* tempNode = 0;
95 ZipNode* node = getRootNode();
96 for (bfs::path::iterator iter = filePath.begin(); iter != filePath.end(); ++iter) {
97 std::string pathString = GetPathIteratorAsString(iter);
98
99 if (pathString == ".." && (node != getRootNode())) {
100 // handle ".." path case by setting the node back to the parent
101 node = node->getParent();
102 }
103 else {
104 // look for the path name in the child nodes
105 tempNode = node->getChild(pathString);
106
107 if (tempNode) {
108 node = tempNode;
109
110 // node was found so we reset the return node
111 // to the newly found node
112 returnNode = node;
113 }
114 }
115 }
116
117 return returnNode;
118 }
119
120 ZipNode* ZipTree::getRootNode() const {
121 return m_rootNode;
122 }
123 }
124
125 std::ostream& operator<<(std::ostream& os, const FIFE::ZipTree& zipTree) {
126 assert(zipTree.getRootNode() != 0);
127
128 os << *(zipTree.getRootNode());
129
130 return os;
131 }