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

来源:互联网 发布:opencv java 识别数字 编辑:程序博客网 时间:2024/06/11 05:38
  1. #include <string>  
  2. #include <algorithm>  
  3. #include <functional>  
  4. #include <map>  
  5. #include <time.h>  
  6. #include <vector>  
  7. #include <sstream>  
  8. #include <fstream>  
  9. #include <sys/types.h>  
  10. #include <sys/stat.h>  
  11. #include <stdio.h>   
  12. #include <errno.h>  
  13.   
  14. #ifdef WIN32  
  15. #include <winsock2.h>  
  16. #include <process.h>  
  17. #pragma comment(lib, "Ws2_32.lib")  
  18. #include <direct.h>  
  19. #include "tlhelp32.h"   
  20. #include "shellapi.h"  
  21. #else  
  22. #include <unistd.h>  
  23. #include <dirent.h>  
  24. #include <stdlib.h>   
  25. #include <sys/procfs.h>   
  26. #include <unistd.h>   
  27. #include <stropts.h>   
  28. #include <fcntl.h>   
  29. #include <signal.h>  
  30. #endif  
  31.   
  32. #ifdef WIN32  
  33. #define rmdir(x) _rmdir(x)  
  34. #define close_file(x) _close(x)  
  35. #define PID DWORD  
  36. #define mkdir(x) _mkdir(x)  
  37. #define sp_sleep(x) Sleep(x)  
  38. #define setWorkingPath(x) SetCurrentDirectory(x)  
  39. #define getErrorNo() GetLastError()  
  40. #else  
  41. #define rmdir(x) rmdir(x)  
  42. #define close_file(x) close(x)  
  43. #define PID pid_t  
  44. #define mkdir(x) mkdir(x, 0)  
  45. #define sp_sleep(x) usleep(x * 1000)  
  46. #define setWorkingPath(x) chdir(x)  
  47. #define getErrorNo() errno  
  48. #endif  
  49.   
  50. /**  
  51. * @brief delDirectory  
  52.  
  53. * Detailed description. 
  54. * @param[in] filePath  
  55. * @return bool   
  56. */  
  57. inline bool delDirectory(std::string filePath)  
  58. {  
  59. #ifdef WIN32  
  60.     std::string tmpStr = "rd /"" + filePath + "/" /s /q";  
  61. #else  
  62.     std::string tmpStr = "rm -rf /"" + filePath + "/"";   
  63. #endif  
  64.     int rt = system(tmpStr.c_str());   
  65.     if (0 == rt)  
  66.     {  
  67.         return true;  
  68.     }  
  69.     else  
  70.     {  
  71.         return false;  
  72.     }  
  73. }  
  74.   
  75. /**  
  76. * @brief copyDirectory  
  77.  
  78. * Detailed description. 
  79. * @param[in] sourcePath  
  80. * @param[in] destPath  
  81. * @return bool   
  82. */  
  83. inline bool copyDirectory(std::string sourcePath, std::string destPath)  
  84. {  
  85.     if (sourcePath == destPath)  
  86.     {  
  87.         return true;  
  88.     }  
  89. #ifdef WIN32  
  90.     if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/'))  
  91.     {  
  92.         sourcePath = sourcePath.substr(0, sourcePath.length() - 1);  
  93.     }  
  94.     if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/'))  
  95.     {  
  96.         destPath = destPath.substr(0, destPath.length() - 1);  
  97.     }  
  98.     std::string tmpStr = "xcopy /"" + sourcePath + "/" /"" + destPath + "/" /e /y /h /c /k /r";  
  99. #else  
  100.     std::string tmpStr = "cp -rf /"" + sourcePath + "/" /"" + destPath + "/"";  
  101. #endif  
  102.     int rt  = system(tmpStr.c_str());   
  103.     if (0 == rt)  
  104.     {  
  105.         return true;  
  106.     }  
  107.     else  
  108.     {  
  109.         return false;  
  110.     }  
  111. }  
  112.   
  113. /**  
  114. * @brief copyFile  
  115.  
  116. * Detailed description. 
  117. * @param[in] sourcePath  
  118. * @param[in] destPath  
  119. */  
  120. inline void copyFile(std::string sourcePath, std::string destPath)  
  121. {  
  122.     if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/'))  
  123.     {  
  124.         sourcePath = sourcePath.substr(0, sourcePath.length() - 1);  
  125.     }  
  126.     if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/'))  
  127.     {  
  128.         destPath = destPath.substr(0, destPath.length() - 1);  
  129.     }  
  130.     std::ifstream input(sourcePath.c_str(), std::ios::binary);  
  131.     std::ofstream output(destPath.c_str(), std::ios::binary);  
  132.     output << input.rdbuf();  
  133.     output.close();  
  134.     input.close();  
  135. }  

原创粉丝点击