aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-09 07:22:42 +0000
committer_ov4 <ov4@ov4.org>2026-10-09 07:22:42 +0000
commitbeadcf1f0d9cf8b9c0b14683bf40d27c142d33c5 (patch)
treec996d716cad78bb24870996d751715e8387d3024 /src
parent091db10e0f5e8e95095f30cef34d51e23fc01abb (diff)
add history load/save
Diffstat (limited to 'src')
-rw-r--r--src/history.cc59
-rw-r--r--src/path.cc12
2 files changed, 71 insertions, 0 deletions
diff --git a/src/history.cc b/src/history.cc
index 3e976ce..4b3eba1 100644
--- a/src/history.cc
+++ b/src/history.cc
@@ -1,5 +1,15 @@
#include "history.h"
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <print>
+#include <cstdio>
+#include <unistd.h>
+#include <fstream>
+#include <iostream>
+
+#include "util/io.h"
+
using namespace std;
namespace ov4
@@ -19,5 +29,54 @@ size_t get_last_index()
{
return history_vec.size() - 1;
}
+
+string get_default_history_path()
+{
+ return get_home_dir() + '/' + ".ccshell_history";
+}
+
+void load_history(const string &path)
+{
+ history_vec.clear();
+ ifstream ifs(path);
+ if (!ifs.is_open())
+ {
+ loggerln("load_history: fail to open history");
+ return;
+ }
+ string tmp;
+ while (getline(ifs, tmp))
+ {
+ history_vec.push_back(tmp);
+ }
+ return;
+}
+
+void save_history(const string &path)
+{
+ int fd = open(path.c_str(), O_WRONLY | O_CREAT);
+ if (fd == -1)
+ {
+ loggerln("save_history: fail to open() history");
+ return;
+ }
+ char f_mode = 'w';
+ FILE *f = fdopen(fd, &f_mode);
+ if (f == nullptr)
+ {
+ loggerln("save_history: fail to fdopen() history");
+ close(fd);
+ return;
+ }
+
+ for (auto &c : history_vec)
+ {
+ println(f, "{}", c);
+ }
+
+ fflush(f);
+ fsync(fd);
+ fclose(f);
+}
}
diff --git a/src/path.cc b/src/path.cc
index 103e2c5..30e81cc 100644
--- a/src/path.cc
+++ b/src/path.cc
@@ -1,5 +1,12 @@
#include "path.h"
+#include <pwd.h>
+#include <unistd.h>
+
+#include "global.h"
+#include "util/string.h"
+#include "util/io.h"
+
namespace ov4
{
@@ -67,5 +74,10 @@ string get_current_dir()
return safe_str(getcwd(buf, MAXLINE));
}
+string get_home_dir()
+{
+ return getpwuid(getuid())->pw_dir;
+}
+
}