Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/filesystem/example/simple_ls.cpp @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

File size: 1.9 KB
Line 
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 <iostream>
14
15namespace fs = boost::filesystem;
16
17int main( int argc, char* argv[] )
18{
19
20  fs::path full_path( fs::initial_path() );
21
22  if ( argc > 1 )
23    full_path = fs::system_complete( fs::path( argv[1], fs::native ) );
24  else
25    std::cout << "\nusage:   simple_ls [path]" << std::endl;
26
27  unsigned long file_count = 0;
28  unsigned long dir_count = 0;
29  unsigned long err_count = 0;
30
31  if ( !fs::exists( full_path ) )
32  {
33    std::cout << "\nNot found: " << full_path.native_file_string() << std::endl;
34    return 1;
35  }
36
37  if ( fs::is_directory( full_path ) )
38  {
39    std::cout << "\nIn directory: "
40              << full_path.native_directory_string() << "\n\n";
41    fs::directory_iterator end_iter;
42    for ( fs::directory_iterator dir_itr( full_path );
43          dir_itr != end_iter;
44          ++dir_itr )
45    {
46      try
47      {
48        if ( fs::is_directory( *dir_itr ) )
49        {
50          ++dir_count;
51          std::cout << dir_itr->leaf()<< " [directory]\n";
52        }
53        else
54        {
55          ++file_count;
56          std::cout << dir_itr->leaf() << "\n";
57        }
58      }
59      catch ( const std::exception & ex )
60      {
61        ++err_count;
62        std::cout << dir_itr->leaf() << " " << ex.what() << std::endl;
63      }
64    }
65    std::cout << "\n" << file_count << " files\n"
66              << dir_count << " directories\n"
67              << err_count << " errors\n";
68  }
69  else // must be a file
70  {
71    std::cout << "\nFound: " << full_path.native_file_string() << "\n";   
72  }
73  return 0;
74}
Note: See TracBrowser for help on using the repository browser.