Commit | Line | Data |
---|---|---|
d26e3629 KY |
1 | /* Shared helper routines for manipulating XML. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2006-2014 Free Software Foundation, Inc. |
d26e3629 KY |
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 | #ifdef GDBSERVER | |
21 | #include "server.h" | |
22 | #else | |
23 | #include "defs.h" | |
24 | #endif | |
25 | ||
26 | #include "xml-utils.h" | |
27 | ||
d26e3629 KY |
28 | /* Return a malloc allocated string with special characters from TEXT |
29 | replaced by entity references. */ | |
30 | ||
31 | char * | |
32 | xml_escape_text (const char *text) | |
33 | { | |
34 | char *result; | |
35 | int i, special; | |
36 | ||
37 | /* Compute the length of the result. */ | |
38 | for (i = 0, special = 0; text[i] != '\0'; i++) | |
39 | switch (text[i]) | |
40 | { | |
41 | case '\'': | |
42 | case '\"': | |
43 | special += 5; | |
44 | break; | |
45 | case '&': | |
46 | special += 4; | |
47 | break; | |
48 | case '<': | |
49 | case '>': | |
50 | special += 3; | |
51 | break; | |
52 | default: | |
53 | break; | |
54 | } | |
55 | ||
56 | /* Expand the result. */ | |
57 | result = xmalloc (i + special + 1); | |
58 | for (i = 0, special = 0; text[i] != '\0'; i++) | |
59 | switch (text[i]) | |
60 | { | |
61 | case '\'': | |
62 | strcpy (result + i + special, "'"); | |
63 | special += 5; | |
64 | break; | |
65 | case '\"': | |
66 | strcpy (result + i + special, """); | |
67 | special += 5; | |
68 | break; | |
69 | case '&': | |
70 | strcpy (result + i + special, "&"); | |
71 | special += 4; | |
72 | break; | |
73 | case '<': | |
74 | strcpy (result + i + special, "<"); | |
75 | special += 3; | |
76 | break; | |
77 | case '>': | |
78 | strcpy (result + i + special, ">"); | |
79 | special += 3; | |
80 | break; | |
81 | default: | |
82 | result[i + special] = text[i]; | |
83 | break; | |
84 | } | |
85 | result[i + special] = '\0'; | |
86 | ||
87 | return result; | |
88 | } |