file_parser_error.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <boost/property_tree/ptree.hpp>
  13. #include <string>
  14. namespace boost { namespace property_tree
  15. {
  16. //! File parse error
  17. class file_parser_error: public ptree_error
  18. {
  19. public:
  20. ///////////////////////////////////////////////////////////////////////
  21. // Construction & destruction
  22. // Construct error
  23. file_parser_error(const std::string &msg,
  24. const std::string &file,
  25. unsigned long l) :
  26. ptree_error(format_what(msg, file, l)),
  27. m_message(msg), m_filename(file), m_line(l)
  28. {
  29. }
  30. ~file_parser_error() throw()
  31. // gcc 3.4.2 complains about lack of throw specifier on compiler
  32. // generated dtor
  33. {
  34. }
  35. ///////////////////////////////////////////////////////////////////////
  36. // Data access
  37. // Get error message (without line and file - use what() to get
  38. // full message)
  39. std::string message() const
  40. {
  41. return m_message;
  42. }
  43. // Get error filename
  44. std::string filename() const
  45. {
  46. return m_filename;
  47. }
  48. // Get error line number
  49. unsigned long line() const
  50. {
  51. return m_line;
  52. }
  53. private:
  54. std::string m_message;
  55. std::string m_filename;
  56. unsigned long m_line;
  57. // Format error message to be returned by std::runtime_error::what()
  58. static std::string format_what(const std::string &msg,
  59. const std::string &file,
  60. unsigned long l)
  61. {
  62. std::stringstream stream;
  63. stream << (file.empty() ? "<unspecified file>" : file.c_str());
  64. if (l > 0)
  65. stream << '(' << l << ')';
  66. stream << ": " << msg;
  67. return stream.str();
  68. }
  69. };
  70. } }
  71. #endif