utility
|
cmd_head.c,
count_tokens.c,
cp_file.c,
errmsg.c,
file_opened.c,
find_line.c,
find_string.c,
fn_base.c,
fn_dir.c,
fn_ext.c,
fn_root.c,
gchoice.c,
gdata_line.c,
gdouble.c,
gfield.c,
gint.c,
gntoken.c,
graphics_ok.c,
gstring.c,
lock.c,
lower.c,
mat_multiply.c,
mat_transform.c,
mat_transpose.c,
mv_file.c,
new_extension.c,
nolead.c,
notrail.c,
ordinal.c,
pfield.c,
squish_blanks.c,
test.c,
unlock.c,
upper.c,
utility.h,
utility.make,
valchars.c,
vec_angle.c,
vec_crossprod.c,
vec_diff.c,
vec_dotprod.c,
vec_length.c,
vec_norm.c,
|
|
|
#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);
}
}
|