2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 // Copyright (c) 2019 Alexander Grund
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <boost/nowide/cstdio.hpp>
12 #include <boost/nowide/convert.hpp>
19 bool file_exists(const std::string
& filename
)
22 FILE* f
= boost::nowide::detail::wfopen(boost::nowide::widen(filename
).c_str(), L
"r");
24 FILE* f
= std::fopen(filename
.c_str(), "r");
35 void create_test_file(const std::string
& filename
)
38 FILE* f
= boost::nowide::detail::wfopen(boost::nowide::widen(filename
).c_str(), L
"w");
40 FILE* f
= std::fopen(filename
.c_str(), "w");
43 TEST(std::fputs("test\n", f
) >= 0);
48 #include <crtdbg.h> // For _CrtSetReportMode
49 void noop_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, uintptr_t)
53 void test_main(int, char** argv
, char**)
55 const std::string prefix
= argv
[0];
56 const std::string filename
= prefix
+ "\xd7\xa9-\xd0\xbc-\xce\xbd.txt";
58 // Prevent abort on freopen(NULL, ...)
59 _set_invalid_parameter_handler(noop_invalid_param_handler
);
62 std::cout
<< " -- fopen - existing file" << std::endl
;
64 create_test_file(filename
);
65 FILE* f
= boost::nowide::fopen(filename
.c_str(), "r");
68 TEST(std::fgets(buf
, 16, f
) != 0);
69 TEST(strcmp(buf
, "test\n") == 0);
72 std::cout
<< " -- remove" << std::endl
;
74 create_test_file(filename
);
75 TEST(file_exists(filename
));
76 TEST(boost::nowide::remove(filename
.c_str()) == 0);
77 TEST(!file_exists(filename
));
79 std::cout
<< " -- fopen non-existing file" << std::endl
;
81 boost::nowide::remove(filename
.c_str());
82 TEST(!file_exists(filename
));
83 TEST(boost::nowide::fopen(filename
.c_str(), "r") == NULL
);
84 TEST(!file_exists(filename
));
86 std::cout
<< " -- freopen" << std::endl
;
88 create_test_file(filename
);
89 FILE* f
= boost::nowide::fopen(filename
.c_str(), "r+");
91 std::cout
<< " -- Can read & write" << std::endl
;
94 TEST(std::fgets(buf
, 32, f
) != 0);
95 TEST(strcmp(buf
, "test\n") == 0);
96 TEST(std::fseek(f
, 0, SEEK_END
) == 0);
97 TEST(std::fputs("foobar\n", f
) >= 0);
99 // Reopen in read mode
100 // Note that changing the mode is not possibly on all implementations
101 // E.g. MSVC disallows NULL completely as the file parameter
102 FILE* f2
= boost::nowide::freopen(NULL
, "r", f
);
104 f2
= boost::nowide::freopen(filename
.c_str(), "r", f
);
105 std::cout
<< " -- no write possible" << std::endl
;
108 TEST(std::fputs("not-written\n", f
) < 0);
109 TEST(std::fseek(f
, 0, SEEK_SET
) == 0);
111 TEST(std::fgets(buf
, 32, f
) != 0);
112 TEST(strcmp(buf
, "test\n") == 0);
113 TEST(std::fgets(buf
, 32, f
) != 0);
114 TEST(strcmp(buf
, "foobar\n") == 0);
116 std::cout
<< " -- Reopen different file" << std::endl
;
117 const std::string filename2
= filename
+ ".1.txt";
118 TEST(boost::nowide::freopen(filename2
.c_str(), "w", f
) == f
);
121 TEST(std::fputs("baz\n", f
) >= 0);
123 f
= boost::nowide::fopen(filename2
.c_str(), "r");
125 TEST(std::fgets(buf
, 32, f
) != 0);
126 TEST(strcmp(buf
, "baz\n") == 0);
129 boost::nowide::remove(filename2
.c_str());
131 std::cout
<< " -- rename" << std::endl
;
133 create_test_file(filename
);
134 const std::string filename2
= filename
+ ".1.txt";
135 boost::nowide::remove(filename2
.c_str());
136 TEST(file_exists(filename
));
137 TEST(!file_exists(filename2
));
138 TEST(boost::nowide::rename(filename
.c_str(), filename2
.c_str()) == 0);
139 TEST(!file_exists(filename
));
140 TEST(file_exists(filename2
));
141 TEST(boost::nowide::remove(filename
.c_str()) < 0);
142 TEST(boost::nowide::remove(filename2
.c_str()) == 0);