pre-ai
This commit is contained in:
parent
be13205ffd
commit
367c19386c
17 changed files with 561 additions and 166 deletions
81
implementations/Nostro-File.cpp
Normal file
81
implementations/Nostro-File.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#include <cerrno>
|
||||
#include <sys/stat.h>
|
||||
|
||||
module Nostro:File_impl;
|
||||
import :File;
|
||||
import std;
|
||||
|
||||
using namespace Nostro;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path Nostro::GetConfigPath() {
|
||||
const char* const xdg = std::getenv("XDG_CONFIG_HOME");
|
||||
if(xdg) {
|
||||
return fs::path(fs::path(std::move(xdg)) / ".config/nostro");
|
||||
}
|
||||
|
||||
const char* const home = std::getenv("HOME");
|
||||
if(home) {
|
||||
return fs::path(fs::path(std::move(home)) / ".config/nostro");
|
||||
}
|
||||
|
||||
throw std::runtime_error("XDG_CONFIG_HOME and HOME not set");
|
||||
}
|
||||
|
||||
fs::path Nostro::GetCachePath() {
|
||||
const char* const xdg = std::getenv("XDG_CACHE_HOME");
|
||||
if(xdg) {
|
||||
return fs::path(fs::path(std::move(xdg)) / "nostro");
|
||||
}
|
||||
|
||||
const char* const home = std::getenv("HOME");
|
||||
if(home) {
|
||||
return fs::path(fs::path(std::move(home)) / ".cache/nostro");
|
||||
}
|
||||
|
||||
throw std::runtime_error("XDG_CACHE_HOME and HOME not set");
|
||||
}
|
||||
|
||||
void Nostro::CreatePrivateFile(const fs::path& path) {
|
||||
const fs::path dir(path.parent_path());
|
||||
|
||||
fs::create_directories(dir.parent_path());
|
||||
|
||||
if(mkdir(dir.c_str(), S_IRWXU) == -1) {
|
||||
if(errno != EEXIST) {
|
||||
throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", dir.string()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(mknod(path.c_str(), S_IFREG | S_IRUSR | S_IWUSR, 0) == -1) {
|
||||
if(errno != EEXIST) {
|
||||
throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", path.string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Nostro::WriteString(std::ofstream& file, std::string_view str) {
|
||||
std::uint16_t size = str.size();
|
||||
file.write(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
file.write(str.data(), size);
|
||||
}
|
||||
|
||||
void Nostro::ReadString(std::ifstream& file, std::string& str) {
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
str.resize(size);
|
||||
file.read(str.data(), size);
|
||||
}
|
||||
|
||||
std::uint16_t Nostro::SeekString(std::ifstream& file) {
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
file.seekg(size, std::ios::cur);
|
||||
return size;
|
||||
}
|
||||
Loading…
Reference in a new issue