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