GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-xml-helper.c
1 /********************************************************************\
2  * gnc-xml-helper.h -- api for xml helpers *
3  * *
4  * Copyright (C) 2001 James LewisMoss <[email protected]> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA [email protected] *
22  * *
23 \********************************************************************/
24 
25 #include <glib.h>
26 #include "gnc-xml-helper.h"
27 
28 xmlChar*
29 checked_char_cast (gchar *val)
30 {
31  const int length = -1; /* Assumes val is null-terminated */
32  gchar *end;
33  if (val == NULL) return NULL;
34  /* Replace any invalid UTF-8 characters with a sequence of '?' */
35  while (!g_utf8_validate (val, length, (const gchar**)(&end)))
36  *end = '?';
37  /* Replace any invalid (for XML) control characters (everything < 0x20
38  * except \n, \t, and \r) with '?'. Technically we should replace
39  * these with a numeric entity, but that will blow up the libxml
40  * functions that expect raw text. It seems unlikely that anyone
41  * would use intentionally use one of these characters anyway.
42  */
43 
44  for (end = val; *end; ++end)
45  if (*end > 0 && *end < 0x20 && *end != 0x09 &&
46  *end != 0x0a && *end != 0x0d)
47  *end = '?';
48  return (xmlChar*)(val);
49 }
50