CCL Home Page
Up Directory CCL file_opened
#include "utility.h"

FILE *file_opened(raw_filename,extensions,mode,real_filename)
/*============================================================================*/
/* PURPOSE: OPEN A FILE USING FIRST AVAILABLE DEFAULT EXTENSION IF NEEDED.
/*	IF THE RAW_FILENAME ALREADY HAS AN EXTENSION, IT IS USED UNCHANGED.
/*	IF NO EXTENSION IS PRESENT, THE LIST OF EXTENSIONS IS TRIED ONE AT A
/*	TIME; THE FIRST VALID EXTENSION IS USED.  VALID MEANS THAT THE FOPEN
/*	CALL SUCCEEDED WITH THAT EXTENSION ON THE FILENAME.
/* INPUTS:
/*	RAW_FILENAME	FILENAME WITH OR WITHOUT EXTENSION (TYPICALLY AS
/*			ENTERED BY THE USER).
/*	EXTENSIONS	LIST OF DEFAULT EXTENSIONS TO APPEND TO RAW_FILENAME IF
/*			RAW_FILENAME DOES NOT CONTAIN A PERIOD (IE, RAW_FILENAME
/*			DOES NOT ALREADY CONTAIN AN EXTENSION).  NOTE THAT EACH
/*			EXTENSION MUST EXPLICITLY CONTAIN A LEADING PERIOD IF
/*			DESIRED.  MULTIPLE EXTENSIONS MUST BE SEPARATED BY ANY
/*			COMBINATION OF COMMAS AND SPACES.
/*	MODE		FOPEN MODE STRING.
/* OUTPUTS:
/*	REAL_FILENAME	REAL FINAL FILENAME PASSED TO FOPEN.
/*	FUNCTION VALUE	FILE DESCRIPTOR RETURNED BY FOPEN.
/* WRITTEN: M.V.GRIESHABER
/* LAST MODIFICATION: 11 JUNE 1991 MVG
*/
   char raw_filename[];
   char extensions[];
   char mode[];	
   char real_filename[];
   {
   char extensions_copy[100];	/* LOCAL COPY OF EXTENSIONS (MODIFIED). */
   char *extension;		/* CURRENT EXTENSION UNDER CONSIDERATION. */
   FILE *file_descriptor;	/* OPENED FILE. */

   if (strchr(raw_filename,'.') != NULL)
      {
      /* AN EXTENSION WAS SPECIFIED ON THE FILENAME; DON'T TRY ALTERNATES. */
      strcpy(real_filename,raw_filename);
      return(fopen(real_filename,mode));
      }
   else
      {
      /* NO EXTENSION SPECIFIED, TRY LISTED EXTENSIONS FOR ONE THAT WORKS. */

      /* GET THE FIRST EXTENSION FROM THE LIST. */
      strcpy(extensions_copy,extensions);
      extension=strtok(extensions_copy,", ");

      while (extension!=NULL)
         {
         /* FORM THE REAL FILENAME FROM THE RAW FILENAME AND THIS EXTENSION. */
         strcpy(real_filename,raw_filename);
         strcat(real_filename,extension);

         /* IF THIS NAME WAS OPENED SUCESSFULLY, RETURN THE FILE DESCRIPTOR, */
         /* OTHERWISE TRY THE NEXT EXTENSION IN THE LIST. */
         if ((file_descriptor=fopen(real_filename,mode))!=NULL) return(file_descriptor);
         else extension=strtok(NULL,", ");
         }

      /* FAILED: FELL THROUGH AFTER CHECKING ALL EXTENSIONS. */
      return(NULL);
      }
   }
Modified: Fri Feb 11 17:00:00 1994 GMT
Page accessed 5000 times since Sat Apr 17 21:58:53 1999 GMT