]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // Copyright (c) 2012 Artyom Beilis (Tonkikh) | |
3 | // Copyright (c) 2019 Alexander Grund | |
4 | // | |
5 | // Distributed under the Boost Software License, Version 1.0. (See | |
20effc67 | 6 | // accompanying file LICENSE or copy at |
f67539c2 TL |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | // | |
9 | ||
10 | #include <boost/nowide/cstdio.hpp> | |
11 | ||
12 | #include <boost/nowide/convert.hpp> | |
1e59de90 | 13 | #include "test.hpp" |
f67539c2 TL |
14 | #include <cstdlib> |
15 | #include <cstring> | |
16 | #include <iostream> | |
17 | ||
f67539c2 TL |
18 | bool file_exists(const std::string& filename) |
19 | { | |
20 | #ifdef BOOST_WINDOWS | |
21 | FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"r"); | |
22 | #else | |
23 | FILE* f = std::fopen(filename.c_str(), "r"); | |
24 | #endif | |
25 | bool result = false; | |
26 | if(f) | |
27 | { | |
28 | std::fclose(f); | |
29 | result = true; | |
30 | } | |
31 | return result; | |
32 | } | |
33 | ||
34 | void create_test_file(const std::string& filename) | |
35 | { | |
36 | #ifdef BOOST_WINDOWS | |
37 | FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"w"); | |
38 | #else | |
39 | FILE* f = std::fopen(filename.c_str(), "w"); | |
40 | #endif | |
41 | TEST(f); | |
42 | TEST(std::fputs("test\n", f) >= 0); | |
43 | std::fclose(f); | |
44 | } | |
45 | ||
46 | #if BOOST_MSVC | |
47 | #include <crtdbg.h> // For _CrtSetReportMode | |
48 | void noop_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, uintptr_t) | |
1e59de90 | 49 | {} // LCOV_EXCL_LINE |
f67539c2 TL |
50 | #endif |
51 | ||
1e59de90 | 52 | // coverity [root_function] |
f67539c2 TL |
53 | void test_main(int, char** argv, char**) |
54 | { | |
55 | const std::string prefix = argv[0]; | |
56 | const std::string filename = prefix + "\xd7\xa9-\xd0\xbc-\xce\xbd.txt"; | |
57 | #if BOOST_MSVC | |
58 | // Prevent abort on freopen(NULL, ...) | |
59 | _set_invalid_parameter_handler(noop_invalid_param_handler); | |
60 | #endif | |
61 | ||
62 | std::cout << " -- fopen - existing file" << std::endl; | |
63 | { | |
64 | create_test_file(filename); | |
65 | FILE* f = boost::nowide::fopen(filename.c_str(), "r"); | |
66 | TEST(f); | |
67 | char buf[16]; | |
68 | TEST(std::fgets(buf, 16, f) != 0); | |
69 | TEST(strcmp(buf, "test\n") == 0); | |
70 | std::fclose(f); | |
71 | } | |
72 | std::cout << " -- remove" << std::endl; | |
73 | { | |
74 | create_test_file(filename); | |
75 | TEST(file_exists(filename)); | |
76 | TEST(boost::nowide::remove(filename.c_str()) == 0); | |
77 | TEST(!file_exists(filename)); | |
78 | } | |
79 | std::cout << " -- fopen non-existing file" << std::endl; | |
80 | { | |
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)); | |
85 | } | |
86 | std::cout << " -- freopen" << std::endl; | |
87 | { | |
88 | create_test_file(filename); | |
89 | FILE* f = boost::nowide::fopen(filename.c_str(), "r+"); | |
90 | TEST(f); | |
91 | std::cout << " -- Can read & write" << std::endl; | |
92 | { | |
93 | char buf[32]; | |
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); | |
98 | } | |
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); | |
103 | if(!f2) | |
104 | f2 = boost::nowide::freopen(filename.c_str(), "r", f); | |
105 | std::cout << " -- no write possible" << std::endl; | |
106 | { | |
107 | TEST(f2 == f); | |
108 | TEST(std::fputs("not-written\n", f) < 0); | |
109 | TEST(std::fseek(f, 0, SEEK_SET) == 0); | |
110 | char buf[32]; | |
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); | |
115 | } | |
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); | |
119 | { | |
120 | char buf[32]; | |
121 | TEST(std::fputs("baz\n", f) >= 0); | |
122 | std::fclose(f); | |
123 | f = boost::nowide::fopen(filename2.c_str(), "r"); | |
124 | TEST(f); | |
125 | TEST(std::fgets(buf, 32, f) != 0); | |
126 | TEST(strcmp(buf, "baz\n") == 0); | |
127 | } | |
128 | std::fclose(f); | |
129 | boost::nowide::remove(filename2.c_str()); | |
130 | } | |
131 | std::cout << " -- rename" << std::endl; | |
132 | { | |
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); | |
143 | } | |
144 | } |