licensed, a bit better file handling, a bit better migration handling

This commit is contained in:
Blue 2023-12-10 20:23:15 -03:00
parent 319895db64
commit 03d7614673
Signed by: blue
GPG key ID: 9B203B252A63EE38
28 changed files with 934 additions and 22 deletions

9
utils/CMakeLists.txt Normal file
View file

@ -0,0 +1,9 @@
set(HEADER
helpers.h
)
set(SOURCES
helpers.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

61
utils/helpers.cpp Normal file
View file

@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "helpers.h"
#include "iostream"
#include "config.h"
static bool installed = false;
static std::filesystem::path sPath;
void setAbsoluteSharedPath () {
installed = true;
sPath = FULL_DATA_DIR "/" PROJECT_NAME; // should be something like /usr/share/pica or /local/usr/share/pica
}
void initPaths(const char* programPath) {
std::filesystem::path pp(programPath);
if (pp.filename() == programPath)
return setAbsoluteSharedPath();
std::filesystem::path cp;
try {
cp = std::filesystem::canonical(pp);
} catch (const std::filesystem::filesystem_error& e) {
setAbsoluteSharedPath();
return;
}
if (cp.parent_path() == FULL_BIN_DIR)
return setAbsoluteSharedPath();
std::cout << cp << std::endl;
std::filesystem::path parent = cp.parent_path();
if (endsWith(parent.string(), BIN_DIR)) { //this is the case when the program is installed somewhere but not system root
std::filesystem::path bin(BIN_DIR); //so it will read from something like ../share/pica/ relative to the binary
for (const auto& hop : bin) {
(void)hop; //I do this just to make as many ups as many members are in bin
parent = parent.parent_path();
}
sPath = parent / DATA_DIR / PROJECT_NAME;
} else {
sPath = parent / DATA_DIR; //this should read something like ./share relative to the binary
}
}
const std::filesystem::path& sharedPath() {
return sPath;
}
bool endsWith(const std::string& string, const std::string& query) {
std::string::size_type sl = string.length();
std::string::size_type ql = query.length();
if (sl >= ql) {
unsigned int result = string.compare(sl - ql, ql, query);
return result == 0;
} else {
return false;
}
}

12
utils/helpers.h Normal file
View file

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <filesystem>
#include <string>
void initPaths(const char* programPath);
const std::filesystem::path& sharedPath();
bool endsWith(const std::string& string, const std::string& query);