您的位置:首页 > 其它

跨平台删除文件夹,拷贝文件夹,拷贝文件

2010-06-02 12:06 113 查看
为Windows和Linux平台。



#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

/** 
* @brief delDirectory 
* 
* Detailed description.
* @param[in] filePath 
* @return bool  
*/
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;
    }
}

/** 
* @brief copyDirectory 
* 
* Detailed description.
* @param[in] sourcePath 
* @param[in] destPath 
* @return bool  
*/
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;
    }
}

/** 
* @brief copyFile 
* 
* Detailed description.
* @param[in] sourcePath 
* @param[in] destPath 
*/
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐