filesys.cpp
来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 2,090 行 · 第 1/5 页
CPP
2,090 行
//------------------------------------------------------------------------------
// $Workfile: FileSys.cpp $
// $Header: /DevNet/SbjCore/SbjCore/Sys/FileSys.cpp 4 4/19/07 3:04p Steve $
//
// Stingray Software Extension Classes
// Copyright (C) 1995 Stingray Software Inc.
// All Rights Reserved
//
// This source code is only intended as a supplement to the
// Stingray Extension Class product.
// See the SEC help files for detailed information
// regarding using SEC classes.
//
//
// $Revision: 4 $
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "FileSys.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#pragma warning (disable:4244) // conversion from 'ULONGLONG'
inline static int compareStringAsc(const void *arg1, const void *arg2)
{
CString str1, str2;
str1 = (*(*(CString **)arg1));
str2 = (*(*(CString **)arg2));
return str1.Compare(str2);
}
inline static int compareStringNoCaseAsc(const void *arg1, const void *arg2)
{
CString str1, str2;
str1 = (*(*(CString **)arg1));
str2 = (*(*(CString **)arg2));
return str1.CompareNoCase(str2);
}
namespace SbjCore
{
namespace Sys
{
//***************************************************************************
//
// Name: FileSys
//
// Purpose: Constructor.
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Constructs a FileSys object.
//@xref <c FileSys>
FileSys::FileSys()
{
// Initialize instance variables
m_nMaxFileNameLength = 1024;
} // FileSys
//***************************************************************************
//
// Name: ~FileSys
//
// Purpose: Destructor.
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc FileSys destructor.
//@xref <c FileSys>
FileSys::~FileSys()
{
// No code.
} // ~FileSys
//***************************************************************************
//
// Name: GetFileStatus
//
// Purpose: To return the status information about the specified file.
// See the MFC help on the CFileStatus struct for more information.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// CFileStatus Status;
// BOOL bRetVal = fs.GetFileStatus("c:\\test.txt", Status);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the file status.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm CFileStatus& | FileStatus | The status information is returned in
// this struct.
//@comm To return the status information about the specified file. See the
// MFC help on the CFileStatus struct for more information.
//@xref <c FileSys> <mf FileSys::GetFileAccessTime>
// <mf FileSys::GetFileModifyTime> <mf FileSys::GetFileCreateTime>
// <mf FileSys::GetFileSize>
//@ex |
//CFileStatus Status;
//BOOL bRetVal = fs.GetFileStatus("c:\\test.txt", Status);
BOOL FileSys::GetFileStatus(const CString& FileName, CFileStatus& FileStatus)
{
return CFile::GetStatus(FileName, FileStatus);
} // GetFileStatus
//***************************************************************************
//
// Name: GetFileCreateTime
//
// Purpose: To return the creation time of the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// CTime CreateTime;
// BOOL bRetVal = fs.GetFileCreateTime("c:\\test.txt", CreateTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was created.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm CTime& | time | The time that the file was created.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileModifyTime> <mf FileSys::GetFileAccessTime>
// <mf FileSys::GetFileSize>
//@ex |
//CTime CreateTime;
//BOOL bRetVal = fs.GetFileCreateTime("c:\\test.txt", CreateTime);
BOOL FileSys::GetFileCreateTime(const CString& FileName, CTime& time)
{
CFileStatus FileStatus;
if (CFile::GetStatus(FileName, FileStatus))
{
time = FileStatus.m_ctime;
return TRUE;
} // if
return FALSE;
} // GetFileCreateTime
//***************************************************************************
//
// Name: GetFileCreateTime
//
// Purpose: To return the creation time of the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// COleDateTime CreateTime;
// BOOL bRetVal = fs.GetFileCreateTime("c:\\test.txt", CreateTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was created.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm COleDateTime& | time | The time that the file was created.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileModifyTime> <mf FileSys::GetFileAccessTime>
// <mf FileSys::GetFileSize>
//@ex |
//COleDateTime CreateTime;
//BOOL bRetVal = fs.GetFileCreateTime("c:\\test.txt", CreateTime);
BOOL FileSys::GetFileCreateTime(const CString& FileName, COleDateTime& time)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(FileName, &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
VERIFY(FindClose(hFind));
time = findFileData.ftCreationTime;
return TRUE;
}
//***************************************************************************
//
// Name: GetFileAccessTime
//
// Purpose: To return the time of last access for the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// COleDateTime AccessTime;
// BOOL bRetVal = fs.GetFileAccessTime("c:\\test.txt", AccessTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was last accessed.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm COleDateTime& | time | The time that the file was last access.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileCreateTime> <mf FileSys::GetFileModifyTime>
// <mf FileSys::GetFileSize>
//@ex |
//COleDateTime AccessTime;
//BOOL bRetVal = fs.GetFileAccessTime("c:\\test.txt", AccessTime);
BOOL FileSys::GetFileAccessTime(const CString& FileName, COleDateTime& time)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(FileName, &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
VERIFY(FindClose(hFind));
time = findFileData.ftLastAccessTime;
return TRUE;
}
//***************************************************************************
//
// Name: GetFileModifyTime
//
// Purpose: To return the last modification time of the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// COleDateTime ModifyTime;
// BOOL bRetVal = fs.GetFileModifyTime("c:\\test.txt", ModifyTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was last modified.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm COleDateTime& | time | The time that the file was modified.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileCreateTime> <mf FileSys::GetFileAccessTime>
// <mf FileSys::GetFileSize>
//@ex |
//COleDateTime ModifyTime;
//BOOL bRetVal = fs.GetFileModifyTime("c:\\test.txt", ModifyTime);
BOOL FileSys::GetFileModifyTime(const CString& FileName, COleDateTime& time)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(FileName, &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
VERIFY(FindClose(hFind));
time = findFileData.ftLastWriteTime;
return TRUE;
}
//***************************************************************************
//
// Name: GetFileModifyTime
//
// Purpose: To return the last modification time of the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// CTime ModifyTime;
// BOOL bRetVal = fs.GetFileModifyTime("c:\\test.txt", ModifyTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was last modified.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm CTime& | time | The time that the file was modified.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileCreateTime> <mf FileSys::GetFileAccessTime>
// <mf FileSys::GetFileSize>
//@ex |
//CTime ModifyTime;
//BOOL bRetVal = fs.GetFileModifyTime("c:\\test.txt", ModifyTime);
BOOL FileSys::GetFileModifyTime(const CString& FileName, CTime& time)
{
CFileStatus FileStatus;
if (CFile::GetStatus(FileName, FileStatus))
{
time = FileStatus.m_mtime;
return TRUE;
} // if
return FALSE;
} // GetFileModifyTime
//***************************************************************************
//
// Name: GetFileAccessTime
//
// Purpose: To return the time of last access for the specified file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// CTime AccessTime;
// BOOL bRetVal = fs.GetFileAccessTime("c:\\test.txt", AccessTime);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the time and date the file was last accessed.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file.
//@parm CTime& | time | The time that the file was last access.
//@xref <c FileSys> <mf FileSys::GetFileStatus>
// <mf FileSys::GetFileCreateTime> <mf FileSys::GetFileModifyTime>
// <mf FileSys::GetFileSize>
//@ex |
//CTime AccessTime;
//BOOL bRetVal = fs.GetFileAccessTime("c:\\test.txt", AccessTime);
BOOL FileSys::GetFileAccessTime(const CString& FileName, CTime& time)
{
CFileStatus FileStatus;
if (CFile::GetStatus(FileName, FileStatus))
{
time = FileStatus.m_atime;
return TRUE;
} // if
return FALSE;
} // GetFileAccessTime
//***************************************************************************
//
// Name: GetFileSize
//
// Purpose: To return the size of the specified file in bytes.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// unsigned long lSize = 0;
// BOOL bRetVal = fs.GetFileSize("c:\\test.txt", lSize);
//
// Notes: None.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the size of the file in bytes.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | The name of the file
//@parm unsigned long& | lSize | If no error, the size of the file in bytes.
//@xref <c FileSys> <mf FileSys::GetDirectorySize>
//@xref <mf FileSys::GetFileStatus>
//@comm To return the size (in bytes) of the specified file.
//@ex |
//unsigned long lSize;
//BOOL bRetVal = fs.GetFileSize("c:\\foo\\bar\\what.txt", lSize);
BOOL FileSys::GetFileSize(const CString& FileName, unsigned long& lSize)
{
CFileStatus FileStatus;
if (CFile::GetStatus(FileName, FileStatus))
{
lSize = FileStatus.m_size;
return TRUE;
} // if
return FALSE;
} // GetFileSize
//***************************************************************************
//
// Name: GetFileAttribute
//
// Purpose: To return the attribute bits associated with the file.
//
// Ret Val: TRUE: If no error.
// FALSE: If error (ie. file does not exist).
//
// Example:
// BOOL bRetVal = fs.GetFileAttribute("c:\\test.txt", bAttr);
//
// Notes: See the Attribute enum in filesys.h for specific attributes.
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the attribute bits for the file.
//@rdesc Nonzero if successful; 0 if an error occurred.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?