Backslashes are correctly escaped with another backslash
[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.2 $
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.2  2003-10-27 11:26:43  arjen
30    Backslashes are correctly escaped with another backslash
31
32    Revision 1.1  2003/08/05 08:15:01  arjen
33    Debug output to the log stream instead of cerr.
34    Fixed namespace problems in XPath searches of the DOM.
35    Moved string utility functions to a separate file.
36
37 *****************************/
38
39 static const char *RCSID = "$Id: string_utils.cpp,v 1.2 2003-10-27 11:26:43 arjen Exp $";
40
41 #include <AXE/String.h>
42
43 /*=========================================================================
44 **  NAME           : SQL_Escape
45 **  SYNOPSIS       : String SQL_Escape(String)
46 **  PARAMETERS     : 
47 **  RETURN VALUE   : 
48 **
49 **  DESCRIPTION    : Insert backslashes before single quotes.
50 **
51 **  VARS USED      :
52 **  VARS CHANGED   :
53 **  FUNCTIONS USED :
54 **  SEE ALSO       :
55 **  LAST MODIFIED  : 
56 **=========================================================================
57 */
58
59 String SQL_Escape(String s)
60 {
61    int i;
62
63    for (i = 0; i < ~s; i++)
64    {
65       if (s[i] == '\'' || s[i] == '\\')
66       {
67          s(i,0) = "\\";
68          i++;
69       }
70    }
71
72    return s;
73 }
74
75 /*=========================================================================
76 **  NAME           : XML_Entities
77 **  SYNOPSIS       : String XML_Entities(String)
78 **  PARAMETERS     : 
79 **  RETURN VALUE   : 
80 **
81 **  DESCRIPTION    : Replace special characters for XML with their entity codes:
82 **                          "<"  =>   "&lt;"
83 **                          ">"  =>   "&gt;"
84 **                          "&"  =>   "&amp;"
85 **
86 **  VARS USED      :
87 **  VARS CHANGED   :
88 **  FUNCTIONS USED :
89 **  SEE ALSO       :
90 **  LAST MODIFIED  : 
91 **=========================================================================
92 */
93
94 String XML_Entities(String s)
95 {
96    int i;
97
98    for (i = 0; i < ~s; i++)
99    {
100       switch (s[i])
101       {
102       case '&':
103          s(i,1) = "&amp;";
104          i++;
105          break;
106       case '<':
107          s(i,1) = "&lt;";
108          i++;
109          break;
110       case '>':
111          s(i,1) = "&gt;";
112          i++;
113          break;
114       }
115    }
116
117    return s;
118 }