file preview

This commit is contained in:
2025-11-02 16:42:08 +01:00
parent 1d86d24ed1
commit f5f6db48b2
5 changed files with 113 additions and 37 deletions

View File

@@ -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;

View File

@@ -1,10 +1,9 @@
#include "mainwindow.h"
#include "common.h"
#include "vault.h"
#include <QDebug>
#include <QFileDialog>
#include <QTreeWidgetItem>
#include <fstream>
#include <iostream>
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<const char *>(&file_name_size), sizeof(u64));
file.write(file_name.data(), static_cast<i64>(file_name_size));
file.write(reinterpret_cast<const char *>(&file_size), sizeof(u64));
file.write(file_content.data(), static_cast<i64>(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<i8, 4> header{};
ASSERT(file.read(header.data(), header.size()));
ASSERT(std::string_view(reinterpret_cast<i8 *>(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<char *>(&name_size), sizeof(u64))) {
auto header = Vault::read_file_header(file);
if (!header) {
break;
}
file.seekg(static_cast<i64>(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<i64>(name_size));
u64 content_size = 0;
file.read(reinterpret_cast<char *>(&content_size), sizeof(u64));
std::string content(content_size, '\0');
file.read(content.data(), static_cast<i64>(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<i64>(header->size));
ui->filePreview->setText(QString::fromStdString(content));
return;
}
file.seekg(static_cast<i64>(header->size), std::ios::cur);
}
ASSERT(false);
}

View File

@@ -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);
};

View File

@@ -13,7 +13,23 @@
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTreeWidget" name="fsTreeWidget">
<column>
<property name="text">
<string>Name</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="filePreview">
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>

52
src/vault.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include "common.h"
#include <array>
#include <fstream>
#include <iostream>
#include <optional>
namespace Vault {
inline void verify_header(std::ifstream &file) {
ASSERT(file.good());
std::array<char, 4> 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<FileHeader> read_file_header(std::ifstream &file) {
FileHeader header{};
u64 name_size = 0;
if (!file.read(reinterpret_cast<char *>(&name_size), sizeof(u64))) {
return std::nullopt;
}
header.name = std::string(name_size, '\0');
file.read(header.name.data(), static_cast<i64>(name_size));
file.read(reinterpret_cast<char *>(&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<const char *>(&name_size), sizeof(u64));
file.write(name.data(), static_cast<i64>(name_size));
file.write(reinterpret_cast<const char *>(&content_size), sizeof(u64));
file.write(content.data(), static_cast<i64>(content_size));
}
}; // namespace Vault