Transform non-ASCII characters into hexadecimal entities.
authorarjen <arjen>
Thu, 4 Dec 2003 09:57:35 +0000 (09:57 +0000)
committerarjen <arjen>
Thu, 4 Dec 2003 09:57:35 +0000 (09:57 +0000)
src/gcm_input/string_utils.cpp

index f23daea..10696ad 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : string_utils.cpp
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.2 $
+**      VERSION NUMBER : $Revision: 1.3 $
 **
 **  DESCRIPTION      :  Utility functions for Strings
 **
 
 /*****************************
    $Log: string_utils.cpp,v $
-   Revision 1.2  2003-10-27 11:26:43  arjen
+   Revision 1.3  2003-12-04 09:57:35  arjen
+   Transform non-ASCII characters into hexadecimal entities.
+
+   Revision 1.2  2003/10/27 11:26:43  arjen
    Backslashes are correctly escaped with another backslash
 
    Revision 1.1  2003/08/05 08:15:01  arjen
@@ -36,7 +39,7 @@
 
 *****************************/
 
-static const char *RCSID = "$Id: string_utils.cpp,v 1.2 2003-10-27 11:26:43 arjen Exp $";
+static const char *RCSID = "$Id: string_utils.cpp,v 1.3 2003-12-04 09:57:35 arjen Exp $";
 
 #include <AXE/String.h>
 
@@ -82,12 +85,14 @@ String SQL_Escape(String s)
 **                          "<"  =>   "&lt;"
 **                          ">"  =>   "&gt;"
 **                          "&"  =>   "&amp;"
+**                   Any non-ASCII characters or ASCII control codes are transformed
+**                   into hexadecimal entities.
 **
 **  VARS USED      :
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : 
+**  LAST MODIFIED  : Nov 30, 2003
 **=========================================================================
 */
 
@@ -111,6 +116,25 @@ String XML_Entities(String s)
          s(i,1) = "&gt;";
          i++;
          break;
+      default:
+         if ((s[i] & 0x80) != 0)
+         {
+            // Construct a hexadecimal entity
+            const char hexdigit[] = "0123456789abcdef";
+            char  entity[] = "&#x..;";
+
+            entity[3] = hexdigit[(s[i] >> 4) & 0x0F];
+            entity[4] = hexdigit[ s[i]       & 0x0F];
+
+            s(i,1) = entity;
+            i++;
+         }
+         else if (s[i] < ' ' && s[i] != '\t' && s[i] != '\r')
+         {
+            std::cerr << "WARNING: discarding illegal character " << int(s[i]) << "\n";
+            s(i,1) = "";
+            i++;
+         }
       }
    }