home *** CD-ROM | disk | FTP | other *** search
/ More MacCube 1: Arcade Games / More MacCube Vol 1 Arcade Games.bin / Games⁄Arcade / Bolo / More information / Sample Code / Std Autopilot / BF_Prefs.c < prev    next >
Encoding:
Text File  |  1995-05-13  |  3.0 KB  |  106 lines  |  [TEXT/KAHL]

  1. // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
  2. // All rights reserved. This code is owned by Stuart Cheshire and is donated
  3. // free of charge for non-commercial use. You may not use this code in any
  4. // product sold for commercial profit, except shareware priced at $25 or less.
  5.  
  6. #include <Types.h>
  7. #include <Files.h>
  8.  
  9. #include "BrainFrame.h"
  10. #include "BF_Main.h"
  11. #include "BF_Utils.h"
  12. #include "BF_Prefs.h"
  13.  
  14. #define CURRENT_PREFS_VERSION 1
  15.  
  16. local PREFS prefs_on_disk;
  17. export PREFS prefs;
  18.  
  19. export FSSpec PrefsFile;
  20. local const u_char defaultname[] = "\pBrainFrame Preferences";
  21.  
  22. local void FindPrefsFile(Boolean create)
  23.     {
  24.     int i;
  25.     Str255 filename;
  26.     HParamBlockRec fi;
  27.     
  28.     fi.fileParam.ioCompletion = 0L;
  29.     fi.fileParam.ioNamePtr    = (StringPtr)&filename;
  30.     fi.fileParam.ioVRefNum    = PrefsFolderWD;
  31.     fi.fileParam.ioDirID      = 0;
  32.     fi.fileParam.ioFDirIndex  = 1;
  33.     
  34.     while (PBHGetFInfo(&fi, false) == noErr)
  35.         {
  36.         if (fi.fileParam.ioFlFndrInfo.fdType    == 'PREF' &&
  37.             fi.fileParam.ioFlFndrInfo.fdCreator == CREATOR_CODE)
  38.                 {
  39.                 PrefsFile.vRefNum = PrefsFolderWD;
  40.                 PrefsFile.parID   = 0;
  41.                 for (i=0; i<=filename[0]; i++) PrefsFile.name[i] = filename[i];
  42.                 return;
  43.                 }
  44.         fi.fileParam.ioFDirIndex++;
  45.         fi.fileParam.ioDirID = 0;
  46.         }
  47.     if (create)
  48.         {
  49.         PrefsFile.vRefNum = PrefsFolderWD;
  50.         PrefsFile.parID   = 0;
  51.         for (i=0; i<=defaultname[0]; i++) PrefsFile.name[i] = defaultname[i];
  52.         Create(PrefsFile.name, PrefsFile.vRefNum, CREATOR_CODE, 'PREF');
  53.         }
  54.     }
  55.  
  56. local short OpenPrefsFile(short *fRef, Boolean for_writing)
  57.     {
  58.     short permission = fsRdPerm;
  59.     if (for_writing) permission = fsRdWrPerm;
  60.     *fRef = 0;
  61.     if (!PrefsFile.vRefNum) FindPrefsFile(for_writing);
  62.     if (!PrefsFile.vRefNum) return(1);
  63.     if (PrefsFile.parID) return(FSpOpenDF(&PrefsFile, permission, fRef));
  64.     else return(FSOpen(PrefsFile.name, PrefsFile.vRefNum, fRef));
  65.     }
  66.  
  67. local void set_default_prefs(void)
  68.     {
  69.     prefs_on_disk.prefs_version = 0;                // mark the disk copy as garbage
  70.     prefs.prefs_version   = CURRENT_PREFS_VERSION;    // and create new prefs in memory
  71.     prefs.frametime       = 2;
  72.     }
  73.  
  74. // if prefs != prefs_on_disk then we write back to disk
  75. // (otherwise don't do it, to avoid spinning up the disk)
  76. export void save_prefs(void)
  77.     {
  78.     if (prefs.prefs_version != CURRENT_PREFS_VERSION) return;
  79.     if (compare_structs(prefs, prefs_on_disk))
  80.         {
  81.         short fRef;
  82.         if (OpenPrefsFile(&fRef, TRUE) == noErr)
  83.             {
  84.             long len = sizeof(PREFS);
  85.             prefs_on_disk = prefs;
  86.             SetEOF(fRef,0);        // truncate the file before writing new data
  87.             FSWrite(fRef, &len, &prefs_on_disk);
  88.             FSClose(fRef);
  89.             }
  90.         }
  91.     }
  92.  
  93. export void readprefs(void)    // Only called ONCE, at startup
  94.     {
  95.     short fRef;
  96.     long len;
  97.     OSErr readcode=0, opencode = OpenPrefsFile(&fRef, FALSE);
  98.     if (opencode == noErr && GetEOF(fRef, &len) == noErr && len == sizeof(PREFS))
  99.         readcode = FSRead(fRef, &len, &prefs_on_disk);
  100.     prefs = prefs_on_disk;
  101.     if (opencode || readcode || prefs.prefs_version != CURRENT_PREFS_VERSION)
  102.         set_default_prefs();
  103.     if (opencode == noErr) FSClose(fRef);
  104.     save_prefs();    // If we created new default prefs this will write them to disk
  105.     }
  106.