1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include "cmt_std.h" |
---|
10 | #include "cmt_string.h" |
---|
11 | |
---|
12 | cmt_string::cmt_string () |
---|
13 | { |
---|
14 | _data = 0; |
---|
15 | _allocated = 0; |
---|
16 | _size = 0; |
---|
17 | } |
---|
18 | |
---|
19 | cmt_string::cmt_string (int n) |
---|
20 | { |
---|
21 | _data = 0; |
---|
22 | _allocated = 0; |
---|
23 | _size = 0; |
---|
24 | allocate (n + 1); |
---|
25 | } |
---|
26 | |
---|
27 | cmt_string::cmt_string (char c) |
---|
28 | { |
---|
29 | _data = 0; |
---|
30 | _allocated = 0; |
---|
31 | _size = 0; |
---|
32 | |
---|
33 | allocate (2); |
---|
34 | |
---|
35 | _data[0] = c; |
---|
36 | _data[1] = 0; |
---|
37 | _size = 1; |
---|
38 | } |
---|
39 | |
---|
40 | cmt_string::cmt_string (const char* text) |
---|
41 | { |
---|
42 | _data = 0; |
---|
43 | _allocated = 0; |
---|
44 | _size = 0; |
---|
45 | |
---|
46 | if (text != 0) |
---|
47 | { |
---|
48 | _size = strlen (text); |
---|
49 | allocate (_size + 1); |
---|
50 | strcpy (_data, text); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | cmt_string::cmt_string (const cmt_string& other) |
---|
55 | { |
---|
56 | const char* text = other._data; |
---|
57 | |
---|
58 | _data = 0; |
---|
59 | _allocated = 0; |
---|
60 | _size = 0; |
---|
61 | |
---|
62 | if (text != 0) |
---|
63 | { |
---|
64 | _size = strlen (text); |
---|
65 | allocate (_size + 1); |
---|
66 | strcpy (_data, text); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | cmt_string::~cmt_string () |
---|
71 | { |
---|
72 | if (_data != 0) |
---|
73 | { |
---|
74 | #ifdef CMT_USE_NEW_DELETE |
---|
75 | delete[] _data; |
---|
76 | #else |
---|
77 | free (_data); |
---|
78 | #endif |
---|
79 | } |
---|
80 | _data = 0; |
---|
81 | _allocated = 0; |
---|
82 | _size = 0; |
---|
83 | } |
---|
84 | |
---|
85 | // |
---|
86 | // Operators |
---|
87 | // |
---|
88 | cmt_string& cmt_string::operator = (char c) |
---|
89 | { |
---|
90 | allocate (2); |
---|
91 | |
---|
92 | _data[0] = c; |
---|
93 | _data[1] = 0; |
---|
94 | |
---|
95 | _size = 1; |
---|
96 | |
---|
97 | return (*this); |
---|
98 | } |
---|
99 | |
---|
100 | cmt_string& cmt_string::operator = (const char* text) |
---|
101 | { |
---|
102 | if (text == _data) return (*this); |
---|
103 | |
---|
104 | if (text != 0) |
---|
105 | { |
---|
106 | _size = strlen (text); |
---|
107 | allocate (_size + 1); |
---|
108 | strcpy (_data, text); |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | _size = 0; |
---|
113 | |
---|
114 | if (_data != 0) |
---|
115 | { |
---|
116 | _data[0] = 0; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | return (*this); |
---|
121 | } |
---|
122 | |
---|
123 | cmt_string& cmt_string::operator = (const cmt_string& other) |
---|
124 | { |
---|
125 | const char* text = other._data; |
---|
126 | cmt_string& me = *this; |
---|
127 | me = text; |
---|
128 | return (me); |
---|
129 | } |
---|
130 | |
---|
131 | cmt_string::operator const char* () const |
---|
132 | { |
---|
133 | if (_data == 0) return (""); |
---|
134 | else return (_data); |
---|
135 | } |
---|
136 | |
---|
137 | const char* cmt_string::c_str () const |
---|
138 | { |
---|
139 | if (_data == 0) return (""); |
---|
140 | else return (_data); |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | char* cmt_string::c_str () |
---|
145 | { |
---|
146 | return (_data); |
---|
147 | } |
---|
148 | */ |
---|
149 | |
---|
150 | void cmt_string::operator += (char c) |
---|
151 | { |
---|
152 | extend (2); |
---|
153 | |
---|
154 | char temp[2] = { c, 0 }; |
---|
155 | |
---|
156 | strcat (&_data[_size], temp); |
---|
157 | _size++; |
---|
158 | } |
---|
159 | |
---|
160 | void cmt_string::operator += (const char* text) |
---|
161 | { |
---|
162 | if (text == 0) return; |
---|
163 | |
---|
164 | int s = strlen (text); |
---|
165 | extend (s + 1); |
---|
166 | |
---|
167 | strcat (&_data[_size], text); |
---|
168 | _size += s; |
---|
169 | } |
---|
170 | |
---|
171 | void cmt_string::operator += (const cmt_string& other) |
---|
172 | { |
---|
173 | const char* text = other._data; |
---|
174 | cmt_string& me = *this; |
---|
175 | |
---|
176 | me += text; |
---|
177 | } |
---|
178 | |
---|
179 | cmt_string cmt_string::operator + (char c) const |
---|
180 | { |
---|
181 | cmt_string result (_data); |
---|
182 | result += c; |
---|
183 | |
---|
184 | return (result); |
---|
185 | } |
---|
186 | |
---|
187 | cmt_string cmt_string::operator + (const char* text) const |
---|
188 | { |
---|
189 | cmt_string result (_data); |
---|
190 | result += text; |
---|
191 | |
---|
192 | return (result); |
---|
193 | } |
---|
194 | |
---|
195 | cmt_string cmt_string::operator + (const cmt_string& other) const |
---|
196 | { |
---|
197 | cmt_string result (_data); |
---|
198 | result += other; |
---|
199 | |
---|
200 | return (result); |
---|
201 | } |
---|
202 | |
---|
203 | char cmt_string::operator [] (int index) const |
---|
204 | { |
---|
205 | if ((_data == 0) || |
---|
206 | (index < 0) || |
---|
207 | (index >= _size)) |
---|
208 | { |
---|
209 | return (0); |
---|
210 | } |
---|
211 | else |
---|
212 | { |
---|
213 | return (_data[index]); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | char& cmt_string::operator [] (int index) |
---|
218 | { |
---|
219 | if ((_data == 0) || |
---|
220 | (index < 0) || |
---|
221 | (index >= _size)) |
---|
222 | { |
---|
223 | static char temp; |
---|
224 | return (temp); |
---|
225 | } |
---|
226 | else |
---|
227 | { |
---|
228 | return (_data[index]); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | int cmt_string::size () const |
---|
233 | { |
---|
234 | if (_data == 0) return (0); |
---|
235 | return (_size); |
---|
236 | } |
---|
237 | |
---|
238 | int cmt_string::size () |
---|
239 | { |
---|
240 | if (_data == 0) return (0); |
---|
241 | return (_size); |
---|
242 | } |
---|
243 | |
---|
244 | void cmt_string::resize (int n) |
---|
245 | { |
---|
246 | allocate (n + 1); |
---|
247 | } |
---|
248 | |
---|
249 | int cmt_string::find (char c) const |
---|
250 | { |
---|
251 | if (_data == 0) return (npos); |
---|
252 | |
---|
253 | char* p = strchr (_data, c); |
---|
254 | if (p == 0) return (npos); |
---|
255 | return (p - _data); |
---|
256 | } |
---|
257 | |
---|
258 | int cmt_string::find (const char* text) const |
---|
259 | { |
---|
260 | if (_data == 0) return (npos); |
---|
261 | if (text == 0) return (npos); |
---|
262 | |
---|
263 | char* p = strstr (_data, text); |
---|
264 | if (p == 0) return (npos); |
---|
265 | return (p - _data); |
---|
266 | } |
---|
267 | |
---|
268 | int cmt_string::find (const cmt_string& other) const |
---|
269 | { |
---|
270 | const char* text = other._data; |
---|
271 | return (find (text)); |
---|
272 | } |
---|
273 | |
---|
274 | int cmt_string::find (int pos, char c) const |
---|
275 | { |
---|
276 | if (_data == 0) return (npos); |
---|
277 | if (pos < 0) return (npos); |
---|
278 | if (pos >= _size) return (npos); |
---|
279 | |
---|
280 | char* p = strchr (&_data[pos], c); |
---|
281 | if (p == 0) return (npos); |
---|
282 | return (p - _data); |
---|
283 | } |
---|
284 | |
---|
285 | int cmt_string::find (int pos, const char* text) const |
---|
286 | { |
---|
287 | if (_data == 0) return (npos); |
---|
288 | if (text == 0) return (npos); |
---|
289 | if (pos < 0) return (npos); |
---|
290 | if (pos >= _size) return (npos); |
---|
291 | |
---|
292 | char* p = strstr (&_data[pos], text); |
---|
293 | if (p == 0) return (npos); |
---|
294 | return (p - _data); |
---|
295 | } |
---|
296 | |
---|
297 | int cmt_string::find (int pos, const cmt_string& other) const |
---|
298 | { |
---|
299 | const char* text = other._data; |
---|
300 | return (find (pos, text)); |
---|
301 | } |
---|
302 | |
---|
303 | int cmt_string::find_last_of (char c) const |
---|
304 | { |
---|
305 | if (_data == 0) return (npos); |
---|
306 | |
---|
307 | char* p = strrchr (_data, c); |
---|
308 | if (p == 0) return (npos); |
---|
309 | return (p - _data); |
---|
310 | } |
---|
311 | |
---|
312 | int cmt_string::find_last_of (const char* text) const |
---|
313 | { |
---|
314 | if (_data == 0) return (npos); |
---|
315 | if (text == 0) return (npos); |
---|
316 | |
---|
317 | char* ptr = _data; |
---|
318 | char* last = 0; |
---|
319 | char* p; |
---|
320 | while ((p = strstr (ptr, text)) != 0) |
---|
321 | { |
---|
322 | last = p; |
---|
323 | ptr = p + 1; |
---|
324 | } |
---|
325 | if (last == 0) return (npos); |
---|
326 | return (last - _data); |
---|
327 | } |
---|
328 | |
---|
329 | int cmt_string::find_last_of (const cmt_string& other) const |
---|
330 | { |
---|
331 | const char* text = other._data; |
---|
332 | return (find_last_of (text)); |
---|
333 | } |
---|
334 | |
---|
335 | void cmt_string::erase (int pos) |
---|
336 | { |
---|
337 | if ((_data == 0) || |
---|
338 | (pos < 0) || |
---|
339 | (pos >= _size)) |
---|
340 | { |
---|
341 | return; |
---|
342 | } |
---|
343 | else |
---|
344 | { |
---|
345 | _data[pos] = 0; |
---|
346 | _size = pos; |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | void cmt_string::erase (int pos, int length) |
---|
351 | { |
---|
352 | if ((_data == 0) || |
---|
353 | (pos < 0) || |
---|
354 | (pos >= _size)) |
---|
355 | { |
---|
356 | return; |
---|
357 | } |
---|
358 | else |
---|
359 | { |
---|
360 | if ((pos + length) >= _size) |
---|
361 | { |
---|
362 | _data[pos] = 0; |
---|
363 | _size = pos; |
---|
364 | } |
---|
365 | else if (length > 0) |
---|
366 | { |
---|
367 | char* d = &_data[pos]; |
---|
368 | char* s = &_data[pos + length]; |
---|
369 | for (;;) |
---|
370 | { |
---|
371 | *d = *s; |
---|
372 | if (*s == 0) break; |
---|
373 | d++; |
---|
374 | s++; |
---|
375 | } |
---|
376 | _size -= length; |
---|
377 | } |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | void cmt_string::replace (const char* pattern, const char* replacement) |
---|
383 | { |
---|
384 | if (_data == 0) return; |
---|
385 | if (_size == 0) return; |
---|
386 | if (pattern == 0) return; |
---|
387 | |
---|
388 | if (replacement == 0) replacement = ""; |
---|
389 | |
---|
390 | if (pattern[0] == 0) return; |
---|
391 | |
---|
392 | int pattern_length = strlen (pattern); |
---|
393 | |
---|
394 | int replacement_length = strlen (replacement); |
---|
395 | int delta = replacement_length - pattern_length; |
---|
396 | |
---|
397 | int pos; |
---|
398 | |
---|
399 | if ((pos = find (pattern)) != npos) |
---|
400 | { |
---|
401 | if (delta > 0) |
---|
402 | { |
---|
403 | // string will be enlarged |
---|
404 | extend (delta); |
---|
405 | |
---|
406 | char* src = &_data[_size]; |
---|
407 | char* dest = src + delta; |
---|
408 | while (src > &_data[pos]) |
---|
409 | { |
---|
410 | *dest = *src; |
---|
411 | src--; |
---|
412 | dest--; |
---|
413 | } |
---|
414 | } |
---|
415 | else if (delta < 0) |
---|
416 | { |
---|
417 | // string will be shortened |
---|
418 | |
---|
419 | char* src = &_data[pos + pattern_length]; |
---|
420 | char* dest = src + delta; |
---|
421 | while (*src != 0) |
---|
422 | { |
---|
423 | *dest = *src; |
---|
424 | src++; |
---|
425 | dest++; |
---|
426 | } |
---|
427 | *dest = *src; |
---|
428 | } |
---|
429 | |
---|
430 | strncpy (&_data[pos], replacement, replacement_length); |
---|
431 | |
---|
432 | _size += delta; |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | void cmt_string::replace (const cmt_string& pattern, |
---|
437 | const cmt_string& replacement) |
---|
438 | { |
---|
439 | const char* p_text = pattern._data; |
---|
440 | const char* r_text = replacement._data; |
---|
441 | cmt_string& me = *this; |
---|
442 | |
---|
443 | me.replace (p_text, r_text); |
---|
444 | } |
---|
445 | |
---|
446 | void cmt_string::replace_all (const char* pattern, const char* replacement) |
---|
447 | { |
---|
448 | if (_data == 0) return; |
---|
449 | if (_size == 0) return; |
---|
450 | if (pattern == 0) return; |
---|
451 | |
---|
452 | if (replacement == 0) replacement = ""; |
---|
453 | |
---|
454 | if (pattern[0] == 0) return; |
---|
455 | |
---|
456 | int pattern_length = strlen (pattern); |
---|
457 | |
---|
458 | int replacement_length = strlen (replacement); |
---|
459 | int delta = replacement_length - pattern_length; |
---|
460 | |
---|
461 | int pos = 0; |
---|
462 | |
---|
463 | while ((pos = find (pos, pattern)) != npos) |
---|
464 | { |
---|
465 | if (delta > 0) |
---|
466 | { |
---|
467 | // string will be enlarged |
---|
468 | extend (delta); |
---|
469 | |
---|
470 | char* src = &_data[_size]; |
---|
471 | char* dest = src + delta; |
---|
472 | while (src > &_data[pos]) |
---|
473 | { |
---|
474 | *dest = *src; |
---|
475 | src--; |
---|
476 | dest--; |
---|
477 | } |
---|
478 | } |
---|
479 | else if (delta < 0) |
---|
480 | { |
---|
481 | // string will be shortened |
---|
482 | |
---|
483 | char* src = &_data[pos + pattern_length]; |
---|
484 | char* dest = src + delta; |
---|
485 | while (*src != 0) |
---|
486 | { |
---|
487 | *dest = *src; |
---|
488 | src++; |
---|
489 | dest++; |
---|
490 | } |
---|
491 | *dest = *src; |
---|
492 | } |
---|
493 | |
---|
494 | strncpy (&_data[pos], replacement, replacement_length); |
---|
495 | pos += replacement_length; |
---|
496 | _size += delta; |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | void cmt_string::replace_all (const cmt_string& pattern, |
---|
501 | const cmt_string& replacement) |
---|
502 | { |
---|
503 | const char* p_text = pattern._data; |
---|
504 | const char* r_text = replacement._data; |
---|
505 | cmt_string& me = *this; |
---|
506 | |
---|
507 | me.replace_all (p_text, r_text); |
---|
508 | } |
---|
509 | |
---|
510 | void cmt_string::trim () |
---|
511 | { |
---|
512 | if (size () == 0) return; |
---|
513 | |
---|
514 | int i = 0; |
---|
515 | |
---|
516 | i = strspn (_data, " \t"); |
---|
517 | if (i > 0) erase (0, i); |
---|
518 | |
---|
519 | for (i = _size - 1; i >= 0; i--) |
---|
520 | { |
---|
521 | char c = _data[i]; |
---|
522 | if ((c == ' ') || (c == '\t')) continue; |
---|
523 | erase (i + 1); |
---|
524 | break; |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | cmt_string cmt_string::substr (int pos) const |
---|
529 | { |
---|
530 | if ((_data == 0) || |
---|
531 | (pos < 0) || |
---|
532 | (pos >= _size)) |
---|
533 | { |
---|
534 | return ((cmt_string) ""); |
---|
535 | } |
---|
536 | else |
---|
537 | { |
---|
538 | return ((cmt_string) &_data[pos]); |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | cmt_string cmt_string::substr (int pos, int length) const |
---|
543 | { |
---|
544 | if ((_data == 0) || |
---|
545 | (pos < 0) || |
---|
546 | (pos >= _size)) |
---|
547 | { |
---|
548 | return ((cmt_string) ""); |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | cmt_string result (&_data[pos]); |
---|
553 | result.erase (length); |
---|
554 | return (result); |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | void cmt_string::substr (int pos, cmt_string& dest) const |
---|
559 | { |
---|
560 | if ((_data == 0) || |
---|
561 | (pos < 0) || |
---|
562 | (pos >= _size)) |
---|
563 | { |
---|
564 | dest = ""; |
---|
565 | } |
---|
566 | else |
---|
567 | { |
---|
568 | dest = (const char*) &_data[pos]; |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | void cmt_string::substr (int pos, int length, cmt_string& dest) const |
---|
573 | { |
---|
574 | if ((_data == 0) || |
---|
575 | (pos < 0) || |
---|
576 | (pos >= _size)) |
---|
577 | { |
---|
578 | dest = ""; |
---|
579 | } |
---|
580 | else |
---|
581 | { |
---|
582 | dest = (const char*) &_data[pos]; |
---|
583 | dest.erase (length); |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | bool cmt_string::operator < (const char* text) const |
---|
588 | { |
---|
589 | if (text == 0) return (false); |
---|
590 | if (_data == 0) return (false); |
---|
591 | |
---|
592 | if (strcmp (_data, text) < 0) return (true); |
---|
593 | return (false); |
---|
594 | } |
---|
595 | |
---|
596 | bool cmt_string::operator < (const cmt_string& other) const |
---|
597 | { |
---|
598 | const char* text = other._data; |
---|
599 | const cmt_string& me = *this; |
---|
600 | |
---|
601 | return (me < text); |
---|
602 | } |
---|
603 | |
---|
604 | bool cmt_string::operator == (const char* text) const |
---|
605 | { |
---|
606 | if (text == 0) |
---|
607 | { |
---|
608 | if (_data == 0) return (true); |
---|
609 | if (_size == 0) return (true); |
---|
610 | return (false); |
---|
611 | } |
---|
612 | if (_data == 0) |
---|
613 | { |
---|
614 | if (text == 0) return (true); |
---|
615 | if (text[0] == 0) return (true); |
---|
616 | return (false); |
---|
617 | } |
---|
618 | |
---|
619 | if (strcmp (_data, text) == 0) return (true); |
---|
620 | return (false); |
---|
621 | } |
---|
622 | |
---|
623 | bool cmt_string::operator == (const cmt_string& other) const |
---|
624 | { |
---|
625 | const char* text = other._data; |
---|
626 | const cmt_string& me = *this; |
---|
627 | |
---|
628 | return (me == text); |
---|
629 | } |
---|
630 | |
---|
631 | bool cmt_string::operator != (const char* text) const |
---|
632 | { |
---|
633 | const cmt_string& me = *this; |
---|
634 | |
---|
635 | if (!(me == text)) return (true); |
---|
636 | return (false); |
---|
637 | } |
---|
638 | |
---|
639 | bool cmt_string::operator != (const cmt_string& other) const |
---|
640 | { |
---|
641 | const char* text = other._data; |
---|
642 | const cmt_string& me = *this; |
---|
643 | |
---|
644 | return (me != text); |
---|
645 | } |
---|
646 | |
---|
647 | bool cmt_string::operator > (const char* text) const |
---|
648 | { |
---|
649 | if (text == 0) return (false); |
---|
650 | if (_data == 0) return (false); |
---|
651 | |
---|
652 | if (strcmp (_data, text) > 0) return (true); |
---|
653 | return (false); |
---|
654 | } |
---|
655 | |
---|
656 | bool cmt_string::operator > (const cmt_string& other) const |
---|
657 | { |
---|
658 | const char* text = other._data; |
---|
659 | const cmt_string& me = *this; |
---|
660 | |
---|
661 | return (me > text); |
---|
662 | } |
---|
663 | |
---|
664 | void cmt_string::extend (int n) |
---|
665 | { |
---|
666 | if (_data != 0) n += _size; |
---|
667 | allocate (n); |
---|
668 | } |
---|
669 | |
---|
670 | void cmt_string::allocate (int n) |
---|
671 | { |
---|
672 | if ((n + 1) > _allocated) |
---|
673 | { |
---|
674 | static const int quantum = 128; |
---|
675 | int frames = ((n + 1)/quantum) + 1; |
---|
676 | _allocated = frames * quantum; |
---|
677 | |
---|
678 | #ifdef CMT_USE_NEW_DELETE |
---|
679 | char* new_data = new char [_allocated + 1]; |
---|
680 | #else |
---|
681 | char* new_data = (char*) malloc (_allocated + 1); |
---|
682 | #endif |
---|
683 | |
---|
684 | |
---|
685 | if (_data != 0) |
---|
686 | { |
---|
687 | strcpy (new_data, _data); |
---|
688 | |
---|
689 | #ifdef CMT_USE_NEW_DELETE |
---|
690 | delete[] _data; |
---|
691 | #else |
---|
692 | free (_data); |
---|
693 | #endif |
---|
694 | |
---|
695 | _data = new_data; |
---|
696 | } |
---|
697 | else |
---|
698 | { |
---|
699 | new_data[0] = 0; |
---|
700 | } |
---|
701 | |
---|
702 | _data = new_data; |
---|
703 | } |
---|
704 | } |
---|
705 | |
---|
706 | ostream& operator << (ostream& o, const cmt_string& s) |
---|
707 | { |
---|
708 | o << (const char*) s; |
---|
709 | return (o); |
---|
710 | } |
---|
711 | |
---|
712 | cmt_string operator + (const char* text, const cmt_string& s) |
---|
713 | { |
---|
714 | cmt_string result = text; |
---|
715 | result += s; |
---|
716 | return (result); |
---|
717 | } |
---|
718 | |
---|
719 | cmt_string operator + (char c, const cmt_string& s) |
---|
720 | { |
---|
721 | cmt_string result = c; |
---|
722 | result += s; |
---|
723 | return (result); |
---|
724 | } |
---|
725 | |
---|
726 | bool cmt_string::read (const cmt_string& file_name) |
---|
727 | { |
---|
728 | FILE* f = fopen (file_name.c_str (), "rb"); |
---|
729 | if (f != NULL) |
---|
730 | { |
---|
731 | fseek (f, 0L, SEEK_END); |
---|
732 | int size = ftell (f); |
---|
733 | fseek (f, 0L, SEEK_SET); |
---|
734 | |
---|
735 | allocate (size + 1); |
---|
736 | |
---|
737 | fread (&_data[0], size, 1, f); |
---|
738 | |
---|
739 | _data[size] = 0; |
---|
740 | _size = size; |
---|
741 | |
---|
742 | fclose (f); |
---|
743 | |
---|
744 | return (true); |
---|
745 | } |
---|
746 | else |
---|
747 | { |
---|
748 | cmt_string& me = *this; |
---|
749 | me = ""; |
---|
750 | |
---|
751 | return (false); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | bool cmt_string::write (const cmt_string& file_name) const |
---|
756 | { |
---|
757 | FILE* f = fopen (file_name.c_str (), "wb"); |
---|
758 | if (f != NULL) |
---|
759 | { |
---|
760 | write (f); |
---|
761 | if (ferror (f)) |
---|
762 | return (false); |
---|
763 | if (fclose (f)) |
---|
764 | return (false); |
---|
765 | return (true); |
---|
766 | } |
---|
767 | else |
---|
768 | { |
---|
769 | return (false); |
---|
770 | } |
---|
771 | } |
---|
772 | |
---|
773 | void cmt_string::write (FILE* f) const |
---|
774 | { |
---|
775 | fwrite (&_data[0], size (), 1, f); |
---|
776 | } |
---|
777 | |
---|
778 | void cmt_string::write (ostream& output) |
---|
779 | { |
---|
780 | output.write (&_data[0], size ()); |
---|
781 | } |
---|
782 | |
---|