Back to index

lightning-sunbird  0.9+nobinonly
nsAECoercionHandlers.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 Communicator client 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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 
00041 #include "nsAEUtils.h"
00042 
00043 #include "nsAECoercionHandlers.h"
00044 
00045 
00046 
00047 AECoercionHandlers*  AECoercionHandlers::sAECoercionHandlers = nil;
00048 
00049 
00050 /*----------------------------------------------------------------------------
00051        AECoercionHandlers 
00052        
00053 ----------------------------------------------------------------------------*/
00054 
00055 AECoercionHandlers::AECoercionHandlers()
00056 :      mTextDescToPascalString(nil)
00057 ,      mPascalStringDescToText(nil)
00058 {
00059        OSErr  err;
00060        
00061        // XXX: Note inconsistent type between NewAECoerceDescProc and AEInstallCoercionHandler. Buggy headers when using Carbon?
00062        mTextDescToPascalString = NewAECoerceDescUPP(TextToPascalStringCoercion);
00063        ThrowIfNil(mTextDescToPascalString);
00064 
00065        err = ::AEInstallCoercionHandler(typeChar, typePascalString,
00066                                                         (AECoercionHandlerUPP) mTextDescToPascalString,
00067                                                         (long)this,
00068                                                         true,                /* Pass a pointer not a descriptor */
00069                                                         false );             /* Application table, not System */
00070        ThrowIfOSErr(err);
00071 
00072        mPascalStringDescToText = NewAECoerceDescUPP(PascalStringToTextCoercion);
00073        ThrowIfNil(mPascalStringDescToText);
00074 
00075        err = ::AEInstallCoercionHandler(typePascalString, typeChar,
00076                                                         (AECoercionHandlerUPP) mPascalStringDescToText,
00077                                                         (long)this,
00078                                                         true,                /* Pass a pointer not a descriptor */
00079                                                         false );             /* Application table, not System */
00080        ThrowIfOSErr(err);
00081 }
00082 
00083 
00084 
00085 /*----------------------------------------------------------------------------
00086        ~AECoercionHandlers 
00087        
00088 ----------------------------------------------------------------------------*/
00089 
00090 AECoercionHandlers::~AECoercionHandlers()
00091 {
00092        if (mTextDescToPascalString)
00093        {
00094               AERemoveCoercionHandler(typeChar, typePascalString, (AECoercionHandlerUPP) mTextDescToPascalString, false);
00095               DisposeAECoerceDescUPP(mTextDescToPascalString);
00096        }
00097 
00098        if (mPascalStringDescToText)
00099        {
00100               AERemoveCoercionHandler(typePascalString, typeChar, (AECoercionHandlerUPP) mPascalStringDescToText, false);
00101               DisposeAECoerceDescUPP(mPascalStringDescToText);
00102        }
00103 }
00104 
00105 
00106 #pragma mark -
00107 
00108 /*----------------------------------------------------------------------------
00109        TextToPascalStringCoercion 
00110        
00111 ----------------------------------------------------------------------------*/
00112 pascal OSErr AECoercionHandlers::TextToPascalStringCoercion(const AEDesc *fromDesc, DescType toType, long handlerRefcon, AEDesc *toDesc)
00113 {
00114        OSErr  err = noErr;
00115        
00116        if (toType != typePascalString)
00117               return errAECoercionFail;
00118 
00119        switch (fromDesc->descriptorType)
00120        {
00121               case typeChar:
00122                      Str255        pString;
00123                      DescToPString(fromDesc, pString, 255);
00124                      err = AECreateDesc(typePascalString, pString, pString[0] + 1, toDesc);
00125                      break;
00126                      
00127               default:
00128                      err = errAECoercionFail;
00129                      break;
00130        }
00131 
00132        return err;
00133 }
00134 
00135 /*----------------------------------------------------------------------------
00136        PascalStringToTextCoercion 
00137        
00138 ----------------------------------------------------------------------------*/
00139 
00140 pascal OSErr AECoercionHandlers::PascalStringToTextCoercion(const AEDesc *fromDesc, DescType toType, long handlerRefcon, AEDesc *toDesc)
00141 {
00142        OSErr  err = noErr;
00143        
00144        if (toType != typeChar)
00145               return errAECoercionFail;
00146 
00147        switch (fromDesc->descriptorType)
00148        {
00149               case typePascalString:
00150                      {
00151                             long stringLen = AEGetDescDataSize(fromDesc);
00152                             if (stringLen > 255)
00153                             {
00154                                    err = errAECoercionFail;
00155                                    break;
00156                             }
00157                             Str255 str;
00158                             AEGetDescData(fromDesc, str, sizeof(str) - 1);
00159                             err = AECreateDesc(typeChar, str + 1, str[0], toDesc);
00160                      }
00161                      break;
00162                      
00163               default:
00164                      err = errAECoercionFail;
00165                      break;
00166        }
00167 
00168        return err;
00169 }
00170 
00171 
00172 
00173 #pragma mark -
00174 
00175 /*----------------------------------------------------------------------------
00176        CreateCoercionHandlers 
00177        
00178 ----------------------------------------------------------------------------*/
00179 
00180 OSErr CreateCoercionHandlers()
00181 {
00182        OSErr  err = noErr;
00183        
00184        if (AECoercionHandlers::sAECoercionHandlers)
00185               return noErr;
00186        
00187        try
00188        {
00189               AECoercionHandlers::sAECoercionHandlers = new AECoercionHandlers;
00190        }
00191        catch(OSErr catchErr)
00192        {
00193               err = catchErr;
00194        }
00195        catch( ... )
00196        {
00197               err = paramErr;
00198        }
00199        
00200        return err;
00201 
00202 }
00203 
00204 
00205 
00206 /*----------------------------------------------------------------------------
00207        ShutdownCoercionHandlers 
00208        
00209 ----------------------------------------------------------------------------*/
00210 
00211 OSErr ShutdownCoercionHandlers()
00212 {
00213        if (!AECoercionHandlers::sAECoercionHandlers)
00214               return noErr;
00215        
00216        try
00217        {
00218               delete AECoercionHandlers::sAECoercionHandlers;
00219        }
00220        catch(...)
00221        {
00222        }
00223        
00224        AECoercionHandlers::sAECoercionHandlers = nil;
00225        return noErr;
00226 }
00227