Back to index

lightning-sunbird  0.9+nobinonly
morkSink.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _MDB_
00039 #include "mdb.h"
00040 #endif
00041 
00042 #ifndef _MORK_
00043 #include "mork.h"
00044 #endif
00045 
00046 #ifndef _MORKSINK_
00047 #include "morkSink.h"
00048 #endif
00049 
00050 #ifndef _MORKENV_
00051 #include "morkEnv.h"
00052 #endif
00053 
00054 #ifndef _MORKBLOB_
00055 #include "morkBlob.h"
00056 #endif
00057 
00058 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00059 
00060 /*virtual*/ morkSink::~morkSink()
00061 {
00062   mSink_At = 0;
00063   mSink_End = 0;
00064 }
00065 
00066 /*virtual*/ void
00067 morkSpool::FlushSink(morkEnv* ev) // sync mSpool_Coil->mBuf_Fill
00068 {
00069   morkCoil* coil = mSpool_Coil;
00070   if ( coil )
00071   {
00072     mork_u1* body = (mork_u1*) coil->mBuf_Body;
00073     if ( body )
00074     {
00075       mork_u1* at = mSink_At;
00076       mork_u1* end = mSink_End;
00077       if ( at >= body && at <= end ) // expected cursor order?
00078       {
00079         mork_fill fill = (mork_fill) (at - body); // current content size
00080         if ( fill <= coil->mBlob_Size )
00081           coil->mBuf_Fill = fill;
00082         else
00083         {
00084           coil->BlobFillOverSizeError(ev);
00085           coil->mBuf_Fill = coil->mBlob_Size; // make it safe
00086         }
00087       }
00088       else
00089         this->BadSpoolCursorOrderError(ev);
00090     }
00091     else
00092       coil->NilBufBodyError(ev);
00093   }
00094   else
00095     this->NilSpoolCoilError(ev);
00096 }
00097 
00098 /*virtual*/ void
00099 morkSpool::SpillPutc(morkEnv* ev, int c) // grow coil and write byte
00100 {
00101   morkCoil* coil = mSpool_Coil;
00102   if ( coil )
00103   {
00104     mork_u1* body = (mork_u1*) coil->mBuf_Body;
00105     if ( body )
00106     {
00107       mork_u1* at = mSink_At;
00108       mork_u1* end = mSink_End;
00109       if ( at >= body && at <= end ) // expected cursor order?
00110       {
00111         mork_size size = coil->mBlob_Size;
00112         mork_fill fill = (mork_fill) (at - body); // current content size
00113         if ( fill <= size ) // less content than medium size?
00114         {
00115           coil->mBuf_Fill = fill;
00116           if ( at >= end ) // need to grow the coil?
00117           {
00118             if ( size > 2048 ) // grow slower over 2K?
00119               size += 512;
00120             else
00121             {
00122               mork_size growth = ( size * 4 ) / 3; // grow by 33%
00123               if ( growth < 64 ) // grow faster under (64 * 3)?
00124                 growth = 64;
00125               size += growth;
00126             }
00127             if ( coil->GrowCoil(ev, size) ) // made coil bigger?
00128             {
00129               body = (mork_u1*) coil->mBuf_Body;
00130               if ( body ) // have a coil body?
00131               {
00132                 mSink_At = at = body + fill;
00133                 mSink_End = end = body + coil->mBlob_Size;
00134               }
00135               else
00136                 coil->NilBufBodyError(ev);
00137             }
00138           }
00139           if ( ev->Good() ) // seem ready to write byte c?
00140           {
00141             if ( at < end ) // morkSink::Putc() would succeed?
00142             {
00143               *at++ = (mork_u1) c;
00144               mSink_At = at;
00145               coil->mBuf_Fill = fill + 1;
00146             }
00147             else
00148               this->BadSpoolCursorOrderError(ev);
00149           }
00150         }
00151         else // fill exceeds size
00152         {
00153           coil->BlobFillOverSizeError(ev);
00154           coil->mBuf_Fill = coil->mBlob_Size; // make it safe
00155         }
00156       }
00157       else
00158         this->BadSpoolCursorOrderError(ev);
00159     }
00160     else
00161       coil->NilBufBodyError(ev);
00162   }
00163   else
00164     this->NilSpoolCoilError(ev);
00165 }
00166 
00167 // ````` ````` ````` `````   ````` ````` ````` `````  
00168 // public: // public non-poly morkSink methods
00169 
00170 /*virtual*/
00171 morkSpool::~morkSpool()
00172 // Zero all slots to show this sink is disabled, but destroy no memory.
00173 // Note it is typically unnecessary to flush this coil sink, since all
00174 // content is written directly to the coil without any buffering.
00175 {
00176   mSink_At = 0;
00177   mSink_End = 0;
00178   mSpool_Coil = 0;
00179 }
00180 
00181 morkSpool::morkSpool(morkEnv* ev, morkCoil* ioCoil)
00182 // After installing the coil, calls Seek(ev, 0) to prepare for writing.
00183 : morkSink()
00184 , mSpool_Coil( 0 )
00185 {
00186   mSink_At = 0; // set correctly later in Seek()
00187   mSink_End = 0; // set correctly later in Seek()
00188   
00189   if ( ev->Good() )
00190   {
00191     if ( ioCoil )
00192     {
00193       mSpool_Coil = ioCoil;
00194       this->Seek(ev, /*pos*/ 0);
00195     }
00196     else
00197       ev->NilPointerError();
00198   }
00199 }
00200 
00201 // ----- All boolean return values below are equal to ev->Good(): -----
00202 
00203 /*static*/ void
00204 morkSpool::BadSpoolCursorOrderError(morkEnv* ev)
00205 {
00206   ev->NewError("bad morkSpool cursor order");
00207 }
00208 
00209 /*static*/ void
00210 morkSpool::NilSpoolCoilError(morkEnv* ev)
00211 {
00212   ev->NewError("nil mSpool_Coil");
00213 }
00214 
00215 mork_bool
00216 morkSpool::Seek(morkEnv* ev, mork_pos inPos)
00217 // Changed the current write position in coil's buffer to inPos.
00218 // For example, to start writing the coil from scratch, use inPos==0.
00219 {
00220   morkCoil* coil = mSpool_Coil;
00221   if ( coil )
00222   {
00223     mork_size minSize = (mork_size) (inPos + 64);
00224     
00225     if ( coil->mBlob_Size < minSize )
00226       coil->GrowCoil(ev, minSize);
00227       
00228     if ( ev->Good() )
00229     {
00230       coil->mBuf_Fill = (mork_fill) inPos;
00231       mork_u1* body = (mork_u1*) coil->mBuf_Body;
00232       if ( body )
00233       {
00234         mSink_At = body + inPos;
00235         mSink_End = body + coil->mBlob_Size;
00236       }
00237       else
00238         coil->NilBufBodyError(ev);
00239     }
00240   }
00241   else
00242     this->NilSpoolCoilError(ev);
00243     
00244   return ev->Good();
00245 }
00246 
00247 mork_bool
00248 morkSpool::Write(morkEnv* ev, const void* inBuf, mork_size inSize)
00249 // write inSize bytes of inBuf to current position inside coil's buffer
00250 {
00251   // This method is conceptually very similar to morkStream::Write(),
00252   // and this code was written while looking at that method for clues.
00253  
00254   morkCoil* coil = mSpool_Coil;
00255   if ( coil )
00256   {
00257     mork_u1* body = (mork_u1*) coil->mBuf_Body;
00258     if ( body )
00259     {
00260       if ( inBuf && inSize ) // anything to write?
00261       {
00262         mork_u1* at = mSink_At;
00263         mork_u1* end = mSink_End;
00264         if ( at >= body && at <= end ) // expected cursor order?
00265         {
00266           // note coil->mBuf_Fill can be stale after morkSink::Putc():
00267           mork_pos fill = at - body; // current content size
00268           mork_num space = (mork_num) (end - at); // space left in body
00269           if ( space < inSize ) // not enough to hold write?
00270           {
00271             mork_size minGrowth = space + 16;
00272             mork_size minSize = coil->mBlob_Size + minGrowth;
00273             if ( coil->GrowCoil(ev, minSize) )
00274             {
00275               body = (mork_u1*) coil->mBuf_Body;
00276               if ( body )
00277               {
00278                 mSink_At = at = body + fill;
00279                 mSink_End = end = body + coil->mBlob_Size;
00280                 space = (mork_num) (end - at); // space left in body
00281               }
00282               else
00283                 coil->NilBufBodyError(ev);
00284             }
00285           }
00286           if ( ev->Good() )
00287           {
00288             if ( space >= inSize ) // enough room to hold write?
00289             {
00290               MORK_MEMCPY(at, inBuf, inSize); // into body
00291               mSink_At = at + inSize; // advance past written bytes
00292               coil->mBuf_Fill = fill + inSize; // "flush" to fix fill
00293             }
00294             else
00295               ev->NewError("insufficient morkSpool space");
00296           }
00297         }
00298         else
00299           this->BadSpoolCursorOrderError(ev);
00300       }
00301     }
00302     else
00303       coil->NilBufBodyError(ev);
00304   }
00305   else
00306     this->NilSpoolCoilError(ev);
00307   
00308   return ev->Good();
00309 }
00310 
00311 mork_bool
00312 morkSpool::PutString(morkEnv* ev, const char* inString)
00313 // call Write() with inBuf=inString and inSize=strlen(inString),
00314 // unless inString is null, in which case we then do nothing at all.
00315 {
00316   if ( inString )
00317   {
00318     mork_size size = MORK_STRLEN(inString);
00319     this->Write(ev, inString, size);
00320   }
00321   return ev->Good();
00322 }
00323 
00324 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789