mbcopy.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Boost.Filesystem mbcopy.cpp ---------------------------------------------//
  2. // Copyright Beman Dawes 2005
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Copy the files in a directory, using mbpath to represent the new file names
  7. // See http://../doc/path.htm#mbpath for more information
  8. // See deprecated_test for tests of deprecated features
  9. #define BOOST_FILESYSTEM_NO_DEPRECATED
  10. #include <boost/filesystem/config.hpp>
  11. # ifdef BOOST_FILESYSTEM_NARROW_ONLY
  12. # error This compiler or standard library does not support wide-character strings or paths
  13. # endif
  14. #include "mbpath.hpp"
  15. #include <iostream>
  16. #include <boost/filesystem/operations.hpp>
  17. #include <boost/filesystem/fstream.hpp>
  18. #include "../src/utf8_codecvt_facet.hpp"
  19. namespace fs = boost::filesystem;
  20. namespace
  21. {
  22. // we can't use boost::filesystem::copy_file() because the argument types
  23. // differ, so provide a not-very-smart replacement.
  24. void copy_file( const fs::wpath & from, const user::mbpath & to )
  25. {
  26. fs::ifstream from_file( from, std::ios_base::in | std::ios_base::binary );
  27. if ( !from_file ) { std::cout << "input open failed\n"; return; }
  28. fs::ofstream to_file( to, std::ios_base::out | std::ios_base::binary );
  29. if ( !to_file ) { std::cout << "output open failed\n"; return; }
  30. char c;
  31. while ( from_file.get(c) )
  32. {
  33. to_file.put(c);
  34. if ( to_file.fail() ) { std::cout << "write error\n"; return; }
  35. }
  36. if ( !from_file.eof() ) { std::cout << "read error\n"; }
  37. }
  38. }
  39. int main( int argc, char * argv[] )
  40. {
  41. if ( argc != 2 )
  42. {
  43. std::cout << "Copy files in the current directory to a target directory\n"
  44. << "Usage: mbcopy <target-dir>\n";
  45. return 1;
  46. }
  47. // For encoding, use Boost UTF-8 codecvt
  48. std::locale global_loc = std::locale();
  49. std::locale loc( global_loc, new fs::detail::utf8_codecvt_facet );
  50. user::mbpath_traits::imbue( loc );
  51. std::string target_string( argv[1] );
  52. user::mbpath target_dir( user::mbpath_traits::to_internal( target_string ) );
  53. if ( !fs::is_directory( target_dir ) )
  54. {
  55. std::cout << "Error: " << argv[1] << " is not a directory\n";
  56. return 1;
  57. }
  58. for ( fs::wdirectory_iterator it( L"." );
  59. it != fs::wdirectory_iterator(); ++it )
  60. {
  61. if ( fs::is_regular_file(it->status()) )
  62. {
  63. copy_file( *it, target_dir / it->path().filename() );
  64. }
  65. }
  66. return 0;
  67. }