home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 20803 < prev    next >
Encoding:
Text File  |  1993-01-06  |  15.0 KB  |  546 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!sproul!Sproul
  2. From: Sproul@sproul.sproul.com (Mark Sproul)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: code for Finding Prefs File (long)
  5. Message-ID: <D2150096.mv05jr@sproul.sproul.com>
  6. Date: 5 Jan 93 18:15:16 GMT
  7. Reply-To: Sproul@sproul.sproul.com (Mark Sproul)
  8. Organization: Sproul Consulting
  9. Lines: 533
  10. X-Mailer: uAccess - Macintosh Release: 1.6v1
  11.  
  12. Below is code for finding and working with prefs file in system 7
  13. prefernce folder and working with said file
  14.  
  15. There are people looking for this code again, I posted it before.
  16. Since I posted it last, I have made some additions to it.  This code
  17. is originally from Inside Mac Comm ToolBox.
  18.  
  19. ============================================================================
  20. FILE: doPrefs.proto.h
  21. ============================================================================
  22.  
  23. void        getPrefsFile(Str255, OSType, OSType);
  24. Boolean        getPrefsResourceStr(Str255, OSType, Str255);
  25. Boolean        getPrefsResourceData(Str255, OSType, char *, long);
  26. void        setPrefsResourceStr(Str255, OSType, Ptr, long);
  27. void        setCTBpref(Str255);
  28. void        DoCommPortSetup(Str255);
  29.  
  30. ============================================================================
  31. FILE: doPrefs.c
  32. ============================================================================
  33.  
  34.  
  35. /******************************************************************************
  36. * adapted from
  37. *  Inside the Macintosh Communications Toolbox
  38. * Page  333
  39. * After initialization, the code shown first checks if a
  40. * preferences folder, which contains tool settings written in preference
  41. * files, already exists. If so, the application uses the settings in this file.
  42. * Otherwise, the code generates a new preferences file.
  43. ******************************************************************************
  44. * Modifications
  45. ******************************************************************************
  46. * Nov 27, 1991    Original version had a bug in it.
  47. *                If the "Preferences" directory already existed, the preferences file
  48. *                was placed in the system folder, not in the "Preferences" directory.
  49. *                If the directory did NOT exist, everything worked fine.
  50. *                I also changed it to check for the existance of the "Preferences" folder
  51. *                first instead of trying to create one and letting an duplicate file error
  52. *                indicate that it already existed.
  53. *                by Mark Sproul
  54. *                AppleLink: Sproul.M
  55. * Jan 16, 1992    Working on a general purpose prefs file manipulator
  56. * Jan 25, 1992    Prefs file resources working great
  57. ******************************************************************************/
  58.  
  59. #include    <Connections.h>
  60. #include    <CommResources.h>
  61. #include    "doPrefs.proto.h"
  62. /*
  63. ** Global Variables used by all of the prefs routines
  64. */
  65. long            prefDirID;            /* Prefs Dir ID number */
  66. long            sysfDirID;            /* System folder Dir ID number */
  67. short            prefVRefNum;        /* Prefs Volume Ref number */
  68. short            prefFileOKFlag = 0;    /* prefs file OK indicator */
  69.  
  70. /*************************************************************
  71. * This makes sure there is a prefs file in the PREFERENCES directory
  72. * if not, it creates it.
  73. *
  74. * getPrefsFile MUST be called before any atempt at getting the prefs values
  75. *
  76. *************************************************************/
  77.  
  78. void getPrefsFile(prefsFileName, creatorType, fileType)
  79. Str255        prefsFileName;
  80. OSType        creatorType;
  81. OSType        fileType;
  82. {
  83. OSErr            osErr        = noErr;
  84. SysEnvRec        theWorld;
  85. CInfoPBPtr        infoPB;
  86. WDPBPtr            wdPB;
  87. HParmBlkPtr        dirPB;
  88.  
  89. short            prefRefNum;
  90. Point            where    = { 75, 75 };
  91. Str63            toolName;
  92. short            procID;
  93. Handle            h;
  94. Ptr                p;
  95.  
  96.  
  97. ConnHandle        prefConn;
  98.  
  99. ConnHandle        docConn;
  100. CMBufferSizes    sizes    = { 0, 0, 0, 0, 0, 0, 0, 0 };
  101.  
  102.  
  103.  
  104.     infoPB        = (CInfoPBPtr)NewPtrClear(sizeof(*infoPB));
  105.     wdPB        = (WDPBPtr)NewPtrClear(sizeof(*wdPB));
  106.     dirPB        = (HParmBlkPtr)NewPtrClear(sizeof(*dirPB));
  107.  
  108.  
  109.     /* find the system folder's volume reference number and directory ID */
  110.     osErr = SysEnvirons(curSysEnvVers, &theWorld);
  111.     (*wdPB).ioVRefNum = theWorld.sysVRefNum;
  112.  
  113.     if (noErr == (osErr = PBGetWDInfo(wdPB, false)))
  114.     {
  115.         /*********************************************************
  116.         * 11-27-91 Modified by Mark Sproul
  117.         **********************************************************/
  118.         /* get the Volume Reference Number and save it */
  119.         prefVRefNum    = (*wdPB).ioWDVRefNum;
  120.         /* get the System directory ID and save it */
  121.         sysfDirID    = (*wdPB).ioWDDirID;
  122.         
  123.         /* check for the preferences folder */
  124.         (*infoPB).hFileInfo.ioFDirIndex    = 0;
  125.         (*infoPB).hFileInfo.ioVRefNum    = prefVRefNum;
  126.         (*infoPB).hFileInfo.ioDirID        = sysfDirID;
  127.         (*infoPB).hFileInfo.ioNamePtr    = "\pPreferences";
  128.         osErr = PBGetCatInfo(infoPB, false);
  129.         /* save the "Preferecnces" dir number */ 
  130.         prefDirID    = (*infoPB).hFileInfo.ioDirID;
  131.         
  132.         if (osErr == fnfErr)
  133.         {
  134.             /* Create "Preferences" folder */
  135.             (*dirPB).fileParam.ioVRefNum    = prefVRefNum;
  136.             (*dirPB).fileParam.ioDirID        = sysfDirID;
  137.             (*dirPB).fileParam.ioNamePtr    = "\pPreferences";
  138.             osErr = PBDirCreate(dirPB, false);
  139.             prefDirID    = (*dirPB).fileParam.ioDirID;
  140.         }
  141.  
  142.         /*********************************************************
  143.         * end of modifications
  144.         **********************************************************/
  145.         if (osErr == noErr)
  146.         {
  147.             /* does the preference file exist? */
  148.             (*infoPB).hFileInfo.ioFDirIndex    = 0;
  149.             (*infoPB).hFileInfo.ioVRefNum    = prefVRefNum;
  150.             (*infoPB).hFileInfo.ioDirID        = prefDirID;
  151.             (*infoPB).hFileInfo.ioNamePtr    = prefsFileName;
  152.             osErr = PBGetCatInfo(infoPB, false);
  153.             if (osErr == noErr)
  154.             {
  155.                 /* set flag saying the prefs file is OK */
  156.                 prefFileOKFlag = 0xAA;
  157.             }
  158.             if (osErr == fnfErr)
  159.             {
  160.                 /* no, so create a new preference file */
  161.                 if (noErr == (osErr = HCreate(prefVRefNum, prefDirID, prefsFileName, creatorType, fileType)))
  162.                 {
  163.                     HCreateResFile(prefVRefNum, prefDirID, prefsFileName);
  164.                     if (noErr == (osErr = ResError()))
  165.                     {
  166.                         /* open the preference file */
  167.                         prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  168.                         if (prefRefNum == -1)
  169.                         {
  170.                             osErr = ResError();
  171.                         }
  172.                         else
  173.                         {
  174.                             /* create a default connection */
  175.                             osErr = CRMGetIndToolName(classCM, 1,toolName);
  176.                             if (noErr == osErr)
  177.                             {
  178.                                 prefConn = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
  179.                                 /* leave the default setting as the preferance */
  180.                                 
  181.                                 /************************************************************
  182.                                 * ORIGINALLY, the code let the user select setup at this point
  183.                                 * I do not want it asking for serial port prefs on startup
  184.                                 * if they are not set.
  185.                                 * ---allow the user to select a prefered tool and configuration
  186.                                 * ---osErr = CMChoose(&prefConn, where, nil);
  187.                                 ************************************************************/
  188.                             
  189.                                 /* write the prefered tool name to the preference file */
  190.                                 HLock((Handle) prefConn);
  191.                                 CMGetToolName((**prefConn).procID, toolName);
  192.                                 HUnlock((Handle) prefConn);
  193.                                 h = NewHandle(1 + toolName[0]);
  194.                                 HLock(h);
  195.                                 BlockMove(toolName, *h, GetHandleSize(h));
  196.                                 HUnlock(h);
  197.                                 AddResource(h, 'pTXT', 0, "");
  198.                                 ReleaseResource(h);
  199.                                 /* write the prefered configuration to the preference file */
  200.                                 p = CMGetConfig(prefConn);
  201.                                 h = NewHandle(GetPtrSize(p));
  202.                                 HLock(h);
  203.                                 BlockMove(p, *h, GetHandleSize(h));
  204.                                 HUnlock(h);
  205.                                 AddResource(h, 'cTXT', 0, "");
  206.                                 ReleaseResource(h);
  207.                                 DisposPtr(p);
  208.                                 
  209.                                 /* dispose of the connection */
  210.                                 CMDispose(prefConn);
  211.                             }
  212.                             /* close the file so that it can be used in a shared environment */
  213.                             CloseResFile(prefRefNum);
  214.                             /* set flag saying the prefs file is OK */
  215.                             prefFileOKFlag = 0xAA;
  216.                         }
  217.                     }
  218.                 }
  219.             }
  220.         }
  221.     }
  222. }
  223.  
  224.  
  225.  
  226. //**************************************
  227. //* get resource String from Prefs file
  228. //*
  229. //***************************************
  230. Boolean    getPrefsResourceStr(prefsFileName, perfsResType, returnString)
  231. Str255        prefsFileName;
  232. OSType        perfsResType;
  233. Str255        returnString;
  234. {
  235. short            prefRefNum;
  236. Handle            h;
  237. int                i;
  238. Size            hSize;
  239. Boolean            returnFlag;
  240.  
  241.     h = nil;
  242.     returnFlag    = false;
  243.     /* did the prefs file get opened or created OK */
  244.     if (prefFileOKFlag == 0xAA)
  245.     {
  246.         /* focus on the preference file */
  247.         prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  248.         if (prefRefNum != -1)
  249.         {
  250.             h = Get1Resource(perfsResType, 0);
  251.             if (h != nil)
  252.             {
  253.                 hSize = GetHandleSize(h);            /* get the size of the handle */
  254.                 if (hSize > 255) hSize = 255;
  255.                 HLock(h);
  256.                 /* had to have this to make it compile under THINK C 5.0 */
  257.                 for (i=0; i< hSize; i++)
  258.                 {
  259.                     returnString[i] = *(*h+i);
  260.                 }
  261.                 HUnlock(h);
  262.                 ReleaseResource(h);
  263.                 returnFlag    = true;            //* prefs string is OK
  264.             }
  265.             CloseResFile(prefRefNum);
  266.         }
  267.     }
  268.     else
  269.     {
  270.         //* generate prefs file alert message
  271.     }
  272.     return(returnFlag);
  273. }
  274.  
  275. //**************************************
  276. //* get resource String from Prefs file
  277. //*
  278. //***************************************
  279. Boolean    getPrefsResourceData(    Str255        prefsFileName,
  280.                                 OSType        perfsResType,
  281.                                 char        *returnString,
  282.                                 long        maxLen)
  283. {
  284. short            prefRefNum;
  285. Handle            h;
  286. int                i;
  287. Size            hSize;
  288. Boolean            returnFlag;
  289.  
  290.     h = nil;
  291.     returnFlag    = false;
  292.     /* did the prefs file get opened or created OK */
  293.     if (prefFileOKFlag == 0xAA)
  294.     {
  295.         /* focus on the preference file */
  296.         prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  297.         if (prefRefNum != -1)
  298.         {
  299.             h = Get1Resource(perfsResType, 0);
  300.             if (h != nil)
  301.             {
  302.                 hSize = GetHandleSize(h);            /* get the size of the handle */
  303.                 if (hSize > maxLen) hSize = maxLen;
  304.                 HLock(h);
  305.                 /* had to have this to make it compile under THINK C 5.0 */
  306.                 for (i=0; i< hSize; i++)
  307.                 {
  308.                     returnString[i] = *(*h+i);
  309.                 }
  310.                 HUnlock(h);
  311.                 ReleaseResource(h);
  312.                 returnFlag    = true;            //* prefs string is OK
  313.             }
  314.             CloseResFile(prefRefNum);
  315.         }
  316.     }
  317.     else
  318.     {
  319.         //* generate prefs file alert message
  320.     }
  321.     return(returnFlag);
  322. }
  323.  
  324. //**************************************
  325. //* save resource to Prefs file
  326. //*
  327. //***************************************
  328. void    setPrefsResourceStr(Str255 prefsFileName, OSType perfsResType,char *buffer, long bufLen)
  329. {
  330. short            prefRefNum;
  331. Handle            h;
  332. int                i;
  333. Size            hSize, newSize;
  334.  
  335.     h = nil;
  336.     /* did the prefs file get opened or created OK */
  337.     if (prefFileOKFlag == 0xAA)
  338.     {
  339.         /* focus on the preference file */
  340.         prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  341.         if (prefRefNum != -1)
  342.         {
  343.             //* get the specified resource */
  344.             h = Get1Resource(perfsResType, 0);
  345.             if (h == nil)
  346.             {
  347.                 /* resource did not exist, add it to resource file */
  348.                 h = NewHandle(1 + bufLen);
  349.                 HLock(h);
  350.                 BlockMove(buffer, *h, GetHandleSize(h));
  351.                 HUnlock(h);
  352.                 AddResource(h, perfsResType, 0, "\p");
  353.             }
  354.             else
  355.             {
  356.                 /* resoure DOES exist, change it */
  357.                 hSize = GetHandleSize(h);            /* get the size of the handle */
  358.                 /* check for size of handle */
  359.                 if (hSize != (1 + bufLen))
  360.                 {
  361.                     newSize = 1 + bufLen;
  362.                     SetHandleSize(h, newSize);
  363.                 }
  364.                 HLock(h);
  365.                 BlockMove(buffer, *h, GetHandleSize(h));
  366.                 HUnlock(h);
  367.                 ChangedResource(h);
  368.             }
  369.             ReleaseResource(h);
  370.             CloseResFile(prefRefNum);
  371.         }
  372.     }
  373.     else
  374.     {
  375.         PutAlertMessage("\pProgram cant find prefs file");
  376.     }
  377. }
  378.  
  379.  
  380. /**************************************
  381. * set Comm Tool Box connection preference
  382. *
  383. ****************************************/
  384. void    setCTBpref(prefsFileName)
  385. Str255        prefsFileName;
  386. {
  387. short            prefRefNum;
  388. OSErr            osErr, cmChooseReturnCode;
  389. int                i;
  390. Str255            prefStr;
  391. short            procID;
  392. Str63            toolName;
  393. Handle            h;
  394. Ptr                p;
  395. short            iErr;
  396. Size            hSize, newSize;
  397.  
  398. ConnHandle        docConn;
  399. CMBufferSizes    sizes    = { 0, 0, 0, 0, 0, 0, 0, 0 };
  400. Point            where    = { 40, 40 };
  401.  
  402.     if (isCTBavailable())
  403.     {
  404.         /* did the prefs file get opened or created OK */
  405.         if (prefFileOKFlag == 0xAA)
  406.         {
  407.             getPrefsResourceStr(prefsFileName,'pTXT', prefStr);
  408.     
  409.             procID = CMGetProcID(prefStr);
  410.             if (procID != -1)
  411.             {
  412.                 /* create a new connection */
  413.                 docConn    = CMNew(procID, cmData, sizes, 0, 0);
  414.                 
  415.                 if (docConn != nil)
  416.                 {
  417.                     /* set the prefered configuration */
  418.         
  419.                     getPrefsResourceStr(prefsFileName,'cTXT', prefStr);
  420.                     osErr    = CMSetConfig(docConn, (char *)prefStr);
  421.     
  422.                     cmChooseReturnCode    = CMChoose(&docConn, where, nil);
  423.                 }
  424.             }
  425.             else
  426.             {
  427.                 /* the prefered tool could not be found so I */
  428.                 osErr    = CRMGetIndToolName(classCM, 1, toolName);
  429.                 docConn    = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
  430.                 if (docConn != nil)
  431.                 {
  432.                     cmChooseReturnCode    = CMChoose(&docConn, where, nil);
  433.                 }
  434.             }
  435.             
  436.             if ((cmChooseReturnCode == chooseOKMinor) || (cmChooseReturnCode == chooseOKMajor))
  437.             {
  438.                 /* change the prefs file */
  439.                 
  440.                 if (prefFileOKFlag == 0xAA)
  441.                 {
  442.                     /* open the preference file */
  443.                     prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  444.                     if (prefRefNum != -1)
  445.                     {
  446.                         /* write the prefered tool name to the preference file */
  447.                         HLock((Handle) docConn);
  448.                         CMGetToolName((**docConn).procID, toolName);
  449.                         HUnlock((Handle) docConn);
  450.                         
  451.                         /* get the port TeXT resource */
  452.                         h = Get1Resource('pTXT', 0);
  453.                         if (h == nil)
  454.                         {
  455.                             /* resource did not exist, add it to resource file */
  456.                             h = NewHandle(1 + toolName[0]);
  457.                             HLock(h);
  458.                             BlockMove(toolName, *h, GetHandleSize(h));
  459.                             HUnlock(h);
  460.                             AddResource(h, 'pTXT', 0, "");
  461.                         }
  462.                         else
  463.                         {
  464.                             /* resoure DOES exist, change it */
  465.                             hSize = GetHandleSize(h);            /* get the size of the handle */
  466.                             /* check for size of handle */
  467.                             if (hSize != (1 + toolName[0]))
  468.                             {
  469.                                 newSize = 1 + toolName[0];
  470.                                 SetHandleSize(h, newSize);
  471.                             }
  472.                             HLock(h);
  473.                             BlockMove(toolName, *h, GetHandleSize(h));
  474.                             HUnlock(h);
  475.                             ChangedResource(h);
  476.                         }
  477.                         ReleaseResource(h);
  478.                         
  479.                         /* write the prefered configuration to the preference file */
  480.                         p = CMGetConfig(docConn);
  481.     
  482.     
  483.                         /* get the configuration TeXT resource */
  484.                         h = Get1Resource('cTXT', 0);
  485.                         if (h == nil)
  486.                         {
  487.                             /* resource did not exist, add it to resource file */
  488.                             h = NewHandle(GetPtrSize(p));
  489.                             HLock(h);
  490.                             BlockMove(p, *h, GetHandleSize(h));
  491.                             HUnlock(h);
  492.                         
  493.                             AddResource(h, 'cTXT', 0, "");
  494.                             iErr = ResError();
  495.                         }
  496.                         else
  497.                         {
  498.                             /* resoure DOES exist, change it */
  499.                             hSize = GetHandleSize(h);            /* get the size of the handle */
  500.                             /* check for size of handle */
  501.                             if (hSize != GetPtrSize(p))
  502.                             {
  503.                                 newSize = GetPtrSize(p);
  504.                                 SetHandleSize(h, newSize);
  505.                             }
  506.                             HLock(h);
  507.                             BlockMove(p, *h, GetHandleSize(h));
  508.                             HUnlock(h);
  509.                             ChangedResource(h);
  510.                         }
  511.                         ReleaseResource(h);
  512.     
  513.                         CloseResFile(prefRefNum);
  514.                         iErr = ResError();
  515.                         DisposPtr(p);
  516.                     }
  517.                 }
  518.             }
  519.             
  520.             if (docConn != nil)
  521.             {
  522.                 /* dispose of the connection */
  523.                 CMDispose(docConn);
  524.             }
  525.     
  526.         }
  527.     }
  528.     else
  529.     {
  530.         SysBeep(1);
  531.         SysBeep(1);
  532.     }
  533. }
  534.  
  535.  
  536. void    DoCommPortSetup(prefsFileName)
  537. Str255    prefsFileName;
  538. {
  539.     setCTBpref(prefsFileName);
  540.  
  541. }
  542.  
  543. -----------------------------------------------------
  544. Mark Sproul - KB2ICI - New Jersey
  545. sproul@sproul.com
  546.