1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Implement cd and pwd commands

This commit is contained in:
King_DuckZ 2016-01-26 18:26:16 +01:00
parent eccf86f59e
commit 138f112254
5 changed files with 159 additions and 7 deletions

View file

@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME}
main.cpp
commandline.cpp
commandprocessor.cpp
dirmanager.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -74,7 +74,7 @@ namespace din {
template <typename Iterator>
struct CommandGrammar : boost::spirit::qi::grammar<Iterator, ParsedCommand(), boost::spirit::unicode::space_type> {
explicit CommandGrammar ( void );
CommandGrammar ( void );
boost::spirit::qi::rule<Iterator, ParsedCommand(), boost::spirit::unicode::space_type> start;
boost::spirit::qi::rule<Iterator, std::string(), boost::spirit::unicode::space_type> quoted_string;
@ -96,9 +96,10 @@ namespace din {
using boost::spirit::qi::lexeme;
using boost::spirit::unicode::char_;
using boost::spirit::unicode::space;
using boost::spirit::qi::string;
quoted_string %= lexeme['"' >> *(char_ - '"') >> '"'];
argument %= lexeme[+(char_ - space)];
argument %= lexeme[+(string("\\ ") | (char_ - space))];
string_arg %= quoted_string | argument;
start = argument >> *string_arg;
}

106
src/navigate/dirmanager.cpp Normal file
View file

@ -0,0 +1,106 @@
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "dirmanager.hpp"
#include "helpers/infix_iterator.hpp"
#include <boost/spirit/include/qi.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <sstream>
#include <boost/phoenix/bind/bind_function.hpp>
#include <boost/phoenix/operator.hpp>
namespace qi = boost::spirit::qi;
namespace din {
namespace {
typedef std::vector<std::string> StringList;
std::string hex_to_str (unsigned int parChara) {
const char curr = static_cast<char>(parChara);
return std::string(&curr, 1);
}
template <typename Iterator>
struct PathGrammar : public qi::grammar<Iterator, StringList()> {
PathGrammar ( void );
qi::rule<Iterator, StringList()> start;
qi::rule<Iterator, std::string()> diritem;
qi::rule<Iterator, std::string()> escape;
qi::rule<Iterator, std::string()> hex_code;
qi::uint_parser<unsigned int, 16, 2, 2> hex_chara;
};
template <typename Iterator>
PathGrammar<Iterator>::PathGrammar() :
PathGrammar<Iterator>::base_type(start)
{
namespace px = boost::phoenix;
using boost::spirit::qi::lit;
using boost::spirit::qi::char_;
using boost::spirit::qi::_val;
using boost::spirit::_1;
start %= *lit("/") >> *(diritem >> *lit("/"));
diritem %= +(escape | (char_ - '/' - '\\' - ' '));
escape %= lit("\\") >> (hex_code | char_);
hex_code = lit("x") >> hex_chara[_val += px::bind(&hex_to_str, _1)];
}
} //unnamed namespace
void DirManager::push_piece (const std::string& parPiece) {
using boost::spirit::qi::parse;
PathGrammar<std::string::const_iterator> gramm;
auto piece_begin = parPiece.begin();
const std::string::const_iterator piece_end = parPiece.end();
StringList result;
const bool parse_result = parse(
piece_begin,
piece_end,
gramm,
result
);
if (not parPiece.empty() and parPiece[0] == '/') {
m_stack.clear();
}
for (auto&& itm : result) {
if (itm == "..") {
if (not m_stack.empty()) {
m_stack.pop_back();
}
}
else if (itm == ".") {
}
else {
m_stack.push_back(std::move(itm));
}
}
assert(parse_result);
}
std::string DirManager::to_string() const {
std::ostringstream oss;
oss << '/';
boost::copy(m_stack, infix_ostream_iterator<std::string>(oss, "/"));
return oss.str();
}
} //namespace din

View file

@ -0,0 +1,39 @@
/* Copyright 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id0F3472647B3E45AE8A30319AE7C76477
#define id0F3472647B3E45AE8A30319AE7C76477
#include <vector>
#include <string>
namespace din {
class DirManager {
public:
DirManager ( void ) = default;
void push_piece ( const std::string& parPiece );
std::string to_string ( void ) const;
private:
using StackType = std::vector<std::string>;
StackType m_stack;
};
} //namespace din
#endif

View file

@ -17,6 +17,7 @@
#include "commandline.hpp"
#include "commandprocessor.hpp"
#include "dirmanager.hpp"
#include <iostream>
#include <ciso646>
#include <string>
@ -27,7 +28,7 @@ namespace {
void do_navigation ( void );
bool on_exit ( void );
void on_cd ( const std::string& parDir );
void on_pwd ( const din::DirManager& parDirMan );
} //unnamed namespace
int main (int parArgc, char* parArgv[]) {
@ -52,8 +53,8 @@ namespace {
bool on_exit() {
return true;
}
void on_cd (const std::string& parDir) {
std::cout << "Would cd into " << parDir << '\n';
void on_pwd (const din::DirManager& parDirMan) {
std::cout << parDirMan.to_string() << '\n';
}
void do_navigation() {
@ -62,10 +63,14 @@ namespace {
bool running = true;
std::string curr_line;
din::CommandProcessor proc;
din::DirManager dir_man;
proc.add_command("exit", &on_exit, 0);
proc.add_command("cd", &on_cd, 1);
proc.add_command("cd", std::function<void(const std::string&)>(std::bind(&din::DirManager::push_piece, &dir_man, std::placeholders::_1)), 1);
proc.add_command("pwd", std::function<void()>(std::bind(&on_pwd, std::ref(dir_man))), 0);
do {
do {
std::getline(inp, curr_line);
} while (curr_line.empty());
running = proc.exec_command(curr_line);
} while (running);
}