src/stream/ZStrim.h

00001 /*  @(#) $Id: ZStrim.h,v 1.21 2007/01/10 01:28:51 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2003 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZStrim__
00026 #define __ZStrim__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZUnicode.h"
00030 #include <stdexcept> // For range_error
00031 
00032 class ZStrimW;
00033 
00034 // =================================================================================================
00035 #pragma mark -
00036 #pragma mark * ZStrimR
00037 
00038 class ZStrimR
00039         {
00040 protected:      //@{
00045         ZStrimR() {}
00046         ~ZStrimR() {}
00047         ZStrimR(const ZStrimR&) {}
00048         ZStrimR& operator=(const ZStrimR&) { return *this; }
00050 
00051 
00052 public:
00053         class ExEndOfStrim;
00054         static void sThrowEndOfStrim();
00055         //@{
00058         bool ReadCP(UTF32& oCP) const;
00059         UTF32 ReadCP() const;
00061         
00062         //@{
00065         string32 ReadUTF32(size_t iCountCP) const;
00066         string16 ReadUTF16(size_t iCountCP) const;
00067         string8 ReadUTF8(size_t iCountCP) const;
00069 
00070         //@{
00073         void Read(UTF32* iDest, size_t iCount, size_t* oCount) const;
00074         void Read(UTF16* iDest, size_t iCountCU, size_t* oCountCU) const;
00075         void Read(UTF8* iDest, size_t iCountCU, size_t* oCountCU) const;
00077 
00078         //@{
00081         void Read(UTF16* iDest,
00082                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP) const;
00083 
00084         void Read(UTF8* iDest,
00085                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP) const;
00087 
00088         //@{
00092         void CopyAllTo(const ZStrimW& iStrimW) const;
00093         void CopyAllTo(const ZStrimW& iStrimW, size_t* oCountCPRead, size_t* oCountCPWritten) const;
00094 
00095         void CopyTo(const ZStrimW& iStrimW, size_t iCountCP) const;
00096 
00097         void CopyTo(const ZStrimW& iStrimW,
00098                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten) const;
00100 
00101         //@{
00105         void Skip(size_t iCountCP) const;
00106         void Skip(size_t iCountCP, size_t* oCountCPSkipped) const;
00107         void SkipAll() const;
00108         void SkipAll(size_t* oCountCPSkipped) const;
00110 
00111         //@{
00115         virtual void Imp_ReadUTF32(UTF32* iDest, size_t iCount, size_t* oCount) = 0;
00117 
00118         //@{
00123         virtual void Imp_ReadUTF16(UTF16* iDest,
00124                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00125 
00126         virtual void Imp_ReadUTF8(UTF8* iDest,
00127                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00128 
00129         virtual void Imp_CopyToDispatch(const ZStrimW& iStrimW,
00130                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00131 
00132         virtual void Imp_CopyTo(const ZStrimW& iStrimW,
00133                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00134 
00135         virtual void Imp_Skip(size_t iCountCP, size_t* oCountCPSkipped);
00137         };
00138 
00139 // =================================================================================================
00140 #pragma mark -
00141 #pragma mark * ZStrimR::ExEndOfStrim
00142 
00143 class ZStrimR::ExEndOfStrim : public std::range_error
00144         {
00145 public:
00146         ExEndOfStrim();
00147         };
00148 
00149 // =================================================================================================
00150 #pragma mark -
00151 #pragma mark * ZStrimU
00152 
00153 class ZStrimU : public ZStrimR
00154         {
00155 public:
00156         void Unread() const;
00157 
00158         virtual void Imp_Unread() = 0;
00159         };
00160 
00161 inline void ZStrimU::Unread() const
00162         { const_cast<ZStrimU*>(this)->Imp_Unread(); }
00163 
00164 // =================================================================================================
00165 #pragma mark -
00166 #pragma mark * ZStrimW
00167 
00168 class ZStrimW
00169         {
00170 protected:      //@{
00175         ZStrimW() {}
00176         ~ZStrimW() {}
00177         ZStrimW(const ZStrimW&) {}
00178         ZStrimW& operator=(const ZStrimW&) { return *this; }
00180 
00181 
00182 public:
00183         class ExEndOfStrim;
00184         static void sThrowEndOfStrim();
00185         //@{
00188         void WriteCP(UTF32 iCP) const;
00190 
00191         //@{
00194         void Write(const UTF32* iString) const;
00195         void Write(const UTF16* iString) const;
00196         void Write(const UTF8* iString) const;
00198 
00199         //@{
00202         void Write(const string32& iString) const;
00203         void Write(const string16& iString) const;
00204         void Write(const string8& iString) const;
00206 
00207         //@{
00210         void Write(const UTF32* iSource, size_t iCountCU) const;
00211         void Write(const UTF16* iSource, size_t iCountCU) const;
00212         void Write(const UTF8* iSource, size_t iCountCU) const;
00214 
00215         //@{
00218         void Write(const UTF32* iSource, size_t iCountCU, size_t* oCountCU) const;
00219         void Write(const UTF16* iSource, size_t iCountCU, size_t* oCountCU) const;
00220         void Write(const UTF8* iSource, size_t iCountCU, size_t* oCountCU) const;
00222 
00223         //@{
00226         void Write(const UTF32* iSource,
00227                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP) const;
00228 
00229         void Write(const UTF16* iSource,
00230                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP) const;
00231 
00232         void Write(const UTF8* iSource,
00233                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP) const;
00235 
00236         //@{
00239         size_t Writef(const UTF8* iString, ...) const;
00241 
00242         //@{
00246         void CopyAllFrom(const ZStrimR& iStrimR) const;
00247         void CopyAllFrom(const ZStrimR& iStrimR, size_t* oCountCPRead, size_t* oCountCPWritten) const;
00248 
00249         void CopyFrom(const ZStrimR& iStrimR, size_t iCountCP) const;
00250 
00251         void CopyFrom(const ZStrimR& iStrimR,
00252                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten) const;
00254 
00255         //@{
00258         void Flush() const;
00260 
00261         //@{
00265         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00266         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00267         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00269 
00270         //@{
00273         virtual void Imp_CopyFromDispatch(const ZStrimR& iStrimR,
00274                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00275 
00276         virtual void Imp_CopyFrom(const ZStrimR& iStrimR,
00277                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00278 
00279         virtual void Imp_Flush();
00281         };
00282 
00283 inline const ZStrimW& operator<<(const ZStrimW& s, const string32& iString)
00284         {
00285         s.Write(iString);
00286         return s;
00287         }
00288 
00289 inline const ZStrimW& operator<<(const ZStrimW& s, const UTF32* iString)
00290         {
00291         s.Write(iString);
00292         return s;
00293         }
00294 
00295 inline const ZStrimW& operator<<(const ZStrimW& s, const string16& iString)
00296         {
00297         s.Write(iString);
00298         return s;
00299         }
00300 
00301 inline const ZStrimW& operator<<(const ZStrimW& s, const UTF16* iString)
00302         {
00303         s.Write(iString);
00304         return s;
00305         }
00306 
00307 inline const ZStrimW& operator<<(const ZStrimW& s, const string8& iString)
00308         {
00309         s.Write(iString);
00310         return s;
00311         }
00312 
00313 inline const ZStrimW& operator<<(const ZStrimW& s, const UTF8* iString)
00314         {
00315         s.Write(iString);
00316         return s;
00317         }
00318 
00319 inline const ZStrimW& operator<<(const ZStrimW& s, const ZStrimR& r)
00320         {
00321         s.CopyAllFrom(r);
00322         return s;
00323         }
00324 
00325 // =================================================================================================
00326 #pragma mark -
00327 #pragma mark * ZStrimW::ExEndOfStrim
00328 
00329 class ZStrimW::ExEndOfStrim : public std::range_error
00330         {
00331 public:
00332         ExEndOfStrim();
00333         };
00334 
00335 // =================================================================================================
00336 #pragma mark -
00337 #pragma mark * ZStrimW_NativeUTF32
00338 
00345 class ZStrimW_NativeUTF32 : public ZStrimW
00346         {
00347 public:
00348         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00349         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU);
00350         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU);
00351         };
00352 
00353 // =================================================================================================
00354 #pragma mark -
00355 #pragma mark * ZStrimW_NativeUTF16
00356 
00363 class ZStrimW_NativeUTF16 : public ZStrimW
00364         {
00365 public:
00366         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU);
00367         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00368         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU);
00369         };
00370 
00371 // =================================================================================================
00372 #pragma mark -
00373 #pragma mark * ZStrimW_NativeUTF8
00374 
00381 class ZStrimW_NativeUTF8 : public ZStrimW
00382         {
00383 public:
00384         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU);
00385         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU);
00386         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU) = 0;
00387         };
00388 
00389 // =================================================================================================
00390 #pragma mark -
00391 #pragma mark * ZStrimR_Null
00392 
00394 
00395 class ZStrimR_Null : public ZStrimR
00396         {
00397 public:
00398         virtual void Imp_ReadUTF32(UTF32* iDest, size_t iCount, size_t* oCount);
00399 
00400         virtual void Imp_ReadUTF16(UTF16* iDest,
00401                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00402 
00403         virtual void Imp_ReadUTF8(UTF8* iDest,
00404                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00405 
00406         virtual void Imp_CopyToDispatch(const ZStrimW& iStrimW,
00407                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00408 
00409         virtual void Imp_CopyTo(const ZStrimW& iStrimW,
00410                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00411 
00412         virtual void Imp_Skip(size_t iCountCP, size_t* oCountCPSkipped);
00413         };
00414 
00415 // =================================================================================================
00416 #pragma mark -
00417 #pragma mark * ZStrimW_Null
00418 
00420 
00421 class ZStrimW_Null : public ZStrimW
00422         {
00423 public:
00424         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU);
00425         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU);
00426         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU);
00427 
00428         virtual void Imp_CopyFromDispatch(const ZStrimR& iStrimR,
00429                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00430 
00431         virtual void Imp_CopyFrom(const ZStrimR& iStrimR,
00432                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00433         };
00434 
00435 typedef ZStrimW_Null ZStrimW_Sink; // Old name.
00436 
00437 // =================================================================================================
00438 #pragma mark -
00439 #pragma mark * ZStrimU_Unreader
00440 
00442 
00443 class ZStrimU_Unreader : public ZStrimU
00444         {
00445 public:
00446         ZStrimU_Unreader(const ZStrimR& iStrimSource);
00447 
00448 // From ZStrimR via ZStrimU 
00449         virtual void Imp_ReadUTF32(UTF32* iDest, size_t iCount, size_t* oCount);
00450 
00451         virtual void Imp_ReadUTF16(UTF16* iDest,
00452                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00453 
00454         virtual void Imp_ReadUTF8(UTF8* iDest,
00455                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00456 
00457 // From ZStrimU
00458         virtual void Imp_Unread();
00459 
00460 private:
00461         const ZStrimR& fStrimSource;
00462         enum { eStateFresh, eStateNormal, eStateUnread, eStateHitEnd } fState;
00463         UTF32 fCP;
00464         };
00465 
00466 // =================================================================================================
00467 #pragma mark -
00468 #pragma mark * ZStrimU_String8
00469 
00471 
00472 class ZStrimU_String8 : public ZStrimU
00473         {
00474 public:
00475         ZStrimU_String8(const string8& iString);
00476         ~ZStrimU_String8();
00477 
00478 // From ZStrimR via ZStrimU
00479         virtual void Imp_ReadUTF32(UTF32* iDest, size_t iCount, size_t* oCount);
00480 
00481         virtual void Imp_ReadUTF16(UTF16* iDest,
00482                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00483 
00484         virtual void Imp_ReadUTF8(UTF8* iDest,
00485                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00486 
00487 // From ZStrimU
00488         virtual void Imp_Unread();
00489 
00490 private:
00491         string8 fString;
00492         size_t fPosition;
00493         };
00494 
00495 typedef ZStrimU_String8 ZStrimU_String;
00496 
00497 // =================================================================================================
00498 #pragma mark -
00499 #pragma mark * ZStrimU_String
00500 
00502 
00503 class ZStrimU_String32 : public ZStrimU
00504         {
00505 public:
00506         ZStrimU_String32(const string32& iString);
00507         ~ZStrimU_String32();
00508 
00509 // From ZStrimR via ZStrimU
00510         virtual void Imp_ReadUTF32(UTF32* iDest, size_t iCount, size_t* oCount);
00511 
00512         virtual void Imp_ReadUTF16(UTF16* iDest,
00513                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00514 
00515         virtual void Imp_ReadUTF8(UTF8* iDest,
00516                 size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP);
00517 
00518 // From ZStrimU
00519         virtual void Imp_Unread();
00520 
00521 private:
00522         string32 fString;
00523         size_t fPosition;
00524         };
00525 
00526 // =================================================================================================
00527 #pragma mark -
00528 #pragma mark * ZStrimW_String
00529 
00531 
00532 class ZStrimW_String : public ZStrimW_NativeUTF8
00533         {
00534 public:
00535         ZStrimW_String(string8& iString);
00536         ~ZStrimW_String();
00537 
00538         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU);
00539 
00540 private:
00541         string8& fString;
00542         };
00543 
00544 // =================================================================================================
00545 #pragma mark -
00546 #pragma mark * ZStrimW_Indirect
00547 
00548 class ZStrimW_Indirect : public ZStrimW
00549         {
00550 public:
00551         ZStrimW_Indirect();
00552         ZStrimW_Indirect(const ZStrimW& iStrimW);
00553         ~ZStrimW_Indirect();
00554 
00555 // From ZStrimW
00556         virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU);
00557         virtual void Imp_WriteUTF16(const UTF16* iSource, size_t iCountCU, size_t* oCountCU);
00558         virtual void Imp_WriteUTF8(const UTF8* iSource, size_t iCountCU, size_t* oCountCU);
00559 
00560         virtual void Imp_CopyFromDispatch(const ZStrimR& iStrimR,
00561                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00562 
00563         virtual void Imp_CopyFrom(const ZStrimR& iStrimR,
00564                 size_t iCountCP, size_t* oCountCPRead, size_t* oCountCPWritten);
00565 
00566         virtual void Imp_Flush();
00567 
00568 // Our protocol
00569         void SetSink(const ZStrimW& iStrimW);
00570         void SetSink(const ZStrimW* iStrimW);
00571 
00572 private:
00573         const ZStrimW* fSink;
00574         };
00575 
00576 #endif // __ZStrim__

Generated on Thu Jul 26 11:21:55 2007 for ZooLib by  doxygen 1.4.7