File: file_status.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (116 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download | duplicates (7)
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
//  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";
    }
}

} // namespace

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;
}