1 | // Boost wide_test.cpp -----------------------------------------------------// |
---|
2 | |
---|
3 | // Copyright Beman Dawes 2005 |
---|
4 | |
---|
5 | // Use, modification, and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | |
---|
9 | // See library home page at http://www.boost.org/libs/filesystem |
---|
10 | |
---|
11 | // VC++ 8.0 warns on various less-than-safe practices. |
---|
12 | // See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx |
---|
13 | // But at least in VC++ 8.0 betas, their own libraries use the problem |
---|
14 | // practices. So turn off the warnings. |
---|
15 | #define _CRT_SECURE_NO_DEPRECATE |
---|
16 | #define _SCL_SECURE_NO_DEPRECATE |
---|
17 | |
---|
18 | #include <boost/filesystem/config.hpp> |
---|
19 | # ifdef BOOST_FILESYSTEM_NARROW_ONLY |
---|
20 | # error This compiler or standard library does not support wide-character strings or paths |
---|
21 | # endif |
---|
22 | |
---|
23 | #include <boost/filesystem/operations.hpp> |
---|
24 | #include <boost/filesystem/fstream.hpp> |
---|
25 | #include <boost/scoped_array.hpp> |
---|
26 | #include <boost/test/minimal.hpp> |
---|
27 | |
---|
28 | #include "../src/utf8_codecvt_facet.hpp" |
---|
29 | |
---|
30 | namespace fs = boost::filesystem; |
---|
31 | |
---|
32 | #include <iostream> |
---|
33 | #include <iomanip> |
---|
34 | #include <string> |
---|
35 | #include <cerrno> |
---|
36 | |
---|
37 | #include "lpath.hpp" |
---|
38 | |
---|
39 | namespace |
---|
40 | { |
---|
41 | template< class Path > |
---|
42 | void create_file( const Path & ph, const std::string & contents ) |
---|
43 | { |
---|
44 | // TODO: why missing symbol error on Darwin |
---|
45 | # ifndef __APPLE__ |
---|
46 | fs::ofstream f( ph ); |
---|
47 | # else |
---|
48 | std::ofstream f( ph.external_file_string().c_str() ); |
---|
49 | # endif |
---|
50 | if ( !f ) |
---|
51 | throw fs::basic_filesystem_error<Path>( "wide_test create_file", |
---|
52 | ph, errno ); |
---|
53 | if ( !contents.empty() ) f << contents; |
---|
54 | } |
---|
55 | |
---|
56 | template< class Path > |
---|
57 | void test( const Path & dir, const Path & file, const Path & dot ) |
---|
58 | { |
---|
59 | Path tmp; |
---|
60 | tmp = file; |
---|
61 | BOOST_CHECK( tmp == file ); |
---|
62 | tmp = file.string(); |
---|
63 | BOOST_CHECK( tmp == file ); |
---|
64 | tmp = file.string().c_str(); |
---|
65 | BOOST_CHECK( tmp == file ); |
---|
66 | fs::initial_path<Path>(); |
---|
67 | fs::current_path<Path>(); |
---|
68 | fs::remove( dir / file ); |
---|
69 | fs::remove( dir ); |
---|
70 | BOOST_CHECK( !fs::exists( dir / file ) ); |
---|
71 | BOOST_CHECK( !fs::exists( dir ) ); |
---|
72 | BOOST_CHECK( fs::create_directory( dir ) ); |
---|
73 | BOOST_CHECK( fs::exists( dir ) ); |
---|
74 | BOOST_CHECK( fs::is_directory( dir ) ); |
---|
75 | BOOST_CHECK( fs::is_empty( dir ) ); |
---|
76 | create_file( dir / file, "wide_test file contents" ); |
---|
77 | BOOST_CHECK( fs::exists( dir / file ) ); |
---|
78 | BOOST_CHECK( !fs::is_directory( dir / file ) ); |
---|
79 | BOOST_CHECK( !fs::is_empty( dir / file ) ); |
---|
80 | BOOST_CHECK( fs::file_size( dir / file ) == 23 || fs::file_size( dir / file ) == 24 ); |
---|
81 | BOOST_CHECK( fs::equivalent( dir / file, dot / dir / file ) ); |
---|
82 | BOOST_CHECK( fs::last_write_time( dir / file ) ); |
---|
83 | typedef fs::basic_directory_iterator<Path> it_t; |
---|
84 | int count(0); |
---|
85 | for ( it_t it( dir ); it != it_t(); ++it ) |
---|
86 | { |
---|
87 | BOOST_CHECK( it->path() == dir / file ); |
---|
88 | BOOST_CHECK( !fs::is_empty( it->path() ) ); |
---|
89 | ++count; |
---|
90 | } |
---|
91 | BOOST_CHECK( count == 1 ); |
---|
92 | } |
---|
93 | |
---|
94 | // test boost::detail::utf8_codecvt_facet - even though it is not used by |
---|
95 | // Boost.Filesystem on Windows, early detection of problems is worthwhile. |
---|
96 | std::string to_external( const std::wstring & src ) |
---|
97 | { |
---|
98 | fs::detail::utf8_codecvt_facet convertor; |
---|
99 | std::size_t work_size( convertor.max_length() * (src.size()+1) ); |
---|
100 | boost::scoped_array<char> work( new char[ work_size ] ); |
---|
101 | std::mbstate_t state; |
---|
102 | const wchar_t * from_next; |
---|
103 | char * to_next; |
---|
104 | if ( convertor.out( |
---|
105 | state, src.c_str(), src.c_str()+src.size(), from_next, work.get(), |
---|
106 | work.get()+work_size, to_next ) != std::codecvt_base::ok ) |
---|
107 | boost::throw_exception( "to_external conversion error" ); |
---|
108 | *to_next = '\0'; |
---|
109 | return std::string( work.get() ); |
---|
110 | } |
---|
111 | |
---|
112 | } // unnamed namespace |
---|
113 | |
---|
114 | // test_main ---------------------------------------------------------------// |
---|
115 | |
---|
116 | int test_main( int argc, char * argv[] ) |
---|
117 | { |
---|
118 | |
---|
119 | // So that tests are run with known encoding, use Boost UTF-8 codecvt |
---|
120 | std::locale global_loc = std::locale(); |
---|
121 | std::locale loc( global_loc, new fs::detail::utf8_codecvt_facet ); |
---|
122 | fs::wpath_traits::imbue( loc ); |
---|
123 | |
---|
124 | std::string s( to_external( L"\x2780" ) ); |
---|
125 | for (std::size_t i = 0; i < s.size(); ++i ) |
---|
126 | std::cout << std::hex << int( static_cast<unsigned char>(s[i]) ) << " "; |
---|
127 | std::cout << std::dec << std::endl; |
---|
128 | BOOST_CHECK( to_external( L"\x2780" ).size() == 3 ); |
---|
129 | BOOST_CHECK( to_external( L"\x2780" ) == "\xE2\x9E\x80" ); |
---|
130 | |
---|
131 | // test fs::path |
---|
132 | std::cout << "begin path test..." << std::endl; |
---|
133 | test( fs::path( "foo" ), fs::path( "bar" ), fs::path( "." ) ); |
---|
134 | std::cout << "complete\n\n"; |
---|
135 | |
---|
136 | // test fs::wpath |
---|
137 | // x2780 is circled 1 against white background == e2 9e 80 in UTF-8 |
---|
138 | // x2781 is circled 2 against white background == e2 9e 81 in UTF-8 |
---|
139 | std::cout << "begin wpath test..." << std::endl; |
---|
140 | test( fs::wpath( L"\x2780" ), fs::wpath( L"\x2781" ), fs::wpath( L"." ) ); |
---|
141 | std::cout << "complete\n\n"; |
---|
142 | |
---|
143 | // test user supplied basic_path |
---|
144 | const long dir[] = { 'b', 'o', 'o', 0 }; |
---|
145 | const long file[] = { 'f', 'a', 'r', 0 }; |
---|
146 | const long dot[] = { '.', 0 }; |
---|
147 | std::cout << "begin lpath test..." << std::endl; |
---|
148 | test( ::user::lpath( dir ), ::user::lpath( file ), ::user::lpath( dot ) ); |
---|
149 | std::cout << "complete\n\n"; |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|