84 lines
2.6 KiB
C++
84 lines
2.6 KiB
C++
#include "FsHelpers.h"
|
|
|
|
#include <cctype>
|
|
#include <cstring>
|
|
|
|
namespace FsHelpers {
|
|
|
|
// Process a finalised component in-place, appending it to `out` (preceded by
|
|
// '/' if `out` is non-empty) or popping the last component for "..". Used by
|
|
// both normalisePath overloads so the parsing rules stay in one place.
|
|
static void appendOrPopComponent(std::string& out, const char* compData, size_t compLen) {
|
|
if (compLen == 0) return;
|
|
if (compLen == 2 && compData[0] == '.' && compData[1] == '.') {
|
|
if (out.empty()) return;
|
|
const auto lastSlash = out.find_last_of('/');
|
|
if (lastSlash == std::string::npos) {
|
|
out.clear();
|
|
} else {
|
|
out.resize(lastSlash);
|
|
}
|
|
return;
|
|
}
|
|
if (!out.empty()) {
|
|
out.push_back('/');
|
|
}
|
|
out.append(compData, compLen);
|
|
}
|
|
|
|
void normalisePath(const std::string& path, std::string& out) {
|
|
out.clear();
|
|
size_t componentStart = 0;
|
|
for (size_t i = 0; i < path.size(); i++) {
|
|
if (path[i] == '/') {
|
|
appendOrPopComponent(out, path.data() + componentStart, i - componentStart);
|
|
componentStart = i + 1;
|
|
}
|
|
}
|
|
appendOrPopComponent(out, path.data() + componentStart, path.size() - componentStart);
|
|
}
|
|
|
|
std::string normalisePath(const std::string& path) {
|
|
std::string result;
|
|
normalisePath(path, result);
|
|
return result;
|
|
}
|
|
|
|
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
|