~ubuntu-branches/ubuntu/utopic/tcm/utopic

« back to all changes in this revision

Viewing changes to src/gl/lstring.h

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------------
 
2
// 
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version. 
 
11
// 
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
//-----------------------------------------------------------------------------
 
22
#ifndef _LSTRING_H
 
23
#define _LSTRING_H
 
24
 
 
25
#include <iostream>
 
26
 
 
27
/// infinite length string class.
 
28
class string {
 
29
/*@Doc: {\large {\bf scope:} global} */
 
30
public:
 
31
        /// the empty string
 
32
        static const string EMPTY;
 
33
 
 
34
// constructors.
 
35
        /// create empty string.
 
36
        string();               
 
37
 
 
38
        /// copy constructor. usage: string x = string
 
39
        string(const string &s);
 
40
 
 
41
        /// copy constructor. usage: string x = "abc" 
 
42
        string(const char* s);
 
43
 
 
44
        /// copy constructor. usage: string x = 'a'
 
45
        string(char c);
 
46
 
 
47
        /// copy constructor (decimals). usage: string x = 12;
 
48
        string(int n);
 
49
 
 
50
        /// copy constructor (decimals). usage: string x = 12;
 
51
        string(unsigned n);
 
52
 
 
53
        /// copy constructor (doubles). usage: string x = 1.2;
 
54
        string(double d);
 
55
 
 
56
// destructor.
 
57
        /// delete string.
 
58
        virtual ~string();              
 
59
 
 
60
// assignment.
 
61
        /// copy operator a la strcpy.
 
62
        string &operator=(const string &s);
 
63
 
 
64
        /// copy operator a la strcpy.
 
65
        string &operator=(const char* s); 
 
66
 
 
67
        /// copy operator a la strcpy.
 
68
        string &operator=(char c); 
 
69
 
 
70
        /// copy operator a la strcpy. s = n in decimal notation.
 
71
        string &operator=(int n); 
 
72
 
 
73
        /// copy operator a la strcpy. s = n in decimal notation.
 
74
        string &operator=(unsigned n); 
 
75
 
 
76
        /// copy operator a la strcpy. s = d in floating point notation.
 
77
        string &operator=(double d); 
 
78
 
 
79
// concatenation.
 
80
        /// concat operator a la strcat.
 
81
        string &operator+=(const string &s);
 
82
 
 
83
        /// concat operator a la strcat.
 
84
        string &operator+=(const char* s);
 
85
 
 
86
        /// concat. n in decimal notation is added.
 
87
        string &operator+=(int n); 
 
88
 
 
89
        /// concat. n in decimal notation is added.
 
90
        string &operator+=(unsigned n); 
 
91
 
 
92
        /// concat. d in floating point notation is added.
 
93
        string &operator+=(double d); 
 
94
 
 
95
        /// concat operator a la strcat.
 
96
        string &operator+=(char c);
 
97
 
 
98
        /// constructive concat operator.
 
99
        friend string operator+(const char* s1, const string &s2);
 
100
 
 
101
        /// constructive concat operator.
 
102
        friend string operator+(const string &s1, const char* s2);
 
103
 
 
104
        /// constructive concat operator.
 
105
        string operator+(const string &s);
 
106
 
 
107
        /// constructive concat operator.
 
108
        string operator+(const char* s);
 
109
 
 
110
        /// add c at end of string.
 
111
        void add(char c);        
 
112
 
 
113
        /// add n characters of s at end of string (0 = without limit).
 
114
        void add(const string &s, unsigned n = 0);
 
115
 
 
116
        /// add n characters of s at end of string (0 = without limit).
 
117
        void add(const char *s, unsigned n = 0);
 
118
 
 
119
        /// add character at n-th position of string.
 
120
        void insert(char c, int pos);
 
121
 
 
122
// searching and matching.
 
123
        /// return 1 if target is in string. 0 for failure.
 
124
        int contains(const string &s) const;
 
125
 
 
126
        /// return 1 if target is in string. 0 for failure.
 
127
        int contains(const char* s) const;
 
128
 
 
129
        /// return 1 if target is in string. 0 for failure.
 
130
        int contains(char c) const;
 
131
 
 
132
        /// return 1 if string starts with target, 0 for failure.
 
133
        int startsWith(const string &s) const;
 
134
 
 
135
        /// return 1 if string starts with target, 0 for failure.
 
136
        int startsWith(const char* s) const;
 
137
 
 
138
        /// return 1 if string ends with target, 0 for failure.
 
139
        int endsWith(const string &s) const;
 
140
 
 
141
        /// return 1 if string ends with target, 0 for failure.
 
142
        int endsWith(const char* s) const;
 
143
 
 
144
        /// adds suffix s. return 1 when suffix is changed, 0 when only added.
 
145
        int setSuffix(const string &s, char delim = '.');
 
146
 
 
147
        /// puts suffix in s. when there is no suffix returns 0, 1 otherwise.
 
148
        int getSuffix(string &s, char delim = '.') const;
 
149
 
 
150
// element extraction.
 
151
        /// selection of char by index a la array.
 
152
        char &operator[](int i);
 
153
 
 
154
        /// selection of char by index a la array.
 
155
        const char &operator[](int i) const;
 
156
 
 
157
// deletion.
 
158
        /// remove last character of string.
 
159
        void remove();           
 
160
 
 
161
        /// remove pos-th character of string.
 
162
        void remove(int pos);    
 
163
 
 
164
// substitution.
 
165
        /// replace each occurence of old char by nw char .
 
166
        int replace(char old, char nw); 
 
167
 
 
168
        /// global substitution.
 
169
        int replace(const string &pat, const string &repl, int casesens=1); 
 
170
 
 
171
        /// first occurrence substitution.
 
172
        int replaceFirst(const string &pat, const string &repl, 
 
173
                int casesens=1); 
 
174
 
 
175
        /// add to word an english plural ending, like horse->horses, sky->skies.
 
176
        void addPlural();
 
177
 
 
178
// transformation.
 
179
        /// move all chars to upper case
 
180
        friend string upcase(const string &s);
 
181
 
 
182
        /// move all chars to lower case
 
183
        friend string downcase(const string &s);
 
184
 
 
185
        /// strip all spurious blank chars.
 
186
        friend string stripblanks(const string &s);
 
187
 
 
188
        /// in-place version of the above.
 
189
        void upcase();
 
190
        ///
 
191
        void downcase();
 
192
        ///
 
193
        void stripblanks();
 
194
// conversion
 
195
        ///
 
196
        unsigned long toulong() const;
 
197
        ///
 
198
        long tolong() const;
 
199
        ///
 
200
        double todouble() const;
 
201
        ///
 
202
        int toint() const;
 
203
// status.
 
204
        /// number of characters in string.
 
205
        unsigned length() const {return len;} 
 
206
 
 
207
        /// the number of lines in string when printed: newlines+1.
 
208
        unsigned lines() const;
 
209
 
 
210
        /// return nr. of chars of line with most chars in string.
 
211
        unsigned width() const; 
 
212
 
 
213
        /// return which line has i-th char. pos becomes position in line.
 
214
        int whichline(int i, int &pos) const; 
 
215
 
 
216
        /// return position of n-th char in line-th line.
 
217
        int nthInline(int line, int n) const; 
 
218
 
 
219
        /// return number of letters (a-z,A-Z) in string.
 
220
        unsigned letters() const;
 
221
 
 
222
        /// set precision when string is interpreted as double.
 
223
        void setPrecision(unsigned p) {precision=p;}
 
224
 
 
225
// IO
 
226
        /// write string to output stream.
 
227
        friend std::ostream &operator<<(std::ostream &o, const string &s);
 
228
 
 
229
        /// read string from input stream.
 
230
        friend std::istream &operator>>(std::istream &i, string &s);
 
231
 
 
232
// comparison.
 
233
        /// comparison operator returns 1 if equal
 
234
        friend int operator==(const string &x, const char* s);
 
235
 
 
236
        /// comparison operator returns 1 if equal.
 
237
        friend int operator==(const string &x, const string &y);
 
238
 
 
239
        /// comparison operator that ignores case
 
240
        friend int operator%=(const string &x, const char* s);
 
241
 
 
242
        /// comparison operator that ignores case
 
243
        friend int operator%=(const string &x, const string &y);
 
244
 
 
245
        /// comparison operator a la !equal.
 
246
        friend int operator!=(const string &x, const char* s);
 
247
 
 
248
        /// comparison operator a la !equal.
 
249
        friend int operator!=(const string &x, const string &y);
 
250
 
 
251
        /// comparison operator a la strcmp
 
252
        friend int compare(const string &x, const char* s);
 
253
 
 
254
        /// comparison operator a la strcmp
 
255
        friend int compare(const string &x, const string &y);
 
256
 
 
257
// messy
 
258
        /// return a const pointer to the data to do dirty stuff.
 
259
        const char* getstr() const {return str;}
 
260
protected:
 
261
        ///
 
262
        int doReplace(const string &pat, const string &repl, 
 
263
                        int global, int casesens=1); 
 
264
private:
 
265
        /// pointer to data.
 
266
        char* str;              
 
267
 
 
268
        /// the length of the data.
 
269
        int len;
 
270
 
 
271
        /// the precision used for floats and doubles.
 
272
        int precision;
 
273
 
 
274
        /// length in which data is allocated.
 
275
        static const int CHUNK;
 
276
};
 
277
#endif