home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / qdtool10 / fileutil.c next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  2.4 KB  |  94 lines

  1. /*
  2.  * Copyright (C) 1996 by Chris Johnson.  All rights reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and
  5.  * its documentation for any purpose and without fee is hereby
  6.  * granted, provided that the above copyright notice appear in all
  7.  * copies and that both that copyright notice and this permission
  8.  * notice appear in supporting documentation.  If more than a few
  9.  * lines of this code are used in a program which displays a copyright
  10.  * notice or credit notice, the following acknowledgment must also be
  11.  * displayed on the same screen: "This product includes software
  12.  * developed by Chris Johnson for use in the QuakeDef Tools package."
  13.  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESSED OR IMPLIED
  14.  * WARRANTY.
  15.  *
  16.  * (Thanks to Raphael Quinet for this nifty disclaimer!)
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "fileutil.h"
  23.  
  24. //    Returns: 1 if file exists
  25. //             0 if file does not exist
  26. int    fexist(const char *filename)
  27. {
  28.     FILE *test;
  29.  
  30.     if ((test = fopen(filename, "rb")) == NULL)
  31.     {
  32.         fprintf(stderr,
  33.                 "\nError -- The following file could not be found:");
  34.         fprintf(stderr, "\n%s\n", filename);
  35.         return (0);
  36.     }
  37.  
  38.     fclose(test);
  39.  
  40.     return (1);
  41. }
  42.  
  43. // Returns: Pointer to file that was opened
  44. //            NULL if there was a problem
  45. FILE *safeopen(const char *filename, const char *mode)
  46. {
  47.     FILE *fp;
  48.  
  49.     if ((fp = fopen(filename, mode)) == NULL)
  50.     {
  51.         fprintf(stderr, "\nError -- Could not ");
  52.  
  53.         if (strchr(mode, 'r') || strchr(mode, 'a'))
  54.             fprintf(stderr, "open ");
  55.         else
  56.             fprintf(stderr, "create ");
  57.  
  58.         fprintf(stderr, "the following file:");
  59.         fprintf(stderr, "\n%s\n", filename);
  60.     }
  61.  
  62.     return (fp);
  63. }
  64.  
  65. // Safe but not descriptive
  66. // Returns: EOF on EOF
  67. //            0 otherwise
  68. int copy(FILE *dest, FILE *src, unsigned long len)
  69. {
  70.     char buffer[BLOCK_SIZE];
  71.     long num_chunks;
  72.     int     last_chunk;
  73.  
  74.     num_chunks = len / BLOCK_SIZE;
  75.     last_chunk = (int)(len % BLOCK_SIZE);
  76.  
  77.     for (; num_chunks > 0; num_chunks--)
  78.     {
  79.         if (fread(buffer, sizeof(char) * BLOCK_SIZE, 1, src) != 1)
  80.             return (EOF);
  81.         if (fwrite(buffer, sizeof(char) * BLOCK_SIZE, 1, dest) != 1)
  82.             return (EOF);
  83.     }
  84.  
  85.     if (last_chunk)
  86.     {
  87.         if (fread(buffer, sizeof(char) * last_chunk, 1, src) != 1)
  88.             return (EOF);
  89.         if (fwrite(buffer, sizeof(char) * last_chunk, 1, dest) != 1)
  90.             return (EOF);
  91.     }
  92.  
  93.     return (0);
  94. }