File: example_io.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 (209 lines) | stat: -rw-r--r-- 6,447 bytes parent folder | download | duplicates (11)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//This program simulates errors on copying simple data files. It demonstrates
//typical Boost Exception usage.

//The output from this program can vary depending on the platform.

#include <boost/throw_exception.hpp>
#include <boost/exception/info.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/errinfo_file_handle.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <iostream>

typedef boost::error_info<struct tag_file_name_src,std::string> errinfo_src_file_name;
typedef boost::error_info<struct tag_file_name_dst,std::string> errinfo_dst_file_name;

char const data[] = "example";
size_t const data_size = sizeof(data);

class
error: //Base for all exception objects we throw.
    public virtual std::exception,
    public virtual boost::exception
    {
    public:

    char const *
    what() const BOOST_NOEXCEPT_OR_NOTHROW
        {
        return "example_io error";
        }

    protected:

    ~error() BOOST_NOEXCEPT_OR_NOTHROW
        {
        }
    };

struct open_error: virtual error { };
struct read_error: virtual error { };
struct write_error: virtual error { };
struct fopen_error: virtual open_error { };
struct fread_error: virtual read_error { };
struct fwrite_error: virtual write_error { };

boost::shared_ptr<FILE>
my_fopen( char const * name, char const * mode )
    {
    if( FILE * f = ::fopen(name,mode) )
        return boost::shared_ptr<FILE>(f,fclose);
    else
        BOOST_THROW_EXCEPTION(fopen_error() <<
            boost::errinfo_errno    (errno) <<
            boost::errinfo_file_name(name) <<
            boost::errinfo_file_open_mode(mode) <<
            boost::errinfo_api_function("fopen"));
    }

void
my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
    {
    assert(stream);
    if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
        BOOST_THROW_EXCEPTION(fread_error() <<
            boost::errinfo_api_function("fread") <<
            boost::errinfo_errno(errno) <<
            boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
    }

void
my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
    {
    assert(stream);
    if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
        BOOST_THROW_EXCEPTION(fwrite_error() <<
            boost::errinfo_api_function("fwrite") <<
            boost::errinfo_errno(errno) <<
            boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
    }

void
reset_file( char const * file_name )
    {
    (void) my_fopen(file_name,"wb");
    }

void
create_data( char const * file_name )
    {
    boost::shared_ptr<FILE> f = my_fopen(file_name,"wb");
    my_fwrite( data, 1, data_size, f );
    }

void
copy_data( char const * src_file_name, char const * dst_file_name )
    {
    boost::shared_ptr<FILE> src = my_fopen(src_file_name,"rb");
    boost::shared_ptr<FILE> dst = my_fopen(dst_file_name,"wb");
    try
        {
        char buffer[data_size];
        my_fread( buffer, 1, data_size, src );
        my_fwrite( buffer, 1, data_size, dst );
        }
    catch(
    boost::exception & x )
        {
        if( boost::weak_ptr<FILE> const * f=boost::get_error_info<boost::errinfo_file_handle>(x) )
            if( boost::shared_ptr<FILE> fs = f->lock() )
                {
                if( fs==src )
                    x << boost::errinfo_file_name(src_file_name);
                else if( fs==dst )
                    x << boost::errinfo_file_name(dst_file_name);
                }
        x <<
            errinfo_src_file_name(src_file_name) <<
            errinfo_dst_file_name(dst_file_name);
        throw;
        }
    }

void
dump_copy_info( boost::exception const & x )
    {
    if( std::string const * src = boost::get_error_info<errinfo_src_file_name>(x) )
        std::cerr << "Source file name: " << *src << "\n";
    if( std::string const * dst = boost::get_error_info<errinfo_dst_file_name>(x) )
        std::cerr << "Destination file name: " << *dst << "\n";
    }

void
dump_file_info( boost::exception const & x )
    {
    if( std::string const * fn = boost::get_error_info<boost::errinfo_file_name>(x) )
        std::cerr << "File name: " << *fn << "\n";
    }

void
dump_clib_info( boost::exception const & x )
    {
    if( int const * err=boost::get_error_info<boost::errinfo_errno>(x) )
        std::cerr << "OS error: " << *err << "\n";
    if( char const * const * fn=boost::get_error_info<boost::errinfo_api_function>(x) )
        std::cerr << "Failed function: " << *fn << "\n";
    }

void
dump_all_info( boost::exception const & x )
    {
    std::cerr << "-------------------------------------------------\n";
    dump_copy_info(x);
    dump_file_info(x);
    dump_clib_info(x);
    std::cerr << "\nOutput from diagnostic_information():\n";
    std::cerr << diagnostic_information(x);
    }

int
main()
    {
    try
        {
        create_data( "tmp1.txt" );
        copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed.

        reset_file( "tmp1.txt" ); //Creates empty file.
        try
            {
            copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty.
            }
        catch(
        read_error & x )
            {
            std::cerr << "\nCaught 'read_error' exception.\n";
            dump_all_info(x);
            }

        remove( "tmp1.txt" );
        remove( "tmp2.txt" );
        try
            {
            copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist.
            }
        catch(
        open_error & x )
            {
            std::cerr << "\nCaught 'open_error' exception.\n";
            dump_all_info(x);
            }
        }
    catch(
    ... )
        {
        std::cerr << "\nCaught unexpected exception!\n";
        std::cerr << boost::current_exception_diagnostic_information();
        }
    }