mp/include/util.h

74 lines
1.6 KiB
C
Raw Normal View History

2024-02-20 14:55:57 +08:00
#ifndef UTIL_H
#define UTIL_H
#include <filesystem>
#include <algorithm>
#include <string>
#include <array>
#include <cstring>
#include <string_view>
enum class FileType {
MUSIC,
VIDEO,
IMG,
ERRORTYPE
2024-02-20 15:59:21 +08:00
};
const std::array<std::string_view, 1> MusicFileExtensive = {
"mp3"
2024-02-20 14:55:57 +08:00
};
const std::array<std::string_view, 1> VideoFileExtensive = {
"mp4"
2024-02-20 14:55:57 +08:00
};
const std::array<std::string_view, 3> ImgFileExtensive = {
"jpeg",
"jpg",
"png",
2024-02-20 14:55:57 +08:00
};
using namespace std::filesystem;
class Util {
2024-02-20 15:59:21 +08:00
private:
static std::string_view GetFileExtensive(const path& filepath) {
static std::string ext = filepath.extension().string();
ext.erase(std::ranges::remove_if(ext, [](char c) {return c == '.'; }).begin(), ext.end());
return std::string_view{ ext.c_str() };
}
static bool IsMusic(const path& filepath) {
const auto ext = GetFileExtensive(filepath);
for (const auto& it : MusicFileExtensive) {
if (it == ext)
return true;
}
return false;
}
static bool IsVideo(const path& filepath) {
const auto ext = GetFileExtensive(filepath);
for (const auto& it : VideoFileExtensive) {
if (it == ext)
return true;
}
return false;
}
static bool IsImg(const path& filepath) {
const auto ext = GetFileExtensive(filepath);
for (const auto& it : ImgFileExtensive) {
if (it == ext)
return true;
}
return false;
}
2024-02-20 15:59:21 +08:00
public:
static FileType GetFileType(const path& filepath) {
if (IsMusic(filepath)) return FileType::MUSIC;
if (IsVideo(filepath)) return FileType::VIDEO;
if (IsImg(filepath)) return FileType::IMG;
return FileType::ERRORTYPE;
}
2024-02-20 14:55:57 +08:00
};
#endif