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