ccbuffil.cpp

Go to the documentation of this file.
00001 // $Id: ccbuffil.cpp 751 2006-03-31 15:43:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // 
00099 
00100 
00101 #include "camtypes.h"
00102 #include "bmpsrc.h"     // for OFFSET - put it in camtypes!
00103 #include "ccbuffil.h"
00104 
00105 
00106 // Place any IMPLEMENT type statements here
00107 CC_IMPLEMENT_DYNAMIC(CCBufferFile, CCStreamFile)
00108 
00109 
00110 // Declare smart memory handling in Debug builds
00111 // We want better memory tracking
00112 #define new CAM_DEBUG_NEW
00113 
00114 
00115 // Functions follow
00116 
00117 /********************************************************************************************
00118 
00119 >   CCBufferFile::CCBufferFile(CCLexFile* pFile, BOOL bReportErrors, BOOL bThrowExceptions)
00120 
00121     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00122     Created:    22/08/96
00123     Inputs:     pFile:              A pointer to the stream to use
00124                 bReportErrors:      See CCStreamFile for details
00125                 bThrowExceptions:   See CCStreamFile for details
00126     
00127     Purpose:    Provides objects representing in-memory files.
00128 
00129     Notes:      This class is only partially implemented to provide support for BitmapSource
00130 
00131 ********************************************************************************************/
00132 CCBufferFile::CCBufferFile(CCLexFile* pFile, BOOL bReportErrors, BOOL bThrowExceptions)
00133     : CCStreamFile(pFile->GetIOFile(), 0, bReportErrors, bThrowExceptions)
00134 {
00135     m_pTrueFile             = pFile;
00136 
00137     m_pBuffer               = NULL;
00138 
00139     m_pBufferFillPosition   = NULL;
00140     m_BytesToFillBuffer     = 0;
00141 
00142     m_pBufferWritePosition  = NULL;
00143     m_BytesToWrite          = 0;
00144 }
00145 
00146 
00147 
00148 /********************************************************************************************
00149 
00150 >   virtual CCBufferFile::~CCBufferFile()
00151 
00152     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00153     Created:    22/08/96
00154     Purpose:    Destructor - stops the base class destructor from deleting the stream we
00155                 borrowed
00156 
00157 ********************************************************************************************/
00158 CCBufferFile::~CCBufferFile()
00159 {
00160     IOFile = NULL;
00161 }
00162 
00163 /********************************************************************************************
00164 
00165 >   virtual void CCBufferFile::init(BYTE* pBuffer, FilePos size)
00166 
00167     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00168     Created:    22/08/96
00169     Purpose:    Initializes the buffer associated with this CCBufferFile
00170                 Having constructed a CCBufferFile object a user can initialize the buffer
00171                 associated with it. Useful for BitmapSources
00172 
00173 ********************************************************************************************/
00174 void CCBufferFile::init(BYTE* pBuffer, FilePos size)
00175 {
00176     m_pBuffer               = pBuffer;
00177 
00178     m_pBufferFillPosition   = pBuffer;
00179     m_BytesToFillBuffer     = size;
00180 
00181     m_pBufferWritePosition  = pBuffer;
00182     m_BytesToWrite          = size;
00183 }
00184 
00185 
00186 /********************************************************************************************
00187 
00188 >   virtual BOOL CCBufferFile::setMode(INT32 fileMode)
00189 
00190     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00191     Created:    22/08/96
00192     Returns:    TRUE
00193     Purpose:    Provides dummy implementation for pure function
00194 
00195 ********************************************************************************************/
00196 BOOL CCBufferFile::setMode(INT32 fileMode)
00197 {
00198     return TRUE;
00199 }
00200 
00201 
00202 /********************************************************************************************
00203 
00204 >   virtual CCFile& CCBufferFile::read(void* pBuffer, UINT32 length)
00205 
00206     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00207     Created:    22/08/96
00208     Returns:    A reference to this CCFile
00209     Purpose:    Attempts to read the given number of bytes into the given buffer while at
00210                 the same time builds up the whole stream in the buffer given in the
00211                 constructor or via init()
00212     Errors:     ERROR3 if internal buffer not initialized
00213 
00214 ********************************************************************************************/
00215 CCFile& CCBufferFile::read(void* pBuffer, UINT32 length)
00216 {
00217     // Read in as many bytes as the internal buffer can take up to a maximum of the
00218     // number given in length
00219     INT32 BytesToRead = m_BytesToFillBuffer > length ? length : m_BytesToFillBuffer;
00220 
00221     ERROR3IF(m_pBufferFillPosition == NULL, "m_pBufferFillPosition == NULL");
00222 
00223     m_pTrueFile->read((BYTE*)m_pBufferFillPosition, BytesToRead);
00224 
00225     // Find the number of bytes actually read
00226     INT32 BytesRead = IOFile->gcount();
00227 
00228     // Copy the read data to the given buffer if any
00229     if (pBuffer != NULL)
00230     {
00231         memcpy(pBuffer, m_pBufferFillPosition, BytesRead);
00232     }
00233 
00234     // Update the internal buffer details
00235     m_pBufferFillPosition += BytesRead;
00236     m_BytesToFillBuffer -= BytesRead;
00237 
00238     return *this;
00239 }
00240 
00241 
00242 
00243 /********************************************************************************************
00244 
00245 >   virtual BOOL CCBufferFile::bad() const
00246 
00247     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00248     Created:    22/08/96
00249     Returns:    TRUE if the CCBufferFile is bad
00250                 FALSE otherwise
00251     Purpose:    Mimics the functionality of the stream class bad() function
00252 
00253 ********************************************************************************************/
00254 /*BOOL CCBufferFile::bad() const
00255 {
00256     return IOFile->bad();
00257 }
00258 */
00259 
00260 /********************************************************************************************
00261 
00262 >   virtual BOOL CCBufferFile::fail() const
00263 
00264     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00265     Created:    22/08/96
00266     Returns:    TRUE if the CCBufferFile has failed
00267                 FALSE otherwise
00268     Purpose:    Mimics the functionality of the stream class fail() function
00269 
00270 ********************************************************************************************/
00271 /*BOOL CCBufferFile::fail() const
00272 {
00273     return IOFile->fail();
00274 }
00275 */
00276 
00277 /********************************************************************************************
00278 
00279 >   CCFile& CCBufferFile::write(const void* pBuffer, UINT32 length = 1)
00280 
00281     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00282     Created:    22/08/96
00283     Inputs:     pBuffer : Buffer to write from. If NULL internal buffer will be used
00284                 length : the number of bytes to write
00285     Returns:    A reference to this CCFile
00286     Purpose:    Attempts to write out the given number of bytes to the CCFile given in the
00287                 constructor.
00288                 Supplying a pBuffer will cause the data in the given buffer to be copied to
00289                 the internal buffer before it is written out. If the write to file is
00290                 unsuccessful therefore, the internal buffer can be considered corrupt (much
00291                 as the file would be).
00292     Errors:     ERROR3 if internal buffer not initialized
00293 
00294 ********************************************************************************************/
00295 CCFile& CCBufferFile::write(const void* pBuffer, UINT32 length)
00296 {
00297     // Write as many bytes as the internal buffer will allow up to a maximum of the
00298     // number given in length
00299     INT32 BytesToWrite = m_BytesToWrite > length ? length : m_BytesToWrite;
00300 
00301     ERROR3IF(m_pBufferWritePosition == NULL, "m_pBufferWritePosition == NULL");
00302 
00303     if (pBuffer != NULL)
00304     {
00305         // Overwrite the data in the internal buffer with that in the given buffer
00306         memcpy(m_pBufferWritePosition, pBuffer, BytesToWrite);
00307     }
00308     // Write out whatever's in that position
00309     m_pTrueFile->write((BYTE*)m_pBufferWritePosition, BytesToWrite);
00310         
00311 
00312     // Update the internal buffer details
00313     m_pBufferWritePosition += BytesToWrite;
00314     m_BytesToWrite -= BytesToWrite;
00315 
00316     return *this;
00317 }
00318 
00319 
00320 /********************************************************************************************
00321 
00322 >   BOOL CCBufferFile::IsAllWritten() const
00323 
00324     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00325     Created:    22/08/96
00326     Returns:    TRUE : if all bytes in the buffer have been written
00327                 FALSE : otherwise
00328     Purpose:    Determines whether write() has been called sufficiently often to have
00329                 written out the whole buffer.
00330 
00331 ********************************************************************************************/
00332 BOOL CCBufferFile::IsAllWritten() const
00333 {
00334     return (m_BytesToWrite == 0);
00335 }

Generated on Sat Nov 10 03:44:38 2007 for Camelot by  doxygen 1.4.4