ccbhfile.cpp

Go to the documentation of this file.
00001 // $Id: ccbhfile.cpp 836 2006-04-18 16:06:15Z gerry $
00002 // Implementation of the CXaraTemplateFile class
00003 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00004 ================================XARAHEADERSTART===========================
00005  
00006                Xara LX, a vector drawing and manipulation program.
00007                     Copyright (C) 1993-2006 Xara Group Ltd.
00008        Copyright on certain contributions may be held in joint with their
00009               respective authors. See AUTHORS file for details.
00010 
00011 LICENSE TO USE AND MODIFY SOFTWARE
00012 ----------------------------------
00013 
00014 This file is part of Xara LX.
00015 
00016 Xara LX is free software; you can redistribute it and/or modify it
00017 under the terms of the GNU General Public License version 2 as published
00018 by the Free Software Foundation.
00019 
00020 Xara LX and its component source files are distributed in the hope
00021 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00022 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00023 See the GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License along
00026 with Xara LX (see the file GPL in the root directory of the
00027 distribution); if not, write to the Free Software Foundation, Inc., 51
00028 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00029 
00030 
00031 ADDITIONAL RIGHTS
00032 -----------------
00033 
00034 Conditional upon your continuing compliance with the GNU General Public
00035 License described above, Xara Group Ltd grants to you certain additional
00036 rights. 
00037 
00038 The additional rights are to use, modify, and distribute the software
00039 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00040 library and any other such library that any version of Xara LX relased
00041 by Xara Group Ltd requires in order to compile and execute, including
00042 the static linking of that library to XaraLX. In the case of the
00043 "CDraw" library, you may satisfy obligation under the GNU General Public
00044 License to provide source code by providing a binary copy of the library
00045 concerned and a copy of the license accompanying it.
00046 
00047 Nothing in this section restricts any of the rights you have under
00048 the GNU General Public License.
00049 
00050 
00051 SCOPE OF LICENSE
00052 ----------------
00053 
00054 This license applies to this program (XaraLX) and its constituent source
00055 files only, and does not necessarily apply to other Xara products which may
00056 in part share the same code base, and are subject to their own licensing
00057 terms.
00058 
00059 This license does not apply to files in the wxXtra directory, which
00060 are built into a separate library, and are subject to the wxWindows
00061 license contained within that directory in the file "WXXTRA-LICENSE".
00062 
00063 This license does not apply to the binary libraries (if any) within
00064 the "libs" directory, which are subject to a separate license contained
00065 within that directory in the file "LIBS-LICENSE".
00066 
00067 
00068 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00069 ----------------------------------------------
00070 
00071 Subject to the terms of the GNU Public License (see above), you are
00072 free to do whatever you like with your modifications. However, you may
00073 (at your option) wish contribute them to Xara's source tree. You can
00074 find details of how to do this at:
00075   http://www.xaraxtreme.org/developers/
00076 
00077 Prior to contributing your modifications, you will need to complete our
00078 contributor agreement. This can be found at:
00079   http://www.xaraxtreme.org/developers/contribute/
00080 
00081 Please note that Xara will not accept modifications which modify any of
00082 the text between the start and end of this header (marked
00083 XARAHEADERSTART and XARAHEADEREND).
00084 
00085 
00086 MARKS
00087 -----
00088 
00089 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00090 designs are registered or unregistered trademarks, design-marks, and/or
00091 service marks of Xara Group Ltd. All rights in these marks are reserved.
00092 
00093 
00094       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00095                         http://www.xara.com/
00096 
00097 =================================XARAHEADEREND============================
00098  */
00099 
00100 #include "camtypes.h"
00101 
00102 #include "ccbhfile.h"   // include the translating file proxy
00103 //#include "tim.h"      // For error IDs
00104 
00105 //-----------------------------------------------
00106 
00107 CC_IMPLEMENT_DYNAMIC(CCBinHexFile, CCLexFile);
00108 
00109 // This will get Camelot to display the filename and linenumber of any memory allocations
00110 // that are not released at program exit
00111 // Declare smart memory handling in Debug builds
00112 #define new CAM_DEBUG_NEW
00113 
00114 
00115 /********************************************************************************************
00116 
00117 >   virtual CCFile& CCBinHexFile::write(const void *buf, UINT32 length = 1)
00118 
00119     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00120     Created:    12/07/97
00121     Inputs:     buf - pointer to buffer to write
00122                 length - number of bytes to write
00123     Purpose:    Writes the buffer to the file
00124 
00125 ********************************************************************************************/
00126 
00127 CCFile& CCBinHexFile::write(const void *buf, UINT32 length)
00128 {
00129     // Number of bytes we can translate in one go (half the buffer)
00130     UINT32 Blocklen = BinHexMaxLineLength / 2;
00131     UINT32 Index;
00132     char* pPtr;
00133     BYTE* pSrc = (BYTE*)buf;
00134     BOOL bCRLF = (length > Blocklen);
00135 
00136     while (length > 0)
00137     {
00138         if (Blocklen > length)
00139             Blocklen = length;
00140 
00141         if (bCRLF)
00142         {
00143             if (m_pFile->write("\r\n", 2).fail())
00144             {
00145                 GotError(_R(IDE_FILE_WRITE_ERROR));
00146                 break;
00147             }
00148         }
00149 
00150         pPtr = (char*)m_Buffer;
00151 
00152         for (Index = 0; Index < Blocklen; Index++)
00153         {
00154             ByteToHex(*pSrc, pPtr);
00155             pSrc += 1;
00156             pPtr += 2;
00157         }
00158 
00159         if (m_pFile->write(m_Buffer, Blocklen * 2).fail())
00160         {
00161             GotError(_R(IDE_FILE_WRITE_ERROR));
00162             break;
00163         }
00164 
00165         length -= Blocklen;
00166     }
00167 
00168     return(*this);
00169 }
00170 
00171 
00172 /********************************************************************************************
00173 
00174 >   virtual CCFile& CCBinHexFile::write(const StringBase& buf, UINT32 length = 0)
00175 
00176     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00177     Created:    12/07/97
00178     Inputs:     buf - string reference to write
00179                 length - number of characters to write
00180     Purpose:    Writes the buffer to the file
00181 
00182 ********************************************************************************************/
00183 
00184 CCFile& CCBinHexFile::write(const StringBase& buf, UINT32 length)
00185 {
00186     if (length == 0)
00187         length = buf.Length();
00188 
00189 
00190 #if 0 != wxUSE_UNICODE
00191     size_t              cchTCSrc = camWcstombs( NULL, (const TCHAR *)buf, 0 ) + 1;
00192     PSTR                pTCSrc = PSTR( alloca( cchTCSrc ) );
00193     camWcstombs( pTCSrc, (const TCHAR *)buf, cchTCSrc );
00194 #else
00195     const char* pTCSrc = buf;
00196 #endif
00197 
00198     const char* pSrc = (const char*) pTCSrc;
00199     
00200     // Number of chars we can translate in one go (max half of buffer size)
00201     UINT32 Blocklen = BinHexMaxLineLength / 2;
00202     UINT32 Index;
00203     char* pPtr;
00204     BOOL bCRLF = (length > Blocklen);
00205 
00206     while (length > 0)
00207     {
00208         if (Blocklen > length)
00209             Blocklen = length;
00210 
00211         if (bCRLF)
00212         {
00213             if (m_pFile->write("\r\n", 2).fail())
00214             {
00215                 GotError(_R(IDE_FILE_WRITE_ERROR));
00216                 break;
00217             }
00218         }
00219 
00220         pPtr = (char*)m_Buffer;
00221 
00222         for (Index = 0; Index < Blocklen; Index++)
00223         {
00224             ByteToHex(*pSrc, pPtr);
00225             pSrc += 1;
00226             pPtr += 2;
00227         }
00228 
00229         if (m_pFile->write(m_Buffer, Blocklen * 2).fail())
00230         {
00231             GotError(_R(IDE_FILE_WRITE_ERROR));
00232             break;
00233         }
00234 
00235         length -= Blocklen;
00236     }
00237 
00238     return(*this);
00239 }
00240 
00241 
00242 /********************************************************************************************
00243 
00244 >   void CCBinHexFile::ByteToHex(BYTE b, char* pPtr)
00245 
00246     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00247     Created:    12/07/97
00248     Inputs:     b - byte to convert
00249     Outputs:    pPtr - pointer to at least two characters
00250     Purpose:    Translates the byte to hex (always writes two chars)
00251 
00252 ********************************************************************************************/
00253 
00254 void CCBinHexFile::ByteToHex(BYTE b, char* pPtr)
00255 {
00256     char nibble = (b >> 4);
00257     if (nibble > 9)
00258         nibble += 55;       // ('A' - 10)
00259     else
00260         nibble += '0';
00261     pPtr[0] = nibble;
00262     nibble = b & 0xF;
00263     if (nibble > 9)
00264         nibble += 55;       // ('A' - 10)
00265     else
00266         nibble += '0';
00267     pPtr[1] = nibble;
00268 }
00269 
00270 
00271 
00272 
00273 /********************************************************************************************
00274 
00275 >   virtual CCFile& CCBinHexFile::write(char& buf)
00276 
00277     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00278     Created:    12/07/97
00279     Inputs:     buf - char reference???
00280     Purpose:    Writes the char to the file
00281 
00282 ********************************************************************************************/
00283 
00284 
00285 CCFile& CCBinHexFile::write(char& buf)
00286 {
00287     char Trans[2];
00288 
00289     ByteToHex((BYTE)buf, Trans);
00290     
00291     return(m_pFile->write(Trans, 2));
00292 }

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