***********************
** FILE NAME : acltest.cpp
** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation
-** VERSION NUMBER : $Revision: 1.4 $
+** VERSION NUMBER : $Revision: 1.5 $
**
** DESCRIPTION : Test routine for non-X classes
**
/*****************************
$Log: acltest.cpp,v $
- Revision 1.4 2002-09-26 14:48:46 arjen
+ Revision 1.5 2002-11-03 13:19:33 arjen
+ New functions - String::escape() and String::unescape()
+
+ Revision 1.4 2002/09/26 14:48:46 arjen
Use the std: namespace for STL objects
Revision 1.3 2002/09/02 06:18:12 arjen
*****************************/
-static const char *RCSID = "$Id: acltest.cpp,v 1.4 2002-09-26 14:48:46 arjen Exp $";
+static const char *RCSID = "$Id: acltest.cpp,v 1.5 2002-11-03 13:19:33 arjen Exp $";
#include "String.h"
#include "integer.h"
String num(3737);
std::cout << num << "\n";
+ std::cout << "***********************\nEscape Test\n*********************\n";
+
+ x = "\a\b\f\n\r\t\v\"'\\\xC0\x02";
+ y = x.escape();
+ std::cout << "Escaped sequence = " << y << "\n";
+ if (y.unescape() == x)
+ {
+ std::cout << "Escape sequence converts back into original string.\n";
+ }
+ else
+ {
+ std::cout << "Escape sequence does NOT convert back into original string.\n";
+ x = y.unescape();
+ std::cout << x.escape() << "\n";
+ }
+
std::cout << "***********************\nInteger Test\n*********************\n";
integer a(2000000000), b(2048);
integer c;
***********************
** FILE NAME : String.h
** SYSTEM NAME : Andromeda X-Windows Encapsulation
-** VERSION NUMBER : $Revision: 1.2 $
+** VERSION NUMBER : $Revision: 1.3 $
**
** DESCRIPTION : Character String class definition
**
********************************
** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
** CREATION DATE : Nov 17, 1995
-** LAST UPDATE : Feb 23, 2001
+** LAST UPDATE : Nov 02, 2002
** MODIFICATIONS :
**************************************************************************/
/*****************************
$Log: String.h,v $
- Revision 1.2 2002-09-28 06:45:51 arjen
+ Revision 1.3 2002-11-03 13:18:57 arjen
+ New functions - String::escape() and String::unescape()
+
+ Revision 1.2 2002/09/28 06:45:51 arjen
New feature: subtring selection by regular expression.
Bugfix: use the std: namespace for STL classes istream and ostream
*****************************/
-// static const char RCSID[] = "$Id: String.h,v 1.2 2002-09-28 06:45:51 arjen Exp $";
+// static const char RCSID[] = "$Id: String.h,v 1.3 2002-11-03 13:18:57 arjen Exp $";
#ifndef STRING_H
#define STRING_H
* Modifiers
*/
- String upper(void);
- String lower(void);
+ String upper(); // Convert to upper case (ASCII)
+ String lower(); // Convert to lower case (ASCII)
+ String escape(); // Insert backslashes to escape special characters
+ String unescape(); // Remove backslashes from escape codes.
/*
* Character searching and Pattern matching
***********************
** FILE NAME : string.cpp
** SYSTEM NAME : Andromeda X-Windows Encapsulation
-** VERSION NUMBER : $Revision: 1.2 $
+** VERSION NUMBER : $Revision: 1.3 $
**
** DESCRIPTION : String class implementation.
**
********************************
** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
** CREATION DATE : Nov 17, 1997
-** LAST UPDATE : Aug 26, 1999
+** LAST UPDATE : Nov 03, 2002
** MODIFICATIONS :
**************************************************************************/
/*****************************
$Log: string.cpp,v $
- Revision 1.2 2002-09-28 06:42:11 arjen
+ Revision 1.3 2002-11-03 13:18:57 arjen
+ New functions - String::escape() and String::unescape()
+
+ Revision 1.2 2002/09/28 06:42:11 arjen
A few small bug fixes.
Revision 1.1 2002/07/25 08:01:27 arjen
*****************************/
-static const char RCSID[] = "$Id: string.cpp,v 1.2 2002-09-28 06:42:11 arjen Exp $";
+static const char RCSID[] = "$Id: string.cpp,v 1.3 2002-11-03 13:18:57 arjen Exp $";
#include <stdio.h>
#include <ctype.h>
return p->s[i];
}
-String String::upper(void)
+String String::upper()
{
String up;
int i;
return up;
}
-String String::lower(void)
+String String::lower()
{
String low;
int i;
return low;
}
+String String::escape()
+{
+ const int BUFSIZE = 500;
+
+ char buffer[BUFSIZE];
+ String escaped = "";
+ int i; // Index in buffer[]
+ int j; // Index in *this
+
+ i = 0;
+ for (j = 0; p->s[j] != '\0'; j++)
+ {
+ if (i + 5 > BUFSIZE)
+ {
+ escaped += buffer;
+ i = 0;
+ }
+
+ switch (p->s[j])
+ {
+ case '\a':
+ buffer[i++] = '\\';
+ buffer[i++] = 'a';
+ break;
+
+ case '\b':
+ buffer[i++] = '\\';
+ buffer[i++] = 'b';
+ break;
+
+ case '\f':
+ buffer[i++] = '\\';
+ buffer[i++] = 'f';
+ break;
+
+ case '\n':
+ buffer[i++] = '\\';
+ buffer[i++] = 'n';
+ break;
+
+ case '\r':
+ buffer[i++] = '\\';
+ buffer[i++] = 'r';
+ break;
+
+ case '\t':
+ buffer[i++] = '\\';
+ buffer[i++] = 't';
+ break;
+
+ case '\v':
+ buffer[i++] = '\\';
+ buffer[i++] = 'v';
+ break;
+
+ case '\'':
+ buffer[i++] = '\\';
+ buffer[i++] = '\'';
+ break;
+
+ case '"':
+ buffer[i++] = '\\';
+ buffer[i++] = '"';
+ break;
+
+ case '\\':
+ buffer[i++] = '\\';
+ buffer[i++] = '\\';
+ break;
+
+ default:
+ if (p->s[j] > '\x20' && p->s[j] < '\x7F')
+ {
+ buffer[i++] = p->s[j];
+ }
+ else
+ {
+ short nibble;
+
+ // Turn into hexadecimal representation
+
+ buffer[i++] = '\\';
+ buffer[i++] = 'x';
+ nibble = (p->s[j] >> 4) & 0x0f;
+ buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
+ nibble = p->s[j] & 0x0f;
+ buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
+ }
+ break;
+ }
+ }
+
+ buffer[i] = '\0';
+ escaped += buffer;
+
+ return escaped;
+}
+
+String String::unescape()
+{
+ String unescaped;
+ char *s, *d;
+
+ unescaped.p->s = new char[strlen(p->s)+1];
+ s = p->s;
+ d = unescaped.p->s;
+
+ while (*s != '\0')
+ {
+ if (*s == '\\')
+ {
+ s++;
+
+ switch (*s)
+ {
+ case 'a':
+ *d = '\a';
+ break;
+
+ case 'b':
+ *d = '\b';
+ break;
+
+ case 'f':
+ *d = '\f';
+ break;
+
+ case 'n':
+ *d = '\n';
+ break;
+
+ case 'r':
+ *d = '\r';
+ break;
+
+ case 't':
+ *d = '\t';
+ break;
+
+ case 'v':
+ *d = '\v';
+ break;
+
+ case '\'':
+ *d = '\'';
+ break;
+
+ case '"':
+ *d = '"';
+ break;
+
+ case '\\':
+ *d = '\\';
+ break;
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ *d = 0;
+ while (*s >= '0' && *s <= '7')
+ {
+ *d *= 8;
+ *d += *s - '0';
+ s++;
+ }
+ s--;
+ break;
+
+ case 'x':
+ *d = 0;
+ s++; // Skip the initial 'x'
+ while (isxdigit(*s))
+ {
+ *d *= 16;
+ *d += *s > '9' ? toupper(*s) - 'A' + 10 : *s - '0';
+ s++;
+ }
+ s--;
+ break;
+
+ default:
+ *d = *s;
+ break;
+ }
+ }
+ else
+ {
+ *d = *s;
+ }
+ s++;
+ d++;
+ }
+
+ return unescaped;
+}
+
+
// Find the first occurance of 'c'
int String::index(char c)