resources.c

Go to the documentation of this file.
00001 
00002 /*  $Id: resources.c 1274 2006-04-16 18:34:45Z andreradke $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "error.h"
00032 #include "memory.h"
00033 #include "strings.h"
00034 #include "shell.rsrc.h"
00035 #include "file.h"
00036 #include "resources.h"
00037 #include "langinternal.h" /* 2006-01-30 creedon */
00038 
00039 
00040 #ifdef WIN95VERSION
00041     extern HINSTANCE hInst;
00042 #endif
00043 
00044 
00045 boolean getstringlist (short listnum, short id, bigstring bs) {
00046 #ifdef MACVERSION   
00047     /*
00048     the Mac routine GetIndString doesn't set ResError false when we fall off
00049     the end of the indicated list, so we return false if the returned string
00050     is of zero length.
00051     */
00052     
00053     #if 0 //#ifdef fldebug  /* 2006-04-16 aradke: disabled for Mac Intel build */
00054     
00055     if (GetResource ('STR#', listnum) == nil)
00056         DebugStr ("\pmissing STR# resource");
00057     
00058     #endif
00059     
00060     GetIndString (bs, listnum, id);
00061     
00062     return (stringlength (bs) > 0);
00063 #endif
00064 
00065 #ifdef WIN95VERSION
00066     HANDLE reshandle, hdata;
00067     
00068     setstringlength(bs,0);
00069     reshandle = FindResource (hInst, MAKEINTRESOURCE(id), MAKEINTRESOURCE(listnum));
00070     if (reshandle != NULL)
00071         {
00072         hdata = LoadResource (hInst, reshandle);
00073         strcpy (stringbaseaddress(bs), LockResource(hdata));
00074         setstringlength (bs, strlen (stringbaseaddress(bs)));
00075         }
00076     return (stringlength (bs) > 0);
00077 #endif
00078     } /*getstringlist*/
00079     
00080 
00081 boolean findstringlist (bigstring bs, short listnum, short *id) {
00082     
00083     /*
00084     search in the indicated STR# resource for a string equal to bs.  if
00085     the string was found, return its index in id.  the search is unicase.
00086     */
00087     
00088     bigstring bsid;
00089     bigstring bsresource;
00090     register short ixresource = 1;
00091     
00092     *id = 0; /*default returned value*/
00093     
00094     copystring (bs, bsid); /*work on a copy*/
00095     
00096     alllower (bsid); /*search is unicase*/
00097     
00098     while (true) {
00099         
00100         if (!getstringlist (listnum, ixresource, bsresource)) /*ran out of strings in STR# resource*/
00101             return (false);
00102             
00103         alllower (bsresource); /*search is unicase*/
00104         
00105         if (equalstrings (bsid, bsresource)) {
00106             
00107             *id = ixresource;
00108             
00109             return (true);
00110             }
00111             
00112         ixresource++; /*advance to next string*/
00113         } /*while*/
00114     } /*findstringlist*/
00115     
00116 
00117 boolean closeresourcefile (short rnum) {
00118 #ifdef MACVERSION   
00119     /*
00120     close the last file opened with openresourcefile.
00121     
00122     8/20/90 dmb:  take rnum as parm.  no need to SetVol to original location
00123     
00124     12/13/91 dmb: make sure we never close the System or ROM resource files!
00125     */
00126     
00127     if (rnum != -1) { /*resource file was successfully opened*/
00128         
00129         if (rnum > 2) /*not System or ROM*/
00130             CloseResFile (rnum);
00131         else
00132             UpdateResFile (rnum); /*make sure map is consistent*/
00133         
00134         if (ResError () != noErr)
00135             return (false);
00136         }
00137 #endif  
00138     return (true);
00139     } /*closeresourcefile*/
00140 
00141 
00142 boolean openresourcefile (const tyfilespec *fs, short *rnum, short forktype) {
00143 #ifdef MACVERSION
00144     /*
00145     2006-01-30 creedon: added check for non-carbon OS and trying to access the data fork of a file, error message
00146     
00147     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00148     */ 
00149     
00150     short resourcerefnum = -1;
00151 #if TARGET_API_MAC_CARBON == 1
00152     HFSUniStr255 fork;
00153     FSRef myRef;
00154 #else
00155     if (forktype == datafork) { // if we're not on carbon then trying to use the data fork for resources is an error
00156         langerrormessage ("\x4A" "Can't open the data fork for use with resources on this version of the OS.");
00157         return (false);
00158         }
00159 #endif
00160         
00161     SetResLoad (false);
00162     
00163 #if TARGET_API_MAC_CARBON == 1
00164 
00165     FSpMakeFSRef (fs, &myRef);
00166         
00167     switch (forktype) {
00168         case resourcefork:
00169             FSGetResourceForkName (&fork);
00170             break;
00171 
00172         case datafork:
00173             FSGetDataForkName (&fork);
00174             break;
00175         }
00176 
00177     /*
00178     2005-09-01 creedon - in my reading about the FSOpenResourceFile function someone mentioned
00179     that it might not deal well with corrupted resources and that dropping back to FSpOpenResFile
00180     seemed to do the trick.  i've not done that here but could be tried if the problem is real
00181     and manifests. 
00182     */
00183 
00184     FSOpenResourceFile (&myRef, fork.length, fork.unicode, fsRdWrPerm, &resourcerefnum);
00185 
00186 #else
00187 
00188     resourcerefnum = FSpOpenResFile (fs, fsRdWrPerm);
00189 
00190 #endif
00191 
00192     SetResLoad (true);
00193 
00194     if (ResError () == -39) { /*eof error, file has no resource fork, create one*/
00195 
00196 #if ((TARGET_API_MAC_CARBON == 1) && !defined(__MWERKS__))
00197 
00198         OSErr errcode = FSCreateResourceFork (&myRef, fork.length, fork.unicode, 0);
00199     
00200         if (errcode != noErr) /*failed to create resource fork*/
00201             goto error;
00202         
00203         FSOpenResourceFile (&myRef, fork.length, fork.unicode, fsRdWrPerm, &resourcerefnum);
00204 #else
00205         HCreateResFile ((*fs).vRefNum, (*fs).parID, (StringPtr) (*fs).name);
00206         
00207         if (ResError () != noErr) /*failed to create resource fork*/
00208             goto error;
00209         
00210         resourcerefnum = FSpOpenResFile (fs, fsRdWrPerm);
00211 
00212 #endif
00213         }
00214 
00215     if (resourcerefnum != -1) /*it's open*/ {
00216         
00217         UseResFile (resourcerefnum); /*in case it was already open*/
00218         
00219         *rnum = resourcerefnum;
00220         
00221         return (true);
00222         }
00223     
00224 error:
00225 
00226     setoserrorparam ((ptrstring) (*fs).name); /*in case error message takes a filename parameter*/
00227 
00228     oserror (ResError ());
00229 
00230     closeresourcefile (resourcerefnum); /*checks for -1*/
00231 
00232     *rnum = -1;
00233 
00234 #endif // MACVERSION
00235     return (false);
00236     } /*openresourcefile*/
00237 
00238 
00239 #ifdef MACVERSION
00240 boolean writeresource (ResType type, short id, bigstring bsname, long ctwrite, void *pdata) {
00241     
00242     /*
00243     6/2/92 dmb: don't setresinfo; if the resource exists, we want to retain 
00244     current name & id.  note that we don't support a verb that allows a resource 
00245     to be put with both name & id specified, nor does Frontier do so internally.
00246     
00247     7/7/92 dmb: use Unique1ID instead of UniqueID
00248     
00249     12/18/92 dmb: don't call oserror from this routine. if the error is encountered 
00250     accessing the program's resource fork, bringing up the Error Info window here 
00251     seems to sqrew up the resource chain irreperably, leading to a bus error. so, 
00252     callers must make sure to check resError themselves.
00253     */
00254     
00255     Handle hresource;
00256     
00257     if (bsname == nil)
00258         bsname = emptystring;
00259     
00260     if ((id == -1) && (!isemptystring (bsname))) { /*no id provided; try to use name*/
00261         
00262         id = Unique1ID (type);
00263         
00264         hresource = Get1NamedResource (type, bsname);
00265         }
00266     else
00267         hresource = Get1Resource (type, id);
00268     
00269     if (hresource != nil) { /*resource already exists*/
00270         
00271         if (!sethandlecontents (pdata, ctwrite, hresource)) /*couldn't increase size of handle*/
00272             return (false);
00273         
00274         //SetResInfo (hresource, id, bsname); /*set the name of the resource*/
00275         
00276         ChangedResource (hresource);
00277         }
00278     else {
00279         
00280         if (!newfilledhandle (pdata, ctwrite, &hresource))
00281             return (false);
00282         
00283         AddResource (hresource, type, id, bsname);
00284         }
00285     
00286     return (true);
00287     } /*writeresource*/
00288 #endif
00289 
00290 
00291 #ifdef MACVERSION
00292 static boolean copyresourcehandle (Handle hresource, short destnum) {
00293     
00294     /*
00295     copy the given resource handle to the destination file.  our caller is 
00296     managing the resource chain; we don't need to preserve the current resource 
00297     file.
00298     
00299     return true if the resource is not nil and is sucessfully copied.  generate an 
00300     oserror only if a memory or IO error occurs.
00301     */
00302     
00303     ResType type;
00304     short id;
00305     bigstring bsname;
00306     Handle htemp;
00307     
00308     if (hresource == nil) { /*didn't get the resource*/
00309         
00310         if (ResError () != resNotFound) /*only generate an alert on memory/IO error*/
00311             oserror (ResError ());
00312         
00313         return (false);
00314         }
00315     
00316     GetResInfo (hresource, &id, &type, bsname); /*get all info*/
00317     
00318     UseResFile (destnum);
00319     
00320     htemp = Get1Resource (type, id); /*try to see if res of same number in dest file*/
00321     
00322     if (htemp != nil) { /*an entry already exists*/
00323         
00324         if (!copyhandlecontents (hresource, htemp)) /*memory failure*/
00325             return (false);
00326         
00327         SetResInfo (htemp, id, bsname); /*set name*/
00328         
00329         ChangedResource (htemp);
00330         }
00331     else {
00332         
00333         DetachResource (hresource); /*required for copying!*/
00334         
00335         AddResource (hresource, type, id, bsname);
00336         
00337         WriteResource (hresource);
00338         }
00339     
00340     return (!oserror (ResError ()));
00341     } /*copyresourcehandle*/
00342 #endif
00343 
00344 
00345 #ifdef MACVERSION
00346 boolean copyresource (short sourcenum, short destnum, ResType type, short id) {
00347     
00348     /*
00349     copy the resource with the indicated type and id from the source resource 
00350     file to the destination.
00351     
00352     return true if the resource is found and is sucessfully copied.  generate an 
00353     oserror if a memory or IO error occurs.
00354     */
00355     
00356     short saveresfile;
00357     boolean fl;
00358     
00359     saveresfile = CurResFile ();
00360     
00361     UseResFile (sourcenum);
00362     
00363     fl = copyresourcehandle (Get1Resource (type, id), destnum);
00364     
00365     UseResFile (saveresfile);
00366     
00367     return (fl);
00368     } /*copyresource*/
00369 #endif
00370 
00371 
00372 boolean copyallresources (short sourcenum, short destnum) {
00373     
00374     /*
00375     copy all of the resources in the source resource file to the destination.
00376     
00377     return true if the resource is found and is sucessfully copied.  generate an 
00378     oserror if a memory or IO error occurs.
00379     */
00380     
00381 #ifdef MACVERSION
00382     short saveresfile;
00383     short cttypes, ixtype;
00384     short ctresources, ixresource;
00385     ResType type;
00386     boolean fl = true;
00387     
00388     saveresfile = CurResFile ();
00389     
00390     UseResFile (sourcenum);
00391     
00392     cttypes = Count1Types ();
00393     
00394     for (ixtype = 1; fl && (ixtype <= cttypes); ++ixtype) {
00395         
00396         Get1IndType (&type, ixtype);
00397         
00398         ctresources = Count1Resources (type);
00399         
00400         for (ixresource = 1; fl && (ixresource <= ctresources); ++ixresource) {
00401             
00402             fl = copyresourcehandle (Get1IndResource (type, ixresource), destnum);
00403             
00404             UseResFile (sourcenum); /*copyresourcehandle changes it*/
00405             }
00406         }
00407     
00408     UseResFile (saveresfile);
00409     
00410     return (fl);
00411 #endif
00412 #ifdef WIN95VERSION
00413     return (false);
00414 #endif
00415     } /*copyallresources*/
00416 
00417 
00418 
00419 static Handle get1resource (ResType type, short id, bigstring bsname) {
00420 #ifdef MACVERSION   
00421     if ((bsname == nil) || isemptystring (bsname))
00422         return (Get1Resource (type, id));
00423     else
00424         return (Get1NamedResource (type, bsname));
00425 #endif
00426 
00427 #ifdef WIN95VERSION
00428     HANDLE reshandle, hdata;
00429     Handle hreturn;
00430     long sz;
00431     char restype[5];
00432     char restypex[5];
00433     memmove (restypex, &type, 4);
00434     restype[0] = restypex[3];
00435     restype[1] = restypex[2];
00436     restype[2] = restypex[1];
00437     restype[3] = restypex[0];
00438 
00439     restype[4] = 0;
00440 
00441     hdata = NULL;
00442     hreturn = NULL;
00443 
00444     if ((bsname == nil) || isemptystring (bsname))
00445         reshandle = FindResource (hInst, MAKEINTRESOURCE(id), restype);
00446     else
00447         reshandle = FindResource (hInst, stringbaseaddress(bsname), restype);
00448 
00449     if (reshandle != NULL) 
00450         {
00451         sz = SizeofResource (hInst, reshandle);
00452         hdata = LoadResource (hInst, reshandle);
00453         
00454         if (hdata != NULL) 
00455             {
00456             hreturn = NewHandle (sz);
00457 
00458             if (hreturn != NULL)
00459                 {
00460 //              memmove (GlobalLock (hreturn), LockResource(hdata), sz);
00461 //              GlobalUnlock (hreturn);
00462                 //5.1.2 new memory handling RAB
00463                 memmove (*hreturn, LockResource(hdata), sz);
00464                 }
00465             }
00466         }
00467     return (hreturn);
00468 
00469 #endif
00470     } /*get1resource*/
00471 
00472 
00473 Handle filegetresource (short rnum, ResType type, short id, bigstring bsname) {
00474     
00475     /*
00476     4/9/91 dmb: now take id and name as parameters; use id if name is nil
00477     */
00478     
00479     register Handle hresource;
00480 #ifdef MACVERSION
00481     short saveresfile;
00482 #endif
00483     
00484     if (rnum == -1)
00485         return (nil);
00486 #ifdef MACVERSION   
00487     saveresfile = CurResFile ();
00488     
00489     UseResFile (rnum);
00490 #endif  
00491 
00492     hresource = get1resource (type, id, bsname);
00493 
00494 #ifdef MACVERSION   
00495     UseResFile (saveresfile);
00496 #endif  
00497     return (hresource);
00498     } /*filegetresource*/
00499 
00500 
00501 boolean filereadresource (short rnum, ResType type, short id, bigstring bsname, long ctread, void *pdata) {
00502     
00503     register Handle hresource;
00504     
00505     hresource = filegetresource (rnum, type, id, bsname);
00506     
00507     if (hresource) {
00508         
00509         long ctactualsize = gethandlesize (hresource);
00510 
00511         moveleft (*hresource, pdata, min (ctread, ctactualsize));
00512         
00513         return (true);
00514         }
00515     
00516     return (false);
00517     } /*filereadresource*/
00518 
00519 #ifdef MACVERSION
00520 boolean filewriteresource (short rnum, ResType type, short id, bigstring bsname, long ctwrite, void *pdata) {
00521     
00522     register boolean fl;
00523     short saveresfile;
00524     OSErr err;
00525     
00526     if (rnum == -1)
00527         return (false);
00528     
00529     saveresfile = CurResFile ();
00530     
00531     UseResFile (rnum);
00532     
00533     fl = writeresource (type, id, bsname, ctwrite, pdata);
00534     
00535     err = ResError ();
00536     
00537     UseResFile (saveresfile);
00538     
00539     return (fl && !oserror (err));
00540     } /*filewriteresource*/
00541 #endif
00542 
00543 #ifdef MACVERSION
00544 boolean saveresource (const tyfilespec *fs, short rnum, ResType type, short id, bigstring bsname, long sizedata, void *pdata, short forktype) {
00545 
00546     /*
00547     open the file indicated by fname and vnum, and assign pdata to the resource
00548     indicated by type and id.  return true if it worked.
00549     
00550     this will only work for resource types that are not used in your application
00551     and are not used in the system.  be careful in choosing your types!
00552     
00553     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00554     */
00555     
00556     register boolean fl;
00557     short oldrnum;
00558     OSErr err;
00559     
00560     if (rnum != -1) /*have open resource fork to write to*/
00561         return (filewriteresource (rnum, type, id, bsname, sizedata, pdata));
00562     
00563     oldrnum = CurResFile ();
00564     
00565     if (!openresourcefile (fs, &rnum, forktype))
00566         return (false);
00567     
00568     fl = writeresource (type, id, bsname, sizedata, pdata);
00569     
00570     err = ResError ();
00571     
00572     if ((rnum == oldrnum) || (rnum == filegetapplicationrnum ())) { /*don't close active resource fork!*/
00573         
00574         UpdateResFile (rnum);
00575         
00576         UseResFile (oldrnum);
00577         }
00578     else {
00579         
00580         if (!closeresourcefile (rnum))
00581             fl = false;
00582         }
00583     
00584     return (fl && !oserror (err));
00585     } /*saveresource*/
00586 #endif
00587 
00588 #ifdef MACVERSION
00589 boolean saveresourcehandle (const tyfilespec *fs, ResType type, short id, bigstring bsname, Handle h, short forktype) {
00590     
00591     /*
00592     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00593     */ 
00594     
00595     boolean fl;
00596     
00597     lockhandle (h);
00598     
00599     fl = saveresource (fs, -1, type, id, bsname, gethandlesize (h), *h, forktype);
00600     
00601     unlockhandle (h);
00602     
00603     return (fl);
00604     } /*saveresourcehandle*/
00605 #endif
00606 
00607 /*
00608 static for push/popresourcefile
00609 */
00610     private short newrnum, oldrnum;
00611 
00612 #ifdef MACVERSION
00613 static boolean pushresourcefile (const tyfilespec *fs, char permission, short forktype) {
00614     
00615     /*
00616     2006-01-30 creedon: added check for non-carbon OS and trying to access the data fork of a file, error message
00617     
00618     2005-09-02 creedon: added support for fork parameter, allows access to resources in data forks
00619 
00620     2.1b8 dmb: call SetResLoad (false) before opening a resource fork to 
00621     prevent all preload resources from being loaded into our heap
00622     */
00623     
00624     register OSErr errcode;
00625 #if TARGET_API_MAC_CARBON == 1
00626     FSRef   myRef;
00627 #else
00628     if (forktype == datafork) { // if we're not on carbon then trying to use the data fork for resources is an error
00629         langerrormessage ("\x4A" "Can't open the data fork for use with resources on this version of the OS.");
00630         return (false);
00631         }
00632 #endif
00633 
00634 #if TARGET_API_MAC_CARBON == 1
00635 
00636     errcode = FSpMakeFSRef (fs, &myRef);
00637     
00638     if (!errcode) {
00639     
00640         HFSUniStr255 fork;
00641         
00642         switch (forktype) {
00643             case resourcefork:
00644                 FSGetResourceForkName (&fork);
00645                 break;
00646                 
00647             case datafork:
00648                 FSGetDataForkName (&fork);
00649                 break;
00650             }
00651 #endif
00652         oldrnum = CurResFile ();
00653     
00654         SetResLoad (false);
00655     
00656         /*
00657         2005-08-31 creedon - in my reading about the FSOpenResourceFile function someone mentioned
00658         that it might not deal well with corrupted resources and that dropping back to FSpOpenResFile
00659         seemed to do the trick.  i've not done that here but could be tried if the problem is real
00660         and manifests. 
00661         */
00662     
00663 #if TARGET_API_MAC_CARBON == 1
00664 
00665         errcode = FSOpenResourceFile (&myRef, fork.length, fork.unicode, permission, &newrnum);
00666 #else
00667         // kw 2005-12-17 - OS9 compliance
00668         newrnum = FSpOpenResFile (fs, permission);
00669 
00670 #endif      
00671         SetResLoad (true);
00672 
00673 #if TARGET_API_MAC_CARBON == 1
00674         if (errcode != -1) /*opened OK*/
00675             return (true);
00676 #else
00677         if (newrnum != -1) /*opened OK*/
00678             return (true);
00679 #endif
00680         errcode = ResError ();
00681 
00682 #if TARGET_API_MAC_CARBON == 1
00683         }
00684 #endif
00685 
00686     if (errcode != eofErr) { /*don't want an alert if there isn't a resource fork*/
00687         
00688         setoserrorparam ((ptrstring) (*fs).name); /*in case error message takes a filename parameter*/
00689         
00690         oserror (errcode);
00691         }
00692     
00693     return (false);
00694     } /*pushresourcefile*/
00695 
00696 static boolean pushresourcefilereadonly (const tyfilespec *fs, short forktype) {
00697     
00698     /*
00699     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00700     */ 
00701     
00702     return (pushresourcefile (fs, fsRdPerm, forktype));
00703     } /*pushresourcefilereadonly*/
00704 
00705 
00706 static boolean pushresourcefilereadwrite (const tyfilespec *fs, short forktype) {
00707     
00708     /*
00709     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00710     */ 
00711     
00712     return (pushresourcefile (fs, fsRdWrPerm, forktype));
00713     } /*pushresourcefilereadwrite*/
00714 
00715 
00716 static boolean popresourcefile (void) {
00717     
00718     if ((newrnum == oldrnum) || (newrnum == filegetapplicationrnum ())) { /*don't close active resource fork!*/
00719         
00720         UpdateResFile (newrnum);
00721         
00722         UseResFile (oldrnum);
00723         }
00724     else
00725         closeresourcefile (newrnum);
00726     
00727     return (true);
00728     } /*popresourcefile*/
00729 #endif
00730 
00731 boolean loadresource (const tyfilespec *fs, short rnum, ResType type, short id, bigstring bsname, long sizedata, void *pdata, short forktype) {
00732     
00733     /*
00734     the inverse of saveresource.  we load in the indicated type and id from
00735     the indicated file on the indicated volume into the indicated data area.
00736     
00737     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00738     */
00739     
00740     register boolean fl = false;
00741     register Handle h;
00742     
00743     if (rnum != -1) /*have an open resource file to read from*/
00744         return (filereadresource (rnum, type, id, bsname, sizedata, pdata));
00745     
00746     #ifdef MACVERSION   
00747         if (!pushresourcefilereadonly (fs, forktype))
00748             return (false);
00749     #endif
00750     
00751     h = get1resource (type, id, bsname);
00752     
00753     if (h != nil) { /*file had the indicated resource*/
00754         
00755         long ctactualsize = gethandlesize (h);
00756         
00757         moveleft (*h, pdata, min (sizedata, ctactualsize)); 
00758         
00759         fl = true;
00760 
00761         #ifdef WIN95VERSION
00762             releaseresourcehandle (h);
00763         #endif
00764         }
00765     
00766     #ifdef MACVERSION
00767         popresourcefile ();
00768     #endif
00769         
00770     return (fl);
00771     } /*loadresource*/
00772 
00773 
00774 #ifdef MACVERSION
00775 boolean loadresourcehandle (const tyfilespec *fs, ResType type, short id, bigstring bsname, Handle *hresource, short forktype) {
00776     
00777     /*
00778     load a resource into a handle.
00779     
00780     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00781     
00782     9/13/91 dmb: now accept nil hresource to just check for resource's existence
00783     
00784     3.0b15 dmb: make sure handle isn't purgable after detaching
00785     */
00786     
00787     register boolean fl = false;
00788     register Handle h;
00789     
00790     if (!pushresourcefilereadonly (fs, forktype))
00791         return (false);
00792     
00793     if (hresource == nil) /*just checking existence*/
00794         SetResLoad (false);
00795     
00796     h = get1resource (type, id, bsname);
00797     
00798     if (hresource == nil) /*restore resload immediately!*/
00799         SetResLoad (true);
00800     
00801     if (h != nil) { /*got the indicated resource*/
00802         
00803         if (hresource != nil) { /*caller actually wants data*/
00804             
00805             DetachResource (h);
00806             
00807             HNoPurge (h); /*in case resource was purgable*/
00808             
00809             *hresource = h;
00810             }
00811         
00812         fl = true;
00813         }
00814     
00815     popresourcefile ();
00816     
00817     return (fl);
00818     } /*loadresourcehandle*/
00819 #endif
00820 
00821 
00822 #ifdef MACVERSION
00823 boolean deleteresource (const tyfilespec *fs, ResType type, short id, bigstring bsname, short forkttype) {
00824     
00825     /*
00826     delete the resource specified by type, id or bsname
00827     
00828     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00829     */
00830     
00831     register boolean fl = false;
00832     register Handle h;
00833     
00834     if (!pushresourcefilereadwrite (fs, forkttype))
00835         return (false);
00836     
00837     SetResLoad (false);
00838     
00839     h = get1resource (type, id, bsname);
00840     
00841     SetResLoad (true);
00842     
00843     if (h != nil) { /*got the indicated resource*/
00844         
00845         RemoveResource (h);
00846         
00847         fl = !oserror (ResError ());
00848         
00849         disposehandle (h);
00850         }
00851     
00852     popresourcefile ();
00853     
00854     return (fl);
00855     } /*deleteresource*/
00856 #endif
00857 
00858 
00859 #ifdef MACVERSION
00860 boolean getnumresourcetypes (const tyfilespec *fs, short *cttypes, short forktype) {
00861     
00862     /*
00863     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00864     */ 
00865     
00866     if (!pushresourcefilereadonly (fs, forktype))
00867         return (false);
00868     
00869     *cttypes = Count1Types ();
00870     
00871     popresourcefile ();
00872     
00873     return (true);
00874     } /*getnumresourcetypes*/
00875 #endif
00876 
00877 #ifdef MACVERSION
00878 boolean getnthresourcetype (const tyfilespec *fs, short n, ResType *type, short forktype) {
00879     
00880     /*
00881     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00882     */ 
00883     
00884     if (!pushresourcefilereadonly (fs, forktype))
00885         return (false);
00886     
00887     Get1IndType (type, n);
00888     
00889     popresourcefile ();
00890     
00891     return (*type != 0L);
00892     } /*getnthresourcetype*/
00893 #endif
00894 
00895 #ifdef MACVERSION
00896 boolean getnumresources (const tyfilespec *fs, ResType type, short *ctresources, short forktype) {
00897     
00898     /*
00899     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00900     */ 
00901     
00902     if (!pushresourcefilereadonly (fs, forktype))
00903         return (false);
00904     
00905     *ctresources = Count1Resources (type);
00906     
00907     popresourcefile ();
00908     
00909     return (true);
00910     } /*getnumresources*/
00911 #endif
00912 
00913 #ifdef MACVERSION
00914 boolean getnthresourcehandle (const tyfilespec *fs, ResType type, short n, short *id, bigstring bsname, Handle *hresource, short forktype) {
00915     
00916     /*
00917     load the nth resource of the given type into a handle.  also return the 
00918     id and name of the resource
00919     
00920     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00921     
00922     6/2/92 dmb: accept nil hresource for info retrieval
00923     
00924     3.0b15 dmb: make sure handle isn't purgable after detaching
00925     */
00926     
00927     register boolean fl = false;
00928     register Handle h;
00929     
00930     if (!pushresourcefilereadonly (fs, forktype))
00931         return (false);
00932     
00933     if (hresource == nil) /*just checking existence*/
00934         SetResLoad (false);
00935     
00936     h = Get1IndResource (type, n);
00937     
00938     if (hresource == nil) /*restore resload immediately!*/
00939         SetResLoad (true);
00940     
00941     if (h != nil) { /*got the indicated resource*/
00942         
00943         GetResInfo (h, id, &type, bsname);
00944         
00945         if (hresource != nil) { /*caller actually wants data*/
00946             
00947             DetachResource (h);
00948             
00949             HNoPurge (h); /*in case resource was purgable*/
00950             
00951             *hresource = h;
00952             }
00953         
00954         fl = true;
00955         }
00956     
00957     popresourcefile ();
00958     
00959     return (fl);
00960     } /*getnthresourcehandle*/
00961 #endif
00962 
00963 
00964 #ifdef MACVERSION
00965 static boolean getemptyresourcehandle (ResType type, short id, bigstring bsname, Handle *hresource) {
00966     
00967     /*
00968     get a handle to the resource in current resoure file specified by id or 
00969     bsname, but don't load the actual data into memory.
00970     */
00971     
00972     register Handle h;
00973     
00974     SetResLoad (false);
00975     
00976     h = get1resource (type, id, bsname);
00977     
00978     SetResLoad (true);
00979     
00980     *hresource = h;
00981     
00982     if (h == nil)  { /*didn't find the indicated resource*/
00983         
00984         oserror (resNotFound);
00985         
00986         return (false);
00987         }
00988     
00989     return (true);
00990     } /*getemptyresourcehandle*/
00991 #endif
00992 
00993 
00994 #ifdef MACVERSION
00995 boolean getresourceattributes (const tyfilespec *fs, ResType type, short id, bigstring bsname, short *resattrs, short forktype) {
00996     
00997     /*
00998     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
00999     
01000     2.1b4 dmb: new routine
01001     */
01002     
01003     Handle h;
01004     register boolean fl = false;
01005     
01006     if (!pushresourcefilereadonly (fs, forktype))
01007         return (false);
01008     
01009     if (getemptyresourcehandle (type, id, bsname, &h)) { /*got the indicated resource*/
01010         
01011         *resattrs = GetResAttrs (h);
01012         
01013         fl = !oserror (ResError ());
01014         }
01015     
01016     popresourcefile ();
01017     
01018     return (fl);
01019     } /*getresourceattributes*/
01020 #endif
01021 
01022 
01023 #ifdef MACVERSION
01024 boolean setresourceattributes (const tyfilespec *fs, ResType type, short id, bigstring bsname, short resattrs, short forktype) {
01025     
01026     /*
01027     2005-09-02 creedon: added support for fork parameter, see resources.c: openresourcefile and pushresourcefile
01028 
01029     2.1b4 dmb: new routine
01030     */
01031     
01032     Handle h;
01033     register boolean fl = false;
01034     short validattrs = (resSysHeap | resPurgeable | resLocked | resProtected | resPreload);
01035 //  #define validattrs (resSysHeap | resPurgeable | resLocked | resProtected | resPreload)
01036     
01037     if (!pushresourcefilereadwrite (fs, forktype))
01038         return (false);
01039     
01040     if (getemptyresourcehandle (type, id, bsname, &h)) { /*got the indicated resource*/
01041         
01042         SetResAttrs (h, resattrs & validattrs);
01043         
01044         fl = !oserror (ResError ());
01045         }
01046     
01047     popresourcefile ();
01048     
01049     return (fl);
01050     } /*setresourceattributes*/
01051 #endif
01052 
01053 
01054 Handle getresourcehandle (ResType type, short id)   {
01055 #ifdef MACVERSION
01056     return (GetResource (type, id));
01057 #endif
01058 
01059 #ifdef WIN95VERSION
01060     return (filegetresource (0, type, id, NULL));
01061 #endif
01062     }
01063 
01064 
01065 void releaseresourcehandle (Handle h) {
01066 #ifdef MACVERSION
01067     ReleaseResource (h);
01068 #endif
01069 
01070 #ifdef WIN95VERSION
01071     DisposeHandle (h);
01072 #endif
01073     } /*releaseresourcehandle*/
01074 

Generated on Wed May 31 18:20:00 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6