2025-12-23 14:14:10 +11:00
|
|
|
#include "FsHelpers.h"
|
|
|
|
|
|
2026-03-05 10:12:22 -06:00
|
|
|
#include <cctype>
|
|
|
|
|
#include <cstring>
|
2025-12-23 14:14:10 +11:00
|
|
|
#include <vector>
|
|
|
|
|
|
2026-03-05 10:12:22 -06:00
|
|
|
namespace FsHelpers {
|
|
|
|
|
|
|
|
|
|
std::string normalisePath(const std::string& path) {
|
2025-12-23 14:14:10 +11:00
|
|
|
std::vector<std::string> components;
|
|
|
|
|
std::string component;
|
|
|
|
|
|
|
|
|
|
for (const auto c : path) {
|
|
|
|
|
if (c == '/') {
|
|
|
|
|
if (!component.empty()) {
|
|
|
|
|
if (component == "..") {
|
|
|
|
|
if (!components.empty()) {
|
|
|
|
|
components.pop_back();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
components.push_back(component);
|
|
|
|
|
}
|
|
|
|
|
component.clear();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
component += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!component.empty()) {
|
|
|
|
|
components.push_back(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
for (const auto& c : components) {
|
|
|
|
|
if (!result.empty()) {
|
|
|
|
|
result += "/";
|
|
|
|
|
}
|
|
|
|
|
result += c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2026-03-05 10:12:22 -06:00
|
|
|
|
|
|
|
|
bool checkFileExtension(std::string_view fileName, const char* extension) {
|
|
|
|
|
const size_t extLen = strlen(extension);
|
|
|
|
|
if (fileName.length() < extLen) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t offset = fileName.length() - extLen;
|
|
|
|
|
for (size_t i = 0; i < extLen; i++) {
|
|
|
|
|
if (tolower(static_cast<unsigned char>(fileName[offset + i])) !=
|
|
|
|
|
tolower(static_cast<unsigned char>(extension[i]))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasJpgExtension(std::string_view fileName) {
|
|
|
|
|
return checkFileExtension(fileName, ".jpg") || checkFileExtension(fileName, ".jpeg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasPngExtension(std::string_view fileName) { return checkFileExtension(fileName, ".png"); }
|
|
|
|
|
|
|
|
|
|
bool hasBmpExtension(std::string_view fileName) { return checkFileExtension(fileName, ".bmp"); }
|
|
|
|
|
|
|
|
|
|
bool hasGifExtension(std::string_view fileName) { return checkFileExtension(fileName, ".gif"); }
|
|
|
|
|
|
|
|
|
|
bool hasEpubExtension(std::string_view fileName) { return checkFileExtension(fileName, ".epub"); }
|
|
|
|
|
|
|
|
|
|
bool hasXtcExtension(std::string_view fileName) {
|
|
|
|
|
return checkFileExtension(fileName, ".xtc") || checkFileExtension(fileName, ".xtch");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasTxtExtension(std::string_view fileName) { return checkFileExtension(fileName, ".txt"); }
|
|
|
|
|
|
|
|
|
|
bool hasMarkdownExtension(std::string_view fileName) { return checkFileExtension(fileName, ".md"); }
|
|
|
|
|
|
|
|
|
|
} // namespace FsHelpers
|