📄 fiufile.cpp
字号:
/*\t*******************************************************************/
/* Creation Date ....... Fri 06-04-1993 07:37:52 */
/* Filename ........... fiufile.cpp */
/* Project ............. File Utilities */
/* Author ............. Matthew J. W. Ratcliff */
/* Language ........... C++ */
/* Operating System ... DOS/Windows */
/* Processor .......... FIU - File Utilities */
/* Function: Support code for file related operations. */
/* */
/*\t*******************************************************************/
/*\r********************************************************************
** Revision History
***********************************************************************/
/*
Date By Change Description
dd-mmm-yy nnn text
--------- ---- -----------------------------------------------
04-JUN-93 MJWR Build file support functions.
30-JUN-92 MJWR Resolve differences between Borland & MSC compilers
14-Sep-93 MJWR Add FIUfopen, FIUfclose, and FIUnumFilesOpened.
This will allow us to check if any files are
left open before exiting a program - debugging
tool.
21-Oct-93 MJWR Allow user to get to file date, size, and
time with one procedure call. Date and
Time are returned as INT32 values, not decoded.
This format is useful for checking if a file
has changed since the last time you looked at
it.
25-Mar-94 MJWR Add BOOLEAN FIUisFloppyFile( const char *pasFileName );
06-Jun-94 MJWR Add FIUlocateFile - find a file in the current
working directory, or in the default path directory,
or in any of the search path's subdirectories. If
it's out there, I'll find it!
07-Jul-94 MJWR Return BOOLEAN not int for "FIUxxx" test functions.
Also modify fiuFindFirst to accept a parameter
allowing it to search for any file type, not just
file names. Used by FIUpathExists function.
**\r*/
/*\i********************************************************************
** Module Include Files
***********************************************************************/
/*********************** System Include Files *************************/
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <direct.h>
#ifdef _MSC_VER
#pragma message("Compiling for Microsoft C")
#else
#include <dir.h>
#pragma message("Compiling for Borland C")
#endif
/********************** Constant Include Files ************************/
#include "liidSys.h"
#include "liuStrg.h"
#include "erapdefs.h"
#include "figfile.h"
#include "fipfile.h"
/***************** External Variable Include Files ********************/
/***************** External Procedure Include Files *******************/
#include "eraprpt.h"
#include "fiufile.h"
/*\i*/
/*\m********************************************************************
** Module Declarations
***********************************************************************/
/************************* Module Constants ***************************/
/************************* Module Variables ***************************/
static FIG_DOS_FBLK_TY fimFblk;
static int fimFilesOpened = 0;
/************************* Module Procedures **************************/
// in FIUFIND.CPP
/*\m*/
/*\p********************************************************************
** **
** **
NAME: FIUcreateUniqueFile
PURPOSE:
A unique file name is created with 1 - 5
character prefix defined in 'pas1_5CharPrefix'.
If null or zero length, the prefix is set to
'$'. If 'pasAltPathOrNull' is NULL, no
alternate path is given for a temporary file.
If not null, and the system variables TMP and
TEMP are NOT defined, then the temporary file
is created at 'pasAltPathOrNull'. If all else
fails the temporary file is created in the
current working directory. Pass in the desired
wrige mode string in 'pasWriteMode'. It should
be the same as the last parameter passed to the
normal 'fopen' function. The new unique file
name, with full drive and path specifiers, is
copied to the string 'refNewUniqueName' of
'pasCbNameLen' bytesz or less. The opened file
handle is returned. If we fail to open the file
a NULL is returned. The alternate path should
NOT be a network drive. This function is
suitable for DOS or Windows, because Windows is
non-preemptive multi-tasking, and we don't
relinquish control to Windows until after the
file name is created and the file opened.
If the entire file name cannot fit in the destination
string, a NULL is returned and no attempt is made to
create the file. (You must get the correct path and
name to the file or you can't delete it - so we won't
create it on such an error condition.)
** **
** **
** INTERFACE DEFINITION: **
** variable def. expected/description **
** ------------ ----- ------------------------------------- **
** filFilePtr FNC FILE *, open file handle or NULL on err**
**\p*******************************************************************/
FILE *FIUcreateUniqueFile( const char *pas1_5CharPrefix,
const char *pasAltPathOrNull,
const char *pasWriteMode, // "wb" or "w" for example
int pasCbNameLen,
char *refNewUniqueName )
{ /* FIUcreateUniqueFile procedure */
/******************* Local Constant Declarations **********************/
/* Proc name for error log */
CHARPTR FIL_PROC_NAME = "FIUcreateUniqueFile";
#define FIL_NOT_FOUND -1
#define FIL_MAX_TRIES 1000
/******************* Local Variable Declarations **********************/
FILE *filFilePtr;
char *filPrefix;
char *filPath;
char *filFilePath;
int filInx;
BOOLEAN filExists;
/************************* Procedure Body *****************************/
filPrefix = new char[FIP_MAX_PREFIX_LEN+4];
filPath = new char[FIP_MAX_NAME_LEN];
filFilePath = new char[2*FIP_MAX_NAME_LEN];
filFilePtr = NULL;
if ((pasCbNameLen < FIP_MIN_NAME_LEN) ||
(refNewUniqueName == NULL) ||
(pasWriteMode == NULL))
{
ERAparameterError( FIL_PROC_NAME );
}
else
{
*refNewUniqueName = '\0';
*filPrefix = '\0';
if (pas1_5CharPrefix)
{
LIUstrncpy(filPrefix, (char *)pas1_5CharPrefix, FIP_MAX_PREFIX_LEN);
}
if (!filPrefix[0])
{
strcpy(filPrefix, FIP_DEFAULT_PREFIX );
}
strcpy(filPath,"C:\\TMP");
if (access(filPath,0) == FIL_NOT_FOUND)
{
strcpy(filPath,"C:\\TEMP");
if (access(filPath,0) == FIL_NOT_FOUND)
{
if (pasAltPathOrNull)
{
LIUstrncpy(filPath,pasAltPathOrNull,FIP_MAX_NAME_LEN);
}
else
{
if (mkdir(filPath) == FIL_NOT_FOUND)
{
strcpy(filPath,"C:");
if (access(filPath,0) == FIL_NOT_FOUND)
{
*filPath = '\0'; // forget it!
}
}
}
}
}
LIUstrncpy(filPrefix, pas1_5CharPrefix, FIP_MAX_PREFIX_LEN);
filInx = 0;
do
{
sprintf(filFilePath,"%s\\%s%03d.$$$", filPath, filPrefix, filInx );
filExists = FIUfileExists( filFilePath );
if (filExists) filInx++;
}
while ((filInx < FIL_MAX_TRIES) && (filExists));
if (filExists)
{
ERAerrorLog( FIL_PROC_NAME,
"Failed to make unique file in path '%s' for prefix '%s'",
filPath, filPrefix );
}
else
{
LIUstrncpy( refNewUniqueName, filFilePath, pasCbNameLen );
if ((int)strlen(filFilePath) > pasCbNameLen)
{
ERAerrorLog( FIL_PROC_NAME,
"Destination string %d bytes, too short for '%s'",
pasCbNameLen, filFilePath );
}
else
{
filFilePtr = FIUfopen( refNewUniqueName, pasWriteMode );
if (filFilePtr == NULL)
{
ERAerrorLog(FIL_PROC_NAME,
"Failed to open file '%s' in write mode '%s'",
refNewUniqueName, pasWriteMode );
}
}
}
}
LIMDELA(filPath);
LIMDELA(filFilePath);
LIMDELA(filPrefix);
return(filFilePtr);
} /* FIUcreateUniqueFile end */
/*\p********************************************************************
** **
** **
NAME: FIUsetExtender
PURPOSE: to set the file extender. If extender pointer is
NULL, set file extender to none. Else, delete
existing extension from file and add the one passed in.
** **
** **
** INTERFACE DEFINITION: **
** variable def. expected/description **
** ------------ ----- ------------------------------------- **
** filErr FNC (SUCCEEDED_ / FAILED_) error return **
**\p*******************************************************************/
STAT_TYPE FIUsetExtender
( const char *pasExtender,
char *refFilePath )
{ /* FIUsetExtender procedure */
/******************* Local Constant Declarations **********************/
/* Proc name for error log */
CHARPTR FIL_PROC_NAME = "FIUsetExtender";
#define FIL_EXT_LEN 3
/******************* Local Variable Declarations **********************/
/* error return flag */
STAT_TYPE filErr;
char *filExtPtr;
/************************* Procedure Body *****************************/
filErr = FAILED_;
if (refFilePath == NULL)
{
ERAerrorLog( FIL_PROC_NAME,
"Invalid parameter");
}
else
{
/****************************************
* Find the last 'dot' in the file
* name. For example, the path could
* be: A:\PATH.EXT\FILE.EXT
*
* That is, directory names can have extenders,
* but we want to modify the extension of
* the file only.
*/
filExtPtr = LIUstrchrLast(refFilePath, '.');
filErr = SUCCEEDED_;
if (filExtPtr)
{
*filExtPtr = '\0';
}
else
{
filExtPtr = refFilePath;
filExtPtr += strlen(refFilePath);
}
if (pasExtender)
{
if (*pasExtender == '.')
{
pasExtender++; // User gave us ".EXT", not "EXT"
}
*filExtPtr = '.';
filExtPtr++;
strncpy(filExtPtr, pasExtender, FIL_EXT_LEN );
filExtPtr += FIL_EXT_LEN;
*filExtPtr = '\0';
}
}
return(filErr);
} /* FIUsetExtender end */
/*\p********************************************************************
** **
NAME: FIUextractFname
PURPOSE: to extract file name only from full path/file specification.
The file will contain filename.ext only.
** Return Val type/expected/description **
** ------------ -------------------------------------------- **
** filErr STAT_TYPE, SUCCEEDED_ or FAILED_ **
**\p*******************************************************************/
STAT_TYPE FIUextractFname( const char *pasFilePath,
int pasDestLen,
char *refFileOnly )
{
/*********** Local Constant & Variable Declarations *******************/
STAT_TYPE filErr = SUCCEEDED_; /* Iniz ret val */
char *filDs;
/************************* Procedure Body *****************************/
if ((pasFilePath == NULL) ||
(pasDestLen <= 0) ||
(refFileOnly == NULL))
{
filErr = FAILED_;
}
else
{
filDs = LIUstrchrLast( pasFilePath, FIP_PATH_SEP );
if (filDs)
{
filDs++;
}
else
{
filDs = strchr(pasFilePath, FIP_DRIVE_SEP );
if (filDs) filDs++;
}
if (!filDs)
{
filDs = (char *)pasFilePath;
}
LIUstrncpy( refFileOnly, (char *)filDs, pasDestLen );
}
return(filErr);
} /* FIUextractFname end */
/*\p********************************************************************
** **
NAME: FIUextractExt
PURPOSE: to extract file name extension from the filename.
** Return Val type/expected/description **
** ------------ -------------------------------------------- **
** filErr STAT_TYPE, SUCCEEDED_ or FAILED_ **
**\p*******************************************************************/
STAT_TYPE FIUextractExt ( const char *pasFilePath,
int pasDestLen,
char *refFileExt,
BOOLEAN pasMakeWcSpec )
{
/*********** Local Constant & Variable Declarations *******************/
STAT_TYPE filErr = SUCCEEDED_; /* Iniz ret val */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -