home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / Find_prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  3.9 KB  |  157 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include <PrintTraps.h>
  3. #include <Folders.h>
  4. #include "mac-specific.h"
  5. #include "dvihead.h"
  6. #include "gendefs.h"
  7. #include "gblprocs.h"
  8.  
  9. Boolean TrapAvailable( short trapnum );
  10.  
  11. #ifndef GESTALT
  12. #define GESTALT            0xA1AD
  13. #endif
  14.  
  15. #ifndef NIL
  16. #define NIL        0L
  17. #endif
  18.  
  19. #define        PREF_TYPES    4
  20.  
  21. short    g_pref_resfile;    /* reference number of preferences file */
  22. short    g_pref_vRefNum;    /* vRefNum of preferences file */
  23.  
  24. void    Find_prefs( void );
  25.  
  26. void    Find_prefs( void )
  27. {
  28.     long    response;
  29.     OSErr    err;
  30.     long    dirID, procID;
  31.     SysEnvRec    world;
  32.     HParamBlockRec    hpb;
  33.     Str255    name;
  34.     Boolean    newfile;
  35.     ResType    pref_type[PREF_TYPES]
  36.         = {'Cstr', 'int ', 'WPos', 'Bool'};
  37.     short    pt_num;
  38.     ResType    what_type;
  39.     THPrint    print_rec_h;
  40.     Handle    a_res;
  41.     short    index, res_id;
  42.     Str255    standard_name = "\pDVIM72-Mac Prefs";
  43.     short    res_attr;
  44.     
  45.     g_pref_resfile = -1;
  46.     newfile = false;
  47.     
  48.     /*
  49.         First on the agenda is to find the right folder.  If the
  50.         Folder Manager is available, we use if to find the Preferences
  51.         folder, otherwise we use SysEnvirons to find the System Folder.
  52.     */
  53.     if (!TrapAvailable(GESTALT) ||
  54.         (noErr != Gestalt( gestaltFindFolderAttr, &response )) ||
  55.         !(response & 1L) ||
  56.         (noErr != (err = FindFolder( kOnSystemDisk,'pref', kCreateFolder,
  57.             &g_pref_vRefNum, &dirID))) )
  58.     {
  59.         err = SysEnvirons( 1, &world );
  60.         procID = NIL;
  61.         err = GetWDInfo( world.sysVRefNum, &g_pref_vRefNum, &dirID, &procID );
  62.     }
  63.     /*
  64.         At this point, unless something awful has happened, we have the
  65.         dirID and vRefNum of the appropriate folder.  Next we try to
  66.         find the file, which should have creator 'dviM' and type 'dviP'.
  67.     */
  68.     hpb.ioParam.ioCompletion = NIL;
  69.     hpb.ioParam.ioNamePtr = name;
  70.     hpb.ioParam.ioVRefNum = g_pref_vRefNum;
  71.     hpb.fileParam.ioDirID = dirID;
  72.     hpb.fileParam.ioFDirIndex = 1;
  73.     err = noErr;
  74.     do {
  75.         err = PBHGetFInfo( &hpb, false );
  76.         hpb.fileParam.ioDirID = dirID;
  77.         hpb.fileParam.ioFDirIndex ++;
  78.     } while ( (err == noErr) &&
  79.         ((hpb.fileParam.ioFlFndrInfo.fdType != 'dviP') ||
  80.             (hpb.fileParam.ioFlFndrInfo.fdCreator != 'dviM') ) );
  81.     /*
  82.         If we didn't find the file, we need to create it.
  83.     */
  84.     if (err != noErr)
  85.     {
  86.         BlockMove( standard_name, name, 256 );
  87.         err = HCreate( g_pref_vRefNum, dirID, name, 'dviM', 'dviP' );
  88.         if (err == noErr)
  89.         {
  90.             HCreateResFile( g_pref_vRefNum, dirID, name );
  91.             err = ResError();
  92.         }
  93.         if (err == noErr)
  94.             Show_error( "Preferences file not found; a new one "
  95.                 "will be created." );
  96.         else
  97.             Show_error( "Preferences file not found; error creating "
  98.                 "new one." );
  99.         newfile = true;
  100.     }
  101.     if (err != noErr)
  102.         return;    /* give up */
  103.     /*
  104.         Now, unless we have an error code, we have a file.
  105.         Time to open it.
  106.     */
  107.     g_pref_resfile = HOpenResFile( g_pref_vRefNum, dirID, name, fsRdWrPerm );
  108.     if (g_pref_resfile == -1)
  109.     {
  110.         fatal( "Error opening preferences file." );
  111.     }
  112.     /*
  113.         If we just created this file, we need to copy some resources to it
  114.         from our application.
  115.     */
  116.     if (newfile)
  117.     {
  118.         print_rec_h = (THPrint) NewHandle( sizeof(TPrint) );
  119.         if ( (print_rec_h == nil) || (MemError() != noErr) )
  120.         {
  121.             fatal("Find_prefs: Can't get memory for print record.");
  122.         }
  123.         PrOpen();
  124.         if (PrError() != noErr)
  125.         {
  126.             fatal("Find_prefs: Can't open printer driver.");
  127.         }
  128.         PrintDefault( print_rec_h );
  129.         PrClose();
  130.         AddResource( (Handle) print_rec_h, 'Prec', 128, "\p" );
  131.         if (ResError() != noErr)
  132.         {
  133.             fatal("Find_prefs: can't add 'Prec' resource.");
  134.         }
  135.         for (pt_num = 0; pt_num < PREF_TYPES; pt_num++)
  136.         {
  137.             what_type = pref_type[pt_num];
  138.             for (index = 1; ; index++)
  139.             {
  140.                 UseResFile( g_app_resfile );
  141.                 a_res = Get1IndResource( what_type, index );
  142.                 if (a_res == NIL)
  143.                     break;
  144.                 GetResInfo( a_res, &res_id, &what_type, name );
  145.                 res_attr = GetResAttrs( a_res );
  146.                 DetachResource( a_res );
  147.                 UseResFile( g_pref_resfile );
  148.                 AddResource( a_res, what_type, res_id, name );
  149.                 SetResAttrs( a_res, res_attr );
  150.                 ChangedResource( a_res );
  151.             }
  152.         }
  153.         UseResFile( g_pref_resfile );
  154.         UpdateResFile( g_pref_resfile );
  155.         err = FlushVol( NIL, g_pref_vRefNum );
  156.     }
  157. }