From f5f6db48b2a3065629ee6c988a04116ba3ff346c Mon Sep 17 00:00:00 2001 From: Toni Date: Sun, 2 Nov 2025 16:42:08 +0100 Subject: [PATCH] file preview --- src/common.h | 3 +- src/mainwindow.cc | 74 +++++++++++++++++++++++++---------------------- src/mainwindow.h | 3 +- src/mainwindow.ui | 18 +++++++++++- src/vault.h | 52 +++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 37 deletions(-) create mode 100644 src/vault.h diff --git a/src/common.h b/src/common.h index 7fa147a..63be8e9 100644 --- a/src/common.h +++ b/src/common.h @@ -10,7 +10,8 @@ } using u8 = unsigned char; -using i8 = char; +using i16 = int16_t; +using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 54eaaca..aa99172 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -1,10 +1,9 @@ #include "mainwindow.h" -#include "common.h" +#include "vault.h" #include #include #include #include -#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -20,19 +19,13 @@ MainWindow::MainWindow(QWidget *parent) { std::ofstream file(path.toStdString(), std::ios::binary); - file.write("DULL", 4); + Vault::write_header(file); - std::string file_name = "hello.txt"; - u64 file_name_size = file_name.size(); - std::string file_content = "Hello, World!"; - u64 file_size = file_content.size(); - file.write(reinterpret_cast(&file_name_size), sizeof(u64)); - file.write(file_name.data(), static_cast(file_name_size)); - file.write(reinterpret_cast(&file_size), sizeof(u64)); - file.write(file_content.data(), static_cast(file_size)); + Vault::write_file(file, "hello.txt", "Hello, World!"); + Vault::write_file(file, "test.txt", "test test test"); } - m_vault_path = path; + m_vault_path = path.toStdString(); reload_vault(); }); @@ -44,39 +37,52 @@ MainWindow::MainWindow(QWidget *parent) return; } - m_vault_path = path; + m_vault_path = path.toStdString(); reload_vault(); }); + + connect(ui->fsTreeWidget, &QTreeWidget::itemClicked, + [this](QTreeWidgetItem *item, int column) { + preview_file(item->text(column).toStdString()); + }); } void MainWindow::reload_vault() { ui->fsTreeWidget->clear(); - std::ifstream file(m_vault_path.toStdString(), std::ios::binary); - ASSERT(file.good()); - - std::array header{}; - ASSERT(file.read(header.data(), header.size())); - - ASSERT(std::string_view(reinterpret_cast(header.data()), - header.size()) == "DULL"); + std::ifstream file(m_vault_path, std::ios::binary); + Vault::verify_header(file); while (true) { - u64 name_size = 0; - if (!file.read(reinterpret_cast(&name_size), sizeof(u64))) { + auto header = Vault::read_file_header(file); + if (!header) { + break; + } + file.seekg(static_cast(header->size), std::ios::cur); + + auto *item = new QTreeWidgetItem(ui->fsTreeWidget); + item->setText(0, QString::fromStdString(header->name)); + } +} + +void MainWindow::preview_file(const std::string &filename) { + std::ifstream file(m_vault_path, std::ios::binary); + Vault::verify_header(file); + + while (true) { + auto header = Vault::read_file_header(file); + if (!header) { break; } - std::string name(name_size, '\0'); - file.read(name.data(), static_cast(name_size)); - - u64 content_size = 0; - file.read(reinterpret_cast(&content_size), sizeof(u64)); - - std::string content(content_size, '\0'); - file.read(content.data(), static_cast(content_size)); - - auto *item = new QTreeWidgetItem(ui->fsTreeWidget); - item->setText(0, QString::fromStdString(name)); + if (header->name == filename) { + std::string content(header->size, '\0'); + file.read(content.data(), static_cast(header->size)); + ui->filePreview->setText(QString::fromStdString(content)); + return; + } + file.seekg(static_cast(header->size), std::ios::cur); } + + ASSERT(false); } diff --git a/src/mainwindow.h b/src/mainwindow.h index c3cf4ec..3686516 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -12,7 +12,8 @@ public: private: Ui::MainWindow *ui; - QString m_vault_path; + std::string m_vault_path; void reload_vault(); + void preview_file(const std::string &filename); }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 20d0e8c..4501feb 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -13,7 +13,23 @@ MainWindow - + + + + + + + Name + + + + + + + + + + diff --git a/src/vault.h b/src/vault.h new file mode 100644 index 0000000..4cd1ee5 --- /dev/null +++ b/src/vault.h @@ -0,0 +1,52 @@ +#pragma once + +#include "common.h" +#include +#include +#include +#include + +namespace Vault { + +inline void verify_header(std::ifstream &file) { + ASSERT(file.good()); + + std::array header{}; + ASSERT(file.read(header.data(), header.size())); + ASSERT(std::string_view(header.data(), header.size()) == "DULL"); +} + +struct FileHeader { + std::string name; + u64 size; +}; + +inline std::optional read_file_header(std::ifstream &file) { + FileHeader header{}; + + u64 name_size = 0; + if (!file.read(reinterpret_cast(&name_size), sizeof(u64))) { + return std::nullopt; + } + + header.name = std::string(name_size, '\0'); + file.read(header.name.data(), static_cast(name_size)); + + file.read(reinterpret_cast(&header.size), sizeof(u64)); + + return header; +} + +inline void write_header(std::ofstream &file) { file.write("DULL", 4); } + +inline void write_file(std::ofstream &file, const std::string &name, + const std::string &content) { + u64 name_size = name.size(); + u64 content_size = content.size(); + file.write(reinterpret_cast(&name_size), sizeof(u64)); + file.write(name.data(), static_cast(name_size)); + file.write(reinterpret_cast(&content_size), sizeof(u64)); + file.write(content.data(), static_cast(content_size)); +} + +}; // namespace Vault \ No newline at end of file