move m_curDir to the parser class
Change-Id: Ib3cf15dfbb7af861a3c2fa8e746a0b0baaaa512e
diff --git a/src/CParser.cpp b/src/CParser.cpp
index c6a42a6..357970f 100644
--- a/src/CParser.cpp
+++ b/src/CParser.cpp
@@ -12,16 +12,27 @@
InvalidCommandException::~InvalidCommandException() = default;
+struct nodeToString : public boost::static_visitor<std::string> {
+ std::string operator()(const nodeup_&) const
+ {
+ return "..";
+ }
+ template <class T>
+ std::string operator()(const T& node) const
+ {
+ return node.m_name;
+ }
+};
+
CParser::CParser(const CTree& tree)
: m_tree(tree)
{
}
-
cd_ CParser::parseCommand(const std::string& line, std::ostream& errorStream)
{
cd_ parsedCommand;
- ParserContext ctx(m_tree);
+ ParserContext ctx(m_tree, m_curDir);
auto it = line.begin();
boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
@@ -38,3 +49,25 @@
return parsedCommand;
}
+
+void CParser::changeNode(const path_& name)
+{
+ if (name.m_nodes.empty()) {
+ m_curDir = "";
+ return;
+ }
+ for (const auto& it : name.m_nodes) {
+ const std::string node = boost::apply_visitor(nodeToString(), it);
+ if (node == "..") {
+ m_curDir = stripLastNodeFromPath(m_curDir);
+ } else {
+ m_curDir = joinPaths(m_curDir, boost::apply_visitor(nodeToString(), it));
+ }
+
+ }
+}
+std::string CParser::currentNode() const
+{
+ return m_curDir;
+}
+
diff --git a/src/CParser.hpp b/src/CParser.hpp
index 4b3e1c4..a7d5790 100644
--- a/src/CParser.hpp
+++ b/src/CParser.hpp
@@ -36,7 +36,10 @@
public:
CParser(const CTree& tree);
cd_ parseCommand(const std::string& line, std::ostream& errorStream);
+ void changeNode(const path_& name);
+ std::string currentNode() const;
private:
const CTree& m_tree;
+ std::string m_curDir;
};
diff --git a/src/CTree.cpp b/src/CTree.cpp
index 69afb58..e68a2bc 100644
--- a/src/CTree.cpp
+++ b/src/CTree.cpp
@@ -12,17 +12,6 @@
InvalidNodeException::~InvalidNodeException() = default;
-struct nodeToString : public boost::static_visitor<std::string> {
- std::string operator()(const nodeup_&) const
- {
- return "..";
- }
- template <class T>
- std::string operator()(const T& node) const
- {
- return node.m_name;
- }
-};
CTree::CTree()
@@ -98,23 +87,3 @@
m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
}
-void CTree::changeNode(const path_& name)
-{
- if (name.m_nodes.empty()) {
- m_curDir = "";
- return;
- }
- for (const auto& it : name.m_nodes) {
- const std::string node = boost::apply_visitor(nodeToString(), it);
- if (node == "..") {
- m_curDir = stripLastNodeFromPath(m_curDir);
- } else {
- m_curDir = joinPaths(m_curDir, boost::apply_visitor(nodeToString(), it));
- }
-
- }
-}
-std::string CTree::currentNode() const
-{
- return m_curDir;
-}
diff --git a/src/CTree.hpp b/src/CTree.hpp
index 6fe54d8..6093ee1 100644
--- a/src/CTree.hpp
+++ b/src/CTree.hpp
@@ -49,12 +49,9 @@
bool listHasKey(const std::string& location, const std::string& name, const std::string& key) const;
bool isList(const std::string& location, const std::string& name) const;
void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys);
- void changeNode(const path_& name);
- std::string currentNode() const;
private:
const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
- std::string m_curDir;
};
diff --git a/src/main.cpp b/src/main.cpp
index 68ddbef..607115e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -35,7 +35,7 @@
using x3::lexeme;
using ascii::space;
-void interpret(cd_ command, CTree& tree)
+void interpret(cd_ command, CParser& tree)
{
tree.changeNode(command.m_path);
}
@@ -62,7 +62,7 @@
CParser parser(tree);
while (true) {
- std::cout << tree.currentNode() << ">";
+ std::cout << parser.currentNode() << ">";
std::string input;
std::getline(std::cin, input);
if (std::cin.eof())
@@ -70,7 +70,7 @@
try {
cd_ command = parser.parseCommand(input, std::cout);
- interpret(command, tree);
+ interpret(command, parser);
} catch (InvalidCommandException& ex) {
std::cerr << ex.what() << std::endl;
}
diff --git a/src/parser_context.cpp b/src/parser_context.cpp
index c3b31c9..5766c2f 100644
--- a/src/parser_context.cpp
+++ b/src/parser_context.cpp
@@ -7,8 +7,8 @@
*/
#include "parser_context.hpp"
-ParserContext::ParserContext(const CTree& tree)
+ParserContext::ParserContext(const CTree& tree, const std::string curDir)
: m_tree(tree)
{
- m_curPath = m_tree.currentNode();
+ m_curPath = curDir;
}
diff --git a/src/parser_context.hpp b/src/parser_context.hpp
index 64b293a..4d6cf99 100644
--- a/src/parser_context.hpp
+++ b/src/parser_context.hpp
@@ -8,7 +8,7 @@
#include "CTree.hpp"
struct ParserContext {
- ParserContext(const CTree& tree);
+ ParserContext(const CTree& tree, const std::string curDir);
const CTree& m_tree;
std::string m_curPath;
std::string m_errorMsg;