MCPcopy Create free account
hub / github.com/boostorg/leaf / main

Function main

example/print_file/print_file_leaf_result.cpp:54–147  ·  view source on GitHub ↗

The main function, which handles all errors.

Source from the content-addressed store, hash-verified

52
53// The main function, which handles all errors.
54int main( int argc, char const * argv[] )
55{
56 return leaf::try_handle_all(
57
58 [&]() -> result<int>
59 {
60 BOOST_LEAF_AUTO(file_name, parse_command_line(argc,argv));
61
62 auto load = leaf::on_error( leaf::e_file_name{file_name} );
63
64 BOOST_LEAF_AUTO(f, file_open(file_name));
65
66 BOOST_LEAF_AUTO(s, file_size(*f));
67
68 std::string buffer(1 + s, '\0');
69 BOOST_LEAF_CHECK(file_read(*f, &buffer[0], buffer.size()-1));
70
71 std::cout << buffer;
72 std::cout.flush();
73 if( std::cout.fail() )
74 return leaf::new_error(output_error, leaf::e_errno{errno});
75
76 return 0;
77 },
78
79 // Each of the lambdas below is an error handler. LEAF will consider
80 // them, in order, and call the first one that matches the available
81 // error objects.
82
83 // This handler will be called if the error includes:
84 // - an object of type error_code equal to open_error, and
85 // - an object of type leaf::e_errno that has .value equal to ENOENT,
86 // and
87 // - an object of type leaf::e_file_name.
88 []( leaf::match<error_code, open_error>, leaf::match_value<leaf::e_errno, ENOENT>, leaf::e_file_name const & fn )
89 {
90 std::cerr << "File not found: " << fn.value << std::endl;
91 return 1;
92 },
93
94 // This handler will be called if the error includes:
95 // - an object of type error_code equal to open_error, and
96 // - an object of type leaf::e_errno (regardless of its .value), and
97 // - an object of type leaf::e_file_name.
98 []( leaf::match<error_code, open_error>, leaf::e_errno const & errn, leaf::e_file_name const & fn )
99 {
100 std::cerr << "Failed to open " << fn.value << ", errno=" << errn << std::endl;
101 return 2;
102 },
103
104 // This handler will be called if the error includes:
105 // - an object of type error_code equal to any of size_error,
106 // read_error, eof_error, and
107 // - an optional object of type leaf::e_errno (regardless of its
108 // .value), and
109 // - an object of type leaf::e_file_name.
110 []( leaf::match<error_code, size_error, read_error, eof_error>, leaf::e_errno const * errn, leaf::e_file_name const & fn )
111 {

Callers

nothing calls this directly

Calls 7

try_handle_allFunction · 0.85
on_errorFunction · 0.85
new_errorFunction · 0.85
parse_command_lineFunction · 0.70
file_openFunction · 0.70
file_sizeFunction · 0.70
file_readFunction · 0.70

Tested by

no test coverage detected