home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄fsmisc.c < prev    next >
Encoding:
Text File  |  1986-09-06  |  3.4 KB  |  109 lines  |  [TEXT/MACA]

  1. /*
  2.  * fsmisc.c - miscellaneous file handling routines
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <packages.h>
  7. #include <pb.h>
  8. #include <syserr.h>
  9. #include <memory.h>
  10.  
  11. #include "progerr.h"
  12. /*
  13.  * FileCreate() - create a file for subsequent writing.  FileCreate
  14.  *  uses the given filename (or asks the user for one) and makes sure
  15.  *  that file exists and is of the proper type & creator (deleting an
  16.  *  existing file if necessary).
  17.  *
  18.  * Needs - a flag that says "ask the user for a new name";
  19.  * Nameptr  - points to a filename buffer containing the original name;
  20.  * vnumptr  - points to the original volume reference number;
  21.  *  NOTE: *Nameptr and *vnumptr are updated if the routine
  22.  *   succeeds.  If the routine fails, they are left unchanged.
  23.  * Filetype - the type to be given the file.
  24.  * Creator  - the signature of the file's creator;
  25.  * Prompt   - the prompt string to be used in the file dialog.
  26.  *
  27.  * Returns: 1 if successful, 0 if cancelled.
  28.  */
  29. int
  30. FileCreate(needs, nameptr, vnumptr, filetype, creator, prompt)
  31. short needs;        /* 1 == ask for a name, 0 == use nameptr */
  32. char *nameptr;        /* (Pascal) string buffer        */
  33. short *vnumptr;        /* resultant vrefnum            */
  34. OSType filetype;    /* type of file to create        */
  35. OSType creator;        /* creator ID to give to that file    */
  36. char *prompt;        /* (Pascal) string            */
  37. {
  38.     short exists;        /* "the file exists" -- exit condition    */
  39.     SFReply repl;        /* the filename being tried        */
  40.     Point dlgorig;        /* upper-left of the alert box        */
  41.     FInfo fstuff;
  42.     OSErr err;
  43.  
  44.     movmem(nameptr, repl.fName, ((int) nameptr[0] & 0xFF) + 1);
  45.     repl.vRefNum = *vnumptr;
  46.     dlgorig.h = 90; dlgorig.v = 100;
  47.     exists = 0;
  48.     while (!exists) {
  49.     if (needs) {    /* if we need a name, ask for one    */
  50.         SFPutFile(pass(dlgorig), prompt, repl.fName, (ProcPtr) 0, &repl);
  51.         if (!repl.good) return(0);
  52.     }
  53.     needs = 1;    /* assume that the operation is going to fail */
  54.     err = GetFInfo(repl.fName, repl.vRefNum, &fstuff);
  55.     if (err != noErr && err != fnfErr) {
  56.         diskerr(err); continue;
  57.     }
  58.     if (err == noErr) {    /* the filename already exists    */
  59.         if (fstuff.fdType == filetype) {
  60.         exists = 1;
  61.         } else {        /* it's the wrong type        */
  62.         progstop(PE_TYPE);
  63.         continue;
  64.         }
  65.     }
  66.     if (!exists) {        /* it doesn't exist -- create it */
  67.         if ((err = Create(repl.fName, repl.vRefNum,
  68.           creator, filetype)) != noErr) {
  69.         diskerr(err); continue;
  70.         }
  71.         if ((err = FlushVol((char *) 0, repl.vRefNum)) != noErr) {
  72.         diskerr(err); continue;
  73.         }
  74.         exists = 1;
  75.     }
  76.     }
  77.     movmem(repl.fName, nameptr, ((int) repl.fName[0] & 0xFF) + 1);
  78.     *vnumptr = repl.vRefNum;
  79.     return(1);
  80. }
  81.  
  82. /*
  83.  * FileFetch() - get the name & volume ID of a file for subsequent reading.
  84.  *
  85.  * Nameptr, Vnumptr, Filetype -- same as FileCreate().
  86.  *  *Nameptr and &vnumptr are modified only if the routine is successful.
  87.  *
  88.  * Returns 1 if successful, 0 if cancelled.
  89.  */
  90. int
  91. FileFetch(nameptr, vnumptr, filetype)
  92. char *nameptr;        /* (Pascal) string buffer    */
  93. short *vnumptr;        /* resultant vrefnum        */
  94. OSType filetype;    /* type of file to fetch    */
  95. {
  96.     SFReply repl;
  97.     Point dlgorig;        /* upper-left of the alert box    */
  98.     SFTypeList appltypes;    /* list of file-types to fetch    */
  99.  
  100.     dlgorig.h = 90; dlgorig.v = 100;
  101.     appltypes[0] = filetype;
  102.     SFGetFile(pass(dlgorig), (char *) 0, (ProcPtr) 0, (short) 1, appltypes,
  103.       (ProcPtr) 0, &repl);
  104.     if (!repl.good) return(0);
  105.     movmem(repl.fName, nameptr, ((int) repl.fName[0] & 0xFF) + 1);
  106.     *vnumptr = repl.vRefNum;
  107.     return(1);
  108. }
  109.