📄 fiufile.cpp
字号:
STAT_TYPE filErr;
FILE *filSFptr;
FILE *filDFptr;
CHAR *filMinBuffer;
CHAR *filBuffer;
unsigned filBufferLen;
unsigned filFread;
unsigned filFwrite;
/************************* Procedure Body *****************************/
/* Initialize return val */
filMinBuffer = new CHAR[FIL_MIN_BUF];
filErr = SUCCEEDED_;
filSFptr = FIUfopen( pasSourceFile, "rb");
if (filSFptr == NULL)
{
filErr = FAILED_;
ERAerrorReport( ER_WARNING, FIL_PROC_NAME,
"Failed to open source file %s to copy",
pasSourceFile );
}
else
{
filDFptr = FIUfopen( pasDestFile, pasWriteMode );
if (filDFptr == NULL)
{
filErr = FAILED_;
ERAerrorReport( ER_WARNING, FIL_PROC_NAME,
"Failed to open file %s for destination copy",
pasDestFile );
}
else
{
filBufferLen = FIL_DEF_BUF;
filBuffer = new CHAR[filBufferLen]; // equivalent to 'malloc'
if (filBuffer == NULL)
{ // Couldn't allocate buffer, so use default local buffer
filBuffer = filMinBuffer;
filBufferLen = FIL_MIN_BUF;
}
do
{
filFread = fread( filBuffer, sizeof(CHAR), filBufferLen, filSFptr );
if (filFread)
{
filFwrite = fwrite( filBuffer, sizeof(CHAR), filFread, filDFptr );
if (filFwrite != filFread)
{
ERAerrorReport( ER_WARNING, FIL_PROC_NAME,
"Only wrote %u of %u bytes to disk, out of disk space?",
filFwrite, filFread );
filFread = 0;
filErr = FAILED_;
}
}
}
while (filFread == filBufferLen);
FIUfclose( &filSFptr );
FIUfclose( &filDFptr );
if (filBuffer != filMinBuffer)
{ // then it was created with 'new', we must delete it
LIMDELA(filBuffer);
}
}
}
LIMDELA(filMinBuffer);
return(filErr);
} /* fiuCopy end */
/*\p********************************************************************
** **
** **
NAME: FIUcopyFile
PURPOSE: to copy from the specified source file name
(including full path) to the destination file
(also including full path in its name).
** **
** **
** INTERFACE DEFINITION: **
** variable def. expected/description **
** ------------ ----- ------------------------------------- **
** filErr FNC (SUCCEEDED_ / FAILED_) error return **
**\p*******************************************************************/
STAT_TYPE FIUcopyFile
( const char *pasSourceFile,
const char *pasDestFile )
{ /* FIUcopyFile procedure */
/************************* Procedure Body *****************************/
return(fiuCopy(pasSourceFile,pasDestFile,"wb"));
} /* FIUcopyFile end */
/*\p********************************************************************
** **
** **
NAME: FIUappendFile
PURPOSE: to copy from the specified source file name
(including full path) to the end of destination file
(also including full path in its name).
** **
** **
** INTERFACE DEFINITION: **
** variable def. expected/description **
** ------------ ----- ------------------------------------- **
** filErr FNC (SUCCEEDED_ / FAILED_) error return **
**\p*******************************************************************/
STAT_TYPE FIUappendFile
( const char *pasSourceFile,
const char *pasDestFile )
{ /* FIUappendFile procedure */
/******************* Local Constant Declarations **********************/
return(fiuCopy(pasSourceFile,pasDestFile,"ab"));
} /* FIUappendFile end */
/*\p********************************************************************
** **
** **
NAME: FIUfileInfo
PURPOSE: to return the size, date, and time of last modification.
Use "findfirst" function. This gives a structure
including file size info. This allows us to
find the size of a file without opening it (the
information comes from the File Allocation Table).
Thus, you can check size on a file that you may
not have permission to open.
** **
** **
** INTERFACE DEFINITION: **
** variable def. expected/description **
** ------------ ----- ------------------------------------- **
** filFound FNC STAT_TYPE, SUCCEEDED_ if file was found**
**\p*******************************************************************/
STAT_TYPE FIUfileInfo( const char *pasFilePath,
INT32 *refSize,
INT32 *refRevDate,
INT32 *refRevTime )
{ /* FIUfileInfo procedure */
/******************* Local Constant Declarations **********************/
/* Proc name for error log */
CHARPTR FIL_PROC_NAME = "FIUfileInfo";
#define FIL_FOUND_FILE 0
/******************* Local Variable Declarations **********************/
STAT_TYPE filFound;
/************************* Procedure Body *****************************/
filFound = fiuFindFirst( pasFilePath, &fimFblk );
if (filFound == FAILED_)
{
filFound = FAILED_;
ERAerrorLog( FIL_PROC_NAME,
"File '%s' not found to check file size on",
pasFilePath );
}
else
{
if (refSize) *refSize = (INT32)fimFblk.fsize;
if (refRevDate) *refRevDate = (INT32)fimFblk.fdate;
if (refRevTime) *refRevTime = (INT32)fimFblk.ftime;
}
return(filFound);
} /* FIUfileInfo end */
/*\p********************************************************************
** **
NAME: FIUlocateNewestFile
PURPOSE: to locate the most recent incarnation of a file
that fits the file spec. template.
** Return Val type/expected/description **
** ------------ -------------------------------------------- **
** filErr STAT_TYPE, SUCCEEDED_ or FAILED_ **
**\p*******************************************************************/
STAT_TYPE FIUlocateNewestFile( const char *pasFileSpec,
int pasStrLen,
char *refFilePath )
{
/*********** Local Constant & Variable Declarations *******************/
STAT_TYPE filErr = SUCCEEDED_; /* Iniz ret val */
STAT_TYPE filFound;
char *filFullPath;
char *filPathOnly;
INT32 filDate;
INT32 filTime;
INT32 filSize;
/************************* Procedure Body *****************************/
if ((pasFileSpec == NULL) ||
(pasStrLen <= FIP_MIN_NAME_LEN) ||
(refFilePath == NULL))
{
ERAparameterError( "FIUlocateNewestFile" );
filErr = FAILED_;
}
else
{
filFullPath = new char[FIP_MAX_NAME_LEN+1];
filPathOnly = new char[FIP_MAX_NAME_LEN+1];
filFound = fiuFindFirst( pasFileSpec, &fimFblk );
if (filFound == FAILED_)
{
filErr = FAILED_;
*refFilePath = '\0';
}
else
{
filSize = (INT32)fimFblk.fsize;
filDate = (INT32)fimFblk.fdate;
filTime = (INT32)fimFblk.ftime;
FIUextractPath( pasFileSpec, FIP_MAX_NAME_LEN, filPathOnly );
FIUconstructFilePath( filPathOnly,
fimFblk.name,
FIP_MAX_NAME_LEN,
filFullPath );
while (filFound == SUCCEEDED_)
{
filFound = fiuFindNext( &fimFblk );
if (filFound == SUCCEEDED_)
{
// Is this file newer than the one we
// already have saved? Or equal date, but
// larger?
if (((INT32)fimFblk.fdate > filDate) ||
(((INT32)fimFblk.fdate == filDate) && ((INT32)fimFblk.ftime > filTime)) ||
(((INT32)fimFblk.fdate == filDate) && ((INT32)fimFblk.ftime == filTime) &&
((INT32)fimFblk.fsize > filSize)))
{
filSize = (INT32)fimFblk.fsize;
filDate = (INT32)fimFblk.fdate;
filTime = (INT32)fimFblk.ftime;
FIUconstructFilePath( filPathOnly,
fimFblk.name,
FIP_MAX_NAME_LEN,
filFullPath );
}
}
}
LIUstrncpy( refFilePath, filFullPath, pasStrLen );
LIMDELA(filFullPath);
LIMDELA(filPathOnly);
}
}
return(filErr);
} /* FIUlocateNewestFile end */
/*\p********************************************************************
** **
NAME: FIUrenameFile
PURPOSE: to rename a file
** Return Val type/expected/description **
** ------------ -------------------------------------------- **
** filErr STAT_TYPE, SUCCEEDED_ or FAILED_ **
**\p*******************************************************************/
STAT_TYPE FIUrenameFile
( const char *pasOldFname,
const char *pasNewFname )
{
/*********** Local Constant & Variable Declarations *******************/
//CHARPTR FIL_PROC_NAME = "FIUrenameFile";
STAT_TYPE filErr;
int filRez;
/************************* Procedure Body *****************************/
filErr = FAILED_;
if ((pasOldFname != NULL) && (pasNewFname != NULL))
{
if ((!FIUfileExists(pasNewFname)) && (FIUfileExists(pasOldFname)))
{
filRez = rename(pasOldFname,pasNewFname);
if (filRez != -1)
{
filErr = SUCCEEDED_;
}
}
}
return(filErr);
} /* FIUrenameFile end */
/*\p********************************************************************
** **
NAME: FIUsetFileDate
PURPOSE: to set the date stamp for the specified file to
the month, day, year indicated
Note this routine does VERY LITTLE VALIDITY CHECKING ON THE DATE.
If you want to make certain it's a valid date, use the
functions in TIUdt class first. (Didn't want to put TIUdt
class here and create an interdependancy between FIU and TIU
code.)
** Return Val type/expected/description **
** ------------ -------------------------------------------- **
** filErr STAT_TYPE, SUCCEEDED_ or FAILED_ **
**\p*******************************************************************/
static STAT_TYPE fiuSetFileDT( const char *pasFileName,
int pasMMorHH,
int pasDDorMM,
int pasYYorSS,
BOOLEAN pasTsetDate )
{
/*********** Local Constant & Variable Declarations *******************/
#define FLL_OPENOK 0
STAT_TYPE filErr;
int fHandle;
unsigned fStat;
unsigned fDate;
unsigned fTime;
/************************* Procedure Body *****************************/
fStat = _dos_open( pasFileName, O_RDONLY, &fHandle );
if (fStat == FLL_OPENOK)
{
filErr = SUCCEEDED_;
_dos_getftime( fHandle, &fDate, &fTime );
if (pasTsetDate)
{
/****************************************
* Massage year into proper form
*/
if ((pasYYorSS >= 1980) && (pasYYorSS <= 2099))
{
pasYYorSS -= 1980; // magic number from IBM OS
}
else
{
if ((pasYYorSS >= 80) && (pasYYorSS <= 99))
{
pasYYorSS -= 80;
}
else
{
if ((pasYYorSS >= 0) && (pasYYorSS <= 79))
{
pasYYorSS += 20;
}
}
}
if ((pasYYorSS <= 0) || (pasYYorSS > 128)) // only 7 bits for year
{
pasYYorSS = 0;
}
if ((pasMMorHH <= 0) || (pasMMorHH > 12))
{
pasMMorHH = 1;
}
if (pasDDorMM > 31) pasDDorMM = 1;
fDate = (pasYYorSS << 9) + (pasMMorHH << 5) + pasDDorMM;
}
else
{
if (pasMMorHH > 24) pasMMorHH = 0;
if (pasDDorMM > 60) pasDDorMM = 0;
if (pasYYorSS > 60) pasYYorSS = 0;
fTime = (pasMMorHH << 11) + (pasDDorMM << 5) + (pasYYorSS >> 1);
}
if (_dos_setftime( fHandle, fDate, fTime ) == 0)
{
filErr = SUCCEEDED_;
}
else
{
filErr = FAILED_;
}
_dos_close( fHandle );
}
else
{
filErr = FAILED_;
}
return(filErr);
} /* fiuSetFileDT end */
STAT_TYPE FIUsetFileDate( const char *pasFileName,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -