home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-09-06 | 3.4 KB | 109 lines | [TEXT/MACA] |
- /*
- * fsmisc.c - miscellaneous file handling routines
- */
-
- #include <quickdraw.h>
- #include <packages.h>
- #include <pb.h>
- #include <syserr.h>
- #include <memory.h>
-
- #include "progerr.h"
- /*
- * FileCreate() - create a file for subsequent writing. FileCreate
- * uses the given filename (or asks the user for one) and makes sure
- * that file exists and is of the proper type & creator (deleting an
- * existing file if necessary).
- *
- * Needs - a flag that says "ask the user for a new name";
- * Nameptr - points to a filename buffer containing the original name;
- * vnumptr - points to the original volume reference number;
- * NOTE: *Nameptr and *vnumptr are updated if the routine
- * succeeds. If the routine fails, they are left unchanged.
- * Filetype - the type to be given the file.
- * Creator - the signature of the file's creator;
- * Prompt - the prompt string to be used in the file dialog.
- *
- * Returns: 1 if successful, 0 if cancelled.
- */
- int
- FileCreate(needs, nameptr, vnumptr, filetype, creator, prompt)
- short needs; /* 1 == ask for a name, 0 == use nameptr */
- char *nameptr; /* (Pascal) string buffer */
- short *vnumptr; /* resultant vrefnum */
- OSType filetype; /* type of file to create */
- OSType creator; /* creator ID to give to that file */
- char *prompt; /* (Pascal) string */
- {
- short exists; /* "the file exists" -- exit condition */
- SFReply repl; /* the filename being tried */
- Point dlgorig; /* upper-left of the alert box */
- FInfo fstuff;
- OSErr err;
-
- movmem(nameptr, repl.fName, ((int) nameptr[0] & 0xFF) + 1);
- repl.vRefNum = *vnumptr;
- dlgorig.h = 90; dlgorig.v = 100;
- exists = 0;
- while (!exists) {
- if (needs) { /* if we need a name, ask for one */
- SFPutFile(pass(dlgorig), prompt, repl.fName, (ProcPtr) 0, &repl);
- if (!repl.good) return(0);
- }
- needs = 1; /* assume that the operation is going to fail */
- err = GetFInfo(repl.fName, repl.vRefNum, &fstuff);
- if (err != noErr && err != fnfErr) {
- diskerr(err); continue;
- }
- if (err == noErr) { /* the filename already exists */
- if (fstuff.fdType == filetype) {
- exists = 1;
- } else { /* it's the wrong type */
- progstop(PE_TYPE);
- continue;
- }
- }
- if (!exists) { /* it doesn't exist -- create it */
- if ((err = Create(repl.fName, repl.vRefNum,
- creator, filetype)) != noErr) {
- diskerr(err); continue;
- }
- if ((err = FlushVol((char *) 0, repl.vRefNum)) != noErr) {
- diskerr(err); continue;
- }
- exists = 1;
- }
- }
- movmem(repl.fName, nameptr, ((int) repl.fName[0] & 0xFF) + 1);
- *vnumptr = repl.vRefNum;
- return(1);
- }
-
- /*
- * FileFetch() - get the name & volume ID of a file for subsequent reading.
- *
- * Nameptr, Vnumptr, Filetype -- same as FileCreate().
- * *Nameptr and &vnumptr are modified only if the routine is successful.
- *
- * Returns 1 if successful, 0 if cancelled.
- */
- int
- FileFetch(nameptr, vnumptr, filetype)
- char *nameptr; /* (Pascal) string buffer */
- short *vnumptr; /* resultant vrefnum */
- OSType filetype; /* type of file to fetch */
- {
- SFReply repl;
- Point dlgorig; /* upper-left of the alert box */
- SFTypeList appltypes; /* list of file-types to fetch */
-
- dlgorig.h = 90; dlgorig.v = 100;
- appltypes[0] = filetype;
- SFGetFile(pass(dlgorig), (char *) 0, (ProcPtr) 0, (short) 1, appltypes,
- (ProcPtr) 0, &repl);
- if (!repl.good) return(0);
- movmem(repl.fName, nameptr, ((int) repl.fName[0] & 0xFF) + 1);
- *vnumptr = repl.vRefNum;
- return(1);
- }
-