[comp.sys.mac.programmer] Can't seem to get SFGetFile to work....!

jtn@potomac.ads.com (John T. Nelson) (09/23/90)

This is going to sound ike a truly naive' question but I've been over
and over the problem and I can't seem to find where I'm going wrong.
Maybe it's someplace I'm not looking.  Here's the problem. The very
first thing I do in a program is call a function I call "selectFile"
which is basically a shell wrapped around SFGetFile....

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define		FILE_NAME_SIZE				30
#define		DEFAULT_DIALOG_X_POS		100
#define		DEFAULT_DIALOG_Y_POS		100
 
FILE	*fi, *fo;
char	buffer[128];
char	fileName[FILE_NAME_SIZE + 1];
 
main()
{
char	sourceFile[FILE_NAME_SIZE + 1];
 
selectFile(sourceFile);

.
.
.
}

Well the call to selectFile causes a Bus Error right on the SFGetFile
and I can't figure out why.  I've checked with other Macintosh books
("Using the Macintosh ToolBox with C", "Macintosh Revealed" and even
"Inside Macintosh") and the call looks kosher.  Problem is that even
the above three references vary on what they say should go into
SFGetFile.

By the way I'm using Lightspeed C with MacHeaders turned on and the
ANSI/unix/MacTraps libraries and it all compiles and links perfectly.

selectFile looks like this....


selectFile(char *name)
{
	char		fName[FILE_NAME_SIZE + 1];
	Point		position;
	int			numTypes;
	OSType		tList;			/* Was SFTypeList */
	SFReply		reply;
	int			workingVrefNum;
	Str255		prompt;
 
	numTypes = -1;
	SetPt(&position,DEFAULT_DIALOG_X_POS,DEFAULT_DIALOG_Y_POS);
	tList = '????';
 
	SFGetFile(position, "\p", 0L, numTypes, &tList, 0L, &reply);
 
	workingVrefNum = reply.vRefNum;
	SetVol(0L,reply.vRefNum);
 
	if ( reply.good ) {
		strcpy(fName, (char *) reply.fName);
		PtoCstr(fName);
		strcpy(name, fName);
		}
}

Help!



-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ORGANIZATION:  Advanced Decision Systems   GEOGRAPHIC: Arlington, VA
UUCP:          kzin!speaker@mimsy.umd.edu  INTERNET:   jtn@potomac.ads.com
SPOKEN:        Yo... John!                 PHONE:      (703) 243-1611
PROJECT:       The Conrail Locomotive/Harpsichord Fusion Program
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

niko@du248-16.cc.iastate.edu (Schuessler Nikolaus E) (09/23/90)

>
>Well the call to selectFile causes a Bus Error right on the SFGetFile
>and I can't figure out why.  I've checked with other Macintosh books

>
>selectFile(char *name)
>{
>	char		fName[FILE_NAME_SIZE + 1];
>	Point		position;
>	int			numTypes;
>	OSType		tList;			/* Was SFTypeList */
>	SFReply		reply;
>	int			workingVrefNum;
>	Str255		prompt;
> 
>	numTypes = -1;
>	SetPt(&position,DEFAULT_DIALOG_X_POS,DEFAULT_DIALOG_Y_POS);
>	tList = '????';
> 
>	SFGetFile(position, "\p", 0L, numTypes, &tList, 0L, &reply);

I know the following code works.... The only difference is that you allocated
1 space for an OStype, where SFTypeList allocates 4.  Hmmph.

{  SFReply reply;
  SFTypeList typeList;
  Point Pt;
  
  Pt.h=100;
  Pt.v=100;
  
  pStrCopy(FileName,"\p");
  SFGetFile(Pt,"\p",0L,-1,typeList,0L,&reply);
  if (reply.good)
     pStrCopy(FileName,reply.fName);
      
  *vref=reply.vRefNum;
  
  return reply.good;
}

It sorta looks like the same piece of code, huh?



--
------------------------------------------------------------------------------
Niko Schuessler               "On a two semester mission to engineer where
niko@iastate.edu               no-one has engineered before.... :-) "
------------------------------------------------------------------------------

phils@chaos.cs.brandeis.edu (Phil Shapiro) (09/24/90)

I may be missing something, but... you can't call SF{Put,Get}File
before you've intialized the ToolBox.  The standard file routines
depend on a initialized QuickDraw world.  If you want to use the
standard file calls with the console library (the part of ANSI that
gives you a menubar, etc.) you can let the console do the inits for
you simply by referencing any of the standard i/o streams, eg:

main()
{
    printf("\n");
    selectfile();
    ...
}

BTW, the 'minimal' SFGetFile call would probably be:

    SFGetFile(0x00500045, "", 0L, -1, 0L, 0L, &reply);
or
    SFGetFile(0x00500045, "", 0L, 1, "TEXT", 0L, &reply);

	-phil shapiro, symantec tech support
--
Phil Shapiro
phils@chaos.cs.brandeis.edu

francis@arthur.uchicago.edu (Francis Stracke) (09/27/90)

In article <9183@potomac.ads.com> jtn@potomac.ads.com (John T. Nelson) writes:
>
[...]>Here's the problem. The very
>first thing I do in a program is call a function I call "selectFile"
>which is basically a shell wrapped around SFGetFile....
[...]
>Well the call to selectFile causes a Bus Error right on the SFGetFile
>and I can't figure out why.  I've checked with other Macintosh books
>("Using the Macintosh ToolBox with C", "Macintosh Revealed" and even
>"Inside Macintosh") and the call looks kosher.  Problem is that even
>the above three references vary on what they say should go into
>SFGetFile.
[...]
>selectFile looks like this....
[...]
>selectFile(char *name)
>{
>	int			numTypes;
>	OSType		tList;			/* Was SFTypeList */
[...] 
>	numTypes = -1;
[...]
>	tList = '????';
> 
>	SFGetFile(position, "\p", 0L, numTypes, &tList, 0L, &reply);
I'm assuming that passing a ptr to an OSType would be OK if SFGetFile
thought that it was an SFTypeList with 1 elt; but for that you probably
need numTypes=1! (Oh, God, it's thinking you've got 65535 file types!)
(I'm assuming because I'm a Pascal programmer myself, & all I've got is
IM, which, as we all know, completely ignores C.)