xml-utils.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Shared helper routines for manipulating XML.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "xml-utils.h"
  16. /* See xml-utils.h. */
  17. std::string
  18. xml_escape_text (const char *text)
  19. {
  20. std::string result;
  21. xml_escape_text_append (&result, text);
  22. return result;
  23. }
  24. /* See xml-utils.h. */
  25. void
  26. xml_escape_text_append (std::string *result, const char *text)
  27. {
  28. /* Expand the result. */
  29. for (int i = 0; text[i] != '\0'; i++)
  30. switch (text[i])
  31. {
  32. case '\'':
  33. *result += "&apos;";
  34. break;
  35. case '\"':
  36. *result += "&quot;";
  37. break;
  38. case '&':
  39. *result += "&amp;";
  40. break;
  41. case '<':
  42. *result += "&lt;";
  43. break;
  44. case '>':
  45. *result += "&gt;";
  46. break;
  47. default:
  48. *result += text[i];
  49. break;
  50. }
  51. }