home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / SLResFil.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  15.6 KB  |  592 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLResFil.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRESFI_K
  13. #include "FWResFil.k"
  14. #endif
  15.  
  16. #ifndef FWRESFI_H
  17. #include "FWResFil.h"
  18. #endif
  19.  
  20. #ifndef   FWSTRS_H
  21. #include "FWStrs.h"
  22. #endif
  23.  
  24. #ifndef   FWFILESP_H
  25. #include "FWFileSp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCDEF_H
  29. #include "FWExcDef.h"
  30. #endif
  31.  
  32. #ifndef   FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #ifndef   FWMEMMGR_H
  37. #include "FWMemMgr.h"
  38. #endif
  39.  
  40. #ifndef   FWBNDSTR_H
  41. #include "FWBndStr.h"
  42. #endif
  43.  
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // Mac-only
  47. //----------------------------------------------------------------------------------------
  48. #ifdef FW_BUILD_MAC
  49.  
  50. #pragma segment FWResource_PrivateResourceFile
  51.  
  52. #ifndef __ERRORS__
  53. #include <errors.h>
  54. #endif
  55.  
  56. #ifndef __ICONS__
  57. #include <Icons.h>
  58. #endif
  59.  
  60. #ifndef __RESOURCES__
  61. #include <Resources.h>
  62. #endif
  63.  
  64. #ifndef __MENUS__
  65. #include <Menus.h>
  66. #endif
  67.  
  68. #ifndef __STRING__
  69. #include <string.h>
  70. #endif
  71.  
  72. #ifndef __MEMORY__
  73. #include <Memory.h>
  74. #endif
  75.  
  76. #ifndef __TOOLUTILS__
  77. #include <ToolUtils.h>
  78. #endif
  79.  
  80. #endif
  81.  
  82.  
  83. /*
  84.  *  This file was generated by the SOM Compiler.
  85.  *  Generated using: 
  86.  *      SOM Emitter emitxtm.dll: 2.33
  87.  */
  88.  
  89. #define VARIABLE_MACROS
  90. #define FW_OResourceFile_Class_Source
  91. #include "SLResFil.xih"
  92.  
  93.  
  94.  
  95. #if defined(__MWERKS__) && GENERATING68K
  96. // A hack to work around a bug
  97. #pragma import list somNewObjectInstance
  98. #endif
  99.  
  100. //----------------------------------------------------------------------------------------
  101. // throwExceptionIfResourceLoadError
  102. //----------------------------------------------------------------------------------------
  103.  
  104. static void throwExceptionIfResourceLoadError(FW_OResourceFile* somSelf,
  105.                                               Environment* ev,
  106.                                               FW_PlatformHandle resourceHandle,
  107.                                               FW_ResourceId resourceId,
  108.                                               FW_ResourceType resourceType)
  109. {
  110. FW_UNUSED(somSelf);
  111. FW_UNUSED(ev);
  112.  
  113.     if (resourceHandle != NULL)
  114.         return;
  115.  
  116.     // If we make it to here, we must exit with an exception.
  117.  
  118. #ifdef FW_BUILD_MAC
  119. FW_UNUSED(resourceId);
  120. FW_UNUSED(resourceType);
  121.     FW_FailOnError(::ResError());
  122. #endif
  123.  
  124. #ifdef FW_BUILD_WIN
  125.     if (!somSelf->HasResource(ev, resourceId, resourceType))
  126.         FW_Failure(FW_xResourceNotFound);
  127. #endif
  128.  
  129.     // If we somehow made it to here, we force an exception
  130.     FW_Failure(FW_xResourceNotLoaded);
  131. }
  132.  
  133.  
  134. //----------------------------------------------------------------------------------------
  135. // newFileSpecificationForID
  136. //----------------------------------------------------------------------------------------
  137.  
  138. static FW_OFileSpecification* newFileSpecificationForID(Environment* ev,
  139.                                                         FW_ResourceFileID resFileID)
  140. {
  141. #ifdef FW_BUILD_WIN
  142.     // Get file name from Windows
  143.     FW_Char buffer[255];
  144.     ::GetModuleFileName(resFileID, buffer, sizeof buffer);
  145.     FW_CString255 fileName(buffer);
  146. #endif
  147.  
  148. #ifdef FW_BUILD_MAC
  149.     FW_PascalChar buffer[255];
  150.     FCBPBRec    pb;
  151.     pb.ioCompletion = 0;
  152.     pb.ioFCBIndx = 0;
  153.     pb.ioVRefNum = 0;
  154.     pb.ioRefNum = resFileID;
  155.     pb.ioNamePtr = buffer;
  156.     OSErr err = ::PBGetFCBInfoSync(&pb);
  157.     if (err != noErr)
  158.         buffer[0] = 0;
  159.     FW_CString255 fileName((FW_Char *) buffer+1, buffer[0]);
  160. #endif
  161.  
  162.     // Return a new value
  163.     FW_OFileSpecification* fileSpec = new FW_OFileSpecification;
  164.  
  165.     fileSpec->InitFromFileName(ev, fileName);
  166.     return fileSpec;
  167. }
  168.  
  169.  
  170. //----------------------------------------------------------------------------------------
  171. // openResourceFile
  172. //----------------------------------------------------------------------------------------
  173.  
  174. static FW_ResourceFileID openResourceFile(Environment* ev, FW_OFileSpecification* fileSpec)
  175. {
  176. #ifdef FW_BUILD_WIN
  177.     FW_CString fileName;
  178.  
  179.     fileSpec->GetFullPath(ev, fileName);
  180.  
  181.     char szFileName[_MAX_PATH];
  182.     fileName.ExportCString(szFileName);
  183.     FW_ResourceFileID result = ::LoadLibrary(szFileName);
  184.     
  185. #ifdef FW_BUILD_WIN16
  186.     if (result < HINSTANCE_ERROR)
  187.     {
  188.         int error = result;
  189. #endif
  190. #ifdef FW_BUILD_WIN32
  191.     if(result == NULL)
  192.     {
  193.         DWORD error = ::GetLastError();
  194. #endif
  195.         switch (error)
  196.         {
  197.             case 2:                                // File not found.
  198.             case 3:                                // Path not found.
  199.                 FW_Failure(FW_xResourceFileNotFound);
  200.             case 8:                                // Insufficient memory
  201.                 FW_Failure(FW_xMemoryExhausted);
  202.             default:
  203.                 FW_Failure(FW_xResourceFileNotFound);
  204.         }
  205. #ifdef FW_BUILD_WIN32
  206.     }
  207. #endif
  208. #ifdef FW_BUILD_WIN16
  209.     }
  210. #endif
  211. #endif
  212.  
  213. #ifdef FW_BUILD_MAC
  214.     FSSpec macSpec;
  215.  
  216.     fileSpec->MacGetFSSpec(ev, &macSpec);
  217.     FW_ResourceFileID result = ::FSpOpenResFile(&macSpec, fsRdPerm);
  218.     if (result == -1)
  219.     {
  220.         short resError = ::ResError();
  221.         if (resError == noErr)
  222.             resError = resFNotFound;
  223.         FW_Failure(resError);
  224.     }
  225. #endif
  226.  
  227.     return result;
  228. }
  229.  
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // closeResourceFile
  233. //----------------------------------------------------------------------------------------
  234.  
  235. static void closeResourceFile(FW_ResourceFileID file)
  236. {
  237. #if defined FW_BUILD_MAC
  238.     ::CloseResFile(file);
  239. #elif defined FW_BUILD_WIN
  240.     ::FreeLibrary(file);
  241. #endif
  242. }
  243.  
  244.  
  245. //----------------------------------------------------------------------------------------
  246. // privGetResource
  247. //
  248. //    Private method.  Attempts to load the resource, but will return NULL on failure.
  249. //----------------------------------------------------------------------------------------
  250.  
  251. static FW_ResourceHandle privGetResource(FW_OResourceFile *somSelf,
  252.                                          Environment* ev,
  253.                                          FW_ResourceId resourceId,
  254.                                          FW_ResourceType resourceType)
  255. {
  256. FW_UNUSED(ev);
  257.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  258.     FW_ResourceHandle resourceHandle = NULL;
  259.  
  260. #if defined FW_BUILD_WIN
  261.     resourceHandle = ::FindResource(_fResourceFileID, 
  262.                                     MAKEINTRESOURCE(resourceId), 
  263.                                     MAKEINTRESOURCE(resourceType));
  264.  
  265.     if (resourceHandle == 0)
  266.     {
  267.         char fileName[255] = {0};
  268.         ::GetModuleFileName(_fResourceFileID, fileName, sizeof fileName);
  269.         __asm int 3
  270.     }
  271.  
  272. #elif defined FW_BUILD_MAC
  273.     // Load the resource.
  274.     short temp = CurResFile();
  275.     if (temp != _fResourceFileID)
  276.         ::UseResFile(_fResourceFileID);
  277.     resourceHandle = ::Get1Resource(resourceType, short(resourceId));    // Must cast!
  278.     if (temp != _fResourceFileID)
  279.         ::UseResFile(temp);
  280. #endif
  281.  
  282.     return resourceHandle;
  283. }
  284.  
  285.  
  286. //----------------------------------------------------------------------------------------
  287. // FW_OResourceFile__InitFromFileSpec
  288. //----------------------------------------------------------------------------------------
  289. /*
  290.  *  Open a resources file by name.
  291.  *  This instance assumes responsibility for closing the file.
  292.  */
  293.  
  294. SOM_Scope void  SOMLINK FW_OResourceFile__InitFromFileSpec(FW_OResourceFile *somSelf, Environment *ev,
  295.         FW_OFileSpecification* newFileSpec)
  296. {
  297.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  298.  
  299.     FW_SOM_TRY
  300.     {
  301.         _fMustCloseFile = TRUE;
  302.         _fResourceFileID = openResourceFile(ev, newFileSpec);
  303.         _fFileSpec = new FW_OFileSpecification;
  304.         _fFileSpec->AssignOFileSpecification(ev, newFileSpec);
  305.     }
  306.     FW_SOM_CATCH
  307. }
  308.  
  309.  
  310. //----------------------------------------------------------------------------------------
  311. // FW_OResourceFile__InitFromResFileID
  312. //----------------------------------------------------------------------------------------
  313. /*
  314.  *  Attach to a previously opened file.
  315.  *  This instance does not assume responsibility for closing the file.
  316.  */
  317.  
  318. SOM_Scope void  SOMLINK FW_OResourceFile__InitFromResFileID(FW_OResourceFile *somSelf, Environment *ev,
  319.         FW_ResourceFileID resFileID)
  320. {
  321.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  322.  
  323.     FW_SOM_TRY
  324.     {
  325.         _fMustCloseFile = FALSE;
  326.         _fResourceFileID = resFileID;
  327.         _fFileSpec = newFileSpecificationForID(ev, resFileID);
  328.     }
  329.     FW_SOM_CATCH
  330. }
  331.  
  332.  
  333. //----------------------------------------------------------------------------------------
  334. // FW_OResourceFile__GetFileSpecification
  335. //----------------------------------------------------------------------------------------
  336. /*
  337.  *  Get the file specification for the resourcesFile.
  338.  */
  339.  
  340. SOM_Scope FW_OFileSpecification*  SOMLINK FW_OResourceFile__GetFileSpecification(FW_OResourceFile *somSelf, Environment *ev)
  341. {
  342. FW_UNUSED(ev);
  343.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  344.  
  345.     return _fFileSpec;
  346. }
  347.  
  348.  
  349. //----------------------------------------------------------------------------------------
  350. // FW_OResourceFile__HasResource
  351. //----------------------------------------------------------------------------------------
  352. /*
  353.  *  Returns TRUE if the resource exists in the file, FALSE if it doesn't.
  354.  */
  355.  
  356. SOM_Scope FW_Boolean  SOMLINK FW_OResourceFile__HasResource(FW_OResourceFile *somSelf, Environment *ev,
  357.         FW_ResourceId resourceId,
  358.         FW_ResourceType resourceType)
  359. {
  360.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  361.  
  362.     FW_SOM_TRY
  363.     {
  364. #ifdef FW_BUILD_WIN
  365.         FW_ResourceHandle resourceHandle;
  366.         resourceHandle = ::FindResource(somThis->fResourceFileID, 
  367.                                         MAKEINTRESOURCE(resourceId), 
  368.                                         MAKEINTRESOURCE(resourceType));
  369. #endif
  370.  
  371. #ifdef FW_BUILD_MAC
  372.         FW_CMacResLoadFalse dontLoadResource;
  373.         FW_PlatformHandle resourceHandle = privGetResource(somSelf, ev, resourceId, resourceType);
  374. #endif
  375.  
  376.         return (resourceHandle != NULL);
  377.     }
  378.     FW_SOM_CATCH
  379.     return FALSE;
  380. }
  381.  
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // FW_OResourceFile__GetResourceHandle
  385. //----------------------------------------------------------------------------------------
  386. /*
  387.  *  Gets the resource handle.  Resource data may still be purgeable or unloaded.
  388.  *  Client assumes responsibility to call ReleaseResourceHandle when done.
  389.  */
  390.  
  391. SOM_Scope FW_ResourceHandle  SOMLINK FW_OResourceFile__GetResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
  392.         FW_ResourceId resourceId,
  393.         FW_ResourceType resourceType)
  394. {
  395.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  396.     FW_ResourceHandle handle = 0;
  397.  
  398.     FW_SOM_TRY
  399.     {
  400.         handle = privGetResource(somSelf, ev, resourceId, resourceType);
  401.     
  402.         // Throw an exception if the resource was not loaded.
  403.         throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceId, resourceType);
  404.     }
  405.     FW_SOM_CATCH
  406.  
  407.     return handle;
  408. }
  409.  
  410.  
  411. //----------------------------------------------------------------------------------------
  412. // FW_OResourceFile__ReleaseResourceHandle
  413. //----------------------------------------------------------------------------------------
  414. /*
  415.  *  Releases the resource handle.  All memory is released.
  416.  */
  417.  
  418. SOM_Scope void  SOMLINK FW_OResourceFile__ReleaseResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
  419.         FW_ResourceHandle handle)
  420. {
  421.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  422.  
  423.     FW_SOM_TRY
  424.     {
  425. #ifdef FW_BUILD_MAC
  426.         ::ReleaseResource(handle);
  427. #else
  428.         FW_UNUSED(handle);
  429. #endif
  430.     }
  431.     FW_SOM_CATCH
  432. }
  433.  
  434.  
  435. //----------------------------------------------------------------------------------------
  436. // FW_OResourceFile__PrivHasSpecialResource
  437. //----------------------------------------------------------------------------------------
  438. /*
  439.  *  Returns TRUE if the resource exists in the file, FALSE if it doesn't.
  440.  */
  441.  
  442. SOM_Scope FW_Boolean  SOMLINK FW_OResourceFile__PrivHasSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
  443.         FW_ResourceId resourceId,
  444.         FW_ResourceType resourceType)
  445. {
  446.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  447.  
  448.     // (JEL) I can't find any reason why this function should be different from
  449.     // HasResource.  For now, I'll just call HasResource, but maybe later I'll
  450.     // have to do something else.
  451.     return somSelf->HasResource(ev, resourceId, resourceType);
  452. }
  453.  
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // FW_OResourceFile__PrivGetSpecialResource
  457. //----------------------------------------------------------------------------------------
  458. /*
  459.  *  Gets the special resource handle.
  460.  *  It is the clients reponsibility to release the handle if necessary,
  461.  *  using whatever platform specific code is required.
  462.  */
  463.  
  464. SOM_Scope FW_PlatformHandle  SOMLINK FW_OResourceFile__PrivGetSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
  465.         FW_ResourceId resourceId,
  466.         FW_ResourceType resourceType)
  467. {
  468.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  469.     FW_PlatformHandle handle = NULL;
  470.  
  471.     FW_SOM_TRY
  472.     {
  473. #if defined FW_BUILD_MAC
  474.         // (JEL) ??? Some resource types are truly "special", in that calling GetResource
  475.         // to load them is a bad idea.  However, several types of resoures have their
  476.         // own GetXxxx toolbox call that is supposedly equivalent to just calling
  477.         // GetResource(XXXX, id).  For some of these types, we call the GetXxxx function,
  478.         // but we allow the default handler to get the rest of these types.  It bothers
  479.         // me a little that we're not consistent, but it seems like there is no good
  480.         // reason to worry about it.
  481.         switch (resourceType)
  482.         {
  483.             case FW_kCursor:
  484.                 handle = (FW_PlatformHandle) ::GetCCursor(resourceId);
  485.                 break;
  486.                 
  487.             case FW_kIcon:
  488.                 handle = (FW_PlatformHandle) ::GetCIcon(resourceId);
  489.                 break;
  490.                 
  491.             case FW_kBlackWhiteIcon:
  492.                 handle = ::GetIcon(resourceId);
  493.                 break;
  494.                 
  495.             case FW_kBlackWhiteCursor:
  496.                 handle = (FW_PlatformHandle) ::GetCursor(resourceId);
  497.                 break;
  498.                 
  499.             case FW_kPicture:
  500.                 handle = (FW_PlatformHandle) ::GetPicture(resourceId);
  501.                 break;
  502.     
  503.             case FW_kBitmap:
  504.                 handle = ::GetResource(resourceType, resourceId);
  505.                 break;
  506.                 
  507.             default:
  508.                 FW_DEBUG_MESSAGE("Not a special resource type");
  509.         }
  510. #elif defined FW_BUILD_WIN
  511.         switch(resourceType)
  512.         {
  513.             case FW_kCursor:
  514.                 handle = ::LoadCursor(_fResourceFileID, MAKEINTRESOURCE(resourceId));
  515.                 break;
  516.                 
  517.             case FW_kIcon:
  518.                 handle = ::LoadIcon(_fResourceFileID, MAKEINTRESOURCE(resourceId));
  519.                 break;
  520.                 
  521.             case FW_kBitmap:
  522.                 handle = ::LoadBitmap(_fResourceFileID, MAKEINTRESOURCE(resourceId));
  523.                 break;
  524.                 
  525.             case FW_kAccelerator:
  526.                 handle = ::LoadAccelerators(_fResourceFileID, MAKEINTRESOURCE(resourceId));
  527.                 break;
  528.                 
  529.             default:
  530.                 FW_DEBUG_MESSAGE("Not a special resource type");
  531.         }
  532. #endif
  533.  
  534.         throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceId, resourceType);
  535.     }
  536.     FW_SOM_CATCH
  537.  
  538.     return handle;
  539. }
  540.  
  541.  
  542. //----------------------------------------------------------------------------------------
  543. // FW_OResourceFile__PrivGetResourceFileID
  544. //----------------------------------------------------------------------------------------
  545. /*
  546.  *  Return the platforms's "file ID" for this resources file.
  547.  */
  548.  
  549. SOM_Scope FW_ResourceFileID  SOMLINK FW_OResourceFile__PrivGetResourceFileID(FW_OResourceFile *somSelf, Environment *ev)
  550. {
  551. FW_UNUSED(ev);
  552.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  553.  
  554.     return _fResourceFileID;
  555. }
  556.  
  557.  
  558. //----------------------------------------------------------------------------------------
  559. // FW_OResourceFile__somInit
  560. //----------------------------------------------------------------------------------------
  561.  
  562. SOM_Scope void  SOMLINK FW_OResourceFile__somInit(FW_OResourceFile *somSelf)
  563. {
  564.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  565.  
  566.     FW_OResourceFile_parent_FW_ORefCount_somInit(somSelf);
  567.  
  568.     _fMustCloseFile  = FALSE;
  569.     _fResourceFileID = 0;
  570.     _fFileSpec       = 0;
  571. }
  572.  
  573.  
  574. //----------------------------------------------------------------------------------------
  575. // FW_OResourceFile__somUninit
  576. //----------------------------------------------------------------------------------------
  577.  
  578. SOM_Scope void  SOMLINK FW_OResourceFile__somUninit(FW_OResourceFile *somSelf)
  579. {
  580.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  581.  
  582.     FW_SOM_UNINIT_TRY
  583.     {
  584.         if (_fMustCloseFile)
  585.             closeResourceFile(_fResourceFileID);
  586.         delete _fFileSpec;
  587.  
  588.         FW_OResourceFile_parent_FW_ORefCount_somUninit(somSelf);
  589.     }
  590.     FW_SOM_UNINIT_CATCH
  591. }
  592.