| 1 | // simple_ls program -------------------------------------------------------// |
|---|
| 2 | |
|---|
| 3 | // Copyright Jeff Garland and Beman Dawes, 2002 |
|---|
| 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 http://www.boost.org/libs/filesystem for documentation. |
|---|
| 10 | |
|---|
| 11 | #include "boost/filesystem/operations.hpp" |
|---|
| 12 | #include "boost/filesystem/path.hpp" |
|---|
| 13 | #include "boost/progress.hpp" |
|---|
| 14 | #include <iostream> |
|---|
| 15 | |
|---|
| 16 | namespace fs = boost::filesystem; |
|---|
| 17 | |
|---|
| 18 | int main( int argc, char* argv[] ) |
|---|
| 19 | { |
|---|
| 20 | boost::progress_timer t( std::clog ); |
|---|
| 21 | |
|---|
| 22 | fs::path full_path( fs::initial_path<fs::path>() ); |
|---|
| 23 | |
|---|
| 24 | if ( argc > 1 ) |
|---|
| 25 | full_path = fs::system_complete( fs::path( argv[1], fs::native ) ); |
|---|
| 26 | else |
|---|
| 27 | std::cout << "\nusage: simple_ls [path]" << std::endl; |
|---|
| 28 | |
|---|
| 29 | unsigned long file_count = 0; |
|---|
| 30 | unsigned long dir_count = 0; |
|---|
| 31 | unsigned long other_count = 0; |
|---|
| 32 | unsigned long err_count = 0; |
|---|
| 33 | |
|---|
| 34 | if ( !fs::exists( full_path ) ) |
|---|
| 35 | { |
|---|
| 36 | std::cout << "\nNot found: " << full_path.native_file_string() << std::endl; |
|---|
| 37 | return 1; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | if ( fs::is_directory( full_path ) ) |
|---|
| 41 | { |
|---|
| 42 | std::cout << "\nIn directory: " |
|---|
| 43 | << full_path.native_directory_string() << "\n\n"; |
|---|
| 44 | fs::directory_iterator end_iter; |
|---|
| 45 | for ( fs::directory_iterator dir_itr( full_path ); |
|---|
| 46 | dir_itr != end_iter; |
|---|
| 47 | ++dir_itr ) |
|---|
| 48 | { |
|---|
| 49 | try |
|---|
| 50 | { |
|---|
| 51 | if ( fs::is_directory( dir_itr->status() ) ) |
|---|
| 52 | { |
|---|
| 53 | ++dir_count; |
|---|
| 54 | std::cout << dir_itr->path().leaf() << " [directory]\n"; |
|---|
| 55 | } |
|---|
| 56 | else if ( fs::is_regular( dir_itr->status() ) ) |
|---|
| 57 | { |
|---|
| 58 | ++file_count; |
|---|
| 59 | std::cout << dir_itr->path().leaf() << "\n"; |
|---|
| 60 | } |
|---|
| 61 | else |
|---|
| 62 | { |
|---|
| 63 | ++other_count; |
|---|
| 64 | std::cout << dir_itr->path().leaf() << " [other]\n"; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | catch ( const std::exception & ex ) |
|---|
| 69 | { |
|---|
| 70 | ++err_count; |
|---|
| 71 | std::cout << dir_itr->path().leaf() << " " << ex.what() << std::endl; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | std::cout << "\n" << file_count << " files\n" |
|---|
| 75 | << dir_count << " directories\n" |
|---|
| 76 | << other_count << " others\n" |
|---|
| 77 | << err_count << " errors\n"; |
|---|
| 78 | } |
|---|
| 79 | else // must be a file |
|---|
| 80 | { |
|---|
| 81 | std::cout << "\nFound: " << full_path.native_file_string() << "\n"; |
|---|
| 82 | } |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|