home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / mac / tclMacLibrary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  4.7 KB  |  190 lines  |  [TEXT/CWIE]

  1. /*
  2.  * tclMacLibrary.c --
  3.  *
  4.  *    This file should be included in Tcl extensions that want to 
  5.  *    automatically oepn their resource forks when the code is linked. 
  6.  *    These routines should not be exported but should be compiled 
  7.  *    locally by each fragment.  Many thanks to Jay Lieske
  8.  *    <lieske@princeton.edu> who provide an initial version of this
  9.  *    file.
  10.  *
  11.  * Copyright (c) 1996 Sun Microsystems, Inc.
  12.  *
  13.  * See the file "license.terms" for information on usage and redistribution
  14.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15.  *
  16.  * SCCS: @(#) tclMacLibrary.c 1.2 96/09/11 21:08:15
  17.  */
  18.  
  19. #include <CodeFragments.h>
  20. #include <Errors.h>
  21. #include <Resources.h>
  22. #include "tclMacInt.h"
  23.  
  24. /*
  25.  * These function are not currently defined in any header file.  The
  26.  * only place they should be used is in the Initialization and
  27.  * Termination entry points for a code fragment.  The prototypes
  28.  * are included here to avoid compile errors.
  29.  */
  30.  
  31. OSErr TclMacInitializeFragment _ANSI_ARGS_((
  32.             struct CFragInitBlock* initBlkPtr));
  33. void TclMacTerminateFragment _ANSI_ARGS_((void));
  34.  
  35. /*
  36.  * Static functions in this file.
  37.  */
  38.  
  39. static OSErr OpenLibraryResource _ANSI_ARGS_((
  40.             struct CFragInitBlock* initBlkPtr));
  41. static void CloseLibraryResource _ANSI_ARGS_((void));
  42.  
  43. /* 
  44.  * The refnum of the opened resource fork.
  45.  */
  46. static short ourResFile = kResFileNotOpened;
  47.  
  48. /*
  49.  *----------------------------------------------------------------------
  50.  *
  51.  * TclMacInitializeFragment --
  52.  *
  53.  *    Called by MacOS CFM when the shared library is loaded. All this
  54.  *    function really does is give Tcl a chance to open and register
  55.  *    the resource fork of the library. 
  56.  *
  57.  * Results:
  58.  *    MacOS error code if loading should be canceled.
  59.  *
  60.  * Side effects:
  61.  *    Opens the resource fork of the shared library file.
  62.  *
  63.  *----------------------------------------------------------------------
  64.  */
  65.  
  66. OSErr
  67. TclMacInitializeFragment(
  68.     struct CFragInitBlock* initBlkPtr)        /* Pointer to our library. */
  69. {
  70.     OSErr err = noErr;
  71.  
  72. #ifdef __MWERKS__
  73.     {
  74.         extern OSErr __initialize( CFragInitBlock* initBlkPtr);
  75.         err = __initialize(initBlkPtr);
  76.     }
  77. #endif
  78.     if (err == noErr)
  79.         err = OpenLibraryResource( initBlkPtr);
  80.     return err;
  81. }
  82.  
  83. /*
  84.  *----------------------------------------------------------------------
  85.  *
  86.  * TclMacTerminateFragment --
  87.  *
  88.  *    Called by MacOS CFM when the shared library is unloaded.
  89.  *
  90.  * Results:
  91.  *    None.
  92.  *
  93.  * Side effects:
  94.  *    The resource fork of the code fragment is closed.
  95.  *
  96.  *----------------------------------------------------------------------
  97.  */
  98.  
  99. void 
  100. TclMacTerminateFragment()
  101. {
  102.     CloseLibraryResource();
  103.  
  104. #ifdef __MWERKS__
  105.     {
  106.         extern void __terminate(void);
  107.         __terminate();
  108.     }
  109. #endif
  110. }
  111.  
  112. /*
  113.  *----------------------------------------------------------------------
  114.  *
  115.  * OpenLibraryResource --
  116.  *
  117.  *    This routine can be called by a MacOS fragment's initialiation 
  118.  *    function to open the resource fork of the file.  
  119.  *    Call it with the same data passed to the initialization function. 
  120.  *    If the fragment loading should fail if the resource fork can't 
  121.  *    be opened, then the initialization function can pass on this 
  122.  *    return value.
  123.  *
  124.  * Results:
  125.  *    It returns noErr on success and a MacOS error code on failure.
  126.  *
  127.  * Side effects:
  128.  *    The resource fork of the code fragment is opened read-only and 
  129.  *    is installed at the head of the resource chain.
  130.  *
  131.  *----------------------------------------------------------------------
  132.  */
  133.  
  134. static OSErr 
  135. OpenLibraryResource(
  136.     struct CFragInitBlock* initBlkPtr)
  137. {
  138.     FSSpec* fileSpec = NULL;
  139.     OSErr err = noErr;
  140.     
  141.     if (initBlkPtr->fragLocator.where == kOnDiskFlat) {
  142.         fileSpec = initBlkPtr->fragLocator.u.onDisk.fileSpec;
  143.     } else if (initBlkPtr->fragLocator.where == kOnDiskSegmented) {
  144.         fileSpec = initBlkPtr->fragLocator.u.inSegs.fileSpec;
  145.     } else {
  146.         err = resFNotFound;
  147.     }
  148.  
  149.     /*
  150.      * Open the resource fork for this library in read-only mode.  
  151.      * This will make it the current res file, ahead of the 
  152.      * application's own resources.
  153.      */
  154.     if (fileSpec != NULL) {
  155.     ourResFile = FSpOpenResFile( fileSpec, fsRdPerm);
  156.     if (ourResFile == kResFileNotOpened) {
  157.         err = ResError();
  158.     }
  159.     }
  160.     
  161.     return err;
  162. }
  163.  
  164. /*
  165.  *----------------------------------------------------------------------
  166.  *
  167.  * CloseLibraryResource --
  168.  *
  169.  *    This routine should be called by a MacOS fragment's termination 
  170.  *    function to close the resource fork of the file 
  171.  *    that was opened with OpenLibraryResource.  
  172.  *
  173.  * Results:
  174.  *    None.
  175.  *
  176.  * Side effects:
  177.  *    The resource fork of the code fragment is closed.
  178.  *
  179.  *----------------------------------------------------------------------
  180.  */
  181.  
  182. static void
  183. CloseLibraryResource()
  184. {
  185.     if (ourResFile != kResFileNotOpened) {
  186.     CloseResFile(ourResFile);
  187.     ourResFile = kResFileNotOpened;
  188.     }
  189. }
  190.