Skip to content

Commit e4c3bcd

Browse files
authored
Hint directory support (temp_file) (synesissoftware#46)
* ~ preparatory refactoring * `xtests::cpp::util::temp_file` : + now supports `hint_dir` parameter, to allow caller to specify temporary directory * ~ VC++ compatibility * `xtests::cpp::util::temp_file` : + now supports `hint_dir` parameter on Windows, to allow caller to specify temporary directory * ~ tidying
1 parent 1bf1274 commit e4c3bcd

File tree

4 files changed

+235
-99
lines changed

4 files changed

+235
-99
lines changed

examples/cpp/example.cpp.temp_file/main.cpp

+61-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/* /////////////////////////////////////////////////////////////////////////
22
* File: examples/cpp/example.cpp.temp_file/main.cpp
33
*
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;
510
*
611
* Created: ... mid 2010s ...
712
* Updated: 31st December 2024
813
*
914
* ////////////////////////////////////////////////////////////////////// */
1015

11-
/** \file example.cpp.temp_file.cpp
16+
/** \file example.cpp.temp_file/main.cpp
1217
*/
1318

1419

@@ -22,7 +27,7 @@
2227
#include <stdlib.h>
2328
#include <string.h>
2429

25-
#include <xtests/internal/checked_main.hpp>
30+
#include <xtests/internal/checked_main.hpp> // wraps the ostensible `main()` in exception handling
2631

2732

2833
/* /////////////////////////////////////////////////////////////////////////
@@ -38,25 +43,13 @@ typedef platformstl::filesystem_traits<char> fs_traits_t;
3843

3944

4045
/* /////////////////////////////////////////////////////////////////////////
41-
* helper functions
46+
* helper function declarations
4247
*/
4348

4449
namespace {
4550

4651
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);
6053
} // anonymous namespace
6154

6255

@@ -108,7 +101,7 @@ int main(int argc, char* argv[])
108101

109102
/* Use `None`, which means no special behaviour */
110103
{
111-
std::cout << "temp_file::None :" << std::endl;
104+
std::cout << "temp_file::None (without hint directory):" << std::endl;
112105

113106
temp_file tf(temp_file::None);
114107

@@ -119,6 +112,19 @@ int main(int argc, char* argv[])
119112
std::cout << "\tfile-size:\t" << get_file_size(tf) << std::endl;
120113
}
121114

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 << "\tpath: \t" << tf << std::endl;
122+
std::cout << "\texist?: \t" << fs_traits_t::file_exists(tf.c_str()) << std::endl;
123+
std::cout << "\tfile?: \t" << fs_traits_t::is_file(tf.c_str()) << std::endl;
124+
std::cout << "\tdir?: \t" << fs_traits_t::is_directory(tf.c_str()) << std::endl;
125+
std::cout << "\tfile-size:\t" << get_file_size(tf) << std::endl;
126+
}
127+
122128
/* Use `DeleteOnClose`, which causes the file to be deleted when the
123129
* `temp_file` instance is destroyed
124130
*/
@@ -179,13 +185,49 @@ int main(int argc, char* argv[])
179185
std::cout << "\tfile-size:\t" << get_file_size(tf) << std::endl;
180186
}
181187

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));
182196

183-
((void)&hint_dir);
197+
std::cout << "\tpath: \t" << tf << std::endl;
198+
std::cout << "\texist?: \t" << fs_traits_t::file_exists(tf.c_str()) << std::endl;
199+
std::cout << "\tfile?: \t" << fs_traits_t::is_file(tf.c_str()) << std::endl;
200+
std::cout << "\tdir?: \t" << fs_traits_t::is_directory(tf.c_str()) << std::endl;
201+
std::cout << "\tfile-size:\t" << get_file_size(tf) << std::endl;
202+
}
184203

185204

186205
return EXIT_SUCCESS;
187206
}
188207

189208

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+
190232
/* ///////////////////////////// end of file //////////////////////////// */
191233

include/xtests/util/temp_directory.hpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Purpose: Definition of the temp_directory class.
55
*
66
* Created: 1st October 2015
7-
* Updated: 4th December 2024
7+
* Updated: 31st December 2024
88
*
99
* Home: https://github.com/synesissoftware/xTests/
1010
*
@@ -51,8 +51,8 @@
5151
#ifndef XTESTS_DOCUMENTATION_SKIP_SECTION
5252
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_MAJOR 0
5353
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_MINOR 2
54-
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_REVISION 2
55-
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_EDIT 15
54+
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_REVISION 3
55+
# define XTESTS_VER_XTESTS_UTIL_HPP_TEMP_DIRECTORY_EDIT 17
5656
#endif /* !XTESTS_DOCUMENTATION_SKIP_SECTION */
5757

5858

@@ -127,11 +127,11 @@ class temp_directory
127127
{
128128
public: // Types
129129
/// The character type
130-
typedef char char_type;
130+
typedef char char_type;
131131
/// The size type
132-
typedef size_t size_type;
132+
typedef size_t size_type;
133133
/// This type
134-
typedef temp_directory class_type;
134+
typedef temp_directory class_type;
135135

136136
/// Flags that control behaviour of ctor and/or dtor
137137
enum Flags
@@ -146,9 +146,9 @@ class temp_directory
146146
class could_not_create_temporary_directory_exception;
147147

148148
private:
149-
typedef std::basic_string<char_type> string_type_;
150-
typedef platformstl::filesystem_traits<char_type> fs_traits_type_;
151-
typedef fs_traits_type_::file_handle_type file_handle_type_;
149+
typedef std::basic_string<char_type> string_type_;
150+
typedef platformstl::filesystem_traits<char_type> fs_traits_type_;
151+
typedef fs_traits_type_::file_handle_type file_handle_type_;
152152

153153
public: // Construction
154154
/// Establishes an empty temporary file according to the given
@@ -641,14 +641,17 @@ namespace stlsoft
641641
!defined(STLSOFT_NO_NAMESPACE)
642642
} /* namespace stlsoft */
643643
# endif
644-
645644
#endif /* !XTESTS_DOCUMENTATION_SKIP_SECTION */
646645

647646

648647
/* /////////////////////////////////////////////////////////////////////////
649648
* inclusion control
650649
*/
651650

651+
#ifdef STLSOFT_CF_PRAGMA_ONCE_SUPPORT
652+
# pragma once
653+
#endif /* STLSOFT_CF_PRAGMA_ONCE_SUPPORT */
654+
652655
#endif /* XTESTS_INCL_XTESTS_UTIL_HPP_TEMP_DIRECTORY */
653656

654657
/* ///////////////////////////// end of file //////////////////////////// */

0 commit comments

Comments
 (0)