oilfiles.cpp

Go to the documentation of this file.
00001 // $Id: oilfiles.cpp 1413 2006-07-06 09:13:18Z 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     OilFiles.cpp    - Contains the function declarations of the WinOil CCFile derived classes.
00102     These include: CCResourceFile, CCResourceTextFile, CCClipBoardFile. 
00103 
00104 ********************************************************************************************/
00105 
00106 
00107 #include "camtypes.h"
00108 #include "oilfiles.h"
00109 //#include "ensure.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00110 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00111 //#include "mario.h"
00112 //#include "reshelp.h"
00113 #include "camelot.h"
00114 #include "oilmods.h"
00115 
00116 
00117 DECLARE_SOURCE("$Revision: 1413 $");
00118                         
00119 /********************************************************************************************
00120                                         CCResourceFile
00121 ********************************************************************************************/
00122 
00123 CCResourceFile::CCResourceFile()
00124 {
00125 }
00126 
00127 /********************************************************************************************
00128 
00129 >   CCResourceFile::CCResourceFile(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00130 
00131     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>. Fixed by Andy.
00132     Inputs:     UINT32 - Resource Identifier representing the Windows resource to be read in
00133                 UINT32 - Resource Type - String Id representing the type of Camelot resource
00134                 fProt - defines the files protection - default = read only
00135                 Use IsOpen to see if worked. If it didn't, then SetError will have been called
00136                 (but InformError not).
00137     Created:    9/9/93
00138     Purpose:    Constructs an instance of CCResourceFile and Opens it
00139 
00140 ********************************************************************************************/
00141 
00142 CCResourceFile::CCResourceFile(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00143 {
00144     // Load Resouce Name 
00145     String_256 ResNameID(ResourceId);
00146 
00147     // Load Resouce Type
00148     String_256 ResType(ResourceType);
00149 
00150     // then do the work - junks return value as IsOpen var is set in function
00151     open( ResNameID, ResType, fProt );
00152 }
00153 
00154 /********************************************************************************************
00155 
00156 >   CCResourceFile::~CCResourceFile()
00157 
00158     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00159     Created:    6/9/93        
00160     Purpose:    Default destructor.
00161 
00162 ********************************************************************************************/
00163 
00164 CCResourceFile::~CCResourceFile()
00165 {                   
00166     if (isOpen()) close();
00167 }
00168 
00169 
00170 
00171 #if 0 // def _DEBUG
00172     void BadResLoad( LPCTSTR ResName, LPCTSTR ResType, HINSTANCE inst )
00173     {
00174         char RealResName[20];
00175         char RealResType[20];
00176 
00177         if (HIWORD(ResName)==0)
00178         {
00179             wsprintf( RealResName, "<#%u>", (UINT32)LOWORD(ResName) );
00180             ResName = RealResName;
00181         }
00182 
00183         if (HIWORD(ResType)==0)
00184         {
00185             wsprintf( RealResType, "<#%u>", (UINT32)LOWORD(ResType) );
00186             ResType = RealResType;
00187         }
00188 
00189         #ifdef _MAC
00190         TRACE( _T("ResError name='%s' type='%s' inst=%x\n"), ResName, ResType, (INT32)inst );
00191         DebugBreak();
00192         #else
00193         ERROR3_PF( ("ResError name='%s' type='%s' inst=%x\n", ResName, ResType, (INT32)inst ) );
00194         #endif
00195     }
00196 #else
00197     #define BadResLoad( ResName, ResType, inst )
00198 #endif
00199 
00200 
00201 /********************************************************************************************
00202 
00203 >   BOOL CCResourceFile::open(LPCTSTR ResName, LPCTSTR ResType, FileAccess fProt = CCMemRead, HINSTANCE hinst = NULL)
00204 
00205     Author:     Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com>
00206     Inputs:     ResName - name of resource
00207                 ResType - type of resource
00208                 fProt - file access type (defaults to read)
00209                 hinst - where to get resource from (defaults to .exe)
00210     Returns:    TRUE if worked, FALSE if failed (SetError done to indicate error). Also
00211                 sets isOpen flag.
00212     Created:    15/9/94
00213     Purpose:    Lowest level code - see Win32 FindResource call for further details of parameters.
00214 
00215 ********************************************************************************************/
00216 
00217 BOOL CCResourceFile::open(LPCTSTR ResName, LPCTSTR WXUNUSED(ResType), FileAccess fProt, HINSTANCE WXUNUSED(inst) )
00218 {
00219     // We assume we'll fail, until we don't
00220     IsOpen = FALSE;
00221 
00222     // Prevent opening file in write mode for the present time
00223     if (fProt == CCMemWrite)
00224     {
00225         Error::SetError(_R(IDE_OPEN_WRITE), 0);
00226         return FALSE;
00227     }
00228 
00229     // Open file from zip FS
00230     CamResource         Resource;
00231     wxFSFile*           pFile = Resource.OpenwxFSFile( ResName );
00232     if( NULL == pFile )
00233     {
00234         Error::SetError(_R(IDE_LOAD_RES_ERROR), 0);
00235         return FALSE;
00236     }
00237     wxInputStream*      pStream = pFile->GetStream();
00238     if( NULL == pStream )
00239     {
00240         Error::SetError(_R(IDE_LOAD_RES_ERROR), 0);
00241         return FALSE;
00242     }
00243 
00244     // Setup house keeping, including allocation of buffer
00245     FileSize        = pStream->GetLength();
00246     MemHandle       = ClaimBlock( FileSize );
00247     if( BAD_MHANDLE == MemHandle )
00248     {
00249         FileSize = 0;
00250         return FALSE;
00251     }
00252     CurrentPos      = 0;    
00253     FileProt        = fProt;
00254     IsOpen          = TRUE;
00255 
00256     // Read into memory
00257     size_t cbDescSize;
00258     DescribeBlock( MemHandle, &MemFile, &cbDescSize );
00259     pStream->Read( MemFile, FileSize );
00260     
00261     // If we haven't read all the file, we must have failed
00262     return (pStream->LastRead() == FileSize);
00263 
00264 #if 0
00265     HRSRC       hResource;
00266 
00267     if (!inst)
00268     {
00269         if(ExtraDLLs[Resources_DLL] != 0)
00270             inst = ExtraDLLs[Resources_DLL];    // use the external resource dll if present
00271         else
00272             inst = 0; // AfxGetResourceHandle();        // use the exe otherwise
00273     }
00274 
00275     // load Camelot Resources
00276     if (hResource = FindResource(inst, ResName, ResType))
00277         ResMemHandle = LoadResource(inst, hResource);
00278 
00279     // If resource not found or could not be loaded
00280     if (!ResMemHandle)
00281     {
00282         BadResLoad( ResName, ResType, inst );
00283         // report error that file is not closed before destructor call
00284         Error::SetError(_R(IDE_LOAD_RES_ERROR), 0);
00285         return FALSE;
00286     }   
00287 
00288     // Lock Resource 
00289     MemFile = (BYTE*) LockResource(ResMemHandle);
00290 
00291     if (!MemFile)
00292     {
00293         BadResLoad( ResName, ResType, inst );
00294         // report error that file is not closed before destructor call
00295         Error::SetError(_R(IDE_LOAD_RES_ERROR), 0);
00296         return FALSE;
00297     }   
00298 
00299     CurrentPos      = 0;    
00300     FileSize        = (UINT32) SizeofResource(inst, hResource);
00301 
00302     // Check that SizeOfResource has worked
00303     if (FileSize == 0)
00304     {
00305         IsOpen = FALSE;
00306         Error::SetError(_R(IDE_RES_SIZE_ERROR), 0);
00307         return FALSE;
00308     }
00309 
00310     FileProt        = fProt;
00311 
00312     // Prevent opening file in write mode for the present time
00313     if (FileProt == CCMemWrite)
00314     {
00315         IsOpen = FALSE;
00316         Error::SetError(_R(IDE_OPEN_WRITE), 0);
00317         return FALSE;
00318     }
00319 
00320     IsOpen          = TRUE;
00321 
00322     return IsOpen; 
00323 #endif
00324 }
00325 
00326 /********************************************************************************************
00327 
00328 >   BOOL CCResourceFile::open(UINT32 ResourceId)
00329 
00330     Author:     Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com>
00331     Created:    20/12/94
00332     Inputs:     UINT32 - Resource ID of CUSTOM_RESOURCE that is needed to open (in read
00333                 mode) as a file. The module instance is calculated from the ID.
00334     Returns:    TRUE if file is opened successfully or FALSE otherwize
00335     Purpose:    Opens an instance of a CCResourceFile
00336 
00337 ********************************************************************************************/
00338 
00339 BOOL CCResourceFile::open(ResourceID ResourceId)
00340 {
00341     const TCHAR*        fn = CamResource::GetTextFail(ResourceId);
00342     return open( fn ? fn : CamResource::GetObjectName( ResourceId ), NULL, CCMemRead, NULL );
00343 }
00344 
00345 /********************************************************************************************
00346 
00347 >   BOOL CCResourceFile::open(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00348 
00349     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00350     Created:    9/9/93
00351     Inputs:     UINT32 - Resource Identifier representing the Windows resource to be read in
00352                 UINT32 - Resource Type - String Id representing the type of Camelot resource
00353                 fProt - defines the files protection - default = read only
00354     Returns:    TRUE if file is opened successfully or FALSE otherwize
00355     Purpose:    Opens an instance of a CCResourceFile
00356 
00357 ********************************************************************************************/
00358 
00359 
00360 BOOL CCResourceFile::open(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00361 {
00362     // Load Resouce Name 
00363     String_256 ResNameID(ResourceId);
00364 
00365     // Load Resouce Type
00366     String_256 ResType(ResourceType);
00367 
00368     // use main function to do work
00369     return open( ResNameID, ResType, fProt );
00370 }
00371 
00372 /********************************************************************************************
00373                                         CCResTextFile
00374 ********************************************************************************************/
00375 
00376 /********************************************************************************************
00377 
00378 >   CCResTextFile::CCResTextFile(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00379 
00380     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00381     Inputs:     UINT32 - Resource Identifier representing the Windows resource to be read in
00382                 UINT32 - Resource Type - String Id representing the type of Camelot resource
00383                 fProt - defines the files protection - default = read only
00384     Created:    9/9/93
00385     Purpose:    Constructs an instance of CCResTextFile and Opens it
00386 
00387 ********************************************************************************************/
00388 
00389 CCResTextFile::CCResTextFile(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00390  : CCResourceFile(ResourceId, ResourceType, fProt)
00391 {
00392 }
00393 
00394 /********************************************************************************************
00395 
00396 >   CCResTextFile::~CCResTextFile()
00397 
00398     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00399     Created:    6/9/93        
00400     Purpose:    Default destructor.
00401 
00402 ********************************************************************************************/
00403 
00404 CCResTextFile::~CCResTextFile()
00405 {                   
00406 }
00407 
00408 /********************************************************************************************
00409 
00410 >   BOOL CCResTextFile::open(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00411 
00412     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00413     Created:    9/9/93
00414     Inputs:     UINT32 - Resource Identifier representing the Windows resource to be read in
00415                 UINT32 - Resource Type - String Id representing the type of Camelot resource
00416                 fProt - defines the files protection - default = read only
00417     Returns:    TRUE if file is opened successfully or FALSE otherwize
00418     Purpose:    Opens an instance of a CCResTextFile
00419 
00420 ********************************************************************************************/
00421 
00422 BOOL CCResTextFile::open(UINT32 ResourceId, UINT32 ResourceType, FileAccess fProt)
00423 {
00424     return CCResourceFile::open(ResourceId, ResourceType, fProt);
00425 }
00426 
00427 BOOL CCResTextFile::open( UINT32 ResourceID )
00428 {
00429     return CCResourceFile::open( ResourceID );
00430 }
00431 
00432 /********************************************************************************************
00433 
00434 >   CCFile& CCResTextFile::read(TCHAR& buf)
00435 
00436     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00437     Created:    9/9/93
00438     Outputs;    character read in
00439     Return:     A reference to a memory file object
00440     Purpose:    Reads a single character from the Memory file 
00441 
00442 ********************************************************************************************/
00443 
00444 CCFile& CCResTextFile::read(TCHAR& buf)
00445 {
00446     // Make sure the file is open before it is read!
00447     if (!isOpen())
00448     {
00449         GotError(_R(IDE_NOTOPEN_ERROR));
00450         return (CCFile&) *this;
00451     }
00452 
00453     // if file is write protected then exit
00454     if (FileProt != CCMemRead)
00455     {
00456         GotError(_R(IDE_WRITE_ONLY));
00457         return (CCFile&) *this;
00458     }
00459 
00460     char* tempResFile   = (char*)MemFile;           // Cast MemFile to a TCHAR pointer
00461 
00462     if (!eof())
00463     {
00464 #if FALSE != wxUSE_UNICODE
00465         mbtowc( &buf, tempResFile + CurrentPos, 1 );
00466 #else
00467         buf = tempResFile[CurrentPos];
00468 #endif
00469         ++CurrentPos;
00470     }
00471     else
00472     {
00473         GotError(_R(IDE_EOF_ERROR));
00474     }
00475 
00476     return (CCFile&) *this;
00477 }
00478 
00479 
00480 
00481 /********************************************************************************************
00482 
00483 >   CCFile& CCResTextFile::read(StringBase* buf)
00484 
00485     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com> & Mark Neves
00486     Created:    6/9/93 (updated 26/4/94 by MarkN)
00487     Inputs:     buf - the String object to place the characters in.
00488     Outputs:    buf - the characters read from the file.
00489     Returns:    Reference to the CCFile object.
00490     Purpose:    Read in a string from the file.  The read operation stops at the first
00491                 newline, or when the string is full.  The newline is removed from the input
00492                 stream.
00493 
00494 ********************************************************************************************/
00495 
00496 CCFile& CCResTextFile::read(StringBase* buf)
00497 {
00498     // Make sure the file is open before it is read!
00499     if (!isOpen())
00500     {
00501         GotError(_R(IDE_NOTOPEN_ERROR));
00502         return (CCFile&) *this;
00503     }
00504 
00505     // if file is write protected then exit
00506     if (FileProt != CCMemRead)
00507     {
00508         GotError(_R(IDE_WRITE_ONLY));
00509         return (CCFile&) *this;
00510     }
00511 
00512     char* tempResFile   = (char*) MemFile;          // Cast MemFile to a TCHAR pointer
00513     TCHAR* pString      = *buf;                     // Cast input string to a TCHAR pointer
00514     UINT32 i = 0;
00515     
00516     UINT32 Len = buf->MaxLength();
00517 
00518     // Added check for '\n' on 26/4/94 by MarkN
00519 
00520     while ((i <= Len) && (!eof()) && (tempResFile[CurrentPos] != '\n') && (tempResFile[CurrentPos] != '\r'))
00521     {
00522 #if FALSE != wxUSE_UNICODE
00523         mbtowc( pString, tempResFile + CurrentPos, 1 );
00524         ++CurrentPos;
00525 #else
00526         *pString = tempResFile[CurrentPos++];
00527 #endif
00528         pString++;
00529         i++;
00530     }
00531 
00532     if ((tempResFile[CurrentPos] == '\n') || (tempResFile[CurrentPos] == '\r'))
00533     {
00534         CurrentPos++;
00535 
00536         // It is possible for the CR to be made up of a \r\n pair , so we have to look for this
00537         if ((!eof()) && ((tempResFile[CurrentPos] == '\n') || (tempResFile[CurrentPos] == '\r')))
00538         {
00539             CurrentPos++;
00540         }
00541     }
00542     
00543     *pString = 0;                           // Added 26/4/94 by MarkN
00544     
00545     return (CCFile&) *this;
00546 }
00547 
00548 
00549 /********************************************************************************************
00550 
00551 >   BOOL CCResTextFile::eof()
00552 
00553     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00554     Created:    6/9/93
00555     Return:     TRUE if end of file and FALSE otherwize
00556     Purpose:    Determines whether an instance of CCResTextFile is open
00557 
00558 ********************************************************************************************/
00559 
00560 BOOL CCResTextFile::eof() const
00561 {                                          
00562     char* tempResFile = (char*) MemFile;            // Cast MemFile to a char pointer
00563 
00564     // Changed the '==' to '>=' in the following check on CurrentPos (MarkN 9/5/94)
00565 
00566     if ((CurrentPos >= (FileSize - 1)) || (tempResFile[CurrentPos] == END_OF_FILE))
00567         return TRUE;
00568     else
00569         return FALSE;
00570 }
00571 
00572 
00573 /********************************************************************************************
00574 
00575 >   void CCResTextFile::close()
00576 
00577     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00578     Created:    6/9/93
00579     Purpose:    Closes an instance of a CCResTextFile
00580 
00581 ********************************************************************************************/
00582 
00583 void CCResTextFile::close()
00584 {   
00585     CCResourceFile::close();
00586 }
00587 
00588 /********************************************************************************************
00589                                         CCClipBoardFile
00590 ********************************************************************************************/
00591 
00592 /********************************************************************************************
00593 
00594 >   CCClipBoardFile::CCClipBoardFile(FileAccess fProt)
00595 
00596     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00597     Inputs:     fProt - defines the files protection - default = read only
00598     Created:    9/9/93
00599     Purpose:    Constructs an instance of CCClipBoardFile and Opens it
00600 
00601 ********************************************************************************************/
00602 
00603 PORTNOTE("clipboard","Removed CCClipBoardFile - MSW clipboard usage")
00604 #ifndef EXCLUDE_FROM_XARALX
00605 CCClipBoardFile::CCClipBoardFile(FileAccess fProt)
00606 {
00607     // Get Current Applications main window and apply the OpenClipboard() member 
00608     // function to it!
00609     ((AfxGetApp())->m_pMainWnd)->OpenClipboard();
00610     
00611     FileProt    = fProt;
00612 
00613     if (FileProt == CCMemRead)
00614     {
00615         // Check to see if there is any text in the clipboard
00616         if (IsClipboardFormatAvailable(CF_TEXT))
00617         {
00618             IsOpen = FALSE;
00619             Error::SetError(_R(IDE_EMPTY_CLIPBOARD), 0);
00620             return;
00621         }   
00622 
00623         // Get a handle to the clipboard data
00624         ClipBoardHandle = GetClipboardData(CF_TEXT);
00625     
00626         // Check if clipboard data could be found
00627         if (ClipBoardHandle == NULL)
00628         {
00629             IsOpen = FALSE;
00630             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00631             return;
00632         }   
00633     
00634         // Get a pointer to the clipboard data
00635         MemFile = (BYTE*) GlobalLock(ClipBoardHandle);
00636            
00637         // Check if memory to be occupied by data was available
00638         if (MemFile == NULL)
00639         {                  
00640             IsOpen = FALSE;
00641             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00642             return;
00643         }   
00644     
00645         // Get the size of the data
00646         FileSize    = (UINT32) GlobalSize(ClipBoardHandle);
00647     }
00648     else if (FileProt == CCMemWrite)
00649     {
00650         // Empty Clipboard
00651         EmptyClipboard();
00652 
00653         // Allocate Global Memory 
00654         ClipBoardHandle = GlobalAlloc(GHND, (DWORD) CCFILE_DEFAULTSIZE);
00655 
00656         // Check if Memory was allocated properly
00657         if (ClipBoardHandle == NULL)
00658         {
00659             IsOpen = FALSE;
00660             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00661             return;
00662         }   
00663 
00664         // Get a pointer to the clipboard
00665         MemFile = (BYTE*) GlobalLock(ClipBoardHandle);
00666            
00667         // Check if memory to be occupied by data was available
00668         if (MemFile == NULL)
00669         {                  
00670             IsOpen = FALSE;
00671             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00672             return;
00673         }   
00674         
00675         // Set to default size.
00676         FileSize = CCFILE_DEFAULTSIZE;
00677     } 
00678      
00679     CurrentPos  = 0;    
00680     IsOpen      = TRUE;
00681 }
00682 
00683 /********************************************************************************************
00684 
00685 >   CCClipBoardFile::~CCClipBoardFile()
00686 
00687     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00688     Created:    6/9/93        
00689     Purpose:    Default destructor.
00690 
00691 ********************************************************************************************/
00692 
00693 CCClipBoardFile::~CCClipBoardFile()
00694 {                   
00695     CCMemFile::~CCMemFile();
00696     
00697     // if the file has been left open then unlock memory and close clipboard
00698     if (isOpen())
00699     {
00700         // Unlock memory allocated
00701         GlobalUnlock(ClipBoardHandle);
00702                    
00703         // Write to clipboard - ClipBoardHandle must be unlocked before given to clipboard
00704         SetClipboardData(CF_TEXT, ClipBoardHandle);                   
00705                    
00706         // Close the clipboard
00707         CloseClipboard();
00708     }
00709 }
00710 
00711 /********************************************************************************************
00712 
00713 >   BOOL CCClipBoardFile::open(FileAccess fProt)
00714 
00715     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00716     Created:    20/9/93
00717     Inputs:     fProt - defines the files protection - default = read only
00718     Returns:    TRUE if file is opened ok and FALSE otherwize
00719     Purpose:    Constructs opens the clipboard for reading or writing.
00720 
00721 ********************************************************************************************/
00722 
00723 BOOL CCClipBoardFile::open(FileAccess fProt)
00724 {
00725     // Get Current Applications main window and apply the OpenClipboard() member 
00726     // function to it!
00727     ((AfxGetApp())->m_pMainWnd)->OpenClipboard();
00728     
00729     FileProt    = fProt;
00730 
00731     if (FileProt == CCMemRead)
00732     {
00733         // Check to see if there is any text in the clipboard
00734         if (IsClipboardFormatAvailable(CF_TEXT))
00735         {
00736             IsOpen = FALSE;
00737             Error::SetError(_R(IDE_EMPTY_CLIPBOARD), 0);
00738             return FALSE;
00739         }   
00740 
00741         // Get a handle to the clipboard data
00742         ClipBoardHandle = GetClipboardData(CF_TEXT);
00743     
00744         // Check if clipboard data could be found
00745         if (ClipBoardHandle == NULL)
00746         {
00747             IsOpen = FALSE;
00748             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00749             return FALSE;
00750         }   
00751     
00752         // Get a pointer to the clipboard data
00753         MemFile = (BYTE*) GlobalLock(ClipBoardHandle);
00754            
00755         // Check if memory to be occupied by data was available
00756         if (MemFile == NULL)
00757         {                  
00758             IsOpen = FALSE;
00759             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00760             return FALSE;
00761         }   
00762     
00763         // Get the size of the data
00764         FileSize    = (UINT32) GlobalSize(ClipBoardHandle);
00765     }
00766     else if (FileProt == CCMemWrite)
00767     {
00768         // Empty Clipboard
00769         EmptyClipboard();
00770 
00771         // Allocate Global Memory 
00772         ClipBoardHandle = GlobalAlloc(GHND, (DWORD) CCFILE_DEFAULTSIZE);
00773 
00774         // Check if Memory was allocated properly
00775         if (ClipBoardHandle == NULL)
00776         {
00777             IsOpen = FALSE;
00778             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00779             return FALSE;
00780         }   
00781 
00782         // Get a pointer to the clipboard
00783         MemFile = (BYTE*) GlobalLock(ClipBoardHandle);
00784            
00785         // Check if memory to be occupied by data was available
00786         if (MemFile == NULL)
00787         {                  
00788             IsOpen = FALSE;
00789             Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00790             return FALSE;
00791         }   
00792         
00793         // Set to default size.
00794         FileSize = CCFILE_DEFAULTSIZE;
00795     } 
00796      
00797     CurrentPos  = 0;    
00798     IsOpen      = TRUE;
00799     
00800     return IsOpen;
00801 }
00802 
00803 /********************************************************************************************
00804 
00805 >   BOOL CCClipBoardFile::GrowClipBoardFile()
00806 
00807     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00808     Created:    20/9/93
00809     Returns:    TRUE if File was increased ok and FALSE otherwize
00810     Purpose:    Increases the size of the file by the default size of 1k.
00811 
00812 ********************************************************************************************/
00813 
00814 BOOL CCClipBoardFile::GrowClipBoardFile()
00815 {                                                 
00816     //Increase File Size
00817     FileSize += CCFILE_DEFAULTSIZE; 
00818     
00819     // Unlock memory allocated
00820     GlobalUnlock(ClipBoardHandle);
00821                    
00822     // Allocate Global Memory 
00823     ClipBoardHandle = GlobalReAlloc(ClipBoardHandle, 
00824                                     (DWORD) FileSize,
00825                                     (GMEM_MOVEABLE | GMEM_ZEROINIT)
00826                                     );
00827 
00828     // Check if Memory was allocated properly
00829     if (ClipBoardHandle == NULL)
00830     {
00831         IsOpen = FALSE;
00832         Error::SetError(_R(IDE_REALLOC_ERROR), 0);
00833         return FALSE;
00834     }                                    
00835     
00836     // Get a pointer to the clipboard
00837     MemFile = (BYTE*) GlobalLock(ClipBoardHandle);
00838 
00839     // Check if memory to be occupied by data was available
00840     if (MemFile == NULL)
00841     {                  
00842         IsOpen = FALSE;
00843         Error::SetError(_R(IDE_LOAD_CLIPBOARD), 0);
00844         return FALSE;
00845     }   
00846 
00847     return TRUE;
00848 }
00849 
00850 /********************************************************************************************
00851 
00852 >   CCFile& CCClipBoardFile::read(TCHAR& buf)
00853 
00854     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00855     Created:    9/9/93
00856     Outputs;    character read in
00857     Return:     A reference to a memory file object
00858     Purpose:    Reads a single character from the Memory file 
00859 
00860 ********************************************************************************************/
00861 
00862 CCFile& CCClipBoardFile::read(TCHAR& buf)
00863 {
00864     // Make sure the file is open before it is read!
00865     if (!isOpen())
00866     {
00867         GotError(_R(IDE_NOTOPEN_ERROR));
00868         return (CCFile&) *this;
00869     }
00870 
00871     TCHAR* tempClipFile = (TCHAR*) MemFile;         // Cast MemFile to a TCHAR pointer
00872 
00873     if (!eof())
00874         buf = tempClipFile[CurrentPos++];
00875     else
00876     {
00877         GotError(_R(IDE_EOF_ERROR));
00878     }
00879 
00880     return (CCFile&) *this;
00881 }
00882 
00883 /********************************************************************************************
00884 
00885 >   CCFile& CCClipBoardFile::read(StringBase* buf)
00886 
00887     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00888     Created:    6/9/93
00889     Inputs:     Length - is number of Bytes to be read in.
00890     Output:     The character string read in.
00891     Return:     A reference to CCClipBoardFile object
00892     Purpose:    Reads a stream of characters from the Memory file 
00893 
00894 ********************************************************************************************/
00895 
00896 CCFile& CCClipBoardFile::read(StringBase* buf)
00897 {
00898     // Make sure the file is open before it is read!
00899     if (!isOpen())
00900     {
00901         GotError(_R(IDE_NOTOPEN_ERROR));
00902         return (CCFile&) *this;
00903     }
00904 
00905     TCHAR* tempClipFile     = (TCHAR*) MemFile;         // Cast MemFile to a TCHAR pointer
00906     TCHAR* pString      = *buf;                     // Cast input string to a TCHAR pointer
00907     UINT32 i = 0;
00908     
00909     UINT32 MaxLen = buf->MaxLength();
00910 
00911     while ((i <= MaxLen) && (!eof()))   
00912     {
00913         *pString = tempClipFile[CurrentPos++];
00914         pString++;
00915         i++;
00916     }
00917     
00918     return (CCFile&) *this;
00919 }
00920 
00921 
00922 /********************************************************************************************
00923 
00924 >   CCFile& CCClipBoardFile::write(const TCHAR& buf)
00925 
00926     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00927     Created:    20/9/93
00928     Inputs:     A pointer to a single char
00929     Return:     A reference to a clipboard file object
00930     Purpose:    Writes a single character to the Clipboard file 
00931 
00932 ********************************************************************************************/
00933 
00934 CCFile& CCClipBoardFile::write(const TCHAR& buf)
00935 {
00936     // Make sure the file is open before it is written!
00937     if (!isOpen())
00938     {
00939         GotError(_R(IDE_NOTOPEN_ERROR));
00940         return (CCFile&) *this;
00941     }
00942 
00943     // if file is read only then exit
00944     if (FileProt != CCMemWrite)
00945     {
00946         GotError(_R(IDE_READ_ONLY));
00947         return (CCFile&) *this;
00948     }
00949     
00950     TCHAR* tempClipFile;
00951     
00952     // Cast pointer to memory file to TCHAR
00953     tempClipFile = (TCHAR*) MemFile;
00954     
00955     // if the file size limit is reached then increase memory file by default amount
00956     if (CurrentPos != (FileSize - 1)) 
00957         tempClipFile[CurrentPos++] = buf;
00958     else 
00959         if (GrowClipBoardFile())
00960             // write input byte
00961             tempClipFile[CurrentPos++] = buf;
00962                 
00963     return (CCFile&) *this;
00964 }
00965 
00966 /********************************************************************************************
00967 
00968 >   CCFile& CCClipBoardFile::write(const StringBase& buf, UINT32 length = 0)
00969 
00970     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00971     Created:    20/9/93
00972     Inputs:     constant unicode string
00973                 Length denotes number of bytes to written 
00974     Return:     A reference to a clipboard file object
00975     Purpose:    Writes a stream of characters to the ClipBoard file 
00976 
00977 ********************************************************************************************/
00978 
00979 CCFile& CCClipBoardFile::write(const StringBase& buf, UINT32 length)
00980 {                                      
00981     // Make sure the file is open before it is written!
00982     if (!isOpen())
00983     {
00984         GotError(_R(IDE_NOTOPEN_ERROR));
00985         return (CCFile&) *this;
00986     }
00987 
00988     // if file is read only then exit
00989     if (FileProt != CCMemWrite)
00990     {
00991         GotError(_R(IDE_READ_ONLY));
00992         return (CCFile&) *this;
00993     }
00994     
00995     // Ensure that the length of the string to be written is not longer
00996     // than the string's maximum size
00997     if (length == 0)
00998         length = buf.Length();
00999     else if ((INT32) length > buf.MaxLength())
01000     {
01001         GotError(_R(IDE_STRING_SIZE_ERROR));
01002         return (CCFile&) *this;
01003     }
01004 
01005     const TCHAR* tempBuf = buf;
01006     TCHAR* tempClipFile;
01007     
01008     // Cast pointer to memory file to TCHAR
01009     tempClipFile = (TCHAR*) MemFile;
01010 
01011     for (UINT32 i = 0; (i <= length); i++)
01012     {
01013         // if the file size limit is reached then increase memory file by default amount
01014         if (CurrentPos != (FileSize - 1))
01015             tempClipFile[CurrentPos++] = *tempBuf++;
01016         else
01017             if (GrowClipBoardFile())
01018                 // write input byte
01019                 tempClipFile[CurrentPos++] = *tempBuf++;
01020             else
01021                 break;
01022     }
01023     
01024     return (CCFile&) *this;
01025 }
01026 
01027 
01028 /********************************************************************************************
01029 
01030 >   BOOL CCClipBoardFile::eof() const
01031 
01032     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
01033     Created:    6/9/93
01034     Return:     TRUE if end of file and FALSE otherwize
01035     Purpose:    Determines whether an instance of CCClipBoardFile is open
01036 
01037 ********************************************************************************************/
01038 
01039 BOOL CCClipBoardFile::eof() const
01040 {                                          
01041     TCHAR* tempClipFile = (TCHAR*) MemFile;             // Cast MemFile to a TCHAR pointer
01042 
01043     // Changed the '==' to '>=' in the following check on CurrentPos (MarkN 9/5/94)
01044 
01045     if ((CurrentPos >= (FileSize - 1)) || (tempClipFile[CurrentPos] == END_OF_FILE))
01046         return TRUE;
01047     else
01048         return FALSE;
01049 }
01050 
01051 
01052 /********************************************************************************************
01053 
01054 >   void CCClipBoardFile::close()
01055 
01056     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
01057     Created:    6/9/93
01058     Purpose:    Closes an instance of a CCClipBoardFile
01059 
01060 ********************************************************************************************/
01061 
01062 void CCClipBoardFile::close()
01063 {   
01064     IsOpen = FALSE;
01065                                  
01066     // Unlock memory allocated
01067     GlobalUnlock(ClipBoardHandle);
01068                    
01069     // Write to clipboard - ClipBoardHandle must be unlocked before given to clipboard
01070     SetClipboardData(CF_TEXT, ClipBoardHandle);                   
01071                    
01072     // Close the clipboard
01073     CloseClipboard();
01074 }
01075 
01076 #endif

Generated on Sat Nov 10 03:48:41 2007 for Camelot by  doxygen 1.4.4