xml.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // -*- C++ -*-
  2. // Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
  17. // Permission to use, copy, modify, sell, and distribute this software
  18. // is hereby granted without fee, provided that the above copyright
  19. // notice appears in all copies, and that both that copyright notice
  20. // and this permission notice appear in supporting documentation. None
  21. // of the above authors, nor IBM Haifa Research Laboratories, make any
  22. // representation about the suitability of this software for any
  23. // purpose. It is provided "as is" without express or implied
  24. // warranty.
  25. /**
  26. * @file xml.hpp
  27. * Contains some xml utilities.
  28. */
  29. #ifndef PB_DS_XML_HPP
  30. #define PB_DS_XML_HPP
  31. #include <string>
  32. #include <sstream>
  33. namespace __gnu_pbds
  34. {
  35. namespace test
  36. {
  37. namespace detail
  38. {
  39. std::string
  40. make_xml_name_start_tag(std::string name)
  41. { return ("<" + name); }
  42. template<typename V>
  43. std::string
  44. make_xml_attrib_val(std::string attrib, const V val)
  45. {
  46. std::ostringstream sstrm;
  47. sstrm << " " << attrib << " = \"" << val << "\"";
  48. return (sstrm.str());
  49. }
  50. std::string
  51. make_xml_name_start_tag_end_delimiter()
  52. { return (">\n"); }
  53. std::string
  54. make_xml_name_end_tag(std::string name)
  55. { return ("</" + name + ">\n"); }
  56. } // namespace detail
  57. std::string
  58. make_xml_tag(const std::string name,
  59. const std::string data = std::string(""))
  60. {
  61. std::ostringstream sstrm;
  62. sstrm << detail::make_xml_name_start_tag(name);
  63. sstrm << detail::make_xml_name_start_tag_end_delimiter();
  64. sstrm << data;
  65. sstrm << detail::make_xml_name_end_tag(name);
  66. return sstrm.str();
  67. }
  68. template<typename Val0>
  69. std::string
  70. make_xml_tag(const std::string name,
  71. const std::string attrib0,
  72. const Val0 val0,
  73. const std::string data = std::string(""))
  74. {
  75. std::ostringstream sstrm;
  76. sstrm << detail::make_xml_name_start_tag(name);
  77. sstrm << detail::make_xml_attrib_val(attrib0, val0);
  78. sstrm << detail::make_xml_name_start_tag_end_delimiter();
  79. sstrm << data;
  80. sstrm << detail::make_xml_name_end_tag(name);
  81. return sstrm.str();
  82. }
  83. template<typename Val0, typename Val1>
  84. std::string
  85. make_xml_tag(const std::string name,
  86. const std::string attrib0,
  87. const Val0 val0,
  88. const std::string attrib1,
  89. const Val1 val1,
  90. const std::string data = std::string(""))
  91. {
  92. std::ostringstream sstrm;
  93. sstrm << detail::make_xml_name_start_tag(name);
  94. sstrm << detail::make_xml_attrib_val(attrib0, val0);
  95. sstrm << detail::make_xml_attrib_val(attrib1, val1);
  96. sstrm << detail::make_xml_name_start_tag_end_delimiter();
  97. sstrm << data;
  98. sstrm << detail::make_xml_name_end_tag(name);
  99. return sstrm.str();
  100. }
  101. } // namespace test
  102. } // namespace __gnu_pbds
  103. #endif // #ifndef PB_DS_XML_HPP