1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 //This example shows how to add arbitrary data to active exception objects.
7
8 #include <boost/exception/all.hpp>
9 #include <boost/shared_ptr.hpp>
10 #include <stdio.h>
11 #include <errno.h>
12
13 //
14
15 struct file_read_error: virtual boost::exception { };
16
17 void
file_read(FILE * f,void * buffer,size_t size)18 file_read( FILE * f, void * buffer, size_t size )
19 {
20 if( size!=fread(buffer,1,size,f) )
21 throw file_read_error() << boost::errinfo_errno(errno);
22 }
23
24 //
25
26 boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
27 void file_read( FILE * f, void * buffer, size_t size );
28
29 void
parse_file(char const * file_name)30 parse_file( char const * file_name )
31 {
32 boost::shared_ptr<FILE> f = file_open(file_name,"rb");
33 assert(f);
34 try
35 {
36 char buf[1024];
37 file_read( f.get(), buf, sizeof(buf) );
38 }
39 catch(
40 boost::exception & e )
41 {
42 e << boost::errinfo_file_name(file_name);
43 throw;
44 }
45 }
46