#include "FileOperator.h"
#include <direct.h>
#include <io.h>
#include <string.h>
#include <sys/stat.h>
#include <iostream>
#include <Windows.h>
FileOperator::FileOperator()
{
}
FileOperator::~FileOperator()
{
}
bool FileOperator::ALIsFile(char* pPath)
{
struct stat s;
if (pPath == nullptr)
return false;
if (stat(pPath, &s) == 0)
{
if (s.st_mode & S_IFDIR)
{
return false;
}
else if (s.st_mode & S_IFREG)
{
return true;
}
}
return false;
}
bool FileOperator::ALDeleteFile(char* pPath)
{
if (ALIsFile(pPath))
{
if (remove(pPath) == 0)
{
return true;
}
}
return false;
}
bool FileOperator::ALIsDir(char* pPath)
{
if (pPath == nullptr)
return false;
struct stat s;
if (pPath == nullptr)
return false;
if (stat(pPath, &s) == 0)
{
if (s.st_mode & S_IFDIR)
{
return true;
}
else if (s.st_mode & S_IFREG)
{
return false;
}
}
return false;
}
bool FileOperator::ALDeleteDir(char* pPath)
{
if (ALIsDir(pPath))
{
std::string strDir = pPath;
if (strDir.at(strDir.length() - 1) != '\\')
strDir += '\\';
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA((strDir + "*.*").c_str(), &wfd);
if (hFind == INVALID_HANDLE_VALUE)
return false;
do
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (_stricmp(wfd.cFileName, ".") != 0 && _stricmp(wfd.cFileName, "..") != 0)
{
ALDeleteDir((char*)(strDir + wfd.cFileName).c_str());
}
}
else
{
DeleteFileA((strDir + wfd.cFileName).c_str());
}
} while (FindNextFileA(hFind, &wfd));
FindClose(hFind);
RemoveDirectoryA(pPath);
return true;
}
return false;
}
std::string FileOperator::ALGetFileNameWithOutExt(char* pPath)
{
std::string strFileName = ALGetFileName(pPath);
if (strFileName.size() != 0)
{
std::size_t found = strFileName.find_last_of(".");
strFileName = strFileName.substr(found + 1);
}
return strFileName;
}
std::string FileOperator::ALGetFileName(char* pPath)
{
std::string strRet;
if (pPath == nullptr)
{
return strRet;
}
strRet = pPath;
std::size_t found = strRet.find_last_of("/\\");
strRet = strRet.substr(found + 1);
return strRet;
}
std::string FileOperator::ALGetProgramDir()
{
TCHAR exeFullPath[MAX_PATH] = { 0 };
std::string strPath = "";
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
char CharString[MAX_PATH] = { 0 };
size_t convertedChars = 0;
wcstombs_s(&convertedChars, CharString, MAX_PATH, exeFullPath, _TRUNCATE);
strPath = (std::string)CharString;
int pos = strPath.find_last_of('\\', strPath.length());
return strPath.substr(0, pos);
}
bool FileOperator::FileExist(std::string strPath)
{
struct stat buffer;
return (stat(strPath.c_str(), &buffer) == 0);
}
int FileOperator::GetAllFormatFiles(std::string path, std::vector<std::string>& files, std::string format)
{
int iFileCnt = 0;
__int64 hFile = 0;
struct _finddata_t fileinfo;
std::string p;
if (ALIsFile((char*)path.c_str()))
{
size_t last_slash_idx = path.rfind('\\');
if (std::string::npos != last_slash_idx)
{
path = path.substr(0, last_slash_idx);
}
}
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
}
}
else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return iFileCnt;
}
#pragma once
#include <string>
#include <vector>
class FileOperator
{
public:
FileOperator();
~FileOperator();
public:
bool ALIsFile(char* pPath);
bool ALDeleteFile(char* pPath);
std::string ALGetFileName(char* pPath);
std::string ALGetFileNameWithOutExt(char* pPath);
bool ALIsDir(char* pPath);
bool ALDeleteDir(char* pPath);
std::string ALGetProgramDir();
bool FileExist(std::string strPath);
int GetAllFormatFiles(std::string path, std::vector<std::string>& files, std::string format);
};