home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / prefsfil.txt < prev    next >
Encoding:
Text File  |  1992-11-05  |  12.3 KB  |  434 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Subject: Prefs file source code (C - long)
  3. From: Sproul@sproul.sproul.com (Mark Sproul)
  4. Date: 2 Oct 92 09:05:30 GMT
  5. Reply-To: Sproul@sproul.sproul.com (Mark Sproul)
  6. Organization: Sproul Consulting
  7. X-Mailer: uAccess - Mac Release: 1.5v2
  8.  
  9.  
  10. Several people asked for this source code.  As explained in the comments,
  11. it came from Inside Mac Comm Toolbox and there is a lot of code dealing
  12. just with the Comm Tool box setup.  It now handles the PREFERENCES
  13. folder in the system folder properly.
  14.  
  15. I have done some additions to the code to make it more general and
  16. usable as far as dealing with preferences.  Such as GET pref and SET
  17. pref.  This code was all written and runs correctly with Think C 5.0.
  18.  
  19. Its been several months since I wrote this and was looking at it.
  20. I may have forgotten to mention something, if you have any problems
  21. let me know and I will post updates.
  22.  
  23. You need to define some things such as this:
  24.  
  25. #define    creatorType        'abcd'
  26. #define    fileType        'PREF'
  27. #define    prefsFileName    "\pMyPreferences"
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. /******************************************************************************
  35. * adapted from
  36. *  Inside the Macintosh Communications Toolbox
  37. * Page  333
  38. * After initialization, the code shown first checks if a
  39. * preferences folder, which contains tool settings written in preference
  40. * files, already exists. If so, the application uses the settings in this file.
  41. * Otherwise, the code generates a new preferences file.
  42. ******************************************************************************
  43. * Modifications
  44. ******************************************************************************
  45. * Nov 27, 1991    Original version had a bug in it.
  46. *                If the "Preferences" directory already existed, the preferences file
  47. *                was placed in the system folder, not in the "Preferences" directory.
  48. *                If the directory did NOT exist, everything worked fine.
  49. *                I also changed it to check for the existance of the "Preferences" folder
  50. *                first instead of trying to create one and letting an duplicate file error
  51. *                indicate that it already existed.
  52. *                by Mark Sproul
  53. *                Internet: sproul@sproul.com
  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.  
  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 directir 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. //* The following code was written by Mark Sproul
  227. //* This code allows easy access to prefs file
  228. //*
  229. //*
  230. //*
  231. //*
  232. //*****************************************************************
  233.  
  234. /**************************************
  235. * get resource from Prefs file
  236. *
  237. ****************************************/
  238. getPrefsResourceStr(prefsFileName, perfsResType, returnString)
  239. Str255        prefsFileName;
  240. OSType        perfsResType;
  241. Str255        returnString;
  242. {
  243. short            prefRefNum;
  244. Handle            h;
  245. int                i;
  246. Size            hSize;
  247.  
  248.     h = nil;
  249.     /* did the prefs file get opened or created OK */
  250.     if (prefFileOKFlag == 0xAA)
  251.     {
  252.         /* focus on the preference file */
  253.         prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  254.         if (prefRefNum != -1)
  255.         {
  256.             h = Get1Resource(perfsResType, 0);
  257.             hSize = GetHandleSize(h);            /* get the size of the handle */
  258.             if (hSize > 255) hSize = 255;
  259.             HLock(h);
  260.             /* had to have this to make it compile under THINK C 5.0 */
  261.             for (i=0; i< hSize; i++)
  262.             {
  263.                 returnString[i] = *(*h+i);
  264.             }
  265.             HUnlock(h);
  266.             ReleaseResource(h);
  267.             CloseResFile(prefRefNum);
  268.         }
  269.     }
  270. }
  271.  
  272.  
  273. /**************************************
  274. * set Comm Tool Box connection preference
  275. *
  276. ****************************************/
  277. setCTBpref(prefsFileName)
  278. Str255        prefsFileName;
  279. {
  280. short            prefRefNum;
  281. OSErr            osErr, cmChooseReturnCode;
  282. int                i;
  283. Str255            prefStr;
  284. short            procID;
  285. Str63            toolName;
  286. Handle            h;
  287. Ptr                p;
  288. short            iErr;
  289. Size            hSize, newSize;
  290.  
  291. ConnHandle        docConn;
  292. CMBufferSizes    sizes    = { 0, 0, 0, 0, 0, 0, 0, 0 };
  293. Point            where    = { 75, 75 };
  294.  
  295.     if (isCTBavailable())
  296.     {
  297.         /* did the prefs file get opened or created OK */
  298.         if (prefFileOKFlag == 0xAA)
  299.         {
  300.             getPrefsResourceStr(prefsFileName,'pTXT', prefStr);
  301.     
  302.             procID = CMGetProcID(prefStr);
  303.             if (procID != -1)
  304.             {
  305.                 /* create a new connection */
  306.                 docConn    = CMNew(procID, cmData, sizes, 0, 0);
  307.                 
  308.                 if (docConn != nil)
  309.                 {
  310.                     /* set the prefered configuration */
  311.         
  312.                     getPrefsResourceStr(prefsFileName,'cTXT', prefStr);
  313.                     osErr    = CMSetConfig(docConn, (char *)prefStr);
  314.     
  315.                     cmChooseReturnCode    = CMChoose(&docConn, where, nil);
  316.                 }
  317.             }
  318.             else
  319.             {
  320.                 /* the prefered tool could not be found so I */
  321.                 osErr    = CRMGetIndToolName(classCM, 1, toolName);
  322.                 docConn    = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
  323.                 if (docConn != nil)
  324.                 {
  325.                     cmChooseReturnCode    = CMChoose(&docConn, where, nil);
  326.                 }
  327.             }
  328.             
  329.             if ((cmChooseReturnCode == chooseOKMinor) || (cmChooseReturnCode == chooseOKMajor))
  330.             {
  331.                 /* change the prefs file */
  332.                 
  333.                 if (prefFileOKFlag == 0xAA)
  334.                 {
  335.                     /* open the preference file */
  336.                     prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
  337.                     if (prefRefNum != -1)
  338.                     {
  339.                         /* write the prefered tool name to the preference file */
  340.                         HLock((Handle) docConn);
  341.                         CMGetToolName((**docConn).procID, toolName);
  342.                         HUnlock((Handle) docConn);
  343.                         
  344.                         /* get the port TeXT resource */
  345.                         h = Get1Resource('pTXT', 0);
  346.                         if (h == nil)
  347.                         {
  348.                             /* resource did not exist, add it to resource file */
  349.                             h = NewHandle(1 + toolName[0]);
  350.                             HLock(h);
  351.                             BlockMove(toolName, *h, GetHandleSize(h));
  352.                             HUnlock(h);
  353.                             AddResource(h, 'pTXT', 0, "");
  354.                         }
  355.                         else
  356.                         {
  357.                             /* resoure DOES exist, change it */
  358.                             hSize = GetHandleSize(h);            /* get the size of the handle */
  359.                             /* check for size of handle */
  360.                             if (hSize != (1 + toolName[0]))
  361.                             {
  362.                                 newSize = 1 + toolName[0];
  363.                                 SetHandleSize(h, newSize);
  364.                             }
  365.                             HLock(h);
  366.                             BlockMove(toolName, *h, GetHandleSize(h));
  367.                             HUnlock(h);
  368.                             ChangedResource(h);
  369.                         }
  370.                         ReleaseResource(h);
  371.                         
  372.                         /* write the prefered configuration to the preference file */
  373.                         p = CMGetConfig(docConn);
  374.     
  375.     
  376.                         /* get the configuration TeXT resource */
  377.                         h = Get1Resource('cTXT', 0);
  378.                         if (h == nil)
  379.                         {
  380.                             /* resource did not exist, add it to resource file */
  381.                             h = NewHandle(GetPtrSize(p));
  382.                             HLock(h);
  383.                             BlockMove(p, *h, GetHandleSize(h));
  384.                             HUnlock(h);
  385.                         
  386.                             AddResource(h, 'cTXT', 0, "");
  387.                             iErr = ResError();
  388.                         }
  389.                         else
  390.                         {
  391.                             /* resoure DOES exist, change it */
  392.                             hSize = GetHandleSize(h);            /* get the size of the handle */
  393.                             /* check for size of handle */
  394.                             if (hSize != GetPtrSize(p))
  395.                             {
  396.                                 newSize = GetPtrSize(p);
  397.                                 SetHandleSize(h, newSize);
  398.                             }
  399.                             HLock(h);
  400.                             BlockMove(p, *h, GetHandleSize(h));
  401.                             HUnlock(h);
  402.                             ChangedResource(h);
  403.                         }
  404.                         ReleaseResource(h);
  405.     
  406.                         CloseResFile(prefRefNum);
  407.                         iErr = ResError();
  408.                         DisposPtr(p);
  409.                     }
  410.                 }
  411.             }
  412.             
  413.             if (docConn != nil)
  414.             {
  415.                 /* dispose of the connection */
  416.                 CMDispose(docConn);
  417.             }
  418.     
  419.         }
  420.     }
  421. }
  422.  
  423.  
  424. DoCommPortSetup(prefsFileName)
  425. Str255    prefsFileName;
  426. {
  427.     setCTBpref(prefsFileName);
  428.  
  429. }
  430.  
  431.  
  432. -------------------------------------
  433. Mark Sproul - KB2ICI
  434.