pre-ai
This commit is contained in:
parent
be13205ffd
commit
367c19386c
17 changed files with 561 additions and 166 deletions
210
implementations/Nostro-Account.cpp
Normal file
210
implementations/Nostro-Account.cpp
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module Nostro:Account_impl;
|
||||
import :Account;
|
||||
import :File;
|
||||
import std;
|
||||
|
||||
using namespace Nostro;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
std::chrono::system_clock::time_point Account::GetLastTransactionTime(std::string_view connector) const {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path) || fs::file_size(path) == 0) {
|
||||
return std::chrono::system_clock::time_point::min();
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
file.seekg(-static_cast<std::streamoff>(sizeof(std::int64_t)), std::ios::end);
|
||||
std::int64_t lastTime;
|
||||
file.read(reinterpret_cast<char*>(&lastTime), sizeof(lastTime));
|
||||
return std::chrono::system_clock::time_point(std::chrono::microseconds(lastTime));
|
||||
}
|
||||
|
||||
void Account::SaveTransactions(std::string_view connector) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
CreatePrivateFile(path);
|
||||
std::ofstream file(path, std::ios::in | std::ios::out | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
const std::chrono::system_clock::time_point last = GetLastTransactionTime(connector);
|
||||
|
||||
std::uint32_t pendingIndex = transactions.size();
|
||||
for(std::uint32_t i = 0; i < transactions.size(); i++) {
|
||||
if(transactions[i].timePoint > last) {
|
||||
pendingIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
file.seekp(0, std::ios::end);
|
||||
for(std::uint32_t i = pendingIndex; i < transactions.size(); i++) {
|
||||
WriteString(file, transactions[i].name);
|
||||
WriteString(file, transactions[i].bic);
|
||||
WriteString(file, transactions[i].iban);
|
||||
WriteString(file, transactions[i].description);
|
||||
file.write(reinterpret_cast<char*>(&transactions[i].amount), sizeof(transactions[i].amount));
|
||||
file.write(reinterpret_cast<char*>(&transactions[i].currency), sizeof(Currency));
|
||||
std::int64_t time = std::chrono::duration_cast<std::chrono::microseconds>(transactions[i].timePoint.time_since_epoch()).count();
|
||||
file.write(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
}
|
||||
}
|
||||
|
||||
Transaction& Account::LoadTransaction(std::ifstream& file) {
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
ReadString(file, tr.iban);
|
||||
ReadString(file, tr.description);
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.currency));
|
||||
std::int64_t time;
|
||||
file.read(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
tr.timePoint = std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::microseconds(time)));
|
||||
return tr;
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::fstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
LoadTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
std::ifstream Account::LoadUntilFrom(std::string_view connector, std::chrono::system_clock::time_point from) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
in.seekg(sizeof(tr.amount)+sizeof(tr.Currency), std::ios::cur);
|
||||
std::int64_t timeI;
|
||||
file.read(reinterpret_cast<char*>(&timeI), sizeof(tr.timeI));
|
||||
std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::from_time_t(timeI);
|
||||
if(timePoint > from) {
|
||||
in.seekg(-seeked-sizeof(tr.amount)-sizeof(tr.Currency)-sizeof(tr.timeI), std::ios::cur);
|
||||
LoadTransaction();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from, std::chrono::system_clock::time_point to) {
|
||||
std::ifstream file = LoadUntilFrom(connector, from);
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
Transaction& tr = LoadTransaction();
|
||||
if(tr.timePoint > to) {
|
||||
transactions.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from) {
|
||||
std::ifstream file = LoadUntilFrom(connector, from);
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
LoadTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Account::LoadTransactionsByProperty(std::string_view connector, std::string_view property) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
if(description.size() == size) {
|
||||
std::string readDescription;
|
||||
readDescription.resize(size);
|
||||
file.read(readDescription.data(), size);
|
||||
if(readDescription == description) {
|
||||
in.seekg(-seeked-readDescription.size()-sizeof(size), std::ios::cur);
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
ReadString(file, tr.iban);
|
||||
SeekString();
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&tr.time), sizeof(tr.time));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Account::LoadTransactionsByIban(std::string_view connector, std::string_view iban) {
|
||||
transactions.clear();
|
||||
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
if(iban.size() == size) {
|
||||
std::string readIban;
|
||||
readIban.resize(size);
|
||||
file.read(readIban.data(), size);
|
||||
if(readIban == iban) {
|
||||
in.seekg(-seeked-readIban.size()-sizeof(size), std::ios::cur);
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
SeekString();
|
||||
ReadString(file, tr.description);
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&tr.time), sizeof(tr.time));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue