#include "stdafx.h" #include #include "FileRecipe.h" #include #include #include #include #include #pragma comment(lib,"Dbghelp.lib") CFileRecipe::CFileRecipe() { } CFileRecipe::~CFileRecipe() { } bool CFileRecipe::fileIsExist(std::string fileName) { /* code */ if (-1 != _access(fileName.c_str(), 0)) return true; return false; } void CFileRecipe::removeDir(std::string dirPath) { /* code */ struct _finddata_t fb; // ²éÕÒÏàͬÊôÐÔÎļþµÄ´æ´¢½á¹¹Ìå string path; long long handle; // ×¢Òâ´Ë´¦ÐèÒªlong*2 int noFile; // ¶ÔϵͳÒþ²ØÎļþµÄ´¦Àí±ê¼Ç noFile = 0; handle = 0; struct stat s; if (stat(dirPath.c_str(), &s) == 0) { if (s.st_mode & S_IFDIR) { std::cout << "it's a directory" << std::endl; } else if (s.st_mode & S_IFREG) { std::cout << "it's a file" << std::endl; } else { std::cout << "not file not directory" << std::endl; } } else { std::cout << "error, doesn't exist" << std::endl; return; } path = dirPath + "/*"; handle = _findfirst(path.c_str(), &fb); // ÕÒµ½µÚÒ»¸öÆ¥ÅäµÄÎļþ if (handle != 0){ // µ±¿ÉÒÔ¼ÌÐøÕÒµ½Æ¥ÅäµÄÎļþ£¬¼ÌÐøÖ´ÐÐ while (0 == _findnext(handle, &fb)){ // windowsÏ£¬³£ÓиöϵͳÎļþ£¬ÃûΪ¡°..¡±,¶ÔËü²»×ö´¦Àí noFile = strcmp(fb.name, ".."); if (0 != noFile){ path = dirPath + "/" + fb.name; // ÊôÐÔֵΪ16£¬Ôò˵Ã÷ÊÇÎļþ¼Ð£¬µü´ú if (fb.attrib == 16){ removeDir(path); } // ·ÇÎļþ¼ÐµÄÎļþ£¬Ö±½Óɾ³ý¡£¶ÔÎļþÊôÐÔÖµµÄÇé¿öû×öÏêϸµ÷²é£¬¿ÉÄÜ»¹ÓÐÆäËûÇé¿ö¡£ else{ remove(path.c_str()); } } } //ɾ³ý¿ÕĿ¼ // _rmdir(dirPath.c_str()); // ¹Ø±ÕÎļþ¼Ð£¬Ö»ÓйرÕÁ˲ÅÄÜɾ³ý¡£ÕÒÕâ¸öº¯ÊýÕÒÁ˺ܾ㬱ê×¼cÖÐÓõÄÊÇclosedir // ¾­Ñé½éÉÜ£ºÒ»°ã²úÉúHandleµÄº¯ÊýÖ´Ðк󣬶¼Òª½øÐйرյ͝×÷¡£ _findclose(handle); } } void CFileRecipe::makeDir(std::string dirName) { /* code */ MakeSureDirectoryPathExists(dirName.c_str()); } bool CFileRecipe::openRecipeFile(CStdioFile &fileRecipe, CString fileName) { /* code */ if (!fileRecipe.Open(fileName, CFile::modeCreate | CFile::modeReadWrite)) return false; return true; } bool CFileRecipe::readRecileFile(CStdioFile &fileRecipe, CString fileName) { /* code */ if (!fileRecipe.Open(fileName, CFile::modeRead)) return false; return true; } CString CFileRecipe::toCString(std::string str) { /* code */ CString strRes = CA2T(str.c_str()); return strRes; } std::string CFileRecipe::toString(CString str) { /* code */ std::string strRes(CT2A(str.GetString())); return strRes; } void CFileRecipe::WriteString(CStdioFile &fileRecipe, std::string strText) { /* code */ CString str = toCString(strText); fileRecipe.WriteString(str); } void CFileRecipe::WriteCString(CStdioFile &fileRecipe, CString strText) { /* code */ fileRecipe.WriteString(strText); } BOOL CFileRecipe::ReadString(CStdioFile &fileRecipe, CString &strRes) { /* code */ return fileRecipe.ReadString(strRes); } void StringTrim(std::string& s) { /* code */ if (!s.empty()) { s.erase(0, s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); } } int CFileRecipe::StringSplit(const std::string& strScr, const std::string& delim, std::vector& strings) { /* code */ strings.clear(); size_t pos = 0; size_t len = strScr.length(); size_t delim_len = delim.length(); if (delim_len == 0) { return 0; } while (pos < len) { size_t find_pos = strScr.find(delim, pos); if (find_pos == strScr.npos) { std::string str2 = strScr.substr(pos, len - pos); StringTrim(str2); strings.push_back(str2); break; } std::string str2 = strScr.substr(pos, find_pos - pos); StringTrim(str2); strings.push_back(str2); pos = find_pos + delim_len; } return (int)strings.size(); } int CFileRecipe::CStringSplit(const CString& strText, const std::string& delim, std::vector& strings) { /* code */ std::string str = toString(strText); return StringSplit(str, delim, strings); } int CFileRecipe::CStringToInt(CString strText) { /* code */ int result = _wtoi(strText); return result; } int CFileRecipe::StringToInt(std::string str) { /* code */ CString strText = toCString(str); return CStringToInt(strText); } double CFileRecipe::CStringToF(CString strText) { /* code */ double result = _wtof(strText); return result; } double CFileRecipe::StringToF(std::string str) { /* code */ CString strText = toCString(str); return CStringToF(strText); }