跨平台删除文件夹,拷贝文件夹,拷贝文件
来源:互联网 发布:opencv java 识别数字 编辑:程序博客网 时间:2024/06/11 05:38
- #include <string>
- #include <algorithm>
- #include <functional>
- #include <map>
- #include <time.h>
- #include <vector>
- #include <sstream>
- #include <fstream>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <errno.h>
-
- #ifdef WIN32
- #include <winsock2.h>
- #include <process.h>
- #pragma comment(lib, "Ws2_32.lib")
- #include <direct.h>
- #include "tlhelp32.h"
- #include "shellapi.h"
- #else
- #include <unistd.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <sys/procfs.h>
- #include <unistd.h>
- #include <stropts.h>
- #include <fcntl.h>
- #include <signal.h>
- #endif
-
- #ifdef WIN32
- #define rmdir(x) _rmdir(x)
- #define close_file(x) _close(x)
- #define PID DWORD
- #define mkdir(x) _mkdir(x)
- #define sp_sleep(x) Sleep(x)
- #define setWorkingPath(x) SetCurrentDirectory(x)
- #define getErrorNo() GetLastError()
- #else
- #define rmdir(x) rmdir(x)
- #define close_file(x) close(x)
- #define PID pid_t
- #define mkdir(x) mkdir(x, 0)
- #define sp_sleep(x) usleep(x * 1000)
- #define setWorkingPath(x) chdir(x)
- #define getErrorNo() errno
- #endif
-
-
-
-
-
-
-
-
- inline bool delDirectory(std::string filePath)
- {
- #ifdef WIN32
- std::string tmpStr = "rd /"" + filePath + "/" /s /q";
- #else
- std::string tmpStr = "rm -rf /"" + filePath + "/"";
- #endif
- int rt = system(tmpStr.c_str());
- if (0 == rt)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
- inline bool copyDirectory(std::string sourcePath, std::string destPath)
- {
- if (sourcePath == destPath)
- {
- return true;
- }
- #ifdef WIN32
- if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/'))
- {
- sourcePath = sourcePath.substr(0, sourcePath.length() - 1);
- }
- if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/'))
- {
- destPath = destPath.substr(0, destPath.length() - 1);
- }
- std::string tmpStr = "xcopy /"" + sourcePath + "/" /"" + destPath + "/" /e /y /h /c /k /r";
- #else
- std::string tmpStr = "cp -rf /"" + sourcePath + "/" /"" + destPath + "/"";
- #endif
- int rt = system(tmpStr.c_str());
- if (0 == rt)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
-
-
-
- inline void copyFile(std::string sourcePath, std::string destPath)
- {
- if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/'))
- {
- sourcePath = sourcePath.substr(0, sourcePath.length() - 1);
- }
- if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/'))
- {
- destPath = destPath.substr(0, destPath.length() - 1);
- }
- std::ifstream input(sourcePath.c_str(), std::ios::binary);
- std::ofstream output(destPath.c_str(), std::ios::binary);
- output << input.rdbuf();
- output.close();
- input.close();
- }