10696ad390920157569a6f0098f929ca8607db2c
[gnucomo.git] / src / gcm_input / string_utils.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2003, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 **      FILE NAME      : string_utils.cpp
10 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
11 **      VERSION NUMBER : $Revision: 1.3 $
12 **
13 **  DESCRIPTION      :  Utility functions for Strings
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
18 ***************************************************************************
19 **  ADMINISTRATIVE INFORMATION *
20 ********************************
21 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 **      CREATION DATE   : Jul 31, 2003
23 **      LAST UPDATE     : Jul 31, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: string_utils.cpp,v $
29    Revision 1.3  2003-12-04 09:57:35  arjen
30    Transform non-ASCII characters into hexadecimal entities.
31
32    Revision 1.2  2003/10/27 11:26:43  arjen
33    Backslashes are correctly escaped with another backslash
34
35    Revision 1.1  2003/08/05 08:15:01  arjen
36    Debug output to the log stream instead of cerr.
37    Fixed namespace problems in XPath searches of the DOM.
38    Moved string utility functions to a separate file.
39
40 *****************************/
41
42 static const char *RCSID = "$Id: string_utils.cpp,v 1.3 2003-12-04 09:57:35 arjen Exp $";
43
44 #include <AXE/String.h>
45
46 /*=========================================================================
47 **  NAME           : SQL_Escape
48 **  SYNOPSIS       : String SQL_Escape(String)
49 **  PARAMETERS     : 
50 **  RETURN VALUE   : 
51 **
52 **  DESCRIPTION    : Insert backslashes before single quotes.
53 **
54 **  VARS USED      :
55 **  VARS CHANGED   :
56 **  FUNCTIONS USED :
57 **  SEE ALSO       :
58 **  LAST MODIFIED  : 
59 **=========================================================================
60 */
61
62 String SQL_Escape(String s)
63 {
64    int i;
65
66    for (i = 0; i < ~s; i++)
67    {
68       if (s[i] == '\'' || s[i] == '\\')
69       {
70          s(i,0) = "\\";
71          i++;
72       }
73    }
74
75    return s;
76 }
77
78 /*=========================================================================
79 **  NAME           : XML_Entities
80 **  SYNOPSIS       : String XML_Entities(String)
81 **  PARAMETERS     : 
82 **  RETURN VALUE   : 
83 **
84 **  DESCRIPTION    : Replace special characters for XML with their entity codes:
85 **                          "<"  =>   "&lt;"
86 **                          ">"  =>   "&gt;"
87 **                          "&"  =>   "&amp;"
88 **                   Any non-ASCII characters or ASCII control codes are transformed
89 **                   into hexadecimal entities.
90 **
91 **  VARS USED      :
92 **  VARS CHANGED   :
93 **  FUNCTIONS USED :
94 **  SEE ALSO       :
95 **  LAST MODIFIED  : Nov 30, 2003
96 **=========================================================================
97 */
98
99 String XML_Entities(String s)
100 {
101    int i;
102
103    for (i = 0; i < ~s; i++)
104    {
105       switch (s[i])
106       {
107       case '&':
108          s(i,1) = "&amp;";
109          i++;
110          break;
111       case '<':
112          s(i,1) = "&lt;";
113          i++;
114          break;
115       case '>':
116          s(i,1) = "&gt;";
117          i++;
118          break;
119       default:
120          if ((s[i] & 0x80) != 0)
121          {
122             // Construct a hexadecimal entity
123             const char hexdigit[] = "0123456789abcdef";
124             char  entity[] = "&#x..;";
125
126             entity[3] = hexdigit[(s[i] >> 4) & 0x0F];
127             entity[4] = hexdigit[ s[i]       & 0x0F];
128
129             s(i,1) = entity;
130             i++;
131          }
132          else if (s[i] < ' ' && s[i] != '\t' && s[i] != '\r')
133          {
134             std::cerr << "WARNING: discarding illegal character " << int(s[i]) << "\n";
135             s(i,1) = "";
136             i++;
137          }
138       }
139    }
140
141    return s;
142 }