Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/mystring.cpp
3206 views
1
2
//**************************************************************
3
//
4
// filename: mystring.cpp
5
//
6
// project: doctoral thesis
7
//
8
// autor: Dipl.-Ing. Gerstmayr Johannes
9
//
10
// generated: 20.12.98
11
// last change: 20.12.98
12
// description: implementation for strings
13
// remarks:
14
//
15
//**************************************************************
16
17
// string class
18
#include <mystdlib.h>
19
#include <myadt.hpp>
20
21
#include <linalg.hpp>
22
#include <gprim.hpp>
23
24
/*
25
#include <iostream.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <stdio.h>
29
#include <math.h>
30
#include <fstream.h>
31
#include "mystring.hh"
32
33
*/
34
35
namespace netgen
36
{
37
38
39
void ReadEnclString(istream & in, string & str, const char encl)
40
{
41
char currchar;
42
str = "";
43
44
in.get(currchar);
45
while(in && currchar == ' ' || currchar == '\t' || currchar == '\n')
46
in.get(currchar);
47
48
if(currchar == encl)
49
{
50
in.get(currchar);
51
while(in && currchar != encl)
52
{
53
str += currchar;
54
in.get(currchar);
55
}
56
}
57
else
58
{
59
in.putback(currchar);
60
in >> str;
61
}
62
}
63
64
65
66
67
68
69
void DefaultStringErrHandler()
70
{
71
cerr << "Error : string operation out of range\n" << flush;
72
}
73
74
void (*MyStr::ErrHandler)() = DefaultStringErrHandler;
75
76
/*
77
MyStr::MyStr()
78
{
79
length = 0;
80
str = shortstr;
81
str[0] = 0;
82
}
83
*/
84
85
MyStr::MyStr(const char *s)
86
{
87
length = unsigned(strlen(s));
88
89
if (length > SHORTLEN)
90
str = new char[length + 1];
91
else
92
str = shortstr;
93
strcpy(str, s);
94
}
95
96
/*
97
MyStr::MyStr(char s)
98
{
99
length = 1;
100
str = shortstr;
101
str[0] = s;
102
str[1] = (char)0;
103
}
104
*/
105
106
MyStr::MyStr(const MyStr& s)
107
{
108
length = s.length;
109
if (length > SHORTLEN)
110
str = new char[length + 1];
111
else
112
str = shortstr;
113
strcpy(str, s.str);
114
}
115
116
MyStr::MyStr(int i)
117
{
118
char buffer[32];
119
sprintf(buffer, "%d", i);
120
length = unsigned(strlen(buffer));
121
if (length > SHORTLEN)
122
str = new char[length + 1];
123
else
124
str = shortstr;
125
strcpy(str, buffer);
126
}
127
128
MyStr::MyStr(void * p)
129
{
130
char buffer[32];
131
sprintf(buffer, "%p", p);
132
length = unsigned(strlen(buffer));
133
if (length > SHORTLEN)
134
str = new char[length + 1];
135
else
136
str = shortstr;
137
strcpy(str, buffer);
138
}
139
140
141
MyStr::MyStr(long l)
142
{
143
char buffer[32];
144
sprintf(buffer, "%ld", l);
145
length = unsigned(strlen(buffer));
146
if (length > SHORTLEN)
147
str = new char[length + 1];
148
else
149
str = shortstr;
150
strcpy(str, buffer);
151
}
152
153
MyStr::MyStr(double d)
154
{
155
char buffer[32];
156
//if (fabs(d) < 1E-100) {d = 0;}
157
sprintf(buffer, "%g", d);
158
length = unsigned(strlen(buffer));
159
if (length > SHORTLEN)
160
str = new char[length + 1];
161
else
162
str = shortstr;
163
strcpy(str, buffer);
164
}
165
166
MyStr::MyStr(const Point3d& p)
167
{
168
char buffer[80];
169
//if (fabs(d) < 1E-100) {d = 0;}
170
sprintf(buffer, "[%g, %g, %g]", p.X(), p.Y(), p.Z());
171
length = unsigned(strlen(buffer));
172
if (length > SHORTLEN)
173
str = new char[length + 1];
174
else
175
str = shortstr;
176
strcpy(str, buffer);
177
}
178
179
MyStr::MyStr(const Vec3d& p)
180
{
181
char buffer[80];
182
//if (fabs(d) < 1E-100) {d = 0;}
183
sprintf(buffer, "[%g, %g, %g]", p.X(), p.Y(), p.Z());
184
length = unsigned(strlen(buffer));
185
if (length > SHORTLEN)
186
str = new char[length + 1];
187
else
188
str = shortstr;
189
strcpy(str, buffer);
190
}
191
192
MyStr::MyStr(unsigned n, int)
193
{
194
length = n;
195
if (length > SHORTLEN)
196
str = new char[length + 1];
197
else
198
str = shortstr;
199
str[n] = 0;
200
}
201
202
MyStr::MyStr(const string & st)
203
{
204
length = unsigned(st.length());
205
if (length > SHORTLEN)
206
str = new char[length + 1];
207
else
208
str = shortstr;
209
strcpy (str, st.c_str());
210
}
211
212
213
214
MyStr MyStr::Left(unsigned r)
215
{
216
if(r > length)
217
{
218
MyStr::ErrHandler();
219
MyStr s;
220
return s;
221
}
222
else
223
{
224
MyStr tmp(r, 0);
225
strncpy(tmp.str, str, r);
226
return tmp;
227
}
228
}
229
230
MyStr MyStr::Right(unsigned l)
231
{
232
if(l > length)
233
{
234
MyStr::ErrHandler();
235
MyStr s;
236
return s;
237
}
238
else
239
{
240
MyStr tmp(l, 0);
241
strncpy(tmp.str, str + length - l, l);
242
return tmp;
243
}
244
}
245
246
MyStr& MyStr::InsertAt(unsigned pos, const MyStr& s)
247
{
248
if(pos > length)
249
{
250
MyStr::ErrHandler();
251
return *this;
252
}
253
int newLength = length + s.length;
254
char *tmp = new char[newLength + 1];
255
strncpy(tmp, str, pos);
256
strcpy(tmp + pos, s.str);
257
strcpy(tmp + pos + s.length, str + pos);
258
259
if (length > SHORTLEN) delete [] str;
260
length = newLength;
261
if (length > SHORTLEN)
262
str = tmp;
263
else
264
{
265
strcpy (shortstr, tmp);
266
delete [] tmp;
267
str = shortstr;
268
}
269
return *this;
270
}
271
272
MyStr &MyStr::WriteAt(unsigned pos, const MyStr& s)
273
{
274
if(pos > length)
275
{
276
MyStr::ErrHandler();
277
return *this;
278
}
279
unsigned n = length - pos;
280
if(s.length < n)
281
n = s.length;
282
strncpy(str + pos, s.str, n);
283
return *this;
284
}
285
286
void MyStr::ConvertTextToExcel()
287
{
288
/*
289
for (int i = 0; i < Length(); i++)
290
{
291
if ((*this)[i]==',') {(*this)[i] = ';';}
292
else if ((*this)[i]=='.') {(*this)[i] = ',';}
293
}
294
*/
295
}
296
297
void MyStr::ConvertExcelToText()
298
{
299
/*
300
for (int i = 0; i < Length(); i++)
301
{
302
if ((*this)[i]==',') {(*this)[i] = '.';}
303
else if ((*this)[i]==';') {(*this)[i] = ',';}
304
}
305
*/
306
}
307
308
MyStr& MyStr::operator = (const MyStr& s)
309
{
310
if (length > SHORTLEN) delete [] str;
311
length = s.length;
312
if (length > SHORTLEN)
313
str = new char[length + 1];
314
else
315
str = shortstr;
316
strcpy(str, s.str);
317
return *this;
318
}
319
320
MyStr operator + (const MyStr& s1, const MyStr& s2)
321
{
322
MyStr tmp(s1.length + s2.length, 0);
323
if (s1.length != 0) strcpy(tmp.str, s1.str);
324
if (s2.length != 0) strcpy(tmp.str + s1.length, s2.str);
325
return tmp;
326
}
327
328
void MyStr::operator += (const MyStr& s)
329
{
330
if (length+s.length <= SHORTLEN)
331
{
332
if (s.length != 0) strcpy(shortstr + length, s.str);
333
}
334
else
335
{
336
char *tmp = new char[length + s.length + 1];
337
if (length != 0) strcpy(tmp, str);
338
if (s.length != 0) strcpy(tmp + length, s.str);
339
if (length > SHORTLEN) delete [] str;
340
length += s.length;
341
str = tmp;
342
}
343
}
344
345
char& MyStr::operator [] (unsigned n)
346
{
347
static char dummy;
348
if(n < length)
349
return str[n];
350
else
351
{
352
MyStr::ErrHandler();
353
return dummy;
354
}
355
}
356
357
char MyStr::operator [] (unsigned n) const
358
{
359
static char dummy;
360
if(n < length)
361
return str[n];
362
else
363
{
364
MyStr::ErrHandler();
365
return dummy;
366
}
367
}
368
369
MyStr MyStr::operator () (unsigned l, unsigned r)
370
{
371
if((l > r) || (r > length))
372
{
373
MyStr::ErrHandler();
374
MyStr s;
375
return s;
376
}
377
else
378
{
379
int n = r - l + 1;
380
MyStr tmp(n, 0);
381
strncpy(tmp.str, str + 1, n);
382
return tmp;
383
}
384
}
385
386
string MyStr::cpp_string(void) const
387
{
388
string aux(str,length);
389
return aux;
390
}
391
392
/*
393
istream& operator >> (istream& is, MyStr& s)
394
{
395
const int buflen = 1000;
396
char buffer[buflen+1];
397
398
int end = 0;
399
s = "";
400
MyStr str;
401
402
while (!end)
403
{
404
is.get(buffer, buflen);
405
str = MyStr(buffer);
406
s += str;
407
if (is.peek() == EOF) {end = 1;}
408
}
409
410
return is;
411
}
412
*/
413
/*
414
#ifdef __borland
415
::ifstream& operator >> (::ifstream& is, MyStr& s) // wb
416
{ // wb
417
const int buflen = 1000; // wb
418
char buffer[buflen+1]; // wb
419
// wb
420
int end = 0; // wb
421
s = ""; // wb
422
MyStr str; // wb
423
// wb
424
while (!end) // wb
425
{ // wb
426
is.get(buffer, buflen); // wb
427
str = MyStr(buffer); // wb
428
s += str; // wb
429
if (is.peek() == EOF) {end = 1;} // wb
430
} // wb
431
// wb
432
return is; // wb
433
}
434
435
#endif
436
*/
437
}
438
439