1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
* CENA C++ Utilities
*
* by Stephane Chatty
*
* Copyright 1992, 1993
* Centre d'Etudes de la Navigation Aerienne (CENA)
*
* String allocation and copying
*
* $Id$
* $CurLog$
*/
#include "String.h"
#include <string.h>
#include <stdlib.h>
/*?
\label{newstring}
Allocate a string the same size as \var{s},
copy \var{s} in it, and return the address. \var{s} must be a null-terminated string.
If \var{s} is 0, return 0.
?*/
char*
NewString (const char* s)
{
char* p;
if (! s)
return 0;
p = new char [strlen (s) + 1];
strcpy (p, s);
return p;
}
/*?
Allocate a string of \var{n} characters,
copy the \var{n} first characters of \var{s} in it,
and return the address. The returned string is null terminated.
If \var{s} is 0, return 0. If it is shorter than \var{n} characters,
the new string is padded with null characters.
?*/
char*
NewString (const char* s, int n)
{
char* p;
if (! s)
return 0;
p = NewString (n);
if (! p)
return 0;
strncpy (p, s, n);
p [n] = 0;
return p;
}
/*?
Allocate a string of \var{n} characters,
and return its address. Actually, \var{n+1} characters are allocated,
to keep space for the terminating null character.
If \var{n} is negative, return 0. Note that \fun{NewString}~\var{0} will allocate
one character that will be able to hold an empty string.
?*/
char*
NewString (int n)
{
if (n < 0)
return 0;
return new char [n+1];
}
/*?
Free a string which has previously been
allocated with \fun{NewString}.
If \var{s} is 0, do nothing.
Please be careful: it is impossible to test whether \var{s} was really allocated with \fun{NewString}.
?*/
void
FreeString (char* s)
{
if (s)
#ifdef CPLUS_BUG4
delete [strlen (s) + 1] s;
#else
delete [] s;
#endif
}
/*?class IvlString
?*/
#ifdef DOC
/*?
Build an empty \typ{IvlString}.
?*/
IvlString :: IvlString ()
{
}
#endif
/*?
Build a \typ{IvlString} from \var{s}. \var{s} can be null.
?*/
IvlString :: IvlString (const char* s)
: Str (NewString (s))
{
}
/*?
Build a \typ{IvlString} from the \var{n} first characters of \var{s}. \var{s} can be null.
If it is shorter than \var{n} characters,
the new string is padded with null characters.
?*/
IvlString :: IvlString (const char* s, int n)
: Str (NewString (s, n))
{
}
/*?
Create a \typ{IvlString} suitable for storing at most \var{n} characters, and
initialize it to the null string.
?*/
IvlString :: IvlString (int n)
: Str (NewString (n))
{
*Str = '\0';
}
/*?
Create a new \typ{IvlString} by copying \var{s}.
?*/
IvlString :: IvlString (const IvlString& s)
: Str (NewString (s))
{
}
/*?
Create a new \typ{IvlString} by copying \var{s}.
?*/
IvlString :: IvlString (const IvlShadowString& s)
{
Str = NewString (s);
}
/*?nodoc?*/
IvlString :: ~IvlString ()
{
if (Str)
FreeString (Str);
}
#ifdef DOC
/*?
Return the value of a string.
?*/
IvlString :: operator const char* () const
{
}
#endif
/*?
Copy \var{s} into this string. The old string is freed.
?*/
IvlString&
IvlString :: operator = (const IvlString& s)
{
if (this != &s) {
FreeString (Str);
Str = NewString (s);
}
return *this;
}
IvlString&
IvlString :: operator = (const char* s)
{
if (Str != s) {
FreeString (Str);
Str = NewString (s);
}
return *this;
}
IvlString&
IvlString :: operator = (const IvlShadowString& s)
{
if (Str != (const char*) (s)) {
FreeString (Str);
Str = NewString (s);
}
return *this;
}
/*?
Assign to a string the \var{n} first characters of \var{s}.
\var{s} can be null.
If it is shorter than \var{n} characters,
the new string is padded with null characters.
?*/
IvlString&
IvlString :: Assign (const char* s, int n)
{
if (Str != s) {
FreeString (Str);
Str = NewString (s, n);
}
return *this;
}
/*?
Compute the length of a string.
?*/
int
IvlString :: Length () const
{
return strlen (Str);
}
#ifdef DOC
/*?
Build an empty \typ{IvlShadowString}.
?*/
IvlShadowString :: IvlShadowString ()
{
Str = 0;
}
#endif
/*?
?*/
IvlShadowString :: IvlShadowString (const char* s)
{
Str = s;
}
/*?
Create a new \typ{IvlShadowString} by referencing \var{s}.
?*/
IvlShadowString :: IvlShadowString (const IvlString& s)
{
Str = (const char*) s;
}
#ifdef DOC
IvlShadowString&
IvlShadowString :: operator = (const IvlString& s)
{
}
IvlShadowString&
IvlShadowString :: operator = (const char* s)
{
}
/*?
Return the value of a string.
?*/
IvlShadowString :: operator const char* () const
{
}
#endif
|