mwdata.h

Go to the documentation of this file.
00001 
00002 
00003 
00013 
00014 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00015 //
00016 // This file is part of the Homology Library.  This library is free software;
00017 // you can redistribute it and/or modify it under the terms of the GNU
00018 // General Public License as published by the Free Software Foundation;
00019 // either version 2 of the License, or (at your option) any later version.
00020 //
00021 // This library is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 // GNU General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License along
00027 // with this software; see the file "license.txt".  If not, write to the
00028 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00029 // MA 02111-1307, USA.
00030 
00031 // Started on August 10, 2004. Last revision: January 25, 2010.
00032 
00033 
00034 #ifndef _CHOMP_MULTIWORK_MWDATA_H_
00035 #define _CHOMP_MULTIWORK_MWDATA_H_
00036 
00037 #include "chomp/multiwork/mwconfig.h"
00038 
00039 // include an appropriate header file for 'string' if necessary
00040 #if mwSTRING
00041         #include <string>
00042 #endif // mwSTRING
00043 
00044 // include an appropriate header for 'istream' and 'ostream' if necessary
00045 #if mwSTREAMS
00046         #include <iostream>
00047         #include <cstdio>
00048 #endif // use mwSTREAMS
00049 
00050 
00051 namespace chomp {
00052 namespace multiwork {
00053 
00054 // --------------------------------------------------
00055 // --------------------- mwData ---------------------
00056 // --------------------------------------------------
00057 
00067 class mwData
00068 {
00069 public:
00071         mwData ();
00072 
00074         ~mwData ();
00075 
00077         mwData (const mwData &x);
00078 
00080         mwData &operator = (const mwData &x);
00081 
00083         mwData &Swap (mwData &x);
00084 
00087         mwData &Take (mwData &x);
00088 
00090         mwData &Take (unsigned char *buffer, int length);
00091 
00093         mwData &Take (char *buffer, int length);
00094 
00096         const char *Buffer () const;
00097 
00099         int Length () const;
00100 
00102         mwData &Rewind ();
00103 
00105         mwData &Reset ();
00106 
00108         int Position (int newpos);
00109 
00111         int Position () const;
00112 
00114         mwData &Append (const char *buffer, int length);
00115 
00117         mwData &Append (const unsigned char *buffer, int length);
00118 
00120         mwData &Append (const mwData &x);
00121 
00123         const char *Current () const;
00124         
00125         // append and retrieve an object to the buffer
00126         mwData &Append (const int &x);
00127         mwData &Retrieve (int &x);
00128         mwData &Append (const unsigned int &x);
00129         mwData &Retrieve (unsigned int &x);
00130         mwData &Append (const short &x);
00131         mwData &Retrieve (short &x);
00132         mwData &Append (const unsigned short &x);
00133         mwData &Retrieve (unsigned short &x);
00134         mwData &Append (const long &x);
00135         mwData &Retrieve (long &x);
00136         mwData &Append (const unsigned long &x);
00137         mwData &Retrieve (unsigned long &x);
00138         #if mwLONGLONG
00139         mwData &Append (const long long &x);
00140         mwData &Retrieve (long long &x);
00141         mwData &Append (const unsigned long long &x);
00142         mwData &Retrieve (unsigned long long &x);
00143         #endif // mwLONGLONG
00144         mwData &Append (const char &x);
00145         mwData &Retrieve (char &x);
00146         mwData &Append (const unsigned char &x);
00147         mwData &Retrieve (unsigned char &x);
00148         #if mwBOOL
00149         mwData &Append (const bool &x);
00150         mwData &Retrieve (bool &x);
00151         #endif // mwBOOL
00152         mwData &Append (const float &x);
00153         mwData &Retrieve (float &x);
00154         mwData &Append (const double &x);
00155         mwData &Retrieve (double &x);
00156         #if mwSTRING
00157         mwData &Append (const std::string &x);
00158         mwData &Retrieve (std::string &x);
00159         #endif // mwSTRING
00160         mwData &Append (const char *x);
00161         mwData &Retrieve (char *x);
00162         mwData &Append (const unsigned char *x);
00163         mwData &Retrieve (unsigned char *x);
00164 
00166         mwData &SkipString ();
00167 
00168 private:
00170         unsigned char *buf;
00171 
00173         int len;
00174                 
00176         int allocated;
00177 
00179         int pos;
00180 
00183         void IncreaseBuffer (int n);
00184         
00186         mwData &AppendBytes (const unsigned char *x, int n);
00187                 
00190         mwData &RetrieveBytes (unsigned char *x, int n);
00191 
00192 }; /* class mwData */
00193 
00194 // --------------------------------------------------
00195 
00196 inline mwData::mwData ()
00197 {
00198         buf = (unsigned char *) 0;
00199         len = 0;
00200         allocated = 0;
00201         pos = 0;
00202         return;
00203 } /* mwData::mwData */
00204 
00205 inline mwData::mwData (const mwData &x)
00206 {
00207         len = x. len;
00208         allocated = x. allocated;
00209         pos = x. pos;
00210         if (allocated)
00211         {
00212                 buf = new unsigned char [allocated];
00213                 if (!buf)
00214                         throw "No memory for mwData copying constructor.";
00215                 for (int i = 0; i < len; ++ i)
00216                         buf [i] = x. buf [i];
00217         }
00218         else
00219                 buf = (unsigned char *) 0;
00220         return;
00221 } /* mwData::mwData */
00222 
00223 inline mwData &mwData::operator = (const mwData &x)
00224 {
00225         if (this == &x)
00226                 return *this;
00227         if (buf)
00228                 delete [] buf;
00229         len = x. len;
00230         allocated = x. allocated;
00231         pos = x. pos;
00232         if (allocated)
00233         {
00234                 buf = new unsigned char [allocated];
00235                 if (!buf)
00236                         throw "No memory for mwData assignment operator.";
00237                 for (int i = 0; i < len; ++ i)
00238                         buf [i] = x. buf [i];
00239         }
00240         else
00241                 buf = (unsigned char *) 0;
00242         return *this;
00243 } /* mwData::operator = */
00244 
00245 inline mwData &mwData::Swap (mwData &x)
00246 {
00247         // swap the buffers
00248         unsigned char *buf0;
00249         buf0 = buf;
00250         buf = x. buf;
00251         x. buf = buf0;
00252 
00253         // swap the numbers
00254         int number;
00255         number = pos;
00256         pos = x. pos;
00257         x. pos = number;
00258         number = len;
00259         len = x. len;
00260         x. len = number;
00261         number = allocated;
00262         allocated = x. allocated;
00263         x. allocated = number;
00264         
00265         return *this;
00266 } /* mwData::Swap */
00267 
00268 inline void swap (mwData &x, mwData &y)
00269 {
00270         x. Swap (y);
00271         return;
00272 } /* swap */
00273 
00274 inline mwData &mwData::Take (mwData &x)
00275 {
00276         if (buf)
00277                 delete [] buf;
00278         buf = x. buf;
00279         x. buf = 0;
00280 
00281         pos = x. pos;
00282         x. pos = 0;
00283         
00284         len = x. len;
00285         x. len = 0;
00286         
00287         allocated = x. allocated;
00288         x. allocated = 0;
00289         
00290         return *this;
00291 } /* mwData::Take */
00292 
00293 inline mwData &mwData::Take (unsigned char *buffer, int length)
00294 {
00295         if (buf)
00296                 delete [] buf;
00297         buf = buffer;
00298         pos = 0;
00299         len = length;
00300         allocated = length;
00301         return *this;
00302 } /* mwData::Take */
00303 
00304 inline mwData &mwData::Take (char *buffer, int length)
00305 {
00306         return Take ((unsigned char *) buffer, length);
00307 } /* mwData::Take */
00308 
00309 inline mwData::~mwData ()
00310 {
00311         if (buf)
00312                 delete [] buf;
00313         return;
00314 } /* mwData::~mwData */
00315 
00316 inline const char *mwData::Buffer () const
00317 {
00318         return reinterpret_cast<const char *> (buf);
00319 } /* mwData::Buffer */
00320         
00321 inline int mwData::Length () const
00322 {
00323         return len;
00324 } /* mwData::Length */
00325         
00326 inline void mwData::IncreaseBuffer (int n)
00327 {
00328         // if it is not necessary to increase the buffer, do nothing
00329         if (len + n <= allocated)
00330                 return;
00331 
00332         // allocate a new buffer
00333         int allocated1 = allocated + allocated + n;
00334         unsigned char *buf1 = new unsigned char [allocated1];
00335         if (!buf1)
00336                 throw "Not enough memory to increase mwData buffer.";
00337 
00338         // copy the previous buffer to the new one
00339         for (int i = 0; i < len; ++ i)
00340                 buf1 [i] = buf [i];
00341                 
00342         // replace the old buffer with the new one
00343         delete [] buf;
00344         buf = buf1;
00345         allocated = allocated1;
00346         return;
00347 } /* mwData::IncreaseBuffer */
00348 
00349 inline mwData &mwData::AppendBytes (const unsigned char *x, int n)
00350 {
00351         // increase the buffer if necessary
00352         IncreaseBuffer (n);
00353 
00354         // if the system is big-endian, the bytes must be copied directly
00355         const int testnumber = 1;
00356         if (!*((char *) &testnumber))
00357         {
00358                 for (int i = 0; i < n; ++ i)
00359                         buf [len ++] = *(x ++);
00360         }
00361         // otherwise the bytes must be copied in the reverse order
00362         else
00363         {
00364                 x += n;
00365                 for (int i = 0; i < n; ++ i)
00366                         buf [len ++] = *(-- x);
00367         }
00368         return *this;
00369 } /* mwData::AppendBytes */
00370 
00371 inline mwData &mwData::RetrieveBytes (unsigned char *x, int n)
00372 {
00373         // if there is not enough data in the buffer, do nothing
00374         if (len - pos < n)
00375                 return *this;
00376 
00377         // if the system is big-endian, the bytes must be just copied
00378         const int testnumber = 1;
00379         if (!*((char *) &testnumber))
00380         {
00381                 for (int i = 0; i < n; ++ i)
00382                         *(x ++) = buf [pos ++];
00383         }
00384         // otherwise the bytes must be swapped
00385         else
00386         {
00387                 x += n;
00388                 for (int i = 0; i < n; ++ i)
00389                         *(-- x) = buf [pos ++];
00390         }
00391         return *this;
00392 } /* mwData::RetrieveBytes */
00393 
00394 inline mwData &mwData::Append (const unsigned char *x, int n)
00395 {
00396         IncreaseBuffer (n);
00397         for (int i = 0; i < n; ++ i)
00398                 buf [len ++] = *(x ++);
00399         return *this;
00400 } /* mwData::Append */
00401 
00402 inline mwData &mwData::Append (const char *x, int n)
00403 {
00404         return Append ((const unsigned char *) x, n);
00405 } /* mwData::Append */
00406 
00407 /*
00408 inline mwData::operator const unsigned char * () const
00409 {
00410         return buf;
00411 }*/ /* mwData::operator const unsigned char * */
00412 
00413 /*
00414 inline mwData::operator const char * () const
00415 {
00416         return (const char *) buf;
00417 }*/ /* mwData::operator const char * */
00418 
00419 /*
00420 inline mwData::operator int () const
00421 {
00422         return len;
00423 }*/ /* mwData::operator int */
00424 
00425 inline const char *mwData::Current () const
00426 {
00427         return (const char *) (buf + pos);
00428 } /* mwData::Current */
00429 
00430 inline mwData &mwData::Append (const mwData &x)
00431 {
00432         return Append (x. Buffer (), x. Length ());
00433 } /* mwData::Append */
00434 
00435 inline mwData &mwData::Rewind ()
00436 {
00437         pos = 0;
00438         return *this;
00439 } /* mwData::Rewind */
00440 
00441 inline mwData &mwData::Reset ()
00442 {
00443         len = 0;
00444         pos = 0;
00445         allocated = 0;
00446         if (buf)
00447                 delete [] buf;
00448         buf = 0;
00449         return *this;
00450 } /* mwData::Reset */
00451 
00452 inline int mwData::Position (int newpos)
00453 {
00454         if ((newpos >= 0) && (newpos <= len))
00455                 pos = newpos;
00456         return pos;
00457 } /* mwData::Position */
00458         
00459 inline int mwData::Position () const
00460 {
00461         return pos;
00462 } /* mwData::Position */
00463 
00464 // --------------------------------------------------
00465 
00466 inline mwData &mwData::Append (const int &x)
00467 {
00468         IncreaseBuffer (4);
00469         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00470         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00471         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00472         buf [len ++] = (unsigned char) (x & 0xFF);
00473         return *this;
00474 } /* mwData::Append */
00475 
00476 inline mwData &mwData::Retrieve (int &x)
00477 {
00478         if (len - pos < 4)
00479                 return *this;
00480         x = (int) (buf [pos ++]) << 24;
00481         x |= (int) (buf [pos ++]) << 16;
00482         x |= (int) (buf [pos ++]) << 8;
00483         x |= buf [pos ++];
00484         return *this;
00485 } /* mwData::Retrieve */
00486 
00487 inline mwData &mwData::Append (const unsigned int &x)
00488 {
00489         IncreaseBuffer (4);
00490         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00491         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00492         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00493         buf [len ++] = (unsigned char) (x & 0xFF);
00494         return *this;
00495 } /* mwData::Append */
00496 
00497 inline mwData &mwData::Retrieve (unsigned int &x)
00498 {
00499         if (len - pos < 4)
00500                 return *this;
00501         x = (int) (buf [pos ++]) << 24;
00502         x |= (int) (buf [pos ++]) << 16;
00503         x |= (int) (buf [pos ++]) << 8;
00504         x |= buf [pos ++];
00505         return *this;
00506 } /* mwData::Retrieve */
00507 
00508 inline mwData &mwData::Append (const short &x)
00509 {
00510         IncreaseBuffer (2);
00511         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00512         buf [len ++] = (unsigned char) (x & 0xFF);
00513         return *this;
00514 } /* mwData::Append */
00515 
00516 inline mwData &mwData::Retrieve (short &x)
00517 {
00518         if (len - pos < 2)
00519                 return *this;
00520         x = (short) ((short) (buf [pos ++]) << 8);
00521         x |= buf [pos ++];
00522         return *this;
00523 } /* mwData::Retrieve */
00524 
00525 inline mwData &mwData::Append (const unsigned short &x)
00526 {
00527         IncreaseBuffer (2);
00528         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00529         buf [len ++] = (unsigned char) (x & 0xFF);
00530         return *this;
00531 } /* mwData::Append */
00532 
00533 inline mwData &mwData::Retrieve (unsigned short &x)
00534 {
00535         if (len - pos < 2)
00536                 return *this;
00537         x = (unsigned short) ((unsigned short) (buf [pos ++]) << 8);
00538         x |= buf [pos ++];
00539         return *this;
00540 } /* mwData::Retrieve */
00541 
00542 inline mwData &mwData::Append (const long &x)
00543 {
00544         IncreaseBuffer (8);
00545 #if (__LONG_MAX__ > 2147483647)
00546         buf [len ++] = (unsigned char) (((unsigned long) x >> 56) & 0xFF);
00547         buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
00548         buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
00549         buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
00550         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00551 #else
00552         buf [len ++] = 0;
00553         buf [len ++] = 0;
00554         buf [len ++] = 0;
00555         buf [len ++] = 0;
00556         buf [len ++] = (unsigned char) (((unsigned long) x >> 24) & 0xFF);
00557 #endif
00558         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00559         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00560         buf [len ++] = (unsigned char) (x & 0xFF);
00561         return *this;
00562 } /* mwData::Append */
00563 
00564 inline mwData &mwData::Retrieve (long &x)
00565 {
00566         if (len - pos < 8)
00567                 return *this;
00568 #if (__LONG_MAX__ > 2147483647)
00569         x = (long) (buf [pos ++]) << 56;
00570         x |= (long) (buf [pos ++]) << 48;
00571         x |= (long) (buf [pos ++]) << 40;
00572         x |= (long) (buf [pos ++]) << 32;
00573         x |= (long) (buf [pos ++]) << 24;
00574 #else
00575         pos += 4;
00576         x = (long) (buf [pos ++]) << 24;
00577 #endif
00578         x |= (long) (buf [pos ++]) << 16;
00579         x |= (long) (buf [pos ++]) << 8;
00580         x |= (long) (buf [pos ++]);
00581         return *this;
00582 } /* mwData::Retrieve */
00583 
00584 inline mwData &mwData::Append (const unsigned long &x)
00585 {
00586         IncreaseBuffer (8);
00587 #if (__LONG_MAX__ > 2147483647)
00588         buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
00589         buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
00590         buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
00591         buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
00592 #else
00593         buf [len ++] = 0;
00594         buf [len ++] = 0;
00595         buf [len ++] = 0;
00596         buf [len ++] = 0;
00597 #endif
00598         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00599         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00600         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00601         buf [len ++] = (unsigned char) (x & 0xFF);
00602         return *this;
00603 } /* mwData::Append */
00604 
00605 inline mwData &mwData::Retrieve (unsigned long &x)
00606 {
00607         if (len - pos < 8)
00608                 return *this;
00609 #if (__LONG_MAX__ > 2147483647)
00610         x = (long) (buf [pos ++]) << 56;
00611         x |= (long) (buf [pos ++]) << 48;
00612         x |= (long) (buf [pos ++]) << 40;
00613         x |= (long) (buf [pos ++]) << 32;
00614         x |= (long) (buf [pos ++]) << 24;
00615 #else
00616         pos += 4;
00617         x = (long) (buf [pos ++]) << 24;
00618 #endif
00619         x |= (long) (buf [pos ++]) << 16;
00620         x |= (long) (buf [pos ++]) << 8;
00621         x |= (long) (buf [pos ++]);
00622         return *this;
00623 } /* mwData::Retrieve */
00624 
00625 #if mwLONGLONG
00626 
00627 inline mwData &mwData::Append (const long long &x)
00628 {
00629         IncreaseBuffer (8);
00630         buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
00631         buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
00632         buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
00633         buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
00634         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00635         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00636         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00637         buf [len ++] = (unsigned char) (x & 0xFF);
00638         return *this;
00639 } /* mwData::Append */
00640 
00641 inline mwData &mwData::Retrieve (long long &x)
00642 {
00643         if (len - pos < 8)
00644                 return *this;
00645         x = (long long) (buf [pos ++]) << 56;
00646         x |= (long long) (buf [pos ++]) << 48;
00647         x |= (long long) (buf [pos ++]) << 40;
00648         x |= (long long) (buf [pos ++]) << 32;
00649         x |= (long long) (buf [pos ++]) << 24;
00650         x |= (long long) (buf [pos ++]) << 16;
00651         x |= (long long) (buf [pos ++]) << 8;
00652         x |= buf [pos ++];
00653         return *this;
00654 } /* mwData::Retrieve */
00655 
00656 inline mwData &mwData::Append (const unsigned long long &x)
00657 {
00658         IncreaseBuffer (8);
00659         buf [len ++] = (unsigned char) ((x >> 56) & 0xFF);
00660         buf [len ++] = (unsigned char) ((x >> 48) & 0xFF);
00661         buf [len ++] = (unsigned char) ((x >> 40) & 0xFF);
00662         buf [len ++] = (unsigned char) ((x >> 32) & 0xFF);
00663         buf [len ++] = (unsigned char) ((x >> 24) & 0xFF);
00664         buf [len ++] = (unsigned char) ((x >> 16) & 0xFF);
00665         buf [len ++] = (unsigned char) ((x >> 8) & 0xFF);
00666         buf [len ++] = (unsigned char) (x & 0xFF);
00667         return *this;
00668 } /* mwData::Append */
00669 
00670 inline mwData &mwData::Retrieve (unsigned long long &x)
00671 {
00672         if (len - pos < 8)
00673                 return *this;
00674         x = (long long) (buf [pos ++]) << 56;
00675         x |= (long long) (buf [pos ++]) << 48;
00676         x |= (long long) (buf [pos ++]) << 40;
00677         x |= (long long) (buf [pos ++]) << 32;
00678         x |= (long long) (buf [pos ++]) << 24;
00679         x |= (long long) (buf [pos ++]) << 16;
00680         x |= (long long) (buf [pos ++]) << 8;
00681         x |= buf [pos ++];
00682         return *this;
00683 } /* mwData::Retrieve */
00684 
00685 #endif // mwLONGLONG
00686 
00687 inline mwData &mwData::Append (const char &x)
00688 {
00689         IncreaseBuffer (1);
00690         buf [len ++] = (unsigned char) x;
00691         return *this;
00692 } /* mwData::Append */
00693 
00694 inline mwData &mwData::Retrieve (char &x)
00695 {
00696         if (len - pos < 1)
00697                 return *this;
00698         x = (char) (buf [pos ++]);
00699         return *this;
00700 } /* mwData::Retrieve */
00701 
00702 inline mwData &mwData::Append (const unsigned char &x)
00703 {
00704         IncreaseBuffer (1);
00705         buf [len ++] = x;
00706         return *this;
00707 } /* mwData::Append */
00708 
00709 inline mwData &mwData::Retrieve (unsigned char &x)
00710 {
00711         if (len - pos < 1)
00712                 return *this;
00713         x = buf [pos ++];
00714         return *this;
00715 } /* mwData::Retrieve */
00716 
00717 #if mwBOOL
00718 
00719 inline mwData &mwData::Append (const bool &x)
00720 {
00721         IncreaseBuffer (1);
00722         buf [len ++] = x ? '\1' : '\0';
00723         return *this;
00724 } /* mwData::Append */
00725 
00726 inline mwData &mwData::Retrieve (bool &x)
00727 {
00728         if (len - pos < 1)
00729                 return *this;
00730         x = buf [pos ++] ? true : false;
00731         return *this;
00732 } /* mwData::Retrieve */
00733 
00734 #endif // mwBOOL
00735 
00736 inline mwData &mwData::Append (const float &x)
00737 {
00738         IncreaseBuffer (4);
00739         return AppendBytes ((const unsigned char *) &x, 4);
00740 } /* mwData::Append */
00741 
00742 inline mwData &mwData::Retrieve (float &x)
00743 {
00744         if (len - pos < 4)
00745                 return *this;
00746         return RetrieveBytes ((unsigned char *) &x, 4);
00747 } /* mwData::Retrieve */
00748 
00749 inline mwData &mwData::Append (const double &x)
00750 {
00751         IncreaseBuffer (8);
00752         return AppendBytes ((const unsigned char *) &x, 8);
00753 } /* mwData::Append */
00754 
00755 inline mwData &mwData::Retrieve (double &x)
00756 {
00757         if (len - pos < 8)
00758                 return *this;
00759         return RetrieveBytes ((unsigned char *) &x, 8);
00760 } /* mwData::Retrieve */
00761 
00762 #if mwSTRING
00763 
00764 inline mwData &mwData::Append (const std::string &x)
00765 {
00766         const char *str = x. c_str ();
00767         int length = 0;
00768         while (str [length ++]);
00769         IncreaseBuffer (length);
00770         return Append (reinterpret_cast<const unsigned char *> (str),
00771                 length);
00772 } /* mwData::Append */
00773 
00774 inline mwData &mwData::Retrieve (std::string &x)
00775 {
00776         int pos0 = pos;
00777         while ((pos0 < len) && buf [pos0])
00778                 ++ pos0;
00779         if (pos0 >= len)
00780                 return *this;
00781         x = std::string (reinterpret_cast<const char *> (buf + pos));
00782         pos = pos0 + 1;
00783         return *this;
00784 } /* mwData::Retrieve */
00785 
00786 #endif // mwSTRING
00787 
00788 inline mwData &mwData::Append (const unsigned char *x)
00789 {
00790         int length = 0;
00791         while (x [length ++]);
00792         IncreaseBuffer (length);
00793         return Append (x, length);
00794 } /* mwData::Append */
00795 
00796 inline mwData &mwData::Retrieve (unsigned char *x)
00797 {
00798         int pos0 = pos;
00799         while ((pos0 < len) && buf [pos0])
00800                 ++ pos0;
00801         if (pos0 >= len)
00802                 return *this;
00803         while (pos <= pos0)
00804                 *(x ++) = buf [pos ++];
00805         return *this;
00806 } /* mwData::Retrieve */
00807 
00808 inline mwData &mwData::Append (const char *x)
00809 {
00810         return Append (reinterpret_cast<const unsigned char *> (x));
00811 } /* mwData::Append */
00812 
00813 inline mwData &mwData::Retrieve (char *x)
00814 {
00815         return Retrieve ((unsigned char *) x);
00816 } /* mwData::Retrieve */
00817 
00818 // --------------------------------------------------
00819 
00820 inline mwData &mwData::SkipString ()
00821 {
00822         while ((pos < len) && buf [pos])
00823                 ++ pos;
00824         if (pos < len)
00825                 ++ pos;
00826         return *this;
00827 } /* mwData::SkipString */
00828 
00829 // --------------------------------------------------
00830 
00831 template <class type>
00832 inline mwData &operator << (mwData &m, const type &x)
00833 {
00834         return m. Append (x);
00835 } /* operator << */
00836 
00837 template <class type>
00838 inline mwData &operator >> (mwData &m, type &x)
00839 {
00840         return m. Retrieve (x);
00841 } /* operator >> */
00842 
00843 inline mwData &operator << (mwData &m, const char *x)
00844 {
00845         return m. Append (x);
00846 } /* operator << */
00847 
00848 inline mwData &operator << (mwData &m, const unsigned char *x)
00849 {
00850         return m. Append (x);
00851 } /* operator << */
00852 
00853 inline mwData &operator >> (mwData &m, char *x)
00854 {
00855         return m. Retrieve (x);
00856 } /* operator >> */
00857 
00858 inline mwData &operator >> (mwData &m, unsigned char *x)
00859 {
00860         return m. Retrieve (x);
00861 } /* operator >> */
00862 
00863 #if mwSTREAMS
00864 inline std::ostream &operator << (std::ostream &s, const mwData &m)
00865 {
00866         if (m. Length ())
00867                 s. write (m. Buffer (), m. Length ());
00868         return s;
00869 } /* operator << */
00870 
00871 inline std::istream &operator >> (std::istream &s, mwData &m)
00872 {
00873         char *buf = NULL;
00874         int pos = 0, len = 0;
00875 
00876         // read the entire stream to the given buffer
00877         int ch = s. get ();
00878         while (ch != EOF)
00879         {
00880                 if (len <= pos)
00881                 {
00882                         len = pos + pos + 3;
00883                         char *newbuf = new char [len];
00884                         if (!newbuf)
00885                                 break;
00886                         for (int i = 0; i < pos; ++ i)
00887                                 newbuf [i] = buf [i];
00888                         delete [] buf;
00889                         buf = newbuf;
00890                 }
00891                 buf [pos ++] = (unsigned char) (ch);
00892                 ch = s. get ();
00893         }
00894 
00895         // allocate a new buffer of the exact size and take the data
00896         if (pos)
00897         {
00898                 char *newbuf = new char [pos];
00899                 for (int i = 0; i < pos; ++ i)
00900                         newbuf [i] = buf [i];
00901                 delete [] buf;
00902                 m. Take (newbuf, pos);
00903         }
00904 
00905         return s;
00906 } /* operator >> */
00907 #endif
00908 
00909 
00910 } // namespace multiwork
00911 } // namespace chomp
00912 
00913 #endif // _CHOMP_MULTIWORK_MWDATA_H_
00914 
00916