GDBserver
xml-utils.c
Go to the documentation of this file.
1 /* Shared helper routines for manipulating XML.
2 
3  Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "common-defs.h"
21 #include "xml-utils.h"
22 
23 /* Return a malloc allocated string with special characters from TEXT
24  replaced by entity references. */
25 
26 char *
27 xml_escape_text (const char *text)
28 {
29  char *result;
30  int i, special;
31 
32  /* Compute the length of the result. */
33  for (i = 0, special = 0; text[i] != '\0'; i++)
34  switch (text[i])
35  {
36  case '\'':
37  case '\"':
38  special += 5;
39  break;
40  case '&':
41  special += 4;
42  break;
43  case '<':
44  case '>':
45  special += 3;
46  break;
47  default:
48  break;
49  }
50 
51  /* Expand the result. */
52  result = xmalloc (i + special + 1);
53  for (i = 0, special = 0; text[i] != '\0'; i++)
54  switch (text[i])
55  {
56  case '\'':
57  strcpy (result + i + special, "&apos;");
58  special += 5;
59  break;
60  case '\"':
61  strcpy (result + i + special, "&quot;");
62  special += 5;
63  break;
64  case '&':
65  strcpy (result + i + special, "&amp;");
66  special += 4;
67  break;
68  case '<':
69  strcpy (result + i + special, "&lt;");
70  special += 3;
71  break;
72  case '>':
73  strcpy (result + i + special, "&gt;");
74  special += 3;
75  break;
76  default:
77  result[i + special] = text[i];
78  break;
79  }
80  result[i + special] = '\0';
81 
82  return result;
83 }
char * xml_escape_text(const char *text)
Definition: xml-utils.c:27
PTR xmalloc(size_t size)
Definition: common-utils.c:34