[net.micro.mac] Megamax C help wanted

emil@rochester.UUCP (03/29/85)

From: Emil Rainero  <emil>



Can someone help me with megamax C?  I need to do very simple file I/O but
would like to use sfputfile/sfgetfile rather than stdio.  My problem is that
I have a problem with cstrings/pstrings and with OSType for creator and
filetype.  What follows is my code for writing and reading a file.  Any 
help would be appreciated.


/* ********************************************************* */

char buffer[100]; /* something to write out */

DoFileSave()
{
    point temppoint;
    sfreply reply;
    char *filename;
    int vrefnum, refnum;
    long count;
    
    temppoint.a.h = 110; temppoint.a.v = 110;
    
    sfputfile(&temppoint, "save maze as", "Untitled", (char *) 0, &reply);
    if (!reply.good) return;
    /* we have a legal file name, but it is in pascal form and that will 
       cause a problem for all of megamax c toolbox traps. */
    filename = reply.fname; /* we should convert it to a C string */
    vrefnum = reply.vrefnum;
    /* I know that ?????" is wrong for OSType, but what is right?
       I was thinkin of taking a long int and storing TEXT into it.
       Any suggestions? */
    if (create(filename, vrefnum, "????", "TEXT") != 0) return;
    if (fsopen(filename, vrefnum, &refnum) != 0) return;
    count = sizeof(buffer);
    if (fswrite(refnum, &count, buffer) != 0) return;
    /* should we check here for the number of actual bytes written? */
    /* is this really needed? */
    if (seteof(refnum, count) != 0) return;
    if (fsclose(refnum) != 0) return;
    /* Do we definitely need to do a flush volume here? */
    if (flushvol((char *) 0, vrefnum) != 0) return;
}


DoFileOpen()
{
    point temppoint;
    sfreply reply;
    char *filename;
    int vrefnum, refnum, count;
    stringhandle astr;
    
    temppoint.a.h = 110; temppoint.a.v = 110;
    
    sfgetfile(&temppoint, "prompt", (char *) 0, -1, (char *) 0, (char *) 0,
        &reply);
    if (!reply.good) return;
    /* open the file */
    filename = reply.fname; /* same problem here with Pascal strings */
    vrefnum = reply.vrefnum;
    if (fsopen(filename, vrefnum, &refnum) != 0) return;
    count = sizeof(buffer);
    /* is it better to get EOF and the length of the file instead of
    the number of bytes we want to read?  I assume so. */
    if (fsread(refnum, &count, buffer) != 0) return;
    /* should we check here for the actual number of bytes read */
    if (fsclose(refnum) != 0) return;
}


If anyone has some megamax C code that uses sfgetfile/sfputfile, could
you please mail me copy.  Any help as to what I am doing wrong would be
appreciated.  Maybe megamax c users would like to form a mailing list 
not to flood the net with a lot of hacker talk.  I've had so much trouble
debugging this code that I wrote a set of very simple functions that allows
me to trace my code with printf style strings.  Also the file constants
like 

#define DirFulErr       -33     /* */
#define DskFulErr       -34     /* Disk full */
#define NSVErr          -35     /* No such volume */
#define IOErr           -36     /* Disk I/O error */
#define BdNamErr        -37     /* Bad file name */
#define FNOpnErr        -38     /* File not open */
#define EOFErr          -39     /* End-of-file */
#define PosErr          -40     /* Tried to position before start of file */
#define MFulErr         -41     /* Memory full */
#define TMFOErr         -42     /* Too many files open */
#define FNFErr          -43     /* File not found */
#define WPrErr          -44     /* Hardware volume lock */
#define FLckdErr        -45     /* File locked */
#define VLckdErr        -46     /* Software volume locked */
#define FBsyErr         -47     /* */
#define DupFNErr        -48     /* */
#define OpWrErr         -49     /* File already open for writing */
#define ParamErr        -50     /* Error in user parameter list */
#define RFNumErr        -51     /* */
#define GFPErr          -52     /* */
#define VolOffLinErr    -53     /* */
#define PermErr         -54     /* */
#define VolOnLinErr     -55     /* */
#define NSDrvErr        -56     /* */
#define NoMacDskErr     -57     /* */
#define ExtFSErr        -58     /* External file system */
#define fsrnerr         -59     /* */ /* fix for case */
#define BadMDBErr       -60     /* */
#define WrPermErr       -61     /* */


This is an old partial listing of the code, and a lot has changed since then.  
If there is enough interest, I could post the newer version.  
Please mail replies to


        Emil Rainero
	UUCP:   (..!{allegra, decvax, seismo}!rochester!emil)
	ARPA:   emil@rochester.arpa
	USmail: Emil Rainero, Dept. of Comp. Sci., U. of Rochester, NY 14627.
	Phone:  Office: (716) 275-5365   Home: (716) 424-5016

tdn@cmu-cs-spice.ARPA (Thomas Newton) (03/29/85)

If you're getting a Pascal string back, and you need to convert it into a
C string (so that the Toolbox interface routines can reconvert it into a
Pascal string . . . aargh), there is a simple routine to do that.  Look in
the "Toolbox Library" section of the manual, page 9 - 1.

                                        -- Thomas Newton
                                           Thomas.Newton@cmu-cs-spice.ARPA