a5f9bf7155278b9f95c2a92fa3b77d22c1234166
[AXE.git] / src / String.h
1 /**************************************************************************
2 **  (c) Copyright 1997, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : String.h
7 **      SYSTEM NAME    : Andromeda X-Windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      :  Character String class definition
11 **
12 **  EXPORTED OBJECTS : class String
13 **  LOCAL    OBJECTS : class substring
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Nov 17, 1995
20 **      LAST UPDATE     : Feb 23, 2001
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: String.h,v $
26    Revision 1.1  2002-07-25 08:01:26  arjen
27    First checkin, AXE release 0.2
28
29 *****************************/
30
31 // static const char RCSID[] = "$Id: String.h,v 1.1 2002-07-25 08:01:26 arjen Exp $";
32
33 #ifndef STRING_H
34 #define STRING_H
35
36 #include <stdlib.h>
37 #include <iostream.h>
38 #include <string.h>
39
40 #include <regex.h>
41
42 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
43 **  NAME           : String - Character String class.
44 **  MEMBERS        : p -> s : Pointer to the actual String data.
45 **                   p -> n : Number of references to this String.
46 **  OPERATORS      : =      : Assign a String, char *, char to a String.
47 **                   +, +=  : concatenate Strings
48 **                   ~      : Length of a String
49 **                   []     : Individual character access.
50 **                   !      : Empty String test.
51 **                   ()     : Substring selection.
52 **                   ostream << : String to output stream.
53 **                   istream << : String from input stream.
54 **                   ==, !=, <,
55 **                   >, <=, >= : String comparison.
56 **  METHODS        :
57 **
58 **  DESCRIPTION    : The String class counts the references to a String to
59 **                   minimize copying and uses standard C++ character strings
60 **                   as constants.
61 **
62 **  RELATIONS      : substring
63 **  SEE ALSO       :
64 **  LAST MODIFIED  : Aug 27, 1999
65 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66 */
67
68
69 class String
70 {
71    friend class substring;
72    friend class regex;
73
74    struct srep
75    {
76       char  *s;    // pointer to data;
77       int    n;    // reference count
78    } *p;
79
80 public:
81
82    String(const char *);      // String x = "abc"
83    String(char);              // String x = 'a'
84    String();                  // String x;
85    String(const String &);    // String x = String ...
86    String(const substring &); // String x = String(s, l)
87
88    String& operator=(const char);
89    String& operator=(const char *);
90    String& operator=(const String &);
91    ~String();
92
93    /*  Numerical conversion */
94
95    String(long);             //  String x(12);  x = "12"
96    String(unsigned long);    //  String x(12);  x = "12"
97    String(int);              //  String x(12);  x = "12"
98    operator long()
99    {
100       return strtol(p->s, 0, 0);
101    }
102
103    operator unsigned long()
104    {
105       return strtoul(p->s, 0, 0);
106    }
107
108    long dec(void)
109    {
110       return strtol(p->s, 0, 10);
111    }
112
113    long oct(void)
114    {
115       return strtol(p->s, 0, 8);
116    }
117
118    long hex(void) 
119    {
120       return strtol(p->s, 0, 16);
121    }
122
123    String(double);
124    operator double()
125    {
126       return strtod(p->s, 0);
127    }
128
129    /*     */
130
131    char& operator[](int i);    //  Individual character access
132
133    int operator~() const      //  Length of the String
134    {
135       return strlen(p->s);
136    }
137
138    operator char*()
139    {
140       if (p->s && p->s[0])
141          return p->s;
142       else
143          return 0;
144    }
145    operator const char*() const
146    {
147       if (p->s && p->s[0])
148          return p->s;
149       else
150          return 0;
151    }
152
153    operator bool()   //  Test for empty String
154    {
155       return p->s != 0 && p->s[0] != '\0';
156    }
157
158    /*
159     *   String concatenation.
160     *   char and char* are handled by constructors.
161     */
162
163    String& operator+=(const String&);
164    String& operator+=(const char *);
165    friend String operator+(const String&, const String&);
166    friend String operator+(const String&, const char *);
167    friend String operator+(const char *,  const String&);
168
169    /*
170     *  Shifting characters out
171     *            "abcdefgh" <<= 3  = "defgh"
172     *            "abcdefgh" >>= 3  = "abcde"
173     */
174
175    friend String operator<<(const String &x, int n);
176    String & operator<<=(int n);
177
178    friend String operator>>(const String &x, int n);
179    String & operator>>=(int n);
180
181    /*
182     *   Substring selection
183     */
184
185    substring operator()(int start, int len);
186
187    /*
188     *   Input and output
189     */
190
191    friend ostream& operator<<(ostream &, const String &);
192    friend istream& operator>>(istream &, String &);
193
194    /*
195     *   String comparison tests
196     */
197
198    friend int operator==(const String& x, const String& y)
199    {
200       return strcmp(x.p->s, y.p->s) == 0;
201    }
202
203    friend int operator!=(const String& x, const String& y)
204    {
205       return strcmp(x.p->s, y.p->s) != 0;
206    }
207
208    friend int operator<=(const String& x, const String& y)
209    {
210       return strcmp(x.p->s, y.p->s) <= 0;
211    }
212
213    friend int operator>=(const String& x, const String& y)
214    {
215       return strcmp(x.p->s, y.p->s) >= 0;
216    }
217
218    friend int operator<(const String& x, const String& y)
219    {
220       return strcmp(x.p->s, y.p->s) < 0;
221    }
222
223    friend int operator>(const String& x, const String& y)
224    {
225       return strcmp(x.p->s, y.p->s) > 0;
226    }
227
228    friend int operator==(const String& x, const char * y)
229    {
230       return strcmp(x.p->s, y) == 0;
231    }
232
233    friend int operator!=(const String& x, const char * y)
234    {
235       return strcmp(x.p->s, y) != 0;
236    }
237
238    friend int operator<=(const String& x, const char * y)
239    {
240       return strcmp(x.p->s, y) <= 0;
241    }
242
243    friend int operator>=(const String& x, const char * y)
244    {
245       return strcmp(x.p->s, y) >= 0;
246    }
247
248    friend int operator<(const String& x, const char * y)
249    {
250       return strcmp(x.p->s, y) < 0;
251    }
252
253    friend int operator>(const String& x, const char * y)
254    {
255       return strcmp(x.p->s, y) > 0;
256    }
257
258    /*
259     *  Modifiers
260     */
261
262    String upper(void);
263    String lower(void);
264
265    /*
266     *  Character searching and Pattern matching
267     */
268
269    int index(char c);
270    int rindex(char c);
271
272    int in(String & x);
273
274    //  Regular expression pattern matching
275  
276    friend bool operator == (const String &s, const regex &r);
277    friend bool operator == (const regex &r, const String &s);
278 };
279    /*
280     *   Other operators to think about...
281     *
282     *
283     *   Numeric conversion with strtod and strtol and vice-versa,
284     *   so we can do:
285     *                 String nr = 123  //  nr = "123"
286     *                 float x = 2.32; String f = x;  // f = "2.32"
287     *                             f = "3.145";
288     *                         int i = f;           // i = 3
289     *
290     *   String matching:
291     *       int index(String&, start=0)  // Find position of substring
292     *
293     *
294     *  Literature:  Bjarne Stroustrup, Section 6.9
295     *               DDJ October 1991, pg 24
296     */
297
298
299 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
300 **  NAME           : substring - Determine part of a String object.
301 **  MEMBERS        : str   : The String object of which this is a substring
302 **                   start : index of the first substring character.
303 **                   len   : Number of characters in the substring
304 **  OPERATORS      : =     : Assignment of a String or char *
305 **  METHODS        :
306 **
307 **  DESCRIPTION    : This implements substring selection from a String object
308 **                   with l-value semantics.
309 **
310 **  RELATIONS      : String
311 **  SEE ALSO       :
312 **  LAST MODIFIED  :
313 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
314 */
315
316 class substring
317 {
318    friend class String;
319
320    String     *str;   //  Where I am a substring of.
321    int  start, len;
322
323 public:
324    String& operator=(const String &); 
325    String& operator=(char *s)
326    {
327       *this = String(s);
328       return *str;
329    }
330 };
331
332 /*  Regular expression matching */
333
334 class regex
335 {
336    regex_t    expression;
337
338 public:
339
340    regex(const String & reg);
341    regex(const char * reg);
342    ~regex();
343
344    friend bool operator == (const String &s, const regex &r);
345    friend bool operator == (const regex &r, const String &s);
346 };
347
348 #endif  /* STRING_H */