Ticket #4677: v3_remove_all.cpp

File v3_remove_all.cpp, 2.2 KB (added by terminatorul@…, 12 years ago)

Sample source code

Line 
1#define BOOST_FILESYSTEM_VERSION 3
2
3#ifndef UNICODE
4# define UNICODE
5#endif
6
7#include <cstdlib>
8#include <exception>
9#include <stdexcept>
10#include <iostream>
11#include <string>
12
13#include <boost/filesystem.hpp>
14
15#include <sys/stat.h> // standard POSIX header for mkdir
16#include <direct.h> // actual header for mkdir :)
17
18#include <tchar.h>
19
20using namespace std;
21
22int main(int argc, char const *argv[])
23try
24{
25 int iRetCode = EXIT_SUCCESS;
26
27 if (argc > 2)
28 {
29 cout << "Attempts to delete specified directory using boost::filesystem::remove_all()\n";
30 cout << "Syntax:\n";
31 cout << "\t" << argv[0] << " dirname\n";
32 cout << endl;
33
34 iRetCode = EXIT_FAILURE;
35 }
36 else
37 {
38 if (argc == 2)
39 {
40 boost::filesystem::remove_all(argv[1]);
41 iRetCode = EXIT_SUCCESS;
42 }
43 else
44 {
45 char const *tmp_dir = getenv("TMP");
46
47 if (tmp_dir == NULL)
48 tmp_dir = getenv("TEMP");
49
50 if (tmp_dir)
51 {
52 int iCode = mkdir((string(tmp_dir) + "/v3_dir").c_str());
53
54 if (iCode == 0)
55 mkdir((string(tmp_dir) + "/v3_dir/dirname").c_str());
56
57 if (iCode == 0)
58 mkdir((string(tmp_dir) + "/v3_dir/dirnam2").c_str());
59
60 if (iCode == 0)
61 mkdir((string(tmp_dir) + "/v3_dir/dirnam3").c_str());
62 else
63 throw
64 runtime_error
65 (
66 string("Failed to create temporary directories for test.\n")
67 +
68 strerror(errno)
69 );
70
71 TCHAR szTempDir[_MAX_PATH]; // Microsoft has _MAX_PATH in <stdlib.h>
72 string strTempDir = tmp_dir;
73
74 strTempDir += "/v3_dir";
75 TCHAR *pCh = szTempDir;
76 string::const_iterator
77 it = strTempDir.begin();
78
79 while
80 (
81 it != strTempDir.end()
82 &&
83 pCh < szTempDir + sizeof szTempDir / sizeof szTempDir[0] - 1
84 )
85 {
86 *pCh++ = *it++;
87 }
88
89 *pCh = _T('\0');
90
91 boost::filesystem::remove_all(szTempDir);
92
93 iRetCode = EXIT_SUCCESS;
94 }
95 else
96 throw runtime_error("TEMP or TMP not set.");
97 }
98 }
99}
100catch(exception const &ex)
101{
102 cerr << "Application error." << endl;
103 cerr << ex.what() << endl;
104
105 return EXIT_FAILURE;
106}
107catch(...)
108{
109 cerr << "Application error.";
110
111 return EXIT_FAILURE;
112}