1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Add stub for new navigate command.

This commit is contained in:
King_DuckZ 2016-01-22 15:55:19 +00:00
parent 588d735593
commit 99732f6dd8
7 changed files with 377 additions and 0 deletions

View file

@ -65,6 +65,7 @@ add_subdirectory(src/scan)
add_subdirectory(src/delete)
add_subdirectory(src/query)
add_subdirectory(src/locate)
add_subdirectory(src/navigate)
target_link_libraries(${PROJECT_NAME}
INTERFACE ${PostgreSQL_LIBRARIES}
@ -76,4 +77,7 @@ target_link_libraries(${PROJECT_NAME}
target_compile_definitions(${PROJECT_NAME}
INTERFACE WITH_PROGRESS_FEEDBACK
INTERFACE BOOST_SPIRIT_USE_PHOENIX_V3=1
#workaround for a bug in gcc 5.3 that is causing exceptions to slip
#through try/catch blocks.
INTERFACE _GLIBCXX_USE_CXX11_ABI=0
)

View file

@ -0,0 +1,21 @@
project(${bare_name}-navigate CXX)
add_executable(${PROJECT_NAME}
main.cpp
commandline.cpp
commandprocessor.cpp
)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PRIVATE ${bare_name}-common
)
string(REPLACE "${bare_name}-" "" ACTION_NAME "${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME}
PRIVATE ACTION_NAME="${ACTION_NAME}"
)

View file

@ -0,0 +1,59 @@
/* 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 "commandline.hpp"
#include "dindexer-common/commandline.hpp"
#include <boost/program_options.hpp>
#include <iostream>
namespace po = boost::program_options;
namespace din {
bool parse_commandline (int parArgc, char* parArgv[], po::variables_map& parVarMap) {
po::options_description set_options(ACTION_NAME " options");
//set_options.add_options()
//("switch,s", "Help message")
//("option,o", po::value<std::string>()->default_value("default_value"), "Help message")
//("option2", po::value<int>(), "Help message")
//;
//po::options_description positional_options("Positional options");
//positional_options.add_options()
//("pos_option", po::value<std::string>(), "pos_option description")
//;
const auto desc = dinlib::get_default_commandline();
po::options_description all("Available options");
//po::positional_options_description pd;
all.add(desc)./*add(positional_options).*/add(set_options);
//pd.add("pos_option", 1);//.add("pos_option2", 1);
try {
po::store(po::command_line_parser(parArgc, parArgv).options(all)/*.positional(pd)*/.run(), parVarMap);
}
catch (const po::validation_error& err) {
throw dinlib::ValidationError(err);
}
po::notify(parVarMap);
if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, "[options...] <search-path>", parVarMap, {std::cref(desc), std::cref(set_options)})) {
return true;
}
return false;
}
} //namespace din

View file

@ -0,0 +1,29 @@
/* 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 id1B7A42F6E46547A6AB0F914E2A91399F
#define id1B7A42F6E46547A6AB0F914E2A91399F
#include "dindexer-common/validationerror.hpp"
#include "dindexer-common/mediatypes.hpp"
#include <boost/program_options/variables_map.hpp>
namespace din {
bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap );
} //namespace din
#endif

View file

@ -0,0 +1,144 @@
/* 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/>.
*/
#define BOOST_SPIRIT_UNICODE
#include "commandprocessor.hpp"
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/phoenix/phoenix.hpp>
#include <ciso646>
#include <cassert>
#include <stdexcept>
#include <algorithm>
#include <utility>
#include <sstream>
namespace din {
struct ParsedCommand {
std::string cmd;
std::vector<std::string> param;
};
} //namespace din
BOOST_FUSION_ADAPT_STRUCT(
din::ParsedCommand,
(std::string, cmd)
(std::vector<std::string>, param)
);
namespace din {
struct CustomCommand {
std::string name;
CommandProcessor::CmdCallback callback;
uint32_t exp_arg_count;
bool operator== ( const std::pair<std::string, uint32_t>& parOther ) const {
return name == parOther.first and exp_arg_count == parOther.second;
}
};
template <typename Iterator>
struct CommandGrammar : boost::spirit::qi::grammar<Iterator, ParsedCommand(), boost::spirit::unicode::space_type> {
explicit 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;
boost::spirit::qi::rule<Iterator, std::string(), boost::spirit::unicode::space_type> string_arg;
boost::spirit::qi::rule<Iterator, std::string(), boost::spirit::unicode::space_type> argument;
};
struct CommandProcessor::LocalData {
using IteratorType = std::string::const_iterator;
CommandGrammar<IteratorType> gramm;
std::vector<CustomCommand> commands;
};
template <typename Iterator>
CommandGrammar<Iterator>::CommandGrammar() :
CommandGrammar<Iterator>::base_type(start)
{
using boost::spirit::qi::lexeme;
using boost::spirit::unicode::char_;
using boost::spirit::unicode::space;
quoted_string %= lexeme['"' >> *(char_ - '"') >> '"'];
argument %= lexeme[+(char_ - space)];
string_arg %= quoted_string | argument;
start = argument >> *string_arg;
}
CommandProcessor::CommandProcessor() :
m_local_data(new LocalData)
{
}
CommandProcessor::~CommandProcessor() noexcept {
}
bool CommandProcessor::exec_command (const std::string& parCommand) {
LocalData::IteratorType cmd_begin = parCommand.begin();
ParsedCommand result;
const bool parse_result = boost::spirit::qi::phrase_parse(
cmd_begin,
parCommand.end(),
m_local_data->gramm,
boost::spirit::unicode::space,
result
);
if (not parse_result or parCommand.end() != cmd_begin) {
//parse error
std::ostringstream oss;
oss << "Error parsing given command at position " << (cmd_begin - parCommand.begin());
throw std::runtime_error(oss.str());
}
{
const uint32_t arg_count = static_cast<uint32_t>(result.param.size());
const auto it_found = std::find(m_local_data->commands.begin(), m_local_data->commands.end(), std::make_pair(result.cmd, arg_count));
if (m_local_data->commands.end() != it_found) {
return not it_found->callback(result.param);
}
else {
std::ostringstream oss;
oss << "Parsed command \"" << result.cmd << "\" with " << arg_count << " argument(s) not recognized";
throw std::runtime_error(oss.str());
}
}
}
void CommandProcessor::add_command (std::string&& parName, CmdCallback parCallback, uint32_t parExpParams) {
//Check that parName is not an empty string
if (parName.size() == 0) {
throw std::runtime_error("Can't register an empty command in CommandProcessor");
}
//Check that parName+parExpParams hasn't been added already
{
const auto it_found = std::find(m_local_data->commands.begin(), m_local_data->commands.end(), std::make_pair(parName, parExpParams));
if (m_local_data->commands.end() != it_found) {
std::ostringstream oss;
oss << "Inserting duplicate command \"" << parName << "\" taking " << parExpParams << " parameter(s)";
throw std::runtime_error(oss.str());
}
}
//Add the new command
m_local_data->commands.push_back(CustomCommand {std::move(parName), std::move(parCallback), parExpParams});
}
} //namespace din

View file

@ -0,0 +1,46 @@
/* 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 idC4CC0CB0AA0445999E75D41C853A1D33
#define idC4CC0CB0AA0445999E75D41C853A1D33
#include <string>
#include <memory>
#include <vector>
#include <functional>
#include <cstdint>
namespace din {
class CommandProcessor {
public:
using CmdCallback = std::function<bool(const std::vector<std::string>&)>;
CommandProcessor ( void );
CommandProcessor ( CommandProcessor&& ) = default;
~CommandProcessor ( void ) noexcept;
void add_command ( std::string&& parName, CmdCallback parCallback, uint32_t parExpParams );
bool exec_command ( const std::string& parCommand );
private:
struct LocalData;
std::unique_ptr<LocalData> m_local_data;
};
} //namespace din
#endif

74
src/navigate/main.cpp Normal file
View file

@ -0,0 +1,74 @@
/* 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 "commandline.hpp"
#include "commandprocessor.hpp"
#include <iostream>
#include <ciso646>
#include <string>
#include <vector>
#include <cassert>
namespace {
void do_navigation ( void );
bool on_exit ( const std::vector<std::string>& );
bool on_cd ( const std::vector<std::string>& parArgs );
} //unnamed namespace
int main (int parArgc, char* parArgv[]) {
using boost::program_options::variables_map;
variables_map vm;
try {
if (din::parse_commandline(parArgc, parArgv, vm)) {
return 0;
}
}
catch (const std::invalid_argument& err) {
std::cerr << err.what() << "\nUse --help for help" << std::endl;
return 2;
}
do_navigation();
return 0;
}
namespace {
bool on_exit (const std::vector<std::string>&) {
return true;
}
bool on_cd (const std::vector<std::string>& parArgs) {
assert(parArgs.size() == 1);
std::cout << "Would cd into " << parArgs.front() << '\n';
return false;
}
void do_navigation() {
auto& inp = std::cin;
bool running = true;
std::string curr_line;
din::CommandProcessor proc;
proc.add_command("exit", &on_exit, 0);
proc.add_command("cd", &on_cd, 1);
do {
std::getline(inp, curr_line);
running = proc.exec_command(curr_line);
} while (running);
}
} //unnamed namespace