1
1
/* /////////////////////////////////////////////////////////////////////////
2
2
* File: examples/cpp/example.cpp.temp_file/main.cpp
3
3
*
4
- * Purpose: Example use of `xtests::cpp::util::temp_file`.
4
+ * Purpose: Example use of `xtests::cpp::util::temp_file`, illustrating:
5
+ * - use of various flags, including `DeleteOnOpen`, etc.;
6
+ * - use of hint-directory, which overrides the default temp
7
+ * directory for a given platform;
8
+ * - specifying temporary file's initial contents from an
9
+ * array;
5
10
*
6
11
* Created: ... mid 2010s ...
7
12
* Updated: 31st December 2024
8
13
*
9
14
* ////////////////////////////////////////////////////////////////////// */
10
15
11
- /* * \file example.cpp.temp_file.cpp
16
+ /* * \file example.cpp.temp_file/main .cpp
12
17
*/
13
18
14
19
22
27
#include < stdlib.h>
23
28
#include < string.h>
24
29
25
- #include < xtests/internal/checked_main.hpp>
30
+ #include < xtests/internal/checked_main.hpp> // wraps the ostensible `main()` in exception handling
26
31
27
32
28
33
/* /////////////////////////////////////////////////////////////////////////
@@ -38,25 +43,13 @@ typedef platformstl::filesystem_traits<char> fs_traits_t;
38
43
39
44
40
45
/* /////////////////////////////////////////////////////////////////////////
41
- * helper functions
46
+ * helper function declarations
42
47
*/
43
48
44
49
namespace {
45
50
46
51
fs_traits_t ::large_size_type
47
- get_file_size (temp_file const & tf)
48
- {
49
- fs_traits_t ::stat_data_type sd;
50
-
51
- if (!fs_traits_t::stat (tf.c_str (), &sd))
52
- {
53
- return 0 ;
54
- }
55
- else
56
- {
57
- return fs_traits_t::get_file_size (sd);
58
- }
59
- }
52
+ get_file_size (temp_file const & tf);
60
53
} // anonymous namespace
61
54
62
55
@@ -108,7 +101,7 @@ int main(int argc, char* argv[])
108
101
109
102
/* Use `None`, which means no special behaviour */
110
103
{
111
- std::cout << " temp_file::None :" << std::endl;
104
+ std::cout << " temp_file::None (without hint directory) :" << std::endl;
112
105
113
106
temp_file tf (temp_file::None);
114
107
@@ -119,6 +112,19 @@ int main(int argc, char* argv[])
119
112
std::cout << " \t file-size:\t " << get_file_size (tf) << std::endl;
120
113
}
121
114
115
+ /* Use `None`, but in a specific directory */
116
+ {
117
+ std::cout << " temp_file::None (with hint directory '" << hint_dir << " '):" << std::endl;
118
+
119
+ temp_file tf (temp_file::None, hint_dir);
120
+
121
+ std::cout << " \t path: \t " << tf << std::endl;
122
+ std::cout << " \t exist?: \t " << fs_traits_t::file_exists (tf.c_str ()) << std::endl;
123
+ std::cout << " \t file?: \t " << fs_traits_t::is_file (tf.c_str ()) << std::endl;
124
+ std::cout << " \t dir?: \t " << fs_traits_t::is_directory (tf.c_str ()) << std::endl;
125
+ std::cout << " \t file-size:\t " << get_file_size (tf) << std::endl;
126
+ }
127
+
122
128
/* Use `DeleteOnClose`, which causes the file to be deleted when the
123
129
* `temp_file` instance is destroyed
124
130
*/
@@ -179,13 +185,49 @@ int main(int argc, char* argv[])
179
185
std::cout << " \t file-size:\t " << get_file_size (tf) << std::endl;
180
186
}
181
187
188
+ /* Specifying some initial contents, but in a specific directory
189
+ */
190
+ {
191
+ std::cout << " some initial contents (with hint directory '" << hint_dir << " ') :" << std::endl;
192
+
193
+ int const initial_contents[] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 };
194
+
195
+ temp_file tf (temp_file::DeleteOnClose | temp_file::EmptyOnOpen, hint_dir, &initial_contents, sizeof (initial_contents));
182
196
183
- ((void )&hint_dir);
197
+ std::cout << " \t path: \t " << tf << std::endl;
198
+ std::cout << " \t exist?: \t " << fs_traits_t::file_exists (tf.c_str ()) << std::endl;
199
+ std::cout << " \t file?: \t " << fs_traits_t::is_file (tf.c_str ()) << std::endl;
200
+ std::cout << " \t dir?: \t " << fs_traits_t::is_directory (tf.c_str ()) << std::endl;
201
+ std::cout << " \t file-size:\t " << get_file_size (tf) << std::endl;
202
+ }
184
203
185
204
186
205
return EXIT_SUCCESS;
187
206
}
188
207
189
208
209
+ /* /////////////////////////////////////////////////////////////////////////
210
+ * helper function implementations
211
+ */
212
+
213
+ namespace {
214
+
215
+ fs_traits_t ::large_size_type
216
+ get_file_size (temp_file const & tf)
217
+ {
218
+ fs_traits_t ::stat_data_type sd;
219
+
220
+ if (!fs_traits_t::stat (tf.c_str (), &sd))
221
+ {
222
+ return 0 ;
223
+ }
224
+ else
225
+ {
226
+ return fs_traits_t::get_file_size (sd);
227
+ }
228
+ }
229
+ } // anonymous namespace
230
+
231
+
190
232
/* ///////////////////////////// end of file //////////////////////////// */
191
233
0 commit comments