file_parser_error.hpp 2.3 KB

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