FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
ziptree.cpp
Go to the documentation of this file.
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
33 
34 #include "ziptree.h"
35 #include "zipnode.h"
36 
37 namespace FIFE {
38 
40  : m_rootNode(new ZipNode("/")) {
41 
42  }
43 
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 
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 }
ZipNode * m_rootNode
Definition: ziptree.h:72
ZipTree()
constructor
Definition: ziptree.cpp:39
ZipNode * addNode(const std::string &nodePath)
adds a node to the proper place in the tree based on the node path name
Definition: ziptree.cpp:48
~ZipTree()
destructor
Definition: ziptree.cpp:44
std::ostream & operator<<(std::ostream &os, const Location &l)
Stream output operator.
Definition: location.cpp:164
std::string GetPathIteratorAsString(const bfs::path::iterator &pathIter)
Helper function to retrieve the path iterator in a string representation.
ZipNode * getParent() const
accessor for the parent node of this node will be NULL if this node has no parent ...
Definition: zipnode.cpp:119
ZipNode * getNode(const std::string &name) const
accessor for getting a node by name
Definition: ziptree.cpp:91
ZipNode * addChild(const std::string &child)
allows adding a child node to this node
Definition: zipnode.cpp:181
ZipNode * getRootNode() const
accessor for the root node of the tree mostly used for debugging purposes
Definition: ziptree.cpp:120
ZipNode * getChild(const std::string &name, ZipContentType::Enum contentType=ZipContentType::All) const
gives access to retrieving a specific child node by name
Definition: zipnode.cpp:147