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
6 //  accompanying file LICENSE or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt)
8 //
9 
10 #include <boost/nowide/cstdio.hpp>
11 
12 #include <boost/nowide/convert.hpp>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 
17 #include "test.hpp"
18 
file_exists(const std::string & filename)19 bool file_exists(const std::string& filename)
20 {
21 #ifdef BOOST_WINDOWS
22     FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"r");
23 #else
24     FILE* f = std::fopen(filename.c_str(), "r");
25 #endif
26     bool result = false;
27     if(f)
28     {
29         std::fclose(f);
30         result = true;
31     }
32     return result;
33 }
34 
create_test_file(const std::string & filename)35 void create_test_file(const std::string& filename)
36 {
37 #ifdef BOOST_WINDOWS
38     FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"w");
39 #else
40     FILE* f = std::fopen(filename.c_str(), "w");
41 #endif
42     TEST(f);
43     TEST(std::fputs("test\n", f) >= 0);
44     std::fclose(f);
45 }
46 
47 #if BOOST_MSVC
48 #include <crtdbg.h> // For _CrtSetReportMode
noop_invalid_param_handler(const wchar_t *,const wchar_t *,const wchar_t *,unsigned,uintptr_t)49 void noop_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, uintptr_t)
50 {}
51 #endif
52 
test_main(int,char ** argv,char **)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 }
145