home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / myMemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-25  |  5.5 KB  |  187 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "FabLibResIDs.h"
  14. #include    "SimResIDs.h"
  15.  
  16. #include    "Globals.h"
  17. #include    "Animation.h"
  18. #include    "Disasm.h"
  19. #include    "Dump.h"
  20. #include    "myMemory.h"
  21. #include    "Registers.h"
  22. #include    "SimUtils.h"
  23.  
  24. #if defined(FabSystem7orlater)
  25.  
  26. #pragma segment Rare
  27.  
  28. /* myOpenFile: opens a dump file */
  29.  
  30. OSErr    myOpenFile(FSSpec *theFile, Ptr where, Size howmuch)
  31. {
  32. ParamBlockRec    myPB;
  33. EventRecord    dummyEv;
  34. short    myFileRefN;
  35. OSErr    err;
  36.  
  37. SetCursor(*gWatchHandle);
  38. if ((err = FSpOpenDFCompat(theFile, fsRdPerm, &myFileRefN)) == noErr) {
  39.     myPB.ioParam.ioCompletion = nil;
  40.     myPB.ioParam.ioRefNum = myFileRefN;
  41.     myPB.ioParam.ioBuffer = where;
  42.     myPB.ioParam.ioReqCount = howmuch;
  43.     myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  44.     myPB.ioParam.ioPosOffset = 0L;
  45.     (void)PBReadAsync(&myPB);
  46.     while (myPB.ioParam.ioResult > 0) {
  47.         SystemTask();
  48.         (void)EventAvail(everyEvent, &dummyEv);
  49.         }
  50.     err = myPB.ioParam.ioResult;
  51.     (void)FSClose(myFileRefN);
  52.     if (where == (Ptr)&gRegs)
  53.         ChangedAllRegisters();
  54.     else {
  55.         InvalDump();
  56.         InvalDisasm();
  57.         }
  58.     }
  59. SetCursor(&qd.arrow);
  60. return(err);
  61. }
  62.  
  63. /* mySaveFile: saves a dump file to disk */
  64.  
  65. OSErr mySaveFile(Ptr where, Size howmuch, OSType fType, short prompt, short defName)
  66. {
  67. StandardFileReply    mySFR;
  68. Str255    tempFName;
  69. ParamBlockRec    myPB;
  70. EventRecord    dummyEv;
  71. FSSpec    thisFSSpec;
  72. long    thisDirID;
  73. short    thisVRefNum, tmpFRefN;
  74. SignedByte    state1, state2;
  75. Handle    sH1 = (Handle)GetString(prompt);
  76. Handle    sH2 = (Handle)GetString(defName);
  77. OSErr    err;
  78.  
  79. err = noErr;
  80. state1 = WantThisHandleSafe(sH1);
  81. state2 = WantThisHandleSafe(sH2);
  82. StandardPutFile((ConstStr255Param)*sH1, (ConstStr255Param)*sH2, &mySFR);
  83. HSetState(sH2, state2);
  84. HSetState(sH1, state1);
  85. if (mySFR.sfGood) {
  86.     SetCursor(*gWatchHandle);
  87.     myPB.ioParam.ioCompletion = nil;
  88.     myPB.ioParam.ioBuffer = where;
  89.     myPB.ioParam.ioReqCount = howmuch;
  90.     myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  91.     if (mySFR.sfReplacing)
  92.         if (0)    /* test if file locked (FSpExchange does not) */
  93.             err = fLckdErr;
  94.         else {
  95.             MyNumToString(TickCount(), tempFName);
  96.             if ((err = FindFolder(mySFR.sfFile.vRefNum, kTemporaryFolderType,
  97.                                 kCreateFolder, &thisVRefNum, &thisDirID)) == noErr) {
  98.                 (void)FSMakeFSSpecCompat(thisVRefNum, thisDirID, (ConstStr255Param)&tempFName, &thisFSSpec);
  99.                 if ((err = FSpCreateCompat(&thisFSSpec, kFCR_MINE, fType,
  100.                                     mySFR.sfScript)) == noErr)
  101.                     if ((err = FSpOpenDFCompat(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  102.                         myPB.ioParam.ioRefNum = tmpFRefN;
  103.                         myPB.ioParam.ioPosOffset = 0L;
  104.                         (void)PBWriteAsync(&myPB);    // in temporary file
  105.                         while (myPB.ioParam.ioResult > 0) {
  106.                             SystemTask();
  107.                             (void)EventAvail(everyEvent, &dummyEv);
  108.                             }
  109.                         (void)FSClose(tmpFRefN);
  110.                         if ((err = myPB.ioParam.ioResult) == noErr) {
  111.                             if ((err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE,
  112.                                                     fType, kSTR_ApplicationName, mySFR.sfScript))
  113.                                     == noErr)
  114.                                 if ((err = FSpExchangeFilesCompat(&thisFSSpec, &(mySFR.sfFile)))
  115.                                         == noErr)
  116.                                     err = FSpDeleteCompat(&thisFSSpec);
  117.                             }
  118.                         else {
  119.                             (void)FSpDeleteCompat(&thisFSSpec);    // the temporary one
  120.                             if (err == dskFulErr) {
  121.                                 if (CautionAlert_UPP(kALRT_NOSAFESAVE, myStdFilterProc) == ok) {
  122.                                     if ((err = FSpOpenDFCompat(&(mySFR.sfFile), fsWrPerm,
  123.                                                         &tmpFRefN)) == noErr) {
  124.                                         myPB.ioParam.ioRefNum = tmpFRefN;
  125.                                         myPB.ioParam.ioPosOffset = 0L;
  126.                                         (void)PBWriteAsync(&myPB);
  127.                                         while (myPB.ioParam.ioResult > 0) {
  128.                                             SystemTask();
  129.                                             (void)EventAvail(everyEvent, &dummyEv);
  130.                                             }
  131.                                         (void)FSClose(tmpFRefN);
  132.                                         err = myPB.ioParam.ioResult;
  133.                                         }
  134.                                     }
  135.                                 else err = noErr; /* user clicked cancel in alert */
  136.                                 }
  137.                             }
  138.                         }
  139.                 }
  140.             }
  141. /* not replacing an existing file */
  142.     else if ((err = FSpCreateCompat(&(mySFR.sfFile), kFCR_MINE, fType, mySFR.sfScript)) == noErr)
  143.         if ((err = FSpOpenDFCompat(&(mySFR.sfFile), fsWrPerm, &tmpFRefN)) == noErr) {
  144.             myPB.ioParam.ioRefNum = tmpFRefN;
  145.             myPB.ioParam.ioPosOffset = 0L;
  146.             (void)PBWriteAsync(&myPB);
  147.             while (myPB.ioParam.ioResult > 0) {
  148.                 SystemTask();
  149.                 (void)EventAvail(everyEvent, &dummyEv);
  150.                 }
  151.             (void)FSClose(tmpFRefN);
  152.             if ((err = myPB.ioParam.ioResult) == noErr)
  153.                 err = AddSTRRes2Doc(&(mySFR.sfFile), kFCR_MINE, fType,
  154.                                     kSTR_ApplicationName, mySFR.sfScript);
  155.             }
  156.     SetCursor(&qd.arrow);
  157.     }
  158. return err;
  159. }
  160.  
  161.  
  162. OSErr    OpenProcessorState(FSSpecPtr theFile)
  163. {
  164. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  165. OSErr    err;
  166.  
  167. if (noErr == (err = myOpenFile(theFile, (Ptr)&state, sizeof(state)))) {
  168.     BlockMoveData((Ptr)&state, (Ptr)&gParts, sizeof(gParts));
  169.     BlockMoveData((Ptr)(state + kNUMOFPARTS), (Ptr)&gRegs, sizeof(state) - sizeof(gParts));
  170.     ChangedAllRegisters();
  171.     ChangedAllBoxes();
  172.     }
  173. return err;
  174. }
  175.  
  176. OSErr    SaveProcessorState(void)
  177. {
  178. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  179.  
  180. BlockMoveData((Ptr)&gParts, (Ptr)&state, sizeof(gParts));
  181. BlockMoveData((Ptr)&gRegs, (Ptr)(state + kNUMOFPARTS), sizeof(state) - sizeof(gParts));
  182. return mySaveFile((Ptr)&state, sizeof(state), kFTY_REG, kSTR_REGSPROMPT, kSTR_REGSDEFNAME);
  183. }
  184.  
  185. #endif
  186.  
  187.