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