1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_FILE_PARSER_ERROR_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_FILE_PARSER_ERROR_HPP_INCLUDED
12 
13 #include <boost/property_tree/ptree.hpp>
14 #include <string>
15 
16 namespace boost { namespace property_tree
17 {
18 
19     //! File parse error
20     class file_parser_error: public ptree_error
21     {
22 
23     public:
24 
25         ///////////////////////////////////////////////////////////////////////
26         // Construction & destruction
27 
28         // Construct error
file_parser_error(const std::string & msg,const std::string & file,unsigned long l)29         file_parser_error(const std::string &msg,
30                           const std::string &file,
31                           unsigned long l) :
32             ptree_error(format_what(msg, file, l)),
33             m_message(msg), m_filename(file), m_line(l)
34         {
35         }
36 
~file_parser_error()37         ~file_parser_error() throw()
38             // gcc 3.4.2 complains about lack of throw specifier on compiler
39             // generated dtor
40         {
41         }
42 
43         ///////////////////////////////////////////////////////////////////////
44         // Data access
45 
46         // Get error message (without line and file - use what() to get
47         // full message)
message() const48         std::string message() const
49         {
50             return m_message;
51         }
52 
53         // Get error filename
filename() const54         std::string filename() const
55         {
56             return m_filename;
57         }
58 
59         // Get error line number
line() const60         unsigned long line() const
61         {
62             return m_line;
63         }
64 
65     private:
66 
67         std::string m_message;
68         std::string m_filename;
69         unsigned long m_line;
70 
71         // Format error message to be returned by std::runtime_error::what()
format_what(const std::string & msg,const std::string & file,unsigned long l)72         static std::string format_what(const std::string &msg,
73                                        const std::string &file,
74                                        unsigned long l)
75         {
76             std::stringstream stream;
77             stream << (file.empty() ? "<unspecified file>" : file.c_str());
78             if (l > 0)
79                 stream << '(' << l << ')';
80             stream << ": " << msg;
81             return stream.str();
82         }
83 
84     };
85 
86 } }
87 
88 #endif
89