Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / filesystem / example / simple_ls.cpp
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 #define BOOST_FILESYSTEM_VERSION 3
12
13 //  As an example program, we don't want to use any deprecated features
14 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED 
15 #  define BOOST_FILESYSTEM_NO_DEPRECATED
16 #endif
17 #ifndef BOOST_SYSTEM_NO_DEPRECATED 
18 #  define BOOST_SYSTEM_NO_DEPRECATED
19 #endif
20
21 #include <boost/filesystem/operations.hpp>
22 #include <boost/filesystem/directory.hpp>
23 #include <boost/filesystem/path.hpp>
24 #include <iostream>
25
26 namespace fs = boost::filesystem;
27
28 int main(int argc, char* argv[])
29 {
30   fs::path p(fs::current_path());
31
32   if (argc > 1)
33     p = fs::system_complete(argv[1]);
34   else
35     std::cout << "\nusage:   simple_ls [path]" << std::endl;
36
37   unsigned long file_count = 0;
38   unsigned long dir_count = 0;
39   unsigned long other_count = 0;
40   unsigned long err_count = 0;
41
42   if (!fs::exists(p))
43   {
44     std::cout << "\nNot found: " << p << std::endl;
45     return 1;
46   }
47
48   if (fs::is_directory(p))
49   {
50     std::cout << "\nIn directory: " << p << "\n\n";
51     fs::directory_iterator end_iter;
52     for (fs::directory_iterator dir_itr(p);
53           dir_itr != end_iter;
54           ++dir_itr)
55     {
56       try
57       {
58         if (fs::is_directory(dir_itr->status()))
59         {
60           ++dir_count;
61           std::cout << dir_itr->path().filename() << " [directory]\n";
62         }
63         else if (fs::is_regular_file(dir_itr->status()))
64         {
65           ++file_count;
66           std::cout << dir_itr->path().filename() << "\n";
67         }
68         else
69         {
70           ++other_count;
71           std::cout << dir_itr->path().filename() << " [other]\n";
72         }
73
74       }
75       catch (const std::exception & ex)
76       {
77         ++err_count;
78         std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
79       }
80     }
81     std::cout << "\n" << file_count << " files\n"
82               << dir_count << " directories\n"
83               << other_count << " others\n"
84               << err_count << " errors\n";
85   }
86   else // must be a file
87   {
88     std::cout << "\nFound: " << p << "\n";
89   }
90
91   return 0;
92 }