GDB (API)
|
00001 /* Shared helper routines for manipulating XML. 00002 00003 Copyright (C) 2006-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #ifdef GDBSERVER 00021 #include "server.h" 00022 #else 00023 #include "defs.h" 00024 #endif 00025 00026 #include "xml-utils.h" 00027 00028 #include <string.h> 00029 00030 /* Return a malloc allocated string with special characters from TEXT 00031 replaced by entity references. */ 00032 00033 char * 00034 xml_escape_text (const char *text) 00035 { 00036 char *result; 00037 int i, special; 00038 00039 /* Compute the length of the result. */ 00040 for (i = 0, special = 0; text[i] != '\0'; i++) 00041 switch (text[i]) 00042 { 00043 case '\'': 00044 case '\"': 00045 special += 5; 00046 break; 00047 case '&': 00048 special += 4; 00049 break; 00050 case '<': 00051 case '>': 00052 special += 3; 00053 break; 00054 default: 00055 break; 00056 } 00057 00058 /* Expand the result. */ 00059 result = xmalloc (i + special + 1); 00060 for (i = 0, special = 0; text[i] != '\0'; i++) 00061 switch (text[i]) 00062 { 00063 case '\'': 00064 strcpy (result + i + special, "'"); 00065 special += 5; 00066 break; 00067 case '\"': 00068 strcpy (result + i + special, """); 00069 special += 5; 00070 break; 00071 case '&': 00072 strcpy (result + i + special, "&"); 00073 special += 4; 00074 break; 00075 case '<': 00076 strcpy (result + i + special, "<"); 00077 special += 3; 00078 break; 00079 case '>': 00080 strcpy (result + i + special, ">"); 00081 special += 3; 00082 break; 00083 default: 00084 result[i + special] = text[i]; 00085 break; 00086 } 00087 result[i + special] = '\0'; 00088 00089 return result; 00090 }