Skip to content
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

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ set(DULCIFICUM_SRC
src/utils/io.cpp
src/utils/svtod.cpp
src/command_types.cpp
src/miracle_jtp/mgjtp_command_file_stream.cpp
include/dulcificum/miracle_jtp/mgjtp_command_file_stream.h
src/miracle_jtp/mgjtp_command_stream.cpp
include/dulcificum/miracle_jtp/mgjtp_command_stream.h
)
add_library(dulcificum ${DULCIFICUM_SRC})
target_link_libraries(dulcificum
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Supported dialects include:
```angular2html
mkdir build
cd build
cmake ..
cmake --build .
```
conan install .. -s build_type=Debug -o with_apps=True
conan build ..
```

42 changes: 42 additions & 0 deletions include/dulcificum/miracle_jtp/mgjtp_command_file_stream.h
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
38 changes: 38 additions & 0 deletions include/dulcificum/miracle_jtp/mgjtp_command_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
67 changes: 67 additions & 0 deletions src/miracle_jtp/mgjtp_command_file_stream.cpp
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ bugprone-narrowing-conversions ⚠️
narrowing conversion from std::basic_streambuf<char>::int_type (aka int) to signed type char is implementation-defined

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())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
if (! stream.is_open())
if (! stream.is_open()) {

return line;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
return line;
return line;
}

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
if (bracket_depth <= 0)
if (bracket_depth <= 0) {

return line;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
return line;
return line;
}

}
}
return line;
}

} // namespace dulcificum::miracle_jtp
38 changes: 38 additions & 0 deletions src/miracle_jtp/mgjtp_command_stream.cpp
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
2 changes: 2 additions & 0 deletions src/miracle_jtp/mgjtp_json_to_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ botcmd::CommandPtr toChangeTool(const nlohmann::json& jcmd)

botcmd::CommandPtr toCommand(const nlohmann::json& jin)
{
if (jin.empty())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
if (jin.empty())
if (jin.empty()) {

return botcmd::spawnCommandPtr(botcmd::CommandType::Invalid);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ google-readability-braces-around-statements ⚠️
statement should be inside braces

Suggested change
return botcmd::spawnCommandPtr(botcmd::CommandType::Invalid);
return botcmd::spawnCommandPtr(botcmd::CommandType::Invalid);
}

auto jcmd = jin[k_key_str::command];
botcmd::CommandType type = jcmd[k_key_str::function];
if (type == botcmd::CommandType::Move)
Expand Down
Loading
Loading