1 /**************************************************************************
2 ** (c) Copyright 1997, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 ** FILE NAME : string.cpp
7 ** SYSTEM NAME : Andromeda X-Windows Encapsulation
8 ** VERSION NUMBER : $Revision: 1.5 $
10 ** DESCRIPTION : String class implementation.
15 ***************************************************************************
16 ** ADMINISTRATIVE INFORMATION *
17 ********************************
18 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 ** CREATION DATE : Nov 17, 1997
20 ** LAST UPDATE : Nov 30, 2003
22 **************************************************************************/
24 /*****************************
26 Revision 1.5 2007-05-04 13:55:18 arjen
27 Dynamically allocate more memory if the string buffer runs out of space when
28 reading a String object from an input stream.
30 Revision 1.4 2003/03/29 07:18:54 arjen
31 String constructor and assignment from char * are more robust fro NULL pointers.
33 Revision 1.3 2002/11/03 13:18:57 arjen
34 New functions - String::escape() and String::unescape()
36 Revision 1.2 2002/09/28 06:42:11 arjen
37 A few small bug fixes.
39 Revision 1.1 2002/07/25 08:01:27 arjen
40 First checkin, AXE release 0.2
42 *****************************/
44 static const char RCSID[] = "$Id: string.cpp,v 1.5 2007-05-04 13:55:18 arjen Exp $";
50 // Constructors and destructors for the String class
52 String::String() // Create an empty String
59 String::String(char c) // Create a String from a char
68 String::String(const char *s) // Create a String from a char *
77 p->s = new char[strlen(s)+1];
83 String::String(const String& x) // Create a String from another String
89 String::String(const substring &x)
93 p->s = new char[x.len+1];
94 strncpy(p->s, x.str->p->s+x.start, x.len);
95 p->s[x.len] = '\0'; // strncpy does not add the \0
109 * Assignment operators must handle cleanup of their left-hand operand.
112 String& String::operator=(const char c)
129 String& String::operator=(const char *s)
141 p->s = new char[strlen(s)+1];
148 /* Make sure that assignment of an object to itself works correctly: */
150 String& String::operator=(const String& x)
163 /* Numerical conversion */
165 String::String(int i)
168 p->s = new char[15]; // A little longer than needed...
169 sprintf(p->s, "%d", i);
173 String::String(long i)
176 p->s = new char[15]; // A little longer than needed...
177 sprintf(p->s, "%ld", i);
181 String::String(unsigned long i)
184 p->s = new char[15]; // A little longer than needed...
185 sprintf(p->s, "%lu", i);
189 String::String(double d)
192 p->s = new char[25]; // A little longer than needed...
193 sprintf(p->s, "%.3f", d);
197 /* String concatenation */
199 String& String::operator+=(const String& x)
201 char *s = new char[strlen(p->s) + strlen(x.p->s) + 1];
219 String& String::operator+=(const char * str)
221 char *s = new char[strlen(p->s) + strlen(str) + 1];
239 String operator+(const String& x, const String& y)
247 String operator+(const String& x, const char * y)
255 String operator+(const char * x, const String& y)
263 /* Shift operators */
266 String operator<<(const String &x, int n)
274 String & String::operator<<=(int n)
276 // Make sure we are the only one being shifted.
280 char *s = new char[strlen(p->s) + 1];
289 // Shift left means we really have to copy all characters.
291 int len = strlen(p->s);
296 // Shift more than we have: the String becomes empty.
301 for (i = 0; i <= len - n; i++)
310 String operator>>(const String &x, int n)
318 String & String::operator>>=(int n)
320 // Make sure we are the only one being shifted.
324 char *s = new char[strlen(p->s) + 1];
333 // Shift right is simple: just put the '\0' n places back.
336 int len = strlen(p->s);
340 // Shift more than we have: the String becomes empty.
345 p->s[len - n] = '\0';
351 /* Substring selection and assignment */
353 substring String::operator()(int start, int len)
363 String& substring::operator=(const String &x)
365 char *s = new char[strlen(x.p->s) + ~*str - len + 1];
367 strncpy(s, str->p->s, start);
370 strcat(s, str->p->s+start+len);
375 /* Input and output */
377 std::ostream& operator<<(std::ostream& s, const String& x)
384 std::istream& operator>>(std::istream& s, String& x)
391 buf = new char[bufsize];
398 // Buffer is too small. Allocate some new space.
400 char *newbuf = new char[bufsize * 2];
401 memcpy(newbuf, buf, bufsize);
410 while (s && buf[i] != '\n');
423 * The subscript operator is provided for access to individual characters
426 char& String::operator[](int i)
431 String String::upper()
436 up.p->s = new char[strlen(p->s)+1];
438 for(i=0; p->s[i]; i++)
440 up.p->s[i] = toupper(p->s[i]);
447 String String::lower()
452 low.p->s = new char[strlen(p->s)+1];
454 for(i=0; p->s[i]; i++)
456 low.p->s[i] = tolower(p->s[i]);
463 String String::escape()
465 const int BUFSIZE = 500;
467 char buffer[BUFSIZE];
469 int i; // Index in buffer[]
470 int j; // Index in *this
473 for (j = 0; p->s[j] != '\0'; j++)
534 if (p->s[j] > '\x20' && p->s[j] < '\x7F')
536 buffer[i++] = p->s[j];
542 // Turn into hexadecimal representation
546 nibble = (p->s[j] >> 4) & 0x0f;
547 buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
548 nibble = p->s[j] & 0x0f;
549 buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
561 String String::unescape()
566 unescaped.p->s = new char[strlen(p->s)+1];
627 while (*s >= '0' && *s <= '7')
638 s++; // Skip the initial 'x'
642 *d += *s > '9' ? toupper(*s) - 'A' + 10 : *s - '0';
665 // Find the first occurance of 'c'
667 int String::index(char c)
671 found = strchr(p->s, c);
678 // Find the last occurance of 'c'
680 int String::rindex(char c)
684 found = strrchr(p->s, c);
691 /* In: see if I am part of x, return -1 if not found */
693 int String::in(String & x)
697 match = strstr(x.p->s, p->s);
699 return match - x.p->s;