[29] | 1 | // Boost.Filesystem mbcopy.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 | // Copy the files in a directory, using mbpath to represent the new file names |
---|
| 10 | // See http://../doc/path.htm#mbpath for more information |
---|
| 11 | |
---|
| 12 | #include <boost/filesystem/config.hpp> |
---|
| 13 | # ifdef BOOST_FILESYSTEM_NARROW_ONLY |
---|
| 14 | # error This compiler or standard library does not support wide-character strings or paths |
---|
| 15 | # endif |
---|
| 16 | |
---|
| 17 | #include "mbpath.hpp" |
---|
| 18 | #include <iostream> |
---|
| 19 | #include <boost/filesystem/operations.hpp> |
---|
| 20 | #include <boost/filesystem/fstream.hpp> |
---|
| 21 | #include "../src/utf8_codecvt_facet.hpp" |
---|
| 22 | |
---|
| 23 | namespace fs = boost::filesystem; |
---|
| 24 | |
---|
| 25 | namespace |
---|
| 26 | { |
---|
| 27 | // we can't use boost::filesystem::copy_file() because the argument types |
---|
| 28 | // differ, so provide a not-very-smart replacement. |
---|
| 29 | |
---|
| 30 | void copy_file( const fs::wpath & from, const user::mbpath & to ) |
---|
| 31 | { |
---|
| 32 | fs::ifstream from_file( from, std::ios_base::in | std::ios_base::binary ); |
---|
| 33 | if ( !from_file ) { std::cout << "input open failed\n"; return; } |
---|
| 34 | |
---|
| 35 | fs::ofstream to_file( to, std::ios_base::out | std::ios_base::binary ); |
---|
| 36 | if ( !to_file ) { std::cout << "output open failed\n"; return; } |
---|
| 37 | |
---|
| 38 | char c; |
---|
| 39 | while ( from_file.get(c) ) |
---|
| 40 | { |
---|
| 41 | to_file.put(c); |
---|
| 42 | if ( to_file.fail() ) { std::cout << "write error\n"; return; } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | if ( !from_file.eof() ) { std::cout << "read error\n"; } |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | int main( int argc, char * argv[] ) |
---|
| 50 | { |
---|
| 51 | if ( argc != 2 ) |
---|
| 52 | { |
---|
| 53 | std::cout << "Copy files in the current directory to a target directory\n" |
---|
| 54 | << "Usage: mbcopy <target-dir>\n"; |
---|
| 55 | return 1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // For encoding, use Boost UTF-8 codecvt |
---|
| 59 | std::locale global_loc = std::locale(); |
---|
| 60 | std::locale loc( global_loc, new fs::detail::utf8_codecvt_facet ); |
---|
| 61 | user::mbpath_traits::imbue( loc ); |
---|
| 62 | |
---|
| 63 | std::string target_string( argv[1] ); |
---|
| 64 | user::mbpath target_dir( user::mbpath_traits::to_internal( target_string ) ); |
---|
| 65 | |
---|
| 66 | if ( !fs::is_directory( target_dir ) ) |
---|
| 67 | { |
---|
| 68 | std::cout << "Error: " << argv[1] << " is not a directory\n"; |
---|
| 69 | return 1; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | for ( fs::wdirectory_iterator it( L"." ); |
---|
| 73 | it != fs::wdirectory_iterator(); ++it ) |
---|
| 74 | { |
---|
| 75 | if ( fs::is_regular(it->status()) ) |
---|
| 76 | { |
---|
| 77 | copy_file( *it, target_dir / it->leaf() ); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|