basic file format
This commit is contained in:
@@ -1 +1 @@
|
||||
Checks: '*,clang-analyzer-*,-llvmlibc-*,-fuchsia-*,-altera-*,-abseil-*,-android-*,-modernize-use-trailing-return-type,-readability-identifier-length,-*-readability-todo,-*-magic-numbers,-readability-function-cognitive-complexity,-*-easily-swappable-parameters'
|
||||
Checks: '*,clang-analyzer-*,-llvmlibc-*,-fuchsia-*,-altera-*,-abseil-*,-android-*,-modernize-use-trailing-return-type,-readability-identifier-length,-*-readability-todo,-*-magic-numbers,-readability-function-cognitive-complexity,-*-easily-swappable-parameters,-*-pro-type-reinterpret-cast,-*-owning-memory'
|
||||
21
src/common.h
Normal file
21
src/common.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#define ASSERT(cond) \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "ASSERTION FAILED at " << __FILE__ << ":" << __LINE__ << "\n" \
|
||||
<< #cond << std::endl; \
|
||||
abort(); \
|
||||
}
|
||||
|
||||
using u8 = unsigned char;
|
||||
using i8 = char;
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
static_assert(sizeof(float) * 8 == 32);
|
||||
using f32 = float;
|
||||
static_assert(sizeof(double) * 8 == 64);
|
||||
using f64 = double;
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "mainwindow.h"
|
||||
#include "common.h"
|
||||
#include <QDebug>
|
||||
#include <QFileDialog>
|
||||
#include <QTreeWidgetItem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
@@ -15,7 +18,65 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
return;
|
||||
}
|
||||
|
||||
std::ofstream file(path.toStdString(), std::ios::binary);
|
||||
file.write("TEST", 4);
|
||||
{
|
||||
std::ofstream file(path.toStdString(), std::ios::binary);
|
||||
file.write("DULL", 4);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
m_vault_path = path;
|
||||
reload_vault();
|
||||
});
|
||||
|
||||
connect(ui->actionOpen, &QAction::triggered, this, [this]() {
|
||||
QString path = QFileDialog::getOpenFileName(this, "Choose vault to open",
|
||||
QDir::currentPath(),
|
||||
"Dull Vaults (*.dull)");
|
||||
if (path.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_vault_path = path;
|
||||
reload_vault();
|
||||
});
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
while (true) {
|
||||
u64 name_size = 0;
|
||||
if (!file.read(reinterpret_cast<char *>(&name_size), sizeof(u64))) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,8 @@ public:
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
QString m_vault_path;
|
||||
|
||||
void reload_vault();
|
||||
};
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
<string>Vault</string>
|
||||
</property>
|
||||
<addaction name="actionNew"/>
|
||||
<addaction name="actionOpen"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
</widget>
|
||||
@@ -36,6 +37,11 @@
|
||||
<string>New</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionOpen">
|
||||
<property name="text">
|
||||
<string>Open</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
Reference in New Issue
Block a user