Bugfix: Use '' instead of \' to escape single quotes in SQL
[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     : Mar 14, 2015
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    : Double backslashes and single quotes as per SQL syntax.
53 **
54 **  VARS USED      :
55 **  VARS CHANGED   :
56 **  FUNCTIONS USED :
57 **  SEE ALSO       :
58 **  LAST MODIFIED  : Mar 14, 2015
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] == '\\')
69       {
70          s(i,0) = "\\";
71          i++;
72       }
73       if (s[i] == '\'')
74       {
75          s(i,0) = "'";
76          i++;
77       }
78    }
79
80    return s;
81 }
82
83 /*=========================================================================
84 **  NAME           : XML_Entities
85 **  SYNOPSIS       : String XML_Entities(String)
86 **  PARAMETERS     : 
87 **  RETURN VALUE   : 
88 **
89 **  DESCRIPTION    : Replace special characters for XML with their entity codes:
90 **                          "<"  =>   "&lt;"
91 **                          ">"  =>   "&gt;"
92 **                          "&"  =>   "&amp;"
93 **                   Any non-ASCII characters or ASCII control codes are transformed
94 **                   into hexadecimal entities.
95 **
96 **  VARS USED      :
97 **  VARS CHANGED   :
98 **  FUNCTIONS USED :
99 **  SEE ALSO       :
100 **  LAST MODIFIED  : Nov 30, 2003
101 **=========================================================================
102 */
103
104 String XML_Entities(String s)
105 {
106    int i;
107
108    for (i = 0; i < ~s; i++)
109    {
110       switch (s[i])
111       {
112       case '&':
113          s(i,1) = "&amp;";
114          i++;
115          break;
116       case '<':
117          s(i,1) = "&lt;";
118          i++;
119          break;
120       case '>':
121          s(i,1) = "&gt;";
122          i++;
123          break;
124       default:
125          if ((s[i] & 0x80) != 0)
126          {
127             // Construct a hexadecimal entity
128             const char hexdigit[] = "0123456789abcdef";
129             char  entity[] = "&#x..;";
130
131             entity[3] = hexdigit[(s[i] >> 4) & 0x0F];
132             entity[4] = hexdigit[ s[i]       & 0x0F];
133
134             s(i,1) = entity;
135             i++;
136          }
137          else if (s[i] < ' ' && s[i] != '\t' && s[i] != '\r')
138          {
139             std::cerr << "WARNING: discarding illegal character " << int(s[i]) << "\n";
140             s(i,1) = "";
141             i++;
142          }
143       }
144    }
145
146    return s;
147 }