-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Miracle Grue .jsontoolpath stream reader #9
base: main
Are you sure you want to change the base?
Changes from all commits
68b01fe
b76b138
b71849d
b69c09b
02de50b
75eafeb
4ac02b9
14a61bd
5b5744b
ce77a7b
a651ff2
d9d907b
610f9ba
ec4b4c6
a6bc3f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef DULCIFICUM_MGJTP_COMMAND_FILE_STREAM_H | ||
#define DULCIFICUM_MGJTP_COMMAND_FILE_STREAM_H | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
|
||
namespace dulcificum::miracle_jtp | ||
{ | ||
|
||
class CommandFileStream | ||
{ | ||
private: | ||
std::ifstream stream; | ||
size_t bracket_depth = 0; | ||
|
||
|
||
public: | ||
static const char K_CMD_OPEN = '{'; | ||
static const char K_CMD_CLOSE = '}'; | ||
|
||
void open(const std::filesystem::path& path) | ||
{ | ||
stream.open(path); | ||
} | ||
|
||
void close() | ||
{ | ||
stream.close(); | ||
} | ||
|
||
bool eof() | ||
{ | ||
return stream.eof(); | ||
} | ||
|
||
std::string getCommandLine(); | ||
}; | ||
} // namespace dulcificum::miracle_jtp | ||
|
||
#endif // DULCIFICUM_MGJTP_COMMAND_FILE_STREAM_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef DULCIFICUM_MGJTP_COMMAND_STREAM_H | ||
#define DULCIFICUM_MGJTP_COMMAND_STREAM_H | ||
|
||
#include "../command_types.h" | ||
#include "mgjtp_command_file_stream.h" | ||
#include "mgjtp_json_to_command.h" | ||
namespace dulcificum::miracle_jtp | ||
{ | ||
|
||
struct CommandStream | ||
{ | ||
CommandFileStream fstream; | ||
|
||
public: | ||
void open(const std::filesystem::path& path) | ||
{ | ||
fstream.open(path); | ||
} | ||
|
||
void close() | ||
{ | ||
fstream.close(); | ||
} | ||
|
||
bool eof() | ||
{ | ||
return fstream.eof(); | ||
} | ||
|
||
nlohmann::json getCommandJson(); | ||
|
||
botcmd::CommandPtr getCommand(); | ||
|
||
botcmd::CommandList getCommandList(); | ||
}; | ||
} // namespace dulcificum::miracle_jtp | ||
|
||
#endif // DULCIFICUM_MGJTP_COMMAND_STREAM_H |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,67 @@ | ||||||||
#include "dulcificum/miracle_jtp/mgjtp_command_file_stream.h" | ||||||||
|
||||||||
namespace dulcificum::miracle_jtp | ||||||||
{ | ||||||||
|
||||||||
std::istream& getCmdLineTillCurlyBracket(std::istream& in_stream, std::string& line, char& end_char) | ||||||||
{ | ||||||||
line.clear(); | ||||||||
std::istream::sentry stream_sentry(in_stream, true); | ||||||||
std::streambuf* buffer = in_stream.rdbuf(); | ||||||||
while (true) | ||||||||
{ | ||||||||
if (buffer->sgetc() == std::streambuf::traits_type::eof()) | ||||||||
{ | ||||||||
break; | ||||||||
} | ||||||||
end_char = buffer->sbumpc(); | ||||||||
line += end_char; | ||||||||
if (end_char == CommandFileStream::K_CMD_OPEN) | ||||||||
{ | ||||||||
return in_stream; | ||||||||
} | ||||||||
if (end_char == CommandFileStream::K_CMD_CLOSE) | ||||||||
{ | ||||||||
return in_stream; | ||||||||
} | ||||||||
} | ||||||||
if (line.empty()) | ||||||||
{ | ||||||||
in_stream.setstate(std::ios::eofbit); | ||||||||
} | ||||||||
return in_stream; | ||||||||
} | ||||||||
|
||||||||
std::string CommandFileStream::getCommandLine() | ||||||||
{ | ||||||||
std::string line; | ||||||||
if (! stream.is_open()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return line; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
while (! stream.eof()) | ||||||||
{ | ||||||||
char last_char = 0; | ||||||||
std::string line_till_bracket; | ||||||||
getCmdLineTillCurlyBracket(stream, line_till_bracket, last_char); | ||||||||
if (bracket_depth > 0) | ||||||||
{ | ||||||||
line += line_till_bracket; | ||||||||
} | ||||||||
if (last_char == CommandFileStream::K_CMD_OPEN) | ||||||||
{ | ||||||||
if (bracket_depth == 0) | ||||||||
{ | ||||||||
line += CommandFileStream::K_CMD_OPEN; | ||||||||
} | ||||||||
bracket_depth++; | ||||||||
} | ||||||||
if (last_char == CommandFileStream::K_CMD_CLOSE) | ||||||||
{ | ||||||||
bracket_depth--; | ||||||||
if (bracket_depth <= 0) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return line; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
return line; | ||||||||
} | ||||||||
|
||||||||
} // namespace dulcificum::miracle_jtp |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "dulcificum/miracle_jtp/mgjtp_command_stream.h" | ||
|
||
namespace dulcificum::miracle_jtp | ||
{ | ||
|
||
nlohmann::json dulcificum::miracle_jtp::CommandStream::getCommandJson() | ||
{ | ||
const auto& line = fstream.getCommandLine(); | ||
nlohmann::json cmd_json; | ||
if (! line.empty()) | ||
{ | ||
cmd_json = nlohmann::json::parse(line); | ||
} | ||
return cmd_json; | ||
} | ||
|
||
botcmd::CommandPtr CommandStream::getCommand() | ||
{ | ||
const auto& cmd_json = getCommandJson(); | ||
return toCommand(getCommandJson()); | ||
} | ||
|
||
botcmd::CommandList CommandStream::getCommandList() | ||
{ | ||
botcmd::CommandList list; | ||
while (! fstream.eof()) | ||
{ | ||
const auto& jcmd = getCommandJson(); | ||
if (! jcmd.empty()) | ||
{ | ||
list.emplace_back(toCommand(jcmd)); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
|
||
} // namespace dulcificum::miracle_jtp |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -96,6 +96,8 @@ botcmd::CommandPtr toChangeTool(const nlohmann::json& jcmd) | |||||||
|
||||||||
botcmd::CommandPtr toCommand(const nlohmann::json& jin) | ||||||||
{ | ||||||||
if (jin.empty()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return botcmd::spawnCommandPtr(botcmd::CommandType::Invalid); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
auto jcmd = jin[k_key_str::command]; | ||||||||
botcmd::CommandType type = jcmd[k_key_str::function]; | ||||||||
if (type == botcmd::CommandType::Move) | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
narrowing conversion from
std::basic_streambuf<char>::int_type
(akaint
) to signed typechar
is implementation-defined