aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
Diffstat (limited to 'frontend')
-rw-r--r--frontend/CMakeLists.txt2
-rw-r--r--frontend/Player.h9
-rw-r--r--frontend/cli.cpp21
-rw-r--r--frontend/cmd.cpp17
-rw-r--r--frontend/cmd.h7
-rw-r--r--frontend/main.cpp7
-rw-r--r--frontend/print.cpp73
-rw-r--r--frontend/print.h27
-rw-r--r--frontend/strings.cpp18
-rw-r--r--frontend/strings.h3
10 files changed, 171 insertions, 13 deletions
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
index eafbe55..a773577 100644
--- a/frontend/CMakeLists.txt
+++ b/frontend/CMakeLists.txt
@@ -2,5 +2,7 @@ target_sources(main PUBLIC
main.cpp
cli.cpp
strings.cpp
+ cmd.cpp
+ print.cpp
)
diff --git a/frontend/Player.h b/frontend/Player.h
index e69de29..a99736d 100644
--- a/frontend/Player.h
+++ b/frontend/Player.h
@@ -0,0 +1,9 @@
+#pragma once
+
+class Player {
+private:
+ unsigned int health_points = 0;
+ unsigned int attack_chance = 0;
+
+};
+
diff --git a/frontend/cli.cpp b/frontend/cli.cpp
index 352de4a..de78bc1 100644
--- a/frontend/cli.cpp
+++ b/frontend/cli.cpp
@@ -5,31 +5,34 @@
#include <readline/history.h>
#include "cli.h"
+#include "frontend/print.h"
+#include "strings.h"
+#include "cmd.h"
using namespace std;
-static void handle_line(const string & line) {
- printf("CMD: %s\n", line.c_str());
-}
-
string cli_readline() {
const char * PROMPT = "> ";
char * input = readline(PROMPT);
- // ctrl-d
- if (input == NULL) exit(EXIT_SUCCESS);
+ SessionLog::get().append(PROMPT);
+ if (input == NULL) exit(EXIT_SUCCESS); // ctrl-d
string out = string(input);
if (out.size() > 0) add_history(input);
free(input);
+ SessionLog::get().append(out);
+ SessionLog::get().append("\n");
+
return out;
}
void cli_main() {
while (1) {
- string cmd = cli_readline();
- if (cmd.size() == 0) continue;
- handle_line(cmd);
+ string line = cli_readline();
+ if (line.length() == 0) continue;
+ vector<string> argv = split_string(line, " ");
+ cmd_handle(argv);
}
}
diff --git a/frontend/cmd.cpp b/frontend/cmd.cpp
new file mode 100644
index 0000000..c5b03ea
--- /dev/null
+++ b/frontend/cmd.cpp
@@ -0,0 +1,17 @@
+#include <functional>
+#include <unordered_map>
+
+#include "cmd.h"
+
+using namespace std;
+
+static const unordered_map<string, function<void(const vector<string> &)>> handlers = {
+ {"gert", [] (const vector<string> & argv) { } },
+};
+
+void cmd_handle(const vector<string> & argv) {
+ string cmd = argv.front();
+ vector<string> args = argv;
+
+}
+
diff --git a/frontend/cmd.h b/frontend/cmd.h
new file mode 100644
index 0000000..2718463
--- /dev/null
+++ b/frontend/cmd.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <vector>
+#include <string>
+
+void cmd_handle(const std::vector<std::string> & argv);
+
diff --git a/frontend/main.cpp b/frontend/main.cpp
index 90cb1d2..2d66888 100644
--- a/frontend/main.cpp
+++ b/frontend/main.cpp
@@ -1,11 +1,12 @@
-#include <memory>
#include <cstdlib>
#include <cstdio>
+#include <memory>
#include "backend/Dungeon.h"
#include "cli.h"
#include "strings.h"
+#include "print.h"
using namespace std;
@@ -15,9 +16,9 @@ int main() {
print_string(strings::INTRO);
string filename = cli_readline();
if (filename.size() == 0) {
- printf("TODO: generate dungeon\n");
+ lprtf("TODO: generate dungeon\n");
} else {
- printf("TODO: load %s\n", filename.c_str());
+ lprtf("TODO: load %s\n", filename.c_str());
}
cli_main();
diff --git a/frontend/print.cpp b/frontend/print.cpp
new file mode 100644
index 0000000..396e9ad
--- /dev/null
+++ b/frontend/print.cpp
@@ -0,0 +1,73 @@
+#include <cstdarg>
+#include <string>
+#include <unistd.h>
+
+#include "print.h"
+
+using namespace std;
+
+static string va_stringf(va_list args, const char * fmt) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1;
+ char * msg = (char *) malloc(sz);
+ va_end(args_copy);
+
+ vsnprintf(msg, sz, fmt, args);
+
+ string out = msg;
+ free(msg);
+
+ va_end(args);
+
+ return out;
+}
+
+static string stringf(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ string out = va_stringf(args, fmt);
+ va_end(args);
+ return out;
+}
+
+void lprtf(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ string formatted = va_stringf(args, fmt);
+ va_end(args);
+
+ fwrite(formatted.c_str(), 1, formatted.size(), stdout);
+ fflush(stdout);
+
+ SessionLog::get().append(formatted);
+}
+
+SessionLog & SessionLog::get() {
+ static SessionLog instance;
+ return instance;
+}
+
+SessionLog::SessionLog() {
+ if (!this->enable) return;
+
+ string filename = stringf("%lu.log", getpid());
+ FILE * file = fopen(filename.c_str(), "w+");
+ this->file = { file, [] (FILE * file) { fclose(file); } };
+}
+
+void SessionLog::append(const string & str) {
+ this->append(str.data(), str.size());
+}
+
+void SessionLog::append(const char * str) {
+ this->append(str, strlen(str));
+}
+
+void SessionLog::append(const char * buf, size_t buf_size) {
+ if (this->file == nullptr) return;
+ if (buf_size == 0) return;
+ fwrite(buf, 1, buf_size, this->file.get());
+}
+
diff --git a/frontend/print.h b/frontend/print.h
new file mode 100644
index 0000000..6b526ae
--- /dev/null
+++ b/frontend/print.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <cstring>
+#include <memory>
+#include <functional>
+#include <string>
+
+void lprtf(const char * fmt, ...);
+
+class SessionLog {
+public:
+ static SessionLog & get();
+
+private:
+ SessionLog();
+ virtual ~SessionLog() = default;
+
+public:
+ virtual void append(const std::string & str);
+ virtual void append(const char * str);
+ virtual void append(const char * buf, size_t buf_size);
+
+private:
+ std::unique_ptr<FILE, std::function<void(FILE*)>> file = nullptr;
+ static constexpr bool enable = true;
+};
+
diff --git a/frontend/strings.cpp b/frontend/strings.cpp
index 0d4dee0..bb4a419 100644
--- a/frontend/strings.cpp
+++ b/frontend/strings.cpp
@@ -1,9 +1,10 @@
#include "strings.h"
+#include "print.h"
using namespace std;
void print_string(const char * str) {
- printf("%s\n", wrap_string(str).c_str());
+ lprtf("%s\n", wrap_string(str).c_str());
}
string wrap_string(const char * str) {
@@ -26,3 +27,18 @@ string wrap_string(const char * str) {
return out;
}
+vector<string> split_string(const string & src, const string & delim) {
+ vector<string> out;
+ size_t start = 0;
+ size_t end = src.find(delim);
+
+ while (end != string::npos) {
+ out.push_back(src.substr(start, end - start));
+ start = end + delim.length();
+ end = src.find(delim, start);
+ }
+ out.push_back(src.substr(start));
+
+ return out;
+}
+
diff --git a/frontend/strings.h b/frontend/strings.h
index 910eaa8..452ad66 100644
--- a/frontend/strings.h
+++ b/frontend/strings.h
@@ -1,6 +1,7 @@
#pragma once
#include <string>
+#include <vector>
namespace strings {
@@ -14,3 +15,5 @@ void print_string(const char *);
std::string wrap_string(const char *);
+std::vector<std::string> split_string(const std::string & src, const std::string & delim);
+