home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / appe Windows 1.51.1 / contexts.c < prev    next >
Encoding:
Text File  |  1995-01-11  |  3.7 KB  |  128 lines  |  [TEXT/KAHL]

  1. // File "contexts.c" -
  2. // This source file is Copyright Matt Slot & Slot-Machines Ltd., © 1994
  3.  
  4. #include <Folders.h>
  5. #include <Processes.h>
  6.  
  7. #include "main.h"
  8. #include "contexts.h"
  9.  
  10. // * ****************************************************************************** *
  11. // Global Vars
  12.  
  13. extern GlobalsRec glob;
  14.     
  15. // * ****************************************************************************** *
  16. // * ****************************************************************************** *
  17.  
  18. short CloneResFile() {
  19.     short err, i, j, makeInvis;
  20.     short rID, appFile, cloneFile;
  21.     long rType;
  22.     Handle rsrc;
  23.     Str63 textBuff;
  24.     FInfo fInfo;
  25.     ProcessSerialNumber psn;
  26.     ProcessInfoRec pInfo;
  27.  
  28.     if (FindFolder(kOnSystemDisk, kTemporaryFolderType, -1, &glob.cloneSpec.vRefNum,
  29.             &glob.cloneSpec.parID) && FindFolder(kOnSystemDisk, kSystemFolderType,
  30.             -1, &glob.cloneSpec.vRefNum ,&glob.cloneSpec.parID)) {
  31.         GetCurrentProcess(&psn);
  32.         pInfo.processInfoLength = sizeof(pInfo);
  33.         pInfo.processName = textBuff;
  34.         pInfo.processAppSpec = &glob.cloneSpec;
  35.         GetProcessInformation(&psn, &pInfo);
  36.         makeInvis = -1;
  37.         }
  38.       else makeInvis = 0;
  39.       
  40.     BlockMove((Ptr) 0x910, glob.cloneSpec.name, sizeof(glob.cloneSpec.name));
  41.     if (glob.cloneSpec.name[0] > 25) glob.cloneSpec.name[0] = 25;
  42.     NumToString(TickCount() % 10000, textBuff);
  43.     BlockMove(textBuff, glob.cloneSpec.name + glob.cloneSpec.name[0] + 1, textBuff[0]+1);
  44.     glob.cloneSpec.name[glob.cloneSpec.name[0] + 1] = '\r';
  45.     glob.cloneSpec.name[0] += textBuff[0] + 1;
  46.     
  47.     FSpCreateResFile(&glob.cloneSpec, kCreatorType, '????', 0);
  48.     if ((err = ResError()) == dupFNErr) {
  49.         FSpDelete(&glob.cloneSpec);
  50.         FSpCreateResFile(&glob.cloneSpec, kCreatorType, '????', 0);
  51.         }
  52.     if (err = ResError()) return(err);
  53.  
  54.     // Only make it invisible if it *had* to be put in the same folder
  55.     if (makeInvis) {
  56.         FSpGetFInfo(&glob.cloneSpec, &fInfo);
  57.         fInfo.fdFlags |= fInvisible;
  58.         FSpSetFInfo(&glob.cloneSpec, &fInfo);
  59.         }
  60.                 
  61.     glob.appFile = appFile = CurResFile();
  62.     cloneFile = FSpOpenResFile(&glob.cloneSpec, fsRdWrPerm);
  63.     UseResFile(appFile);
  64.     for(i=Count1Types(); i; i--) {
  65.         Get1IndType((ResType *) &rType, i);
  66.         if (rType == 'CODE') continue;
  67.         for(j=Count1Resources(rType); j; j--) {
  68.             rsrc = Get1IndResource(rType, j);
  69.             GetResInfo(rsrc, &rID, (ResType *) &rType, textBuff);
  70.             DetachResource(rsrc);
  71.             UseResFile(cloneFile);
  72.             AddResource(rsrc, rType, rID, textBuff);
  73.             WriteResource(rsrc);
  74.             if (ResError()) return(err);
  75.             UseResFile(appFile);
  76.             }
  77.         }
  78.     CloseResFile(cloneFile);
  79.     return(err = ResError());
  80.     }
  81.  
  82. // * ****************************************************************************** *
  83. // * ****************************************************************************** *
  84.  
  85. ContextPtr SaveContext(short homeContext) {
  86.     ContextPtr context;
  87.     
  88.     context = (ContextPtr) NewPtrClear(sizeof(*context));
  89.     if (!context) return;
  90.     
  91.     asm {
  92.         move.l        context, a0
  93.         move.l        a5, (a0)
  94.         }
  95.         
  96.     GetPort(&context->savePort);
  97.     context->heapZone = GetZone();
  98.     context->curResFile = (homeContext) ? 0 : CurResFile();
  99.     
  100.     return(context);
  101.     }
  102.  
  103. // * ****************************************************************************** *
  104. // * ****************************************************************************** *
  105.  
  106. void RestoreContext(ContextPtr context) {
  107.     if (!context) return;
  108.     
  109.     asm {
  110.         move.l        context, a0
  111.         move.l        (a0), a5
  112.         }
  113.         
  114.     SetPort(context->savePort);
  115.     SetZone(context->heapZone);
  116.     if (context->curResFile) UseResFile(context->curResFile);
  117.     }
  118.  
  119.  
  120. // * ****************************************************************************** *
  121. // * ****************************************************************************** *
  122.  
  123. void DisposeContext(ContextPtr context) {
  124.     if (! context) return;
  125.     
  126.     DisposePtr((Ptr) context);
  127.     }
  128.