home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / FILE.C < prev    next >
C/C++ Source or Header  |  1993-11-16  |  6KB  |  205 lines

  1. /*  $Id$
  2.  *  
  3.  *  File    file.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    File manipulation
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    16/10/93  (Created)
  12.  *          03/11/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*  This file contains a number of file manipulation functions
  17.  *  that may or may not be different depending on the hardware or
  18.  *  software environment being used.  At the moment the ANSI
  19.  *  library functions are used.
  20.  */
  21.  
  22. /*------------------------------------------------------------
  23.  *  Directives
  24.  *------------------------------------------------------------*/
  25.  
  26. #include "cbuff.h"
  27.  
  28.  
  29. /*------------------------------------------------------------
  30.  *  Opening files with extensions
  31.  *------------------------------------------------------------*/
  32.  
  33. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  34. @node fopenExtension
  35. @deftypefun {FILE *} fopenExtension (char *@var{base}, char *@var{ext}, char *@var{mode}, bool @var{overwrite})
  36. Returns a @code{FILE *} for file specified by @var{base} and @var{ext}.
  37. The handle is returned by calling @code{fopen} with the file name
  38. and the @var{mode} argument.  If @var{overwrite} is @code{FALSE} the
  39. file may not exist before this call (an error message is generated
  40. if the file exists).  Examples:
  41. @example
  42. fopenExtension("mybase", "cbi", "rb", TRUE)
  43. fopenExtension("newbase", "cbi", "wb", FALSE)
  44. @end example
  45. The first example opens @code{mybase.cbi} for reading.  The second creates
  46. @code{newbase.cbi} for writing, it may not yet exist.
  47. @end deftypefun
  48. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  49.  
  50. FILE *
  51. fopenExtension(char *base, char *ext, char *mode, bool overwrite)
  52. { FILE *fd;
  53.   char buf[MAX_FILE_NAME_SIZE+1];    /* Real file name */
  54.  
  55.   if ((strlen(base)+strlen(".")+strlen(ext)) > MAX_FILE_NAME_SIZE)
  56.   { fprintf(stderr, "File name %s.%s too long\n", base, ext);
  57.     exit(1);
  58.   }
  59.  
  60.   strcpy(buf, base);
  61.   strcat(buf, ".");
  62.   strcat(buf, ext);
  63.  
  64.   if (overwrite == FALSE)
  65.   { if ((fd = fopen(buf, "r")))        /* Determine if file exists */
  66.     { setNameError(buf);
  67.       setError(ERR_COULD_NOT_OVERWRITE_FILE);
  68.       fclose(fd);
  69.       return NULL;
  70.     }
  71.   }
  72.   
  73.   if ((fd = fopen(buf, mode)) == NULL)
  74.   { setNameError(buf);
  75.     if (mode[0] == 'r')
  76.       setError(ERR_COULD_NOT_OPEN_READ);
  77.     else
  78.       setError(ERR_COULD_NOT_OPEN_WRITE);
  79.     return NULL;
  80.   }
  81.   
  82.   return fd;
  83. }
  84.  
  85.  
  86. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87. @node fopenCbuffFile
  88. @deftypefun {FILE *} fopenCbuffFile (char *@var{name}, char *@var{mode})
  89. Returns a @code{FILE *} for the file specified by @var{name}.
  90. @code{fopenCbuffFile} is used for ``standard'' CBUFF files.  It first
  91. tries to find the file in the current directory and, if not present,
  92. searches the CBUFF directory (as specified by @code{CBUFFDIR} environment
  93. variables).
  94. @end deftypefun
  95. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  96.  
  97. FILE *
  98. fopenCbuffFile(char *name, char *mode)
  99. { FILE *fd;
  100.   char buf[MAX_FILE_NAME_SIZE+1];    /* Real file name */
  101.   char *dir;                /* CBUFF directory */
  102.  
  103.   if (strlen(name) > MAX_FILE_NAME_SIZE)
  104.   { fprintf(stderr, "File name %s too long\n", name);
  105.     exit(1);
  106.   }
  107.  
  108.   if ((fd = fopen(name, mode)))        /* Find file in current dir */
  109.     return fd;
  110.  
  111.   if ((dir = getCbuffDirectory()))    /* Find file in CBUFF directory */
  112.   { strcpy(buf, dir);
  113.     strcat(buf, name);
  114.   
  115.     if ((fd = fopen(buf, mode)))
  116.       return fd;
  117.   }
  118.   
  119.   setNameError(name);
  120.   if (mode[0] == 'r')
  121.     setError(ERR_COULD_NOT_OPEN_READ);
  122.   else
  123.     setError(ERR_COULD_NOT_OPEN_WRITE);
  124.  
  125.   return NULL;
  126. }
  127.  
  128.  
  129. /*------------------------------------------------------------
  130.  *  Reading and writing binary data
  131.  *------------------------------------------------------------*/
  132.  
  133. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  134. @node readLong
  135. @deftypefun long readLong (FILE *@var{fd})
  136. Read a @code{long} from the given file.  The function takes the ChessBase
  137. byte order into account.
  138. @end deftypefun
  139. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  140.  
  141. long
  142. readLong(FILE *fd)
  143. { unsigned char d[4];
  144.   long val;
  145.  
  146.   if (fread(d,1,4,fd) != 4)
  147.   { setError(ERR_UNEXPECTED_EOF);
  148.     reportError(stderr);
  149.   }
  150.  
  151.              val  = d[0];
  152.   val <<= 8; val |= d[1];
  153.   val <<= 8; val |= d[2];
  154.   val <<= 8; val |= d[3];
  155.  
  156.   return val;
  157. }
  158.  
  159.  
  160. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  161. @node writeLong
  162. @deftypefun void writeLong (long val, FILE *@var{fd})
  163. Writes a @code{long} to the given @var{fd}, taking into account the
  164. byte order used by ChessBase.
  165. @end deftypefun
  166. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  167.  
  168. void
  169. writeLong(long val, FILE *fd)
  170. { unsigned char d[4];
  171.  
  172.   d[0] = (unsigned char) (val >> 24);
  173.   d[1] = (unsigned char) (val >> 16);
  174.   d[2] = (unsigned char) (val >> 8);
  175.   d[3] = (unsigned char) (val >> 0);
  176.  
  177.   fwrite(d, 1, 4, fd);
  178. }
  179.  
  180.  
  181. /*------------------------------------------------------------
  182.  *  CBUFF directory
  183.  *------------------------------------------------------------*/
  184.  
  185. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  186. @node getCbuffDirectory
  187. @deftypefun {char *} getCbuffDirectory ()
  188. Returns a string containing the name of the CBUFF directory (as specified
  189. with the @code{CBUFFDIR} environment variable.  @xref{CBUFF directory}.
  190. Some environments may not define the @code{getenv} function that is
  191. necessary to implement this function (see @code{HAS_GETENV} in the file
  192. @code{machine.h} for details).
  193. @end deftypefun
  194. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  195.  
  196. char *
  197. getCbuffDirectory()
  198. {
  199. #if HAS_GETENV
  200.   return getenv("CBUFFDIR");
  201. #else
  202.   return NULL;
  203. #endif
  204. }
  205.