The Original CHomP Software
mwdata.h
Go to the documentation of this file.
1
3
13
14// Copyright (C) 1997-2020 by Pawel Pilarczyk.
15//
16// This file is part of my research software package. This is free software:
17// you can redistribute it and/or modify it under the terms of the GNU
18// General Public License as published by the Free Software Foundation,
19// either version 3 of the License, or (at your option) any later version.
20//
21// This software is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with this software; see the file "license.txt". If not,
28// please, see <https://www.gnu.org/licenses/>.
29
30// Started on August 10, 2004. Last revision: January 25, 2010.
31
32
33#ifndef _CHOMP_MULTIWORK_MWDATA_H_
34#define _CHOMP_MULTIWORK_MWDATA_H_
35
37
38// include an appropriate header file for 'string' if necessary
39#if mwSTRING
40 #include <string>
41#endif // mwSTRING
42
43// include an appropriate header for 'istream' and 'ostream' if necessary
44#if mwSTREAMS
45 #include <iostream>
46 #include <cstdio>
47#endif // use mwSTREAMS
48
49
50namespace chomp {
51namespace multiwork {
52
53// --------------------------------------------------
54// --------------------- mwData ---------------------
55// --------------------------------------------------
56
66class mwData
67{
68public:
70 mwData ();
71
73 ~mwData ();
74
76 mwData (const mwData &x);
77
79 mwData &operator = (const mwData &x);
80
82 mwData &Swap (mwData &x);
83
86 mwData &Take (mwData &x);
87
89 mwData &Take (unsigned char *buffer, int length);
90
92 mwData &Take (char *buffer, int length);
93
95 const char *Buffer () const;
96
98 int Length () const;
99
101 mwData &Rewind ();
102
104 mwData &Reset ();
105
107 int Position (int newpos);
108
110 int Position () const;
111
113 mwData &Append (const char *buffer, int length);
114
116 mwData &Append (const unsigned char *buffer, int length);
117
119 mwData &Append (const mwData &x);
120
122 const char *Current () const;
123
124 // append and retrieve an object to the buffer
125 mwData &Append (const int &x);
126 mwData &Retrieve (int &x);
127 mwData &Append (const unsigned int &x);
128 mwData &Retrieve (unsigned int &x);
129 mwData &Append (const short &x);
130 mwData &Retrieve (short &x);
131 mwData &Append (const unsigned short &x);
132 mwData &Retrieve (unsigned short &x);
133 mwData &Append (const long &x);
134 mwData &Retrieve (long &x);
135 mwData &Append (const unsigned long &x);
136 mwData &Retrieve (unsigned long &x);
137 #if mwLONGLONG
138 mwData &Append (const long long &x);
139 mwData &Retrieve (long long &x);
140 mwData &Append (const unsigned long long &x);
141 mwData &Retrieve (unsigned long long &x);
142 #endif // mwLONGLONG
143 mwData &Append (const char &x);
144 mwData &Retrieve (char &x);
145 mwData &Append (const unsigned char &x);
146 mwData &Retrieve (unsigned char &x);
147 #if mwBOOL
148 mwData &Append (const bool &x);
149 mwData &Retrieve (bool &x);
150 #endif // mwBOOL
151 mwData &Append (const float &x);
152 mwData &Retrieve (float &x);
153 mwData &Append (const double &x);
154 mwData &Retrieve (double &x);
155 #if mwSTRING
156 mwData &Append (const std::string &x);
157 mwData &Retrieve (std::string &x);
158 #endif // mwSTRING
159 mwData &Append (const char *x);
160 mwData &Retrieve (char *x);
161 mwData &Append (const unsigned char *x);
162 mwData &Retrieve (unsigned char *x);
163
165 mwData &SkipString ();
166
167private:
169 unsigned char *buf;
170
172 int len;
173
176
178 int pos;
179
182 void IncreaseBuffer (int n);
183
185 mwData &AppendBytes (const unsigned char *x, int n);
186
189 mwData &RetrieveBytes (unsigned char *x, int n);
190
191}; /* class mwData */
192
193// --------------------------------------------------
194
196{
197 buf = (unsigned char *) 0;
198 len = 0;
199 allocated = 0;
200 pos = 0;
201 return;
202} /* mwData::mwData */
203
204inline mwData::mwData (const mwData &x)
205{
206 len = x. len;
207 allocated = x. allocated;
208 pos = x. pos;
209 if (allocated)
210 {
211 buf = new unsigned char [allocated];
212 if (!buf)
213 throw "No memory for mwData copying constructor.";
214 for (int i = 0; i < len; ++ i)
215 buf [i] = x. buf [i];
216 }
217 else
218 buf = (unsigned char *) 0;
219 return;
220} /* mwData::mwData */
221
223{
224 if (this == &x)
225 return *this;
226 if (buf)
227 delete [] buf;
228 len = x. len;
229 allocated = x. allocated;
230 pos = x. pos;
231 if (allocated)
232 {
233 buf = new unsigned char [allocated];
234 if (!buf)
235 throw "No memory for mwData assignment operator.";
236 for (int i = 0; i < len; ++ i)
237 buf [i] = x. buf [i];
238 }
239 else
240 buf = (unsigned char *) 0;
241 return *this;
242} /* mwData::operator = */
243
245{
246 // swap the buffers
247 unsigned char *buf0;
248 buf0 = buf;
249 buf = x. buf;
250 x. buf = buf0;
251
252 // swap the numbers
253 int number;
254 number = pos;
255 pos = x. pos;
256 x. pos = number;
257 number = len;
258 len = x. len;
259 x. len = number;
260 number = allocated;
261 allocated = x. allocated;
262 x. allocated = number;
263
264 return *this;
265} /* mwData::Swap */
266
267inline void swap (mwData &x, mwData &y)
268{
269 x. Swap (y);
270 return;
271} /* swap */
272
274{
275 if (buf)
276 delete [] buf;
277 buf = x. buf;
278 x. buf = 0;
279
280 pos = x. pos;
281 x. pos = 0;
282
283 len = x. len;
284 x. len = 0;
285
286 allocated = x. allocated;
287 x. allocated = 0;
288
289 return *this;
290} /* mwData::Take */
291
292inline mwData &mwData::Take (unsigned char *buffer, int length)
293{
294 if (buf)
295 delete [] buf;
296 buf = buffer;
297 pos = 0;
298 len = length;
299 allocated = length;
300 return *this;
301} /* mwData::Take */
302
303inline mwData &mwData::Take (char *buffer, int length)
304{
305 return Take ((unsigned char *) buffer, length);
306} /* mwData::Take */
307
309{
310 if (buf)
311 delete [] buf;
312 return;
313} /* mwData::~mwData */
314
315inline const char *mwData::Buffer () const
316{
317 return reinterpret_cast<const char *> (buf);
318} /* mwData::Buffer */
319
320inline int mwData::Length () const
321{
322 return len;
323} /* mwData::Length */
324
325inline void mwData::IncreaseBuffer (int n)
326{
327 // if it is not necessary to increase the buffer, do nothing
328 if (len + n <= allocated)
329 return;
330
331 // allocate a new buffer
332 int allocated1 = allocated + allocated + n;
333 unsigned char *buf1 = new unsigned char [allocated1];
334 if (!buf1)
335 throw "Not enough memory to increase mwData buffer.";
336
337 // copy the previous buffer to the new one
338 for (int i = 0; i < len; ++ i)
339 buf1 [i] = buf [i];
340
341 // replace the old buffer with the new one
342 delete [] buf;
343 buf = buf1;
344 allocated = allocated1;
345 return;
346} /* mwData::IncreaseBuffer */
347
348inline mwData &mwData::AppendBytes (const unsigned char *x, int n)
349{
350 // increase the buffer if necessary
351 IncreaseBuffer (n);
352
353 // if the system is big-endian, the bytes must be copied directly
354 const int testnumber = 1;
355 if (!*((char *) &testnumber))
356 {
357 for (int i = 0; i < n; ++ i)
358 buf [len ++] = *(x ++);
359 }
360 // otherwise the bytes must be copied in the reverse order
361 else
362 {
363 x += n;
364 for (int i = 0; i < n; ++ i)
365 buf [len ++] = *(-- x);
366 }
367 return *this;
368} /* mwData::AppendBytes */
369
370inline mwData &mwData::RetrieveBytes (unsigned char *x, int n)
371{
372 // if there is not enough data in the buffer, do nothing
373 if (len - pos < n)
374 return *this;
375
376 // if the system is big-endian, the bytes must be just copied
377 const int testnumber = 1;
378 if (!*((char *) &testnumber))
379 {
380 for (int i = 0; i < n; ++ i)
381 *(x ++) = buf [pos ++];
382 }
383 // otherwise the bytes must be swapped
384 else
385 {
386 x += n;
387 for (int i = 0; i < n; ++ i)
388 *(-- x) = buf [pos ++];
389 }
390 return *this;
391} /* mwData::RetrieveBytes */
392
393inline mwData &mwData::Append (const unsigned char *x, int n)
394{
395 IncreaseBuffer (n);
396 for (int i = 0; i < n; ++ i)
397 buf [len ++] = *(x ++);
398 return *this;
399} /* mwData::Append */
400
401inline mwData &mwData::Append (const char *x, int n)
402{
403 return Append ((const unsigned char *) x, n);
404} /* mwData::Append */
405
406/*
407inline mwData::operator const unsigned char * () const
408{
409 return buf;
410}*/ /* mwData::operator const unsigned char * */
411
412/*
413inline mwData::operator const char * () const
414{
415 return (const char *) buf;
416}*/ /* mwData::operator const char * */
417
418/*
419inline mwData::operator int () const
420{
421 return len;
422}*/ /* mwData::operator int */
423
424inline const char *mwData::Current () const
425{
426 return (const char *) (buf + pos);
427} /* mwData::Current */
428
429inline mwData &mwData::Append (const mwData &x)
430{
431 return Append (x. Buffer (), x. Length ());
432} /* mwData::Append */
433
435{
436 pos = 0;
437 return *this;
438} /* mwData::Rewind */
439
441{
442 len = 0;
443 pos = 0;
444 allocated = 0;
445 if (buf)
446 delete [] buf;
447 buf = 0;
448 return *this;
449} /* mwData::Reset */
450
451inline int mwData::Position (int newpos)
452{
453 if ((newpos >= 0) && (newpos <= len))
454 pos = newpos;
455 return pos;
456} /* mwData::Position */
457
458inline int mwData::Position () const
459{
460 return pos;
461} /* mwData::Position */
462
463// --------------------------------------------------
464
465inline mwData &mwData::Append (const int &x)
466{
467 IncreaseBuffer (4);
468 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
469 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
470 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
471 buf [len ++] = (unsigned char) (x & 0xFF);
472 return *this;
473} /* mwData::Append */
474
475inline mwData &mwData::Retrieve (int &x)
476{
477 if (len - pos < 4)
478 return *this;
479 x = (int) (buf [pos ++]) << 24;
480 x |= (int) (buf [pos ++]) << 16;
481 x |= (int) (buf [pos ++]) << 8;
482 x |= buf [pos ++];
483 return *this;
484} /* mwData::Retrieve */
485
486inline mwData &mwData::Append (const unsigned int &x)
487{
488 IncreaseBuffer (4);
489 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
490 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
491 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
492 buf [len ++] = (unsigned char) (x & 0xFF);
493 return *this;
494} /* mwData::Append */
495
496inline mwData &mwData::Retrieve (unsigned int &x)
497{
498 if (len - pos < 4)
499 return *this;
500 x = (int) (buf [pos ++]) << 24;
501 x |= (int) (buf [pos ++]) << 16;
502 x |= (int) (buf [pos ++]) << 8;
503 x |= buf [pos ++];
504 return *this;
505} /* mwData::Retrieve */
506
507inline mwData &mwData::Append (const short &x)
508{
509 IncreaseBuffer (2);
510 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
511 buf [len ++] = (unsigned char) (x & 0xFF);
512 return *this;
513} /* mwData::Append */
514
515inline mwData &mwData::Retrieve (short &x)
516{
517 if (len - pos < 2)
518 return *this;
519 x = (short) ((short) (buf [pos ++]) << 8);
520 x |= buf [pos ++];
521 return *this;
522} /* mwData::Retrieve */
523
524inline mwData &mwData::Append (const unsigned short &x)
525{
526 IncreaseBuffer (2);
527 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
528 buf [len ++] = (unsigned char) (x & 0xFF);
529 return *this;
530} /* mwData::Append */
531
532inline mwData &mwData::Retrieve (unsigned short &x)
533{
534 if (len - pos < 2)
535 return *this;
536 x = (unsigned short) ((unsigned short) (buf [pos ++]) << 8);
537 x |= buf [pos ++];
538 return *this;
539} /* mwData::Retrieve */
540
541inline mwData &mwData::Append (const long &x)
542{
543 IncreaseBuffer (8);
544#if (__LONG_MAX__ > 2147483647)
545 buf [len ++] = (unsigned char) (((unsigned long) x >> 56) & 0xFF);
546 buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
547 buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
548 buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
549 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
550#else
551 buf [len ++] = 0;
552 buf [len ++] = 0;
553 buf [len ++] = 0;
554 buf [len ++] = 0;
555 buf [len ++] = (unsigned char) (((unsigned long) x >> 24) & 0xFF);
556#endif
557 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
558 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
559 buf [len ++] = (unsigned char) (x & 0xFF);
560 return *this;
561} /* mwData::Append */
562
563inline mwData &mwData::Retrieve (long &x)
564{
565 if (len - pos < 8)
566 return *this;
567#if (__LONG_MAX__ > 2147483647)
568 x = (long) (buf [pos ++]) << 56;
569 x |= (long) (buf [pos ++]) << 48;
570 x |= (long) (buf [pos ++]) << 40;
571 x |= (long) (buf [pos ++]) << 32;
572 x |= (long) (buf [pos ++]) << 24;
573#else
574 pos += 4;
575 x = (long) (buf [pos ++]) << 24;
576#endif
577 x |= (long) (buf [pos ++]) << 16;
578 x |= (long) (buf [pos ++]) << 8;
579 x |= (long) (buf [pos ++]);
580 return *this;
581} /* mwData::Retrieve */
582
583inline mwData &mwData::Append (const unsigned long &x)
584{
585 IncreaseBuffer (8);
586#if (__LONG_MAX__ > 2147483647)
587 buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
588 buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
589 buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
590 buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
591#else
592 buf [len ++] = 0;
593 buf [len ++] = 0;
594 buf [len ++] = 0;
595 buf [len ++] = 0;
596#endif
597 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
598 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
599 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
600 buf [len ++] = (unsigned char) (x & 0xFF);
601 return *this;
602} /* mwData::Append */
603
604inline mwData &mwData::Retrieve (unsigned long &x)
605{
606 if (len - pos < 8)
607 return *this;
608#if (__LONG_MAX__ > 2147483647)
609 x = (long) (buf [pos ++]) << 56;
610 x |= (long) (buf [pos ++]) << 48;
611 x |= (long) (buf [pos ++]) << 40;
612 x |= (long) (buf [pos ++]) << 32;
613 x |= (long) (buf [pos ++]) << 24;
614#else
615 pos += 4;
616 x = (long) (buf [pos ++]) << 24;
617#endif
618 x |= (long) (buf [pos ++]) << 16;
619 x |= (long) (buf [pos ++]) << 8;
620 x |= (long) (buf [pos ++]);
621 return *this;
622} /* mwData::Retrieve */
623
624#if mwLONGLONG
625
626inline mwData &mwData::Append (const long long &x)
627{
628 IncreaseBuffer (8);
629 buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
630 buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
631 buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
632 buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
633 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
634 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
635 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
636 buf [len ++] = (unsigned char) (x & 0xFF);
637 return *this;
638} /* mwData::Append */
639
640inline mwData &mwData::Retrieve (long long &x)
641{
642 if (len - pos < 8)
643 return *this;
644 x = (long long) (buf [pos ++]) << 56;
645 x |= (long long) (buf [pos ++]) << 48;
646 x |= (long long) (buf [pos ++]) << 40;
647 x |= (long long) (buf [pos ++]) << 32;
648 x |= (long long) (buf [pos ++]) << 24;
649 x |= (long long) (buf [pos ++]) << 16;
650 x |= (long long) (buf [pos ++]) << 8;
651 x |= buf [pos ++];
652 return *this;
653} /* mwData::Retrieve */
654
655inline mwData &mwData::Append (const unsigned long long &x)
656{
657 IncreaseBuffer (8);
658 buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
659 buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
660 buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
661 buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
662 buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
663 buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
664 buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
665 buf [len ++] = (unsigned char) (x & 0xFF);
666 return *this;
667} /* mwData::Append */
668
669inline mwData &mwData::Retrieve (unsigned long long &x)
670{
671 if (len - pos < 8)
672 return *this;
673 x = (long long) (buf [pos ++]) << 56;
674 x |= (long long) (buf [pos ++]) << 48;
675 x |= (long long) (buf [pos ++]) << 40;
676 x |= (long long) (buf [pos ++]) << 32;
677 x |= (long long) (buf [pos ++]) << 24;
678 x |= (long long) (buf [pos ++]) << 16;
679 x |= (long long) (buf [pos ++]) << 8;
680 x |= buf [pos ++];
681 return *this;
682} /* mwData::Retrieve */
683
684#endif // mwLONGLONG
685
686inline mwData &mwData::Append (const char &x)
687{
688 IncreaseBuffer (1);
689 buf [len ++] = (unsigned char) x;
690 return *this;
691} /* mwData::Append */
692
693inline mwData &mwData::Retrieve (char &x)
694{
695 if (len - pos < 1)
696 return *this;
697 x = (char) (buf [pos ++]);
698 return *this;
699} /* mwData::Retrieve */
700
701inline mwData &mwData::Append (const unsigned char &x)
702{
703 IncreaseBuffer (1);
704 buf [len ++] = x;
705 return *this;
706} /* mwData::Append */
707
708inline mwData &mwData::Retrieve (unsigned char &x)
709{
710 if (len - pos < 1)
711 return *this;
712 x = buf [pos ++];
713 return *this;
714} /* mwData::Retrieve */
715
716#if mwBOOL
717
718inline mwData &mwData::Append (const bool &x)
719{
720 IncreaseBuffer (1);
721 buf [len ++] = x ? '\1' : '\0';
722 return *this;
723} /* mwData::Append */
724
725inline mwData &mwData::Retrieve (bool &x)
726{
727 if (len - pos < 1)
728 return *this;
729 x = buf [pos ++] ? true : false;
730 return *this;
731} /* mwData::Retrieve */
732
733#endif // mwBOOL
734
735inline mwData &mwData::Append (const float &x)
736{
737 IncreaseBuffer (4);
738 return AppendBytes ((const unsigned char *) &x, 4);
739} /* mwData::Append */
740
741inline mwData &mwData::Retrieve (float &x)
742{
743 if (len - pos < 4)
744 return *this;
745 return RetrieveBytes ((unsigned char *) &x, 4);
746} /* mwData::Retrieve */
747
748inline mwData &mwData::Append (const double &x)
749{
750 IncreaseBuffer (8);
751 return AppendBytes ((const unsigned char *) &x, 8);
752} /* mwData::Append */
753
754inline mwData &mwData::Retrieve (double &x)
755{
756 if (len - pos < 8)
757 return *this;
758 return RetrieveBytes ((unsigned char *) &x, 8);
759} /* mwData::Retrieve */
760
761#if mwSTRING
762
763inline mwData &mwData::Append (const std::string &x)
764{
765 const char *str = x. c_str ();
766 int length = 0;
767 while (str [length ++]);
768 IncreaseBuffer (length);
769 return Append (reinterpret_cast<const unsigned char *> (str),
770 length);
771} /* mwData::Append */
772
773inline mwData &mwData::Retrieve (std::string &x)
774{
775 int pos0 = pos;
776 while ((pos0 < len) && buf [pos0])
777 ++ pos0;
778 if (pos0 >= len)
779 return *this;
780 x = std::string (reinterpret_cast<const char *> (buf + pos));
781 pos = pos0 + 1;
782 return *this;
783} /* mwData::Retrieve */
784
785#endif // mwSTRING
786
787inline mwData &mwData::Append (const unsigned char *x)
788{
789 int length = 0;
790 while (x [length ++]);
791 IncreaseBuffer (length);
792 return Append (x, length);
793} /* mwData::Append */
794
795inline mwData &mwData::Retrieve (unsigned char *x)
796{
797 int pos0 = pos;
798 while ((pos0 < len) && buf [pos0])
799 ++ pos0;
800 if (pos0 >= len)
801 return *this;
802 while (pos <= pos0)
803 *(x ++) = buf [pos ++];
804 return *this;
805} /* mwData::Retrieve */
806
807inline mwData &mwData::Append (const char *x)
808{
809 return Append (reinterpret_cast<const unsigned char *> (x));
810} /* mwData::Append */
811
812inline mwData &mwData::Retrieve (char *x)
813{
814 return Retrieve ((unsigned char *) x);
815} /* mwData::Retrieve */
816
817// --------------------------------------------------
818
820{
821 while ((pos < len) && buf [pos])
822 ++ pos;
823 if (pos < len)
824 ++ pos;
825 return *this;
826} /* mwData::SkipString */
827
828// --------------------------------------------------
829
830template <class type>
831inline mwData &operator << (mwData &m, const type &x)
832{
833 return m. Append (x);
834} /* operator << */
835
836template <class type>
837inline mwData &operator >> (mwData &m, type &x)
838{
839 return m. Retrieve (x);
840} /* operator >> */
841
842inline mwData &operator << (mwData &m, const char *x)
843{
844 return m. Append (x);
845} /* operator << */
846
847inline mwData &operator << (mwData &m, const unsigned char *x)
848{
849 return m. Append (x);
850} /* operator << */
851
852inline mwData &operator >> (mwData &m, char *x)
853{
854 return m. Retrieve (x);
855} /* operator >> */
856
857inline mwData &operator >> (mwData &m, unsigned char *x)
858{
859 return m. Retrieve (x);
860} /* operator >> */
861
862#if mwSTREAMS
863inline std::ostream &operator << (std::ostream &s, const mwData &m)
864{
865 if (m. Length ())
866 s. write (m. Buffer (), m. Length ());
867 return s;
868} /* operator << */
869
870inline std::istream &operator >> (std::istream &s, mwData &m)
871{
872 char *buf = NULL;
873 int pos = 0, len = 0;
874
875 // read the entire stream to the given buffer
876 int ch = s. get ();
877 while (ch != EOF)
878 {
879 if (len <= pos)
880 {
881 len = pos + pos + 3;
882 char *newbuf = new char [len];
883 if (!newbuf)
884 break;
885 for (int i = 0; i < pos; ++ i)
886 newbuf [i] = buf [i];
887 delete [] buf;
888 buf = newbuf;
889 }
890 buf [pos ++] = (unsigned char) (ch);
891 ch = s. get ();
892 }
893
894 // allocate a new buffer of the exact size and take the data
895 if (pos)
896 {
897 char *newbuf = new char [pos];
898 for (int i = 0; i < pos; ++ i)
899 newbuf [i] = buf [i];
900 delete [] buf;
901 m. Take (newbuf, pos);
902 }
903
904 return s;
905} /* operator >> */
906#endif
907
908
909} // namespace multiwork
910} // namespace chomp
911
912#endif // _CHOMP_MULTIWORK_MWDATA_H_
913
915
This class is used to convert data structures into a single sequence of bytes and to retrieve this da...
Definition: mwdata.h:67
mwData & Swap(mwData &x)
Swaps the data with another data structure.
Definition: mwdata.h:244
mwData & Take(mwData &x)
Takes the data from another data structure and makes the other data empty.
Definition: mwdata.h:273
unsigned char * buf
The data buffer.
Definition: mwdata.h:169
mwData & SkipString()
Skips a zero-terminated string in the buffer.
Definition: mwdata.h:819
int Length() const
Returns the length of all the data in the buffer.
Definition: mwdata.h:320
int pos
The current read position in the buffer.
Definition: mwdata.h:178
mwData & Append(const char *buffer, int length)
Appends raw data to the buffer.
Definition: mwdata.h:401
int len
The length of the data in the buffer and the write position.
Definition: mwdata.h:172
mwData & Retrieve(int &x)
Definition: mwdata.h:475
int Position() const
Returns the current reading position in the buffer.
Definition: mwdata.h:458
const char * Current() const
Returns the currently pointed data in the buffer.
Definition: mwdata.h:424
~mwData()
The destructor.
Definition: mwdata.h:308
void IncreaseBuffer(int n)
Increases the buffer by the given number of bytes (or more) beyond the current position.
Definition: mwdata.h:325
const char * Buffer() const
Returns a pointer to the data buffer.
Definition: mwdata.h:315
mwData & Rewind()
Rewinds the buffer to the beginning for reading.
Definition: mwdata.h:434
int allocated
The length of the allocated buffer memory space.
Definition: mwdata.h:175
mwData & RetrieveBytes(unsigned char *x, int n)
Retrieve a data piece from the buffer and swaps bytes if necessary.
Definition: mwdata.h:370
mwData & Reset()
Forgets the buffer and makes the data empty.
Definition: mwdata.h:440
mwData & operator=(const mwData &x)
The assignment operator.
Definition: mwdata.h:222
mwData & AppendBytes(const unsigned char *x, int n)
Appends a data piece to the buffer and swaps bytes if necessary.
Definition: mwdata.h:348
mwData()
The default constructor of empty data.
Definition: mwdata.h:195
This file contains the basic configuration of the MultiWork module, mainly as a definition of a serie...
int write(std::ostream &out, const coordtype *c, int dim, char parenthesis=40, char closing=0)
Definition: pointset.h:2148
void swap(mwWorkerData &data1, mwWorkerData &data2)
Definition: mwcoord.h:108
mwData & operator>>(mwData &m, type &x)
Definition: mwdata.h:837
mwData & operator<<(mwData &m, const type &x)
Definition: mwdata.h:831
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51