Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "script.h" | ||
2 | #include <fstream> | ||
3 | #include <functional> | ||
4 | #include <iostream> | ||
5 | #include <map> | ||
6 | #include <sstream> | ||
7 | #include <text_conversion_constexpr.h> | ||
8 | #include <vector> | ||
9 | |||
10 | namespace script | ||
11 | { | ||
12 | |||
13 | struct cmd_args | ||
14 | { | ||
15 | std::string& memory; | ||
16 | std::string& error; | ||
17 | const std::string& operand; | ||
18 | std::function<void(const char* msg)> print; | ||
19 | }; | ||
20 | |||
21 | struct engine_command | ||
22 | { | ||
23 | std::function<void(cmd_args& args)> func; | ||
24 | const command cmd; | ||
25 | }; | ||
26 | |||
27 | template <typename F> | ||
28 | 156 | void add_command(std::vector<engine_command>& cmds, command cmd, F&& f) | |
29 | { | ||
30 |
1/2✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
156 | cmds.emplace_back(engine_command{f, cmd}); |
31 | 156 | } | |
32 | |||
33 | 13 | void set_commands(std::vector<engine_command>& cmds) | |
34 | { | ||
35 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command( |
36 | cmds, command::PROCESS, | ||
37 | 15 | [](cmd_args& args) | |
38 | 15 | { text_conversion_constexpr::convert_to_title_case(args.memory); }); | |
39 | |||
40 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command(cmds, command::INVALID, |
41 | ✗ | [](cmd_args& args) { args.error = "Invalid command"; }); | |
42 | |||
43 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command(cmds, command::TEXT, |
44 | 16 | [](cmd_args& args) { args.memory = args.operand; }); | |
45 | |||
46 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command(cmds, command::PRINT, |
47 | 6 | [](cmd_args& args) | |
48 | { | ||
49 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | if (args.print) |
50 | { | ||
51 | 6 | args.print(args.memory.c_str()); | |
52 | 6 | return; | |
53 | } | ||
54 | |||
55 | ✗ | args.error = | |
56 | ✗ | "print command called, but no print callback provided"; | |
57 | }); | ||
58 | |||
59 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command(cmds, command::LOAD, |
60 | 2 | [](cmd_args& args) | |
61 | { | ||
62 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | std::ifstream file{args.operand}; |
63 | |||
64 |
3/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | if (!file.is_open()) |
65 | { | ||
66 | 1 | args.error = | |
67 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | std::string{"Could not open file "} + args.operand; |
68 | 1 | return; | |
69 | } | ||
70 | |||
71 | 1 | args.memory.clear(); | |
72 | |||
73 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::stringstream buffer; |
74 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | buffer << file.rdbuf(); |
75 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | args.memory = buffer.str(); |
76 | |||
77 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | file.close(); |
78 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | }); |
79 | |||
80 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | add_command(cmds, command::SAVE, |
81 | 1 | [](cmd_args& args) | |
82 | { | ||
83 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::ofstream output_file{args.operand}; |
84 | |||
85 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (!output_file.is_open()) |
86 | { | ||
87 | ✗ | args.error = | |
88 | ✗ | std::string{"Could not open file "} + args.operand; | |
89 | ✗ | return; | |
90 | } | ||
91 | |||
92 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | output_file << args.memory; |
93 | |||
94 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | output_file.close(); |
95 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | }); |
96 | 13 | } | |
97 | |||
98 | class engine::engine_impl | ||
99 | { | ||
100 | public: | ||
101 |
1/2✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
|
13 | engine_impl(std::function<void(const char* msg)> print) : m_print(print) |
102 | { | ||
103 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | m_commands.reserve(6); |
104 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | set_commands(m_commands); |
105 | 13 | } | |
106 | |||
107 | 40 | std::string run_command(command cmd_id, const std::string& operand) | |
108 | { | ||
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (cmd_id == command::COMMENT) |
110 | ✗ | return ""; | |
111 | |||
112 |
1/2✓ Branch 5 taken 103 times.
✗ Branch 6 not taken.
|
103 | for (auto& cmd : m_commands) |
113 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 63 times.
|
103 | if (cmd.cmd == cmd_id) |
114 | { | ||
115 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | cmd_args args{m_memory, m_error, operand, m_print}; |
116 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | cmd.func(args); |
117 | |||
118 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 39 times.
|
40 | if (!m_error.empty()) |
119 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | return m_error; |
120 | |||
121 |
1/2✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
|
39 | return ""; |
122 | 40 | } | |
123 | |||
124 | ✗ | return "Command not found"; | |
125 | } | ||
126 | |||
127 | std::string m_memory = {}; | ||
128 | std::string m_error = {}; | ||
129 | std::function<void(const char* msg)> m_print; | ||
130 | std::vector<engine_command> m_commands; | ||
131 | }; | ||
132 | |||
133 | 13 | engine::engine(std::function<void(const char* msg)> print) | |
134 | 13 | : m_impl(std::make_unique<engine_impl>(print)) | |
135 | { | ||
136 | 13 | } | |
137 | |||
138 | 13 | engine::~engine() {} | |
139 | |||
140 | 40 | std::string engine::run(command cmd, const std::string& operand) | |
141 | { | ||
142 | 40 | return m_impl->run_command(cmd, operand); | |
143 | } | ||
144 | |||
145 | 12 | std::string engine::get_memory() const | |
146 | { | ||
147 | 12 | return m_impl->m_memory; | |
148 | } | ||
149 | |||
150 | } // namespace script | ||
151 |