[comp.sys.mac.programmer] File Manager question

xxiaoye@eleazar.dartmouth.edu (Xiaoxia Ye) (08/18/89)

I am fairly new to Mac Programming.  The other day I tackled the
dreaded IM IV File Manager section because I am writing an application
that need to save some preference parameters.  There are a number of
ways that I can think of solving this problem, but I can find a
solution or a safe solution to this:

1)  Saving the preferences in the data fork of the currently running
application.

Question:  How do I get the refNum, vRefNum of the currently running
application?  Here is a naive question: is the refNum for the resource
fork the same for the data fork ?  I can get the refNum from
CurResFile() from resource manager, but can I use it for FSWrite to the
data fork of the currently running application?  What about the vRefNum?
Can I simply pass '0' to stand for default working folder?  What happens
when the default working folder changes ?

2) Saving the preferences in a file in the currently blessed system
folder.

Question:  How do I get the vRefNum of the current system Folder ?

3) Saving the preference in a file in the same folder as the application
is in.

Question:  Similar question as in 1, how can I be sure that the default
working directory will not be changed ? is passing '0' as vRefNum always
safe?

Well, I have only read the High level routines in the File Manager
section of IM IV, should I read the low level routines too ?

Are these things discussed in any tech notes, which one ?

Much thanks in advance



________________________________________________________________________
Xiaoxia  Ye          INTERNET/BITNET/UUCP: xxiaoye@eleazar.dartmouth.edu
Dartmouth College    For more info: finger xxiaoye@eleazar.dartmouth.edu

tim@hoptoad.uucp (Tim Maroney) (08/19/89)

In article <15088@dartvax.Dartmouth.EDU> xxiaoye@eleazar.dartmouth.edu
(Xiaoxia Ye) writes:
>I am fairly new to Mac Programming.  The other day I tackled the
>dreaded IM IV File Manager section because I am writing an application
>that need to save some preference parameters.  There are a number of
>ways that I can think of solving this problem, but I can find a
>solution or a safe solution to this:
>
>1)  Saving the preferences in the data fork of the currently running
>application.

Don't do it.  You are making yourself gratuitously incompatible with
network file servers and the rumored multiuser Macintosh.  Never write
to the data or resource fork of your application file; treat it as
read-only in all cases.

>Question:  How do I get the refNum, vRefNum of the currently running
>application?  Here is a naive question: is the refNum for the resource
>fork the same for the data fork ?  I can get the refNum from
>CurResFile() from resource manager, but can I use it for FSWrite to the
>data fork of the currently running application?  What about the vRefNum?
>Can I simply pass '0' to stand for default working folder?  What happens
>when the default working folder changes ?

Might as well answer these, even though they're moot.  The refnums are
different.  If you wanted to get the refnum of the data fork of a file,
you would have to call PBOpen or PBHOpen.  The refnums for resource
forks come from PBOpenRF and PBHOpenRF; the refnums for data forks come
from PBOpen and PBHOpen.  As for volume zero, I hardly ever use it any
more, but if you want to, you can.  It's set to be the volume and
folder of the application file when the application is launched, and
provided you don't do PBSetVol's yourself, it shouldn't change.  Notice
that volume 0 is completely unrelated to Standard File's idea of the
current directory.

>2) Saving the preferences in a file in the currently blessed system
>folder.
>
>Question:  How do I get the vRefNum of the current system Folder ?

Fortunately, SysEnvirons will return it for you, as long as all you
want is a working directory reference number (which pretends to be
a volume reference number) rather than a volume number/folder number
pair.  Here's how I do it:

short GetSysWD()
{	SysEnvRec theWorld; SysEnvirons (1, &theWorld);
	return theWorld.sysVRefNum;
}

>3) Saving the preference in a file in the same folder as the application
>is in.
>
>Question:  Similar question as in 1, how can I be sure that the default
>working directory will not be changed ? is passing '0' as vRefNum always
>safe?

The system folder solution is the one everyone else uses, so you really
ought to do the same to avoid confusing the user.

It is remotely possible that when your application is launched, volume
0 will not be the application's folder.  The reason is that the _Launch
trap doesn't do this, the Finder does.  If your application was
launched from non-Finder software, it may have its own
oh-so-very-clever ideas about where volume 0 should be.  Presumably,
the person doing this strange launching is a power user and can cope
with the consequences, but even so....  You can always do a
PBGetFCBInfo on the application resource file refnum to determine
exactly where the file resides if you like.

>Well, I have only read the High level routines in the File Manager
>section of IM IV, should I read the low level routines too ?

Yes, afraid so.  You can skip "Data Organization on Volumes", though.

Hope this helps....
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

"Conversion, fastidious Goddess, loves blood better than brick, and feasts
 most subtly on the human will." - Virginia Woolf, "Mrs. Dalloway"

) (08/19/89)

In article <8334@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) writes:
[ABout saving prefs in App folder or System folder]
>The system folder solution is the one everyone else uses, so you really
>ought to do the same to avoid confusing the user.

Well, actually, no it isn't. AppleScan, for instance, save prefs in the
Application folder, so far as I have noticed. I've seen this once or
twice in other Apple-ications as well...

h+
-- 
This is your fortune from h+@nada.kth.se:
Computers are not intelligent.  They only think they are.

ech@cbnewsk.ATT.COM (ned.horvath) (08/20/89)

In article <8334@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) writes:
 [ABout saving prefs in App folder or System folder]
>The system folder solution is the one everyone else uses, so you really
>ought to do the same to avoid confusing the user.

From article <1437@draken.nada.kth.se>, by d88-jwa@nada.kth.se (All this was brought to you by h+@nada.kth.se  Replys via email welcome!):
> Well, actually, no it isn't. AppleScan, for instance, save prefs in the
> Application folder, so far as I have noticed. I've seen this once or
> twice in other Apple-ications as well...

There are two issues: keep conceptually "read only" files -- thesaurus
and dictionary if you're a WP, "toolkits" if you're doing CAD, Host files
if you're communications, etc. -- in the application folder.  That folder
is easy to find, just do a "GetVol" at launch time and remember the vRefNum.

However, despite the ugly fact that the System Folder is already too damned
big, preference files are writeable (by definition), and the blessed folder
is the only reliably safe place to put them.  This is particularly important
in network environments, where we can share multilaunched apps (and
associated resource files), but have to retain separate preference files
and documents.

=Ned Horvath=

svc@well.UUCP (Leonard Rosenthol) (08/20/89)

>I am fairly new to Mac Programming.  The other day I tackled the
>dreaded IM IV File Manager section because I am writing an application
>that need to save some preference parameters.  There are a number of
>ways that I can think of solving this problem, but I can find a
>solution or a safe solution to this:
>
>1)  Saving the preferences in the data fork of the currently running
>application.
>
	DO NOT DO THIS!  Writing to yourself (either in the datafork or the
resource fork) is not recommended!  Some of reasons for this include the fact
that by doing so, you make you application no longer usuable from a server
under certain conditions...and your users would not be very happy.
>
>2) Saving the preferences in a file in the currently blessed system
>folder.
>
	This is the Apple Approved (and in many ways the easiest) of the ways
to save off preference info.

>Question:  How do I get the vRefNum of the current system Folder ?
>
	Call SysEnvirons (IMV-5).  One of the fields of the sysEnvRec which
is returned to you is the sysVRefNum (ie. the vRefNum of the blessed system
folder).  Quick and painless.
>
>3) Saving the preference in a file in the same folder as the application
>is in.
>
	This will also work, but again this may also have problems on server
volumes depending on the permissions of the apps folder.

>Question:  Similar question as in 1, how can I be sure that the default
>working directory will not be changed ? is passing '0' as vRefNum always
>safe?
>
	What I would do to solve this one is to do a GetVol at the beginning
of the application (right after the rest of the Toolbox inits) and that will
give you the vRefNum of the appls folder for use in writing it out.  This is
also useful when you have owned files such as spelling dictionaries and help
files.

>Much thanks in advance
>
	Anytime!


+--------------------------------------------------+
Leonard Rosenthol        |  GEnie : MACgician
Lazerware, inc.          |  MacNet: MACgician
UUCP: svc@well.UUCP      |  ALink : D0025
-- 
+--------------------------------------------------+
Leonard Rosenthol        |  GEnie : MACgician
Lazerware, inc.          |  MacNet: MACgician
UUCP: svc@well.UUCP      |  ALink : D0025

urlichs@smurf.ira.uka.de (Matthias Urlichs) (08/22/89)

In article <8334@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) writes:
 [ABout saving prefs in App folder or System folder]
>The system folder solution is the one everyone else uses, so you really
>ought to do the same to avoid confusing the user.

In comp.sys.mac.programmer, ech@cbnewsk.ATT.COM (ned.horvath) writes:
< There are two issues: keep conceptually "read only" files -- thesaurus
< and dictionary if you're a WP, "toolkits" if you're doing CAD, Host files
< if you're communications, etc. -- in the application folder.  That folder
< is easy to find, just do a "GetVol" at launch time and remember the vRefNum.
< 
Why keep them anywhere special?
  If I want to get at any "special" files, I just do an _Open with the volume
refnum set to zero so that the PMSP gets walked down. (If the file is found,
of course, I try to check the file type..)
  Same for Preferences files. If it's there I never delete it, just overwrite
and set EOF. That way, the user can keep his/her files anywhere he/she wants.
  If the Prefs file is not there, I create it in the System Folder. If the
user want to move it somewhere else, no problem; if it's still in the PMSP
somewhere, my app will find it.

Apple: Any documentation (Technote??) on how to set and/or examine the PMSP?
-- 
Matthias Urlichs -- urlichs@smurf.ira.uka.de