mp/include/util.h
2024-02-23 16:24:42 +08:00

74 lines
1.6 KiB
C++

#ifndef UTIL_H
#define UTIL_H
#include <filesystem>
#include <algorithm>
#include <string>
#include <array>
#include <cstring>
#include <string_view>
enum class FileType {
AUDIO,
VIDEO,
IMG,
ERRORTYPE
};
const std::array<std::string_view, 1> MusicFileExtensive = {
"mp3"
};
const std::array<std::string_view, 1> VideoFileExtensive = {
"mp4"
};
const std::array<std::string_view, 3> ImgFileExtensive = {
"jpeg",
"jpg",
"png",
};
using namespace std::filesystem;
class Util {
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;
}
public:
static FileType GetFileType(const path& filepath) {
if (IsMusic(filepath)) return FileType::AUDIO;
if (IsVideo(filepath)) return FileType::VIDEO;
if (IsImg(filepath)) return FileType::IMG;
return FileType::ERRORTYPE;
}
};
#endif