home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Information
/
CSMP Digest
/
volume 1
/
csmp-v1-206.txt
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Text File
|
1994-12-08
|
33.1 KB
|
1,068 lines
|
[
TEXT/R*ch
]
C.S.M.P. Digest Tue, 03 Nov 92 Volume 1 : Issue 206
Today's Topics:
Prefs file source code
The Comp.Sys.Mac.Programmer Digest is moderated by Michael A. Kelly.
The digest is a collection of article threads from the internet newsgroup
comp.sys.mac.programmer. It is designed for people who read c.s.m.p. semi-
regularly and want an archive of the discussions. If you don't know what a
newsgroup is, you probably don't have access to it. Ask your systems
administrator(s) for details. (This means you can't post questions to the
digest.)
Each issue of the digest contains one or more sets of articles (called
threads), with each set corresponding to a 'discussion' of a particular
subject. The articles are not edited; all articles included in this digest
are in their original posted form (as received by our news server at
cs.uoregon.edu). Article threads are not added to the digest until the last
article added to the thread is at least one month old (this is to ensure that
the thread is dead before adding it to the digest). Article threads that
consist of only one message are generally not included in the digest.
The entire digest is available for anonymous ftp from ftp.cs.uoregon.edu
[128.223.8.8] in the directory /pub/mac/csmp-digest. Be sure to read the
file /pub/mac/csmp-digest/README before downloading any files. The most
recent issues are available from sumex-aim.stanford.edu [36.44.0.6] in the
directory /info-mac/digest/csmp. If you don't have ftp capability, the sumex
archive has a mail server; send a message with the text '$MACarch help' (no
quotes) to LISTSERV@ricevm1.rice.edu for more information.
The digest is also available via email. Just send a note saying that you
want to be on the digest mailing list to mkelly@cs.uoregon.edu, and you will
automatically receive each new issue as it is created. Sorry, back issues
are not available through the mailing list.
Send administrative mail to mkelly@cs.uoregon.edu.
-------------------------------------------------------
From: Sproul@sproul.sproul.com (Mark Sproul)
Subject: Prefs file source code
Date: 29 Sep 92 08:05:41 GMT
Organization: Sproul Consulting
Several people asked for this source code. As explained in the comments,
it came from Inside Mac Comm Toolbox and there is a lot of code dealing
just with the Comm Tool box setup. It now handles the PREFERENCES
folder in the system folder properly.
I have done some additions to the code to make it more general and
usable as far as dealing with preferences. Such as GET pref and SET
pref. This code was all written and runs correctly with Think C 5.0.
Its been several months since I wrote this and was looking at it.
I may have forgotten to mention something, if you have any problems
let me know and I will post updates.
You need to define some things such as this:
#define creatorType 'abcd'
#define fileType 'PREF'
#define prefsFileName "\pMyPreferences"
/******************************************************************************
* adapted from
* Inside the Macintosh Communications Toolbox
* Page 333
* After initialization, the code shown first checks if a
* preferences folder, which contains tool settings written in preference
* files, already exists. If so, the application uses the settings in this file.
* Otherwise, the code generates a new preferences file.
******************************************************************************
* Modifications
******************************************************************************
* Nov 27, 1991 Original version had a bug in it.
* If the "Preferences" directory already existed, the preferences file
* was placed in the system folder, not in the "Preferences" directory.
* If the directory did NOT exist, everything worked fine.
* I also changed it to check for the existance of the "Preferences" folder
* first instead of trying to create one and letting an duplicate file error
* indicate that it already existed.
* by Mark Sproul
* Internet: sproul@sproul.com
* AppleLink: Sproul.M
* Jan 16, 1992 Working on a general purpose prefs file manipulator
* Jan 25, 1992 Prefs file resources working great
******************************************************************************/
#include <Connections.h>
#include <CommResources.h>
/*
** Global Variables used by all of the prefs routines
*/
long prefDirID; /* Prefs Dir ID number */
long sysfDirID; /* System folder Dir ID number */
short prefVRefNum; /* Prefs Volume Ref number */
short prefFileOKFlag = 0; /* prefs file OK indicator */
/*************************************************************
* This makes sure there is a prefs file in the PREFERENCES directory
* if not, it creates it.
*
* getPrefsFile MUST be called before any atempt at getting the prefs values
*
*************************************************************/
void getPrefsFile(prefsFileName, creatorType, fileType)
Str255 prefsFileName;
OSType creatorType;
OSType fileType;
{
OSErr osErr = noErr;
SysEnvRec theWorld;
CInfoPBPtr infoPB;
WDPBPtr wdPB;
HParmBlkPtr dirPB;
short prefRefNum;
Point where = { 75, 75 };
Str63 toolName;
short procID;
Handle h;
Ptr p;
ConnHandle prefConn;
ConnHandle docConn;
CMBufferSizes sizes = { 0, 0, 0, 0, 0, 0, 0, 0 };
infoPB = (CInfoPBPtr)NewPtrClear(sizeof(*infoPB));
wdPB = (WDPBPtr)NewPtrClear(sizeof(*wdPB));
dirPB = (HParmBlkPtr)NewPtrClear(sizeof(*dirPB));
/* find the system folder's volume reference number and directory ID */
osErr = SysEnvirons(curSysEnvVers, &theWorld);
(*wdPB).ioVRefNum = theWorld.sysVRefNum;
if (noErr == (osErr = PBGetWDInfo(wdPB, false)))
{
/*********************************************************
* 11-27-91 Modified by Mark Sproul
**********************************************************/
/* get the Volume Reference Number and save it */
prefVRefNum = (*wdPB).ioWDVRefNum;
/* get the System directir ID and save it */
sysfDirID = (*wdPB).ioWDDirID;
/* check for the preferences folder */
(*infoPB).hFileInfo.ioFDirIndex = 0;
(*infoPB).hFileInfo.ioVRefNum = prefVRefNum;
(*infoPB).hFileInfo.ioDirID = sysfDirID;
(*infoPB).hFileInfo.ioNamePtr = "\pPreferences";
osErr = PBGetCatInfo(infoPB, false);
/* save the "Preferecnces" dir number */
prefDirID = (*infoPB).hFileInfo.ioDirID;
if (osErr == fnfErr)
{
/* Create "Preferences" folder */
(*dirPB).fileParam.ioVRefNum = prefVRefNum;
(*dirPB).fileParam.ioDirID = sysfDirID;
(*dirPB).fileParam.ioNamePtr = "\pPreferences";
osErr = PBDirCreate(dirPB, false);
prefDirID = (*dirPB).fileParam.ioDirID;
}
/*********************************************************
* end of modifications
**********************************************************/
if (osErr == noErr)
{
/* does the preference file exist? */
(*infoPB).hFileInfo.ioFDirIndex = 0;
(*infoPB).hFileInfo.ioVRefNum = prefVRefNum;
(*infoPB).hFileInfo.ioDirID = prefDirID;
(*infoPB).hFileInfo.ioNamePtr = prefsFileName;
osErr = PBGetCatInfo(infoPB, false);
if (osErr == noErr)
{
/* set flag saying the prefs file is OK */
prefFileOKFlag = 0xAA;
}
if (osErr == fnfErr)
{
/* no, so create a new preference file */
if (noErr == (osErr = HCreate(prefVRefNum, prefDirID, prefsFileName, creatorType, fileType)))
{
HCreateResFile(prefVRefNum, prefDirID, prefsFileName);
if (noErr == (osErr = ResError()))
{
/* open the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum == -1)
{
osErr = ResError();
}
else
{
/* create a default connection */
osErr = CRMGetIndToolName(classCM, 1,toolName);
if (noErr == osErr)
{
prefConn = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
/* leave the default setting as the preferance */
/************************************************************
* ORIGINALLY, the code let the user select setup at this point
* I do not want it asking for serial port prefs on startup
* if they are not set.
* ---allow the user to select a prefered tool and configuration
* ---osErr = CMChoose(&prefConn, where, nil);
************************************************************/
/* write the prefered tool name to the preference file */
HLock((Handle) prefConn);
CMGetToolName((**prefConn).procID, toolName);
HUnlock((Handle) prefConn);
h = NewHandle(1 + toolName[0]);
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'pTXT', 0, "");
ReleaseResource(h);
/* write the prefered configuration to the preference file */
p = CMGetConfig(prefConn);
h = NewHandle(GetPtrSize(p));
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'cTXT', 0, "");
ReleaseResource(h);
DisposPtr(p);
/* dispose of the connection */
CMDispose(prefConn);
}
/* close the file so that it can be used in a shared environment */
CloseResFile(prefRefNum);
/* set flag saying the prefs file is OK */
prefFileOKFlag = 0xAA;
}
}
}
}
}
}
}
//*****************************************************************
//*
//* The following code was written by Mark Sproul
//* This code allows easy access to prefs file
//*
//*
//*
//*
//*****************************************************************
/**************************************
* get resource from Prefs file
*
****************************************/
getPrefsResourceStr(prefsFileName, perfsResType, returnString)
Str255 prefsFileName;
OSType perfsResType;
Str255 returnString;
{
short prefRefNum;
Handle h;
int i;
Size hSize;
h = nil;
/* did the prefs file get opened or created OK */
if (prefFileOKFlag == 0xAA)
{
/* focus on the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum != -1)
{
h = Get1Resource(perfsResType, 0);
hSize = GetHandleSize(h); /* get the size of the handle */
if (hSize > 255) hSize = 255;
HLock(h);
/* had to have this to make it compile under THINK C 5.0 */
for (i=0; i< hSize; i++)
{
returnString[i] = *(*h+i);
}
HUnlock(h);
ReleaseResource(h);
CloseResFile(prefRefNum);
}
}
}
/**************************************
* set Comm Tool Box connection preference
*
****************************************/
setCTBpref(prefsFileName)
Str255 prefsFileName;
{
short prefRefNum;
OSErr osErr, cmChooseReturnCode;
int i;
Str255 prefStr;
short procID;
Str63 toolName;
Handle h;
Ptr p;
short iErr;
Size hSize, newSize;
ConnHandle docConn;
CMBufferSizes sizes = { 0, 0, 0, 0, 0, 0, 0, 0 };
Point where = { 75, 75 };
if (isCTBavailable())
{
/* did the prefs file get opened or created OK */
if (prefFileOKFlag == 0xAA)
{
getPrefsResourceStr(prefsFileName,'pTXT', prefStr);
procID = CMGetProcID(prefStr);
if (procID != -1)
{
/* create a new connection */
docConn = CMNew(procID, cmData, sizes, 0, 0);
if (docConn != nil)
{
/* set the prefered configuration */
getPrefsResourceStr(prefsFileName,'cTXT', prefStr);
osErr = CMSetConfig(docConn, (char *)prefStr);
cmChooseReturnCode = CMChoose(&docConn, where, nil);
}
}
else
{
/* the prefered tool could not be found so I */
osErr = CRMGetIndToolName(classCM, 1, toolName);
docConn = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
if (docConn != nil)
{
cmChooseReturnCode = CMChoose(&docConn, where, nil);
}
}
if ((cmChooseReturnCode == chooseOKMinor) || (cmChooseReturnCode == chooseOKMajor))
{
/* change the prefs file */
if (prefFileOKFlag == 0xAA)
{
/* open the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum != -1)
{
/* write the prefered tool name to the preference file */
HLock((Handle) docConn);
CMGetToolName((**docConn).procID, toolName);
HUnlock((Handle) docConn);
/* get the port TeXT resource */
h = Get1Resource('pTXT', 0);
if (h == nil)
{
/* resource did not exist, add it to resource file */
h = NewHandle(1 + toolName[0]);
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'pTXT', 0, "");
}
else
{
/* resoure DOES exist, change it */
hSize = GetHandleSize(h); /* get the size of the handle */
/* check for size of handle */
if (hSize != (1 + toolName[0]))
{
newSize = 1 + toolName[0];
SetHandleSize(h, newSize);
}
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
ChangedResource(h);
}
ReleaseResource(h);
/* write the prefered configuration to the preference file */
p = CMGetConfig(docConn);
/* get the configuration TeXT resource */
h = Get1Resource('cTXT', 0);
if (h == nil)
{
/* resource did not exist, add it to resource file */
h = NewHandle(GetPtrSize(p));
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'cTXT', 0, "");
iErr = ResError();
}
else
{
/* resoure DOES exist, change it */
hSize = GetHandleSize(h); /* get the size of the handle */
/* check for size of handle */
if (hSize != GetPtrSize(p))
{
newSize = GetPtrSize(p);
SetHandleSize(h, newSize);
}
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
ChangedResource(h);
}
ReleaseResource(h);
CloseResFile(prefRefNum);
iErr = ResError();
DisposPtr(p);
}
}
}
if (docConn != nil)
{
/* dispose of the connection */
CMDispose(docConn);
}
}
}
}
DoCommPortSetup(prefsFileName)
Str255 prefsFileName;
{
setCTBpref(prefsFileName);
}
- -------------------------------------
Mark Sproul - KB2ICI
+++++++++++++++++++++++++++
From: grobbins@Apple.COM (Grobbins)
Date: 29 Sep 92 23:58:55 GMT
Organization: Apple Computer Inc., Cupertino, CA
In article <D2150096.o1pwue@sproul.sproul.com> Sproul@sproul.sproul.com (Mark Sproul) writes:
>Several people asked for this source code. ...It now handles the PREFERENCES
>folder in the system folder properly.
No, it hardcodes the name "Preferences" and assumes that it is in the
System folder. Applications should instead use FindFolder under
Systems 6 or 7 to locate the preferences folder. FindFolder is
documented starting on page 9-42 of Inside Mac VI.
Pasted below is the Q&A from issue 11 of develop magazine on
preference files.
Grobbins grobbins@apple.com
Usual disclaimers apply.
- -------------------------------------------------------
I want to place my user's preference file into the
Preferences folder in the System folder under System 7, but I
can't seem to get that folder's DirID and other information
so my file will appear there! Additionally, how do I get to
that folder if the user changes the names of the System
Folder and Preferences folder? Also, once it's there, am I
assuming correctly that the best way to find it again is to
make an alias record to track the file ID?
___
System 7 introduced the routine FindFolder for locating the
Preferences folder. Just make this call
err := FindFolder(kOnSystemDisk, kPreferencesFolderType,
kCreateFolder, prefVRefNum, prefDirID);
and, if err is noErr, then prefVRefNum and prefDirID will
contain the vRefNum and dirID of the Preferences folder. This
can be used with HCreateResFile, HOpenResFile, PBHGetFInfo,
or other File Manager calls to get to your preferences file.
If a Preferences folder does not already exist, the
kCreateFolder parameter instructs FindFolder to make one and
return the vRefNum and dirID of the new folder as prefVRefNum
and prefDirID.
FindFolder is documented in Chapter 9 of Inside Macintosh
Volume VI, under "The System Folder and its Related
Directories." While the FindFolder trap is only implemented
under System 7 (check the gestalt selector
gestaltFindFolderAttr, documented in Chapter 3 of Inside
Macintosh VI, to see if FindFolder is available), if you're
using MPW 3.2 (or the current Think compilers) glue is
automatically included in your compiled code, making it safe
to call FindFolder under System 6. The glue will check if
FindFolder is available, and if it is not, will return the
System folder's vRefNum and dirID as as prefVRefNum and
prefDirID for the kPreferencesFolderType selector. Go ahead
and use the System folder values as the location for the
preferences file under System 6.
If you're not using a development system that provides the
FindFolder glue, your code should check the FindFolder
gestalt attribute to see if FindFolder is available. If
FindFolder is available, call it. FindFolder is defined as
FUNCTION FindFolder(vRefNum: INTEGER;folderType: OSType;
createFolder: BOOLEAN;
VAR foundVRefNum: INTEGER;VAR foundDirID: LONGINT):OSErr;
INLINE $7000,$A823;
If FindFolder is not available, call SysEnvirons to find the
System folder's WDRefNum, call PBGetWDInfo or GetWDInfo to
convert the WDRefNum to a true vRefNum and dirID, and use
those System folder numbers for the location of the
preferences file. Example code for this is in the Q&A Stack,
under Operating System:File System:Code for identifying
vRefNum and dirID of Macintosh System Folder.
To locate the Preferences folder, use the above procedure
rather than try to keep an alias of the Preferences folder or
of the preferences file. However, if there are any other
files in the System Folder upon which the application depends
(such as dictionaries) those should be tracked with aliases,
stored as 'alis' resources in the preferences file. See
Chapter 27 of Inside Macintosh Volume VI for information on
using aliases.
+++++++++++++++++++++++++++
From: peter@cujo.curtin.edu.au (Peter N Lewis)
Organization: NCRPDA, Curtin University
Date: Thu, 1 Oct 1992 02:27:00 GMT
In article <72915@apple.Apple.COM>, grobbins@Apple.COM (Grobbins) wrote:
>
> In article <D2150096.o1pwue@sproul.sproul.com> Sproul@sproul.sproul.com (Mark Sproul) writes:
> >Several people asked for this source code. ...It now handles the PREFERENCES
> >folder in the system folder properly.
>
> No, it hardcodes the name "Preferences" and assumes that it is in the
> System folder. Applications should instead use FindFolder under
> Systems 6 or 7 to locate the preferences folder. FindFolder is
> documented starting on page 9-42 of Inside Mac VI.
Q&A:
> gestaltFindFolderAttr, documented in Chapter 3 of Inside
> Macintosh VI, to see if FindFolder is available), if you're
> using MPW 3.2 (or the current Think compilers) glue is
> automatically included in your compiled code, making it safe
> to call FindFolder under System 6. The glue will check if
> FindFolder is available, and if it is not, will return the
> System folder's vRefNum and dirID as as prefVRefNum and
> prefDirID for the kPreferencesFolderType selector. Go ahead
> and use the System folder values as the location for the
> preferences file under System 6.
While I agree you should use FindFolder (definitely don't hard code it!),
and its really nice that you can just call it without worrying about
whether its implemented or not, I disagree with using the System Folder on
System 6 machines. Many programs (mostly sharware programs I think :-)
were using the Preferences folder before System 7 was even released. Its
sad that its always the english spelling, but we can't do much about that
except put it in the resource fork so it can be internationalized, but the
Preferences folder is at least a good approximation.
So I'd say (blatantly ignoring Apple's recomendation, and waiting for the
Apple Thought Police to come break down my door :), check if you are
running in System 6, and create and use the Preferences folder if you are
(you can (of course) use FindFolder to find the System Folder so you know
where to look for/put the Preferences folder...)
Have fun all, I'll go lock my door :-)
Peter.
_______________________________________________________________________
Peter N Lewis, NCRPDA, Curtin University peter@cujo.curtin.edu.au
GPO Box U1987, Perth WA 6001, AUSTRALIA FAX: +61 9 367 8141
+++++++++++++++++++++++++++
From: Sproul@sproul.sproul.com (Mark Sproul)
Date: 2 Oct 92 09:05:30 GMT
Organization: Sproul Consulting
Several people asked for this source code. As explained in the comments,
it came from Inside Mac Comm Toolbox and there is a lot of code dealing
just with the Comm Tool box setup. It now handles the PREFERENCES
folder in the system folder properly.
I have done some additions to the code to make it more general and
usable as far as dealing with preferences. Such as GET pref and SET
pref. This code was all written and runs correctly with Think C 5.0.
Its been several months since I wrote this and was looking at it.
I may have forgotten to mention something, if you have any problems
let me know and I will post updates.
You need to define some things such as this:
#define creatorType 'abcd'
#define fileType 'PREF'
#define prefsFileName "\pMyPreferences"
/******************************************************************************
* adapted from
* Inside the Macintosh Communications Toolbox
* Page 333
* After initialization, the code shown first checks if a
* preferences folder, which contains tool settings written in preference
* files, already exists. If so, the application uses the settings in this file.
* Otherwise, the code generates a new preferences file.
******************************************************************************
* Modifications
******************************************************************************
* Nov 27, 1991 Original version had a bug in it.
* If the "Preferences" directory already existed, the preferences file
* was placed in the system folder, not in the "Preferences" directory.
* If the directory did NOT exist, everything worked fine.
* I also changed it to check for the existance of the "Preferences" folder
* first instead of trying to create one and letting an duplicate file error
* indicate that it already existed.
* by Mark Sproul
* Internet: sproul@sproul.com
* AppleLink: Sproul.M
* Jan 16, 1992 Working on a general purpose prefs file manipulator
* Jan 25, 1992 Prefs file resources working great
******************************************************************************/
#include <Connections.h>
#include <CommResources.h>
/*
** Global Variables used by all of the prefs routines
*/
long prefDirID; /* Prefs Dir ID number */
long sysfDirID; /* System folder Dir ID number */
short prefVRefNum; /* Prefs Volume Ref number */
short prefFileOKFlag = 0; /* prefs file OK indicator */
/*************************************************************
* This makes sure there is a prefs file in the PREFERENCES directory
* if not, it creates it.
*
* getPrefsFile MUST be called before any atempt at getting the prefs values
*
*************************************************************/
void getPrefsFile(prefsFileName, creatorType, fileType)
Str255 prefsFileName;
OSType creatorType;
OSType fileType;
{
OSErr osErr = noErr;
SysEnvRec theWorld;
CInfoPBPtr infoPB;
WDPBPtr wdPB;
HParmBlkPtr dirPB;
short prefRefNum;
Point where = { 75, 75 };
Str63 toolName;
short procID;
Handle h;
Ptr p;
ConnHandle prefConn;
ConnHandle docConn;
CMBufferSizes sizes = { 0, 0, 0, 0, 0, 0, 0, 0 };
infoPB = (CInfoPBPtr)NewPtrClear(sizeof(*infoPB));
wdPB = (WDPBPtr)NewPtrClear(sizeof(*wdPB));
dirPB = (HParmBlkPtr)NewPtrClear(sizeof(*dirPB));
/* find the system folder's volume reference number and directory ID */
osErr = SysEnvirons(curSysEnvVers, &theWorld);
(*wdPB).ioVRefNum = theWorld.sysVRefNum;
if (noErr == (osErr = PBGetWDInfo(wdPB, false)))
{
/*********************************************************
* 11-27-91 Modified by Mark Sproul
**********************************************************/
/* get the Volume Reference Number and save it */
prefVRefNum = (*wdPB).ioWDVRefNum;
/* get the System directir ID and save it */
sysfDirID = (*wdPB).ioWDDirID;
/* check for the preferences folder */
(*infoPB).hFileInfo.ioFDirIndex = 0;
(*infoPB).hFileInfo.ioVRefNum = prefVRefNum;
(*infoPB).hFileInfo.ioDirID = sysfDirID;
(*infoPB).hFileInfo.ioNamePtr = "\pPreferences";
osErr = PBGetCatInfo(infoPB, false);
/* save the "Preferecnces" dir number */
prefDirID = (*infoPB).hFileInfo.ioDirID;
if (osErr == fnfErr)
{
/* Create "Preferences" folder */
(*dirPB).fileParam.ioVRefNum = prefVRefNum;
(*dirPB).fileParam.ioDirID = sysfDirID;
(*dirPB).fileParam.ioNamePtr = "\pPreferences";
osErr = PBDirCreate(dirPB, false);
prefDirID = (*dirPB).fileParam.ioDirID;
}
/*********************************************************
* end of modifications
**********************************************************/
if (osErr == noErr)
{
/* does the preference file exist? */
(*infoPB).hFileInfo.ioFDirIndex = 0;
(*infoPB).hFileInfo.ioVRefNum = prefVRefNum;
(*infoPB).hFileInfo.ioDirID = prefDirID;
(*infoPB).hFileInfo.ioNamePtr = prefsFileName;
osErr = PBGetCatInfo(infoPB, false);
if (osErr == noErr)
{
/* set flag saying the prefs file is OK */
prefFileOKFlag = 0xAA;
}
if (osErr == fnfErr)
{
/* no, so create a new preference file */
if (noErr == (osErr = HCreate(prefVRefNum, prefDirID, prefsFileName, creatorType, fileType)))
{
HCreateResFile(prefVRefNum, prefDirID, prefsFileName);
if (noErr == (osErr = ResError()))
{
/* open the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum == -1)
{
osErr = ResError();
}
else
{
/* create a default connection */
osErr = CRMGetIndToolName(classCM, 1,toolName);
if (noErr == osErr)
{
prefConn = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
/* leave the default setting as the preferance */
/************************************************************
* ORIGINALLY, the code let the user select setup at this point
* I do not want it asking for serial port prefs on startup
* if they are not set.
* ---allow the user to select a prefered tool and configuration
* ---osErr = CMChoose(&prefConn, where, nil);
************************************************************/
/* write the prefered tool name to the preference file */
HLock((Handle) prefConn);
CMGetToolName((**prefConn).procID, toolName);
HUnlock((Handle) prefConn);
h = NewHandle(1 + toolName[0]);
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'pTXT', 0, "");
ReleaseResource(h);
/* write the prefered configuration to the preference file */
p = CMGetConfig(prefConn);
h = NewHandle(GetPtrSize(p));
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'cTXT', 0, "");
ReleaseResource(h);
DisposPtr(p);
/* dispose of the connection */
CMDispose(prefConn);
}
/* close the file so that it can be used in a shared environment */
CloseResFile(prefRefNum);
/* set flag saying the prefs file is OK */
prefFileOKFlag = 0xAA;
}
}
}
}
}
}
}
//*****************************************************************
//*
//* The following code was written by Mark Sproul
//* This code allows easy access to prefs file
//*
//*
//*
//*
//*****************************************************************
/**************************************
* get resource from Prefs file
*
****************************************/
getPrefsResourceStr(prefsFileName, perfsResType, returnString)
Str255 prefsFileName;
OSType perfsResType;
Str255 returnString;
{
short prefRefNum;
Handle h;
int i;
Size hSize;
h = nil;
/* did the prefs file get opened or created OK */
if (prefFileOKFlag == 0xAA)
{
/* focus on the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum != -1)
{
h = Get1Resource(perfsResType, 0);
hSize = GetHandleSize(h); /* get the size of the handle */
if (hSize > 255) hSize = 255;
HLock(h);
/* had to have this to make it compile under THINK C 5.0 */
for (i=0; i< hSize; i++)
{
returnString[i] = *(*h+i);
}
HUnlock(h);
ReleaseResource(h);
CloseResFile(prefRefNum);
}
}
}
/**************************************
* set Comm Tool Box connection preference
*
****************************************/
setCTBpref(prefsFileName)
Str255 prefsFileName;
{
short prefRefNum;
OSErr osErr, cmChooseReturnCode;
int i;
Str255 prefStr;
short procID;
Str63 toolName;
Handle h;
Ptr p;
short iErr;
Size hSize, newSize;
ConnHandle docConn;
CMBufferSizes sizes = { 0, 0, 0, 0, 0, 0, 0, 0 };
Point where = { 75, 75 };
if (isCTBavailable())
{
/* did the prefs file get opened or created OK */
if (prefFileOKFlag == 0xAA)
{
getPrefsResourceStr(prefsFileName,'pTXT', prefStr);
procID = CMGetProcID(prefStr);
if (procID != -1)
{
/* create a new connection */
docConn = CMNew(procID, cmData, sizes, 0, 0);
if (docConn != nil)
{
/* set the prefered configuration */
getPrefsResourceStr(prefsFileName,'cTXT', prefStr);
osErr = CMSetConfig(docConn, (char *)prefStr);
cmChooseReturnCode = CMChoose(&docConn, where, nil);
}
}
else
{
/* the prefered tool could not be found so I */
osErr = CRMGetIndToolName(classCM, 1, toolName);
docConn = CMNew(CMGetProcID(toolName), cmData, sizes, 0, 0);
if (docConn != nil)
{
cmChooseReturnCode = CMChoose(&docConn, where, nil);
}
}
if ((cmChooseReturnCode == chooseOKMinor) || (cmChooseReturnCode == chooseOKMajor))
{
/* change the prefs file */
if (prefFileOKFlag == 0xAA)
{
/* open the preference file */
prefRefNum = HOpenResFile(prefVRefNum, prefDirID, prefsFileName, fsRdWrPerm);
if (prefRefNum != -1)
{
/* write the prefered tool name to the preference file */
HLock((Handle) docConn);
CMGetToolName((**docConn).procID, toolName);
HUnlock((Handle) docConn);
/* get the port TeXT resource */
h = Get1Resource('pTXT', 0);
if (h == nil)
{
/* resource did not exist, add it to resource file */
h = NewHandle(1 + toolName[0]);
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'pTXT', 0, "");
}
else
{
/* resoure DOES exist, change it */
hSize = GetHandleSize(h); /* get the size of the handle */
/* check for size of handle */
if (hSize != (1 + toolName[0]))
{
newSize = 1 + toolName[0];
SetHandleSize(h, newSize);
}
HLock(h);
BlockMove(toolName, *h, GetHandleSize(h));
HUnlock(h);
ChangedResource(h);
}
ReleaseResource(h);
/* write the prefered configuration to the preference file */
p = CMGetConfig(docConn);
/* get the configuration TeXT resource */
h = Get1Resource('cTXT', 0);
if (h == nil)
{
/* resource did not exist, add it to resource file */
h = NewHandle(GetPtrSize(p));
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
AddResource(h, 'cTXT', 0, "");
iErr = ResError();
}
else
{
/* resoure DOES exist, change it */
hSize = GetHandleSize(h); /* get the size of the handle */
/* check for size of handle */
if (hSize != GetPtrSize(p))
{
newSize = GetPtrSize(p);
SetHandleSize(h, newSize);
}
HLock(h);
BlockMove(p, *h, GetHandleSize(h));
HUnlock(h);
ChangedResource(h);
}
ReleaseResource(h);
CloseResFile(prefRefNum);
iErr = ResError();
DisposPtr(p);
}
}
}
if (docConn != nil)
{
/* dispose of the connection */
CMDispose(docConn);
}
}
}
}
DoCommPortSetup(prefsFileName)
Str255 prefsFileName;
{
setCTBpref(prefsFileName);
}
- -------------------------------------
Mark Sproul - KB2ICI
+++++++++++++++++++++++++++
From: winer@husc8.harvard.edu (Adam Winer)
Date: 2 Oct 92 20:12:00 GMT
Organization: Harvard University Science Center
If I remember correctly, there is MPW (and hence THINK C) glue that, if
FindFolder is called in a system without it, will just return the location
of the System Folder. Sounds pretty good to me, if it works.
Adam
- --
Adam Winer | The number you have reached is imaginary.
WINER@HARVARD.EDU | Please rotate your phone 90 degrees and
| try again.
---------------------------
End of C.S.M.P. Digest
**********************