home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / pref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  9.3 KB  |  399 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    pref.c
  5. *    Preferences file
  6. *        
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *    
  15. *    The structure of the Prefs file is simple yet flexible. Each element of gPrefs has
  16. *    got a "tag" to identify it. Written out to the Prefs file is the tag (0-255), two
  17. *    bytes specifying the length of the following data, and the value of the gPrefs
  18. *    element. Using this tag-value approach keeps the Prefs file upward and downward
  19. *    compatible and extendible compared to a simple dump of gPrefs.
  20. **********************************************************************
  21. *    
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. #include "EMBL-Search.h"
  27. #include "EMBL-Search.rsrc.h"
  28. #include "tag.h"
  29.  
  30. typedef struct PrefHeader {
  31.     Byte tag;
  32.     unsigned short dataLen;
  33. } PrefHeader;
  34.  
  35. /*
  36. ******************************* Prototypes ****************************
  37. */
  38.  
  39. #include "pref.h"
  40. #include "util.h"
  41. #include "pstr.h"
  42. #include "window.h"
  43.  
  44. static OSErr ReadPrefs(void);
  45. static void SetPref(Byte tag, Byte *data);
  46. static void InitPrefs(void);
  47. static Boolean Write1Pref(short output, PrefHeader *ph, Byte *data);
  48.  
  49.  
  50.  
  51. /*
  52. ********************************* Globals *****************************
  53. */
  54.  
  55. extern short gPrefVRefNum;
  56. extern char gError[256];
  57.  
  58. Prefs gPrefs;
  59. OSType gCreatorSig[6] = {'????','MSWD','MACA','ttxt','????','????'};
  60.  
  61. /**************************************
  62. *    Read and set preferences
  63. */
  64.  
  65. void GetPrefs()
  66. {
  67.     OSErr err;
  68.     
  69.     InitPrefs();
  70.     err=ReadPrefs();
  71.     if(err != noErr) {        /* on error, we reset preferences */
  72.         InitPrefs();
  73.         if( err == fnfErr)    /* and create a new Prefs file if necessary */
  74.             WritePrefs();
  75.     }
  76. }
  77.  
  78. /**************************************
  79. *    Read Preferences file
  80. *    Return value:    noErr, if successful
  81. *                        OS error code, if error occurred
  82. */
  83.  
  84. static OSErr ReadPrefs()
  85. {
  86.     OSErr        err;
  87.     short        input;
  88.     unsigned long        count;
  89.     short        wdRefNum;
  90.     Str255    fName;
  91.     PrefHeader ph;
  92.     Byte        data[512];
  93.     
  94.     /* get file name */
  95.     GetIndString(fName,OTHERS,PREFSNAME);
  96.     wdRefNum=gPrefVRefNum;
  97.  
  98.     /* open file */
  99.     err = OpenMacFileReadOnly(fName,wdRefNum,&input,FALSE);
  100.     if(err != noErr) {
  101.         if(err == fnfErr)
  102.            ErrorMsg(ERR_NOPREF);
  103.        else {
  104.             sprintf(gError,LoadErrorStr(ERR_OPENPREF,FALSE),err);
  105.             ErrorMsg(0);
  106.         }
  107.         return(err);
  108.     }
  109.     else {
  110.         while(err == noErr) {    /* continue until we hit EOF */
  111.             /* read header */
  112.             count = sizeof(PrefHeader);
  113.             err=ReadMacFile( input, (long *)&count, &ph,fName, FALSE );
  114.             if(err == eofErr) {    /* hit end of file */
  115.                 err = noErr;
  116.                 break;
  117.             }
  118.             if(err == noErr) {
  119.                 /* read data */
  120.                 if(ph.dataLen >512) ph.dataLen = 512; /* should issue a warning here */
  121.                 count=(unsigned long)ph.dataLen;
  122.                 err=ReadMacFile( input, (long *)&count, &data,fName, FALSE );
  123.             }
  124.             if( err != noErr ) {
  125.                 sprintf(gError,LoadErrorStr(ERR_READPREF,FALSE),err);
  126.                 ErrorMsg(0);
  127.             }
  128.             else
  129.                 SetPref(ph.tag,data);
  130.         }
  131.         
  132.        FSClose(input);
  133.        return(err);
  134.    }
  135. }
  136.  
  137. /**************************************
  138. *    Set a global preference defined by "tag" to the value given in "data"
  139. */
  140.  
  141. static void SetPref(Byte tag, Byte *data)
  142. {
  143.     switch(tag) {
  144.         case T_EMBLINXDIR:
  145.             gPrefs.inxDirSpec[DB_EMBL] = *(DirSpec *)data;
  146.             break;
  147.         case T_SWISSINXDIR:
  148.             gPrefs.inxDirSpec[DB_SWISS] = *(DirSpec *)data;
  149.             break;
  150.         case T_FORMAT:
  151.             gPrefs.format = *(short *)data;
  152.             break;
  153.         case T_CREATOR:
  154.             gPrefs.creator = *(short *)data;
  155.             break;
  156.         case T_CREATORNAME:
  157.             pstrcpy(gPrefs.creatorName,(StringPtr)data);
  158.             break;
  159.         case T_CREATORSIG:
  160.             gPrefs.creatorSig = *(OSType *)data;
  161.             break;
  162.         case T_CONFIRMCHG:
  163.             gPrefs.confirmChg = *(Boolean *)data;
  164.             break;
  165.         case T_STARTQOPEN:
  166.             gPrefs.startQOpen = *(Boolean *)data;
  167.             break;
  168.     }
  169. }
  170.  
  171. /**************************************
  172. *    Set default values for Preferences
  173. *    Return value:    none
  174. */
  175.  
  176. static void InitPrefs()
  177. {
  178.     register short i;
  179.     
  180.     for(i=0;i<DB_NUM;++i) {
  181.         *gPrefs.inxDirSpec[i].volName = EOS;
  182.         gPrefs.inxDirSpec[i].dirID = 0;
  183.     }
  184.     gPrefs.format=EMBL_FORMAT;
  185.     gPrefs.creator=WORD_I;
  186.     *gPrefs.creatorName = EOS;
  187.     gPrefs.creatorSig = gCreatorSig[WORD_I];
  188.     gPrefs.confirmChg = TRUE;
  189.     gPrefs.startQOpen = TRUE;
  190. }
  191.  
  192. /**************************************
  193. *    Write Preferences file
  194. */
  195.  
  196. void WritePrefs()
  197. {
  198.     OSErr        err;
  199.     short        output;
  200.     long        count;
  201.     short        wdRefNum;
  202.     Str255    fName;
  203.     PrefHeader ph;
  204.     Byte        data[512];
  205.     Boolean    ret;
  206.     
  207.     /* get file name */    
  208.     GetIndString(fName,OTHERS,PREFSNAME);
  209.     wdRefNum=gPrefVRefNum;
  210.     
  211.     /* create new version */
  212.     if( (err=CreateMacFile(fName,wdRefNum,kApplSignature,kPrefFileType,FALSE)) != noErr) {
  213.         sprintf(gError,LoadErrorStr(ERR_CREATPREF,FALSE),err);
  214.         ErrorMsg(0);
  215.     }
  216.    else {    /* open file */
  217.        if( (err=OpenMacFile(fName,wdRefNum,&output,TRUE)) == noErr) {
  218.            /* write data */
  219.            
  220.            ph.tag = T_EMBLINXDIR;
  221.            ph.dataLen = (unsigned short) sizeof(DirSpec);
  222.            *(DirSpec *)data = gPrefs.inxDirSpec[DB_EMBL];
  223.            ret = Write1Pref(output,&ph,data);
  224.  
  225.             if(ret) {
  226.                 ph.tag = T_SWISSINXDIR;
  227.                 ph.dataLen = (unsigned short) sizeof(DirSpec);
  228.                *(DirSpec *)data = gPrefs.inxDirSpec[DB_SWISS];
  229.                ret = Write1Pref(output,&ph,data);
  230.            }
  231.            
  232.             if(ret) {
  233.                 ph.tag = T_FORMAT;
  234.                 ph.dataLen = (unsigned short) sizeof(short);
  235.                *(short *)data = gPrefs.format;
  236.                ret = Write1Pref(output,&ph,data);
  237.            }
  238.            
  239.              if(ret) {
  240.                 ph.tag = T_CREATOR;
  241.                 ph.dataLen = (unsigned short) sizeof(short);
  242.                *(short *)data = gPrefs.creator;
  243.                ret = Write1Pref(output,&ph,data);
  244.            }
  245.            
  246.              if(ret) {
  247.                 ph.tag = T_CREATORNAME;
  248.                 ph.dataLen = (unsigned short) pstrlen(gPrefs.creatorName) + 1;
  249.                pstrcpy((StringPtr)data,gPrefs.creatorName);
  250.                ret = Write1Pref(output,&ph,data);
  251.            }
  252.            
  253.             if(ret) {
  254.                 ph.tag = T_CREATORSIG;
  255.                 ph.dataLen = (unsigned short) sizeof(OSType);
  256.                *(OSType *)data = gPrefs.creatorSig;
  257.                ret = Write1Pref(output,&ph,data);
  258.            }
  259.            
  260.             if(ret) {
  261.                 ph.tag = T_CONFIRMCHG;
  262.                 ph.dataLen = (unsigned short) sizeof(Boolean);
  263.                *(Boolean *)data = gPrefs.confirmChg;
  264.                ret = Write1Pref(output,&ph,data);
  265.            }
  266.            
  267.             if(ret) {
  268.                 ph.tag = T_STARTQOPEN;
  269.                 ph.dataLen = (unsigned short) sizeof(Boolean);
  270.                *(Boolean *)data = gPrefs.startQOpen;
  271.                ret = Write1Pref(output,&ph,data);
  272.            }
  273.  
  274.            if(!ret) {
  275.                 sprintf(gError,LoadErrorStr(ERR_WRITEPREF,FALSE),err);
  276.                 ErrorMsg(0);
  277.             }
  278.             
  279.            FSClose(output);
  280.         }
  281.    }
  282. }
  283.  
  284. /**************************************
  285. *    Write header and data block for one Preferences field
  286. */
  287.  
  288. static Boolean Write1Pref(short output,PrefHeader *ph,Byte *data)
  289. {
  290.     OSErr                err;
  291.     unsigned long    count;
  292.  
  293.     count = sizeof(PrefHeader);
  294.     err = WriteMacFile( output, (long *)&count, ph,"\p",FALSE);
  295.     if(err == noErr) {
  296.          count = (unsigned long) ph->dataLen;
  297.          err = WriteMacFile( output, (long *)&count, data,"\p",FALSE);
  298.     }
  299.  
  300.     return(err == noErr);
  301. }
  302.  
  303. /**************************************
  304. *    Select a new text file creator
  305. *    Return value:    True, if user selected one and no error occurred
  306. *                        False, if user canceled or an error occured
  307. */
  308.  
  309. Boolean PickNewCreator()
  310. {
  311.     DialogPtr    myDialog;
  312.     Point            where;
  313.     SFTypeList    myTypes;
  314.     SFReply         reply;
  315.     FInfo            fndrInfo;
  316.     OSErr            err;
  317.     
  318.     /* show pick dialog */
  319.     myDialog=GetNewDialog(PICKCREATOR_DLG,NULL,(WindowPtr)-1);
  320.     
  321.     CenterSFDlg(getDlgID,&where);
  322.     MoveWindow(myDialog,where.h,where.v-39,TRUE);
  323.     ShowWindow(myDialog);
  324.     DrawDialog(myDialog);
  325.  
  326.     myTypes[0]='APPL';
  327.     InitCursor();
  328.     SFGetFile(where,"\p",NULL,1,myTypes,NULL,&reply); /* ask for file name */
  329.    DisposDialog(myDialog);
  330.  
  331.    if (reply.good) {                            /* File was selected                     */
  332.        if((err = GetFInfo(reply.fName,reply.vRefNum,&fndrInfo)) != noErr) {
  333.               sprintf(gError,LoadErrorStr(ERR_OPENFILE,FALSE),PtoCstr(reply.fName),err);
  334.            CtoPstr((char *)reply.fName);
  335.               return(ErrorMsg(0));
  336.           }
  337.        else {
  338.            pstrcpy(gPrefs.creatorName,reply.fName);
  339.            gPrefs.creatorSig = fndrInfo.fdCreator;
  340.        }
  341.     }
  342.     
  343.     return(reply.good);
  344. }
  345.  
  346. /**************************************
  347. *    Show Options dialog and let user set some general preferences
  348. */
  349.  
  350. void GeneralOptions()
  351. {
  352.     DialogPtr    myDialog;
  353.     Boolean        bQuit = FALSE,bChanged = FALSE;
  354.     short            itemHit;
  355.     
  356.     CenterDA('DLOG',OPTIONS_DLG,66);
  357.     myDialog=GetNewDialog(OPTIONS_DLG,NULL,(WindowPtr)-1);
  358.  
  359.     /* show current settings */
  360.     if(gPrefs.confirmChg)
  361.         SetRadioButton(myDialog,CONFIRM_BOX,BTNON);
  362.     else
  363.         SetRadioButton(myDialog,CONFIRM_BOX,BTNOFF);
  364.         
  365.     if(gPrefs.startQOpen)
  366.         SetRadioButton(myDialog,STARTQ_BOX,BTNON);
  367.     else
  368.         SetRadioButton(myDialog,STARTQ_BOX,BTNOFF);    
  369.     
  370.     /* install user items to draw frame and default button outline */
  371.     InstallUserItem(myDialog,OPTIONS_USRITEM1,-1,DrawFrame);
  372.     InstallUserItem(myDialog,OPTIONS_USRITEM2,OK,DrawOKBoxRect);
  373.  
  374.     ShowWindow(myDialog);
  375.     while(!bQuit) {    /* display dialog using our standard filter proc */            
  376.         ModalDialog((ProcPtr)myDialogFilter,&itemHit);
  377.         switch(itemHit) {
  378.             case OK:
  379.             case Cancel:
  380.                 bQuit=TRUE;
  381.                 break;
  382.             case CONFIRM_BOX:
  383.             case STARTQ_BOX:
  384.                 ToggleRadioButton(myDialog,itemHit);
  385.                 bChanged = TRUE;
  386.                 break;
  387.         }
  388.     }
  389.         
  390.     /* if user clicked OK and something has changed we keep new settings */
  391.     if(itemHit == OK && bChanged) {
  392.         gPrefs.confirmChg = (Boolean)GetRadioButton(myDialog,CONFIRM_BOX);
  393.         gPrefs.startQOpen = (Boolean)GetRadioButton(myDialog,STARTQ_BOX);
  394.         
  395.         ErrorMsg(ERR_PREFCHGWARN);
  396.     }
  397.     
  398.     DisposDialog(myDialog);
  399. }