home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / midi / midi_playground / sourcecode / files.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  3KB  |  150 lines

  1. /**************************************************************************
  2. * files.c:    File open/close, input/output, etc.
  3. *        Part of MP, the MIDI Playground.
  4. *
  5. * Author:    Daniel Barrett
  6. * Version:    See the file "version.h".
  7. * Copyright:    None!  This program is in the Public Domain.
  8. *        Please share it with others.
  9. ***************************************************************************/
  10.  
  11.     
  12. #include <stdio.h>
  13. #include <exec/types.h>
  14. #include <libraries/dos.h>
  15. #include <libraries/dosextens.h>
  16.  
  17.  
  18. /* Open a file and return a pointer to it. */
  19.     
  20. FILE *OpenAFile(char *filename, char *mode, char *readOrWrite)
  21. {
  22.     FILE *fp;
  23.  
  24.         if ((fp = fopen(filename, mode)) == NULL)
  25.         fprintf(stderr, "Cannot %s the file \"%s\",\n",
  26.             readOrWrite, filename);
  27.     return(fp);
  28. }
  29.  
  30.     
  31. /* Open a file for reading.  We might also have a macro with the same name,
  32.  * so we used ifdef's. */
  33.  
  34. #ifndef OpenReadFile    
  35. FILE *OpenReadFile(char *filename)
  36. {
  37.     return(OpenAFile(filename, "r", "read"));
  38. }
  39. #endif
  40.  
  41.     
  42. /* Open a file for writing.  We might also have a macro with the same name,
  43.  * so we used ifdef's. */
  44.  
  45. #ifndef OpenWriteFile
  46. FILE *OpenWriteFile(char *filename)
  47. {
  48.     return(OpenAFile(filename, "w", "write"));
  49. }
  50. #endif
  51.  
  52.     
  53. /* Tell me whether or not a file exists. */
  54.  
  55. BOOL FileExists(char *filename)
  56. {
  57.     BPTR lock;
  58.  
  59.     if ((lock = Lock(filename, ACCESS_READ)) != NULL)
  60.     {
  61.         UnLock(lock);
  62.         return(TRUE);
  63.     }
  64.     return(FALSE);
  65. }
  66.  
  67.     
  68. /* See if we want to overwrite an existing file. */
  69.  
  70. /* BUFSIZ is too large, perhaps -- change later -- but don't compromise
  71.  * on correct reading of the user's input.  Too small, and extra unread
  72.  * chars are read on the NEXT call (yuck).  If you use gets(), too much
  73.  * input crashes the program. */
  74.  
  75. BOOL DontOverwriteExistingFile(char *filename)
  76. {
  77.     char buf[BUFSIZ];
  78.     BOOL safety = TRUE;
  79.  
  80.     if (FileExists(filename))
  81.     {
  82.         fprintf(stderr,
  83.              "File \"%s\" already exists.\nOVERWRITE it? [%s]: ",
  84.              filename, "Yes/No, RETURN=No");
  85.         fflush(stderr);
  86.  
  87.         if (fgets(buf, BUFSIZ-1, stdin))
  88.             safety = (toupper(buf[0]) != 'Y');
  89.         else
  90.             safety = TRUE;
  91.     }
  92.     else
  93.         safety = FALSE;            /* File doesn't exist. */
  94.  
  95.     if (safety)
  96.         fprintf(stderr, "%s not overwritten.\n\n", filename);
  97.  
  98.     return(safety);
  99. }
  100.  
  101.  
  102. /* Open the input and output files.
  103.  * We open input AFTER output in case the user has specified CON: windows,
  104.  * so the input window is the active one. */
  105.  
  106. BOOL SetupFiles(char *infile, char *outfile, FILE **in, FILE **out)
  107. {
  108.     if (!outfile || (outfile[0] == '\0'))
  109.         *out = stdout;
  110.     else if (DontOverwriteExistingFile(outfile))
  111.         return(FALSE);
  112.     else if ((*out = OpenWriteFile(outfile)) == NULL)
  113.         return(FALSE);
  114.  
  115.     if (!infile || (infile[0] == '\0'))
  116.         *in = stdin;
  117.     else if ((*in = OpenReadFile(infile)) == NULL)
  118.         return(FALSE);
  119.  
  120.     return(TRUE);
  121. }
  122.  
  123.     
  124. /* Close the files used by the program, and deallocate the arrays used
  125.  * for storing the filenames. */
  126.  
  127. void CloseFiles(FILE *in, FILE *out, char *filein, char *fileout)
  128. {
  129.     if (in && (in != stdin))    fclose(in);
  130.     if (out && (out != stdout))    fclose(out);
  131.     if (filein)            free(filein);
  132.     if (fileout)            free(fileout);
  133. }
  134.  
  135.     
  136. /* Allocate space to store a filename, and copy the contents of "src" into
  137.  * it. */
  138.  
  139. BOOL MakeFilename(char **dest, char *src)
  140. {
  141.     if ((!src) || (*src == '\0')
  142.     ||  ((*dest = (char *)malloc(strlen(src)+1)) == NULL))
  143.         return(FALSE);
  144.     else
  145.     {
  146.         strcpy(*dest, src);
  147.         return(TRUE);
  148.     }
  149. }
  150.