summaryrefslogtreecommitdiff
path: root/libs/filesystem/example/file_status.cpp
blob: 5d3774fdde4aef96efbb6e3745163c2170289dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//  status.cpp  ------------------------------------------------------------------------//

//  Copyright Beman Dawes 2011

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <iostream>
#include <boost/config.hpp>
#include <boost/version.hpp>
#include <boost/filesystem.hpp>
#include <boost/detail/lightweight_main.hpp>

using std::cout; using std::endl;
using namespace boost::filesystem;

namespace
{
  path p;
  
  void print_boost_macros()
  {
    std::cout << "Boost "
              << BOOST_VERSION / 100000 << '.'
              << BOOST_VERSION / 100 % 1000 << '.'
              << BOOST_VERSION % 100 << ", "
#           ifndef _WIN64
              << BOOST_COMPILER << ", "
#           else
              << BOOST_COMPILER << " with _WIN64 defined, "
#           endif
              << BOOST_STDLIB << ", "
              << BOOST_PLATFORM
              << std::endl;
  }

  const char* file_type_tab[] = 
    { "status_error", "file_not_found", "regular_file", "directory_file",
      "symlink_file", "block_file", "character_file", "fifo_file", "socket_file",
      "type_unknown"
    };

  const char* file_type_c_str(enum file_type t)
  {
    return file_type_tab[t];
  }

  void show_status(file_status s, boost::system::error_code ec)
  {
    boost::system::error_condition econd;

    if (ec)
    {
      econd = ec.default_error_condition();
      cout << "sets ec to indicate an error:\n"
           << "   ec.value() is " << ec.value() << '\n'
           << "   ec.message() is \"" << ec.message() << "\"\n"
           << "   ec.default_error_condition().value() is " << econd.value() << '\n'
           << "   ec.default_error_condition().message() is \"" << econd.message() << "\"\n";
    }
    else
      cout << "clears ec.\n";

    cout << "s.type() is " << s.type()
         << ", which is defined as \"" << file_type_c_str(s.type()) << "\"\n";

    cout << "exists(s) is " << (exists(s) ? "true" : "false") << "\n";
    cout << "status_known(s) is " << (status_known(s) ? "true" : "false") << "\n";
    cout << "is_regular_file(s) is " << (is_regular_file(s) ? "true" : "false") << "\n";
    cout << "is_directory(s) is " << (is_directory(s) ? "true" : "false") << "\n";
    cout << "is_other(s) is " << (is_other(s) ? "true" : "false") << "\n";
    cout << "is_symlink(s) is " << (is_symlink(s) ? "true" : "false") << "\n";
  }

  void try_exists()
  {
    cout << "\nexists(" << p << ") ";
    try
    {
      bool result = exists(p);
      cout << "is " << (result ? "true" : "false") << "\n";
    }
    catch (const filesystem_error& ex)
    {
      cout << "throws a filesystem_error exception: " << ex.what() << "\n";
    }
  }

}

int cpp_main(int argc, char* argv[])
{
  print_boost_macros();

  if (argc < 2)
  {
    std::cout << "Usage: file_status <path>\n";
    p = argv[0];
  }
  else
    p = argv[1];

  boost::system::error_code ec;
  file_status s = status(p, ec);
  cout << "\nfile_status s = status(" << p << ", ec) ";
  show_status(s, ec);

  s = symlink_status(p, ec);
  cout << "\nfile_status s = symlink_status(" << p << ", ec) ";
  show_status(s, ec);

  try_exists();

  return 0;
}