fthelper.cpp

Go to the documentation of this file.
00001 // $Id: fthelper.cpp 1282 2006-06-09 09:46:49Z alex $
00002 // Implementation of the FlareTemplateHelper 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 "fthelper.h"
00103 #include "fileutil.h"
00104 //#include "ccfile.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00105 #define NO_CAMTYPES
00106 #include "templman.h"   // Scary bits
00107 
00108 #include "txtmacro.h"   // MFC/ATL text converion macros
00109 //#include "resource.h" // out of memory error
00110 //#include "filtrres.h"
00111 
00112 extern "C"
00113 {
00114 //  const IID IID_IFTManipulator = {0x31A6E651,0xEBA2,0x11D0,{0x8C,0x33,0x00,0xA0,0x24,0x72,0x9D,0xDE}};
00115     const IID IID_IFTManipulator = {0x7B14A5DD,0xAA72,0x403b,{0xB0,0x1C,0x12,0x56,0xCA,0xF6,0x9D,0x67}};
00116 //  const CLSID CLSID_FTManipulator = {0x31A6E652,0xEBA2,0x11D0,{0x8C,0x33,0x00,0xA0,0x24,0x72,0x9D,0xDE}};
00117     const CLSID CLSID_FTManipulator = {0x424C099D,0x6A26,0x4fea,{0xA5,0x66,0x45,0xCD,0xB0,0xEF,0x06,0x94}};
00118 }
00119 
00120 
00121 //-----------------------------------------------
00122 
00123 // This will get Camelot to display the filename and linenumber of any memory allocations
00124 // that are not released at program exit
00125 #define new CAM_DEBUG_NEW
00126 
00127 /********************************************************************************************
00128 
00129 >   CCLexFile* FlareTemplateHelper::ConvertFile(CCLexFile* pFile)
00130 
00131     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00132     Created:    09/07/97
00133     Purpose:    Default constructor
00134     SeeAlso:    -
00135 
00136 ********************************************************************************************/
00137 
00138 CCLexFile* FlareTemplateHelper::ConvertFile(CCLexFile* pFile)
00139 {
00140     ERROR2IF(pFile == NULL, FALSE, "NULL file in FTHelper::ConvertFile");
00141 
00142     String_256 InputFile = pFile->GetPathName().GetPath();
00143 
00144     if (InputFile.IsEmpty())
00145         return(NULL);
00146 
00147     BSTR pInputName = T2BSTR((TCHAR*)InputFile);
00148     if (pInputName == NULL)
00149     {
00150         Error::SetError(_R(IDS_OUT_OF_MEMORY));
00151         return(NULL);
00152     }
00153 
00154     HRESULT hr = S_OK;
00155     CCDiskFile* pNewFile = NULL;
00156 
00157     IFTManipulator* pTemplMan = CreateTemplMan();
00158     if (pTemplMan != NULL)
00159     {
00160         // Initialise the manipulator object
00161         HRESULT hr = pTemplMan->Init(pInputName);
00162         if (SUCCEEDED(hr))
00163         {
00164             PathName TempPath = FileUtil::GetTemporaryPathName();
00165             String_256 OutFile = TempPath.GetPath();
00166             if (!OutFile.IsEmpty())
00167             {
00168                 // Get file name as a BSTR
00169                 BSTR pOutputName = T2BSTR((TCHAR*)OutFile);
00170                 // And process it (will fail if NULL)
00171                 hr = pTemplMan->ProcessTemplateToFile(pOutputName);
00172                 if (SUCCEEDED(hr))
00173                 {
00174                     // Create a default diskfile
00175                     pNewFile = new CCDiskFile();
00176                     if (pNewFile != NULL)
00177                     {
00178                         if (!pNewFile->open(TempPath, ios::in | ios::binary))
00179                         {
00180                             // If we failed then delete the file
00181                             // and set the pointer to NULL for return
00182                             delete pNewFile;
00183                             pNewFile = NULL;
00184                         }
00185                     }
00186                 }
00187                 SysFreeString(pOutputName);
00188             }
00189         }
00190         // Release the interface
00191         pTemplMan->Release();
00192         pTemplMan = NULL;
00193     }
00194 
00195     if (FAILED(hr))
00196     {
00197         SetHResultError(hr);        // set the error
00198         ERROR3IF(pNewFile != NULL, "ConvertFile failed but created new file");
00199     }
00200 
00201     // Free the input BSTR
00202     SysFreeString(pInputName);
00203 
00204     return(pNewFile);
00205 }
00206 
00207 
00208 /********************************************************************************************
00209 
00210 >   static IFTManipulator* FlareTemplateHelper::CreateTemplMan(void)
00211 
00212     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00213     Created:    09/07/97
00214     Returns:    IFTManipulator pointer or NULL if error
00215     Purpose:    Creates a FlareTemplateManipulator object
00216     Errors:     
00217 
00218 ********************************************************************************************/
00219 
00220 IFTManipulator* FlareTemplateHelper::CreateTemplMan(void)
00221 {
00222     IFTManipulator* pTemplMan = NULL;
00223         
00224     // Create an FTManipulator object
00225     HRESULT hr = CoCreateInstance(CLSID_FTManipulator,
00226                                     NULL,
00227                                     CLSCTX_INPROC_SERVER,
00228                                     IID_IFTManipulator,
00229                                     (void**)&pTemplMan);
00230 
00231     if (SUCCEEDED(hr))
00232     {
00233         return(pTemplMan);
00234     }
00235     else
00236     {
00237         SetHResultError(hr);
00238         return(NULL);
00239     }
00240 }
00241 
00242 /********************************************************************************************
00243 
00244 >   static void FlareTemplateHelper::SetHResultError(HRESULT hr)
00245 
00246     Author:     Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00247     Created:    09/07/97
00248     Purpose:    Sets an error from a TemplMan error HRESULT
00249 
00250 ********************************************************************************************/
00251 
00252 void FlareTemplateHelper::SetHResultError(HRESULT hr)
00253 {
00254     if (SUCCEEDED(hr))
00255         return;
00256 
00257     INT32 ErrorID = _R(IDS_TEMPLMAN_BAD_TEMPLATE);  // Default error
00258     INT32 PostFixID = 0;
00259 
00260     switch (hr)
00261     {
00262         case REGDB_E_CLASSNOTREG:
00263             ErrorID = _R(IDS_TEMPLMAN_NOT_INSTALLED);
00264             break;
00265     
00266         case E_OUTOFMEMORY:
00267             ErrorID = _R(IDS_OUT_OF_MEMORY);
00268             break;
00269 
00270         case E_BADTEMPLATE:
00271             break;
00272 
00273         case E_BADRECORD:
00274             {
00275                 PostFixID = _R(IDS_TEMPLMAN_BAD_RECORD);
00276                 break;
00277             }
00278         case E_BADDATAITEM:
00279             {
00280                 PostFixID = _R(IDS_TEMPLMAN_BAD_DATA_ITEM);
00281                 break;
00282             }
00283         default:
00284             {
00285                 Error::SetError(_R(IDS_NOT_A_FLARETEMPLATE_FILE));
00286                 return;
00287             }
00288     }
00289 
00290     String_256 strError;
00291 
00292     if (!strError.Load(ErrorID))            // Try to load the string
00293     {
00294         Error::SetError(ErrorID);           // Just set the error if it fails
00295         return;
00296     }
00297 
00298     if (PostFixID != 0)
00299     {
00300         String_256 strPostFix;
00301         if (!strPostFix.Load(PostFixID))    // Load the postfix string
00302         {
00303             Error::SetError(ErrorID);       // Just set the error if it fails
00304             return;
00305         }
00306         strError += strPostFix;             // and append it
00307     }
00308 
00309     // And set the error
00310     Error::SetError(ErrorID, strError, 0);      // Why doesn't the module param default to 0
00311 }

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