Files
EgtGeneral/FileUtils.cpp
T
DarioS 714c4e1fba EgtGeneral 2.3l1 :
- in CopyFileEgt si copia esplicitamente la data di ultima modifica dal file originale.
2021-12-28 08:31:36 +01:00

278 lines
8.3 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2013-2020
//----------------------------------------------------------------------------
// File : FileUtils.cpp Data : 14.02.20 Versione : 2.2b1
// Contenuto : Funzioni di utilità sui file.
//
//
//
// Modifiche : 11.12.13 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "\EgtDev\Include\EGnFileUtils.h"
#include "\EgtDev\Include\EGnStringUtils.h"
#include "\EgtDev\Include\EgtStringConverter.h"
#include <io.h>
using namespace std ;
//----------------------------------------------------------------------------
bool
ExistsFile( const string& sFile)
{
// leggo gli attributi del file
DWORD dwAttr = ::GetFileAttributesW( stringtoW( sFile)) ;
// se non esiste
if ( dwAttr == INVALID_FILE_ATTRIBUTES ||
dwAttr == INVALID_FILE_SIZE)
return false ;
// se è un direttorio
if ( dwAttr & FILE_ATTRIBUTE_DIRECTORY)
return false ;
return true ;
}
//-----------------------------------------------------------------------------
bool
CopyFileEgt( const string& sFile, const string& sNewFile)
{
// copio file
if ( ::CopyFileW( stringtoW( sFile), stringtoW( sNewFile), false) == 0)
return false ;
// recupero data ultima scrittura file sorgente
HANDLE hFile = CreateFileW( stringtoW( sFile), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL) ;
if ( hFile == INVALID_HANDLE_VALUE)
return true ;
FILETIME ftWrite ;
bool bOk = ( GetFileTime( hFile, NULL, NULL, &ftWrite) != FALSE) ;
CloseHandle( hFile) ;
if ( ! bOk)
return true ;
// scrivo data ultima scrittura file destinazione
HANDLE hNewFile = CreateFileW( stringtoW( sNewFile), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL) ;
if ( hNewFile == INVALID_HANDLE_VALUE)
return true ;
bool bNewOk = ( SetFileTime( hNewFile, NULL, NULL, &ftWrite) != FALSE) ;
CloseHandle( hNewFile) ;
return true ;
}
//-----------------------------------------------------------------------------
bool
RenameFile( const string& sFile, const string& sNewFile)
{
return ( ::MoveFileW( stringtoW( sFile), stringtoW( sNewFile)) != 0) ;
}
//-----------------------------------------------------------------------------
bool
EraseFile( const string& sFile)
{
return ( ::DeleteFileW( stringtoW( sFile)) != 0) ;
}
//-----------------------------------------------------------------------------
static bool
GetFileLastWriteTime( const string& sFile, FILETIME& ftWrite)
{
HANDLE hFile = CreateFileW( stringtoW( sFile), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL) ;
if ( hFile == INVALID_HANDLE_VALUE)
return false ;
bool bOk = ( GetFileTime( hFile, NULL, NULL, &ftWrite) != FALSE) ;
CloseHandle( hFile) ;
return bOk ;
}
//-----------------------------------------------------------------------------
bool
CompareFilesLastWriteTime( const string& sFile1, const string& sFile2, int& nRes)
{
FILETIME ftWrite1 ;
if ( ! GetFileLastWriteTime( sFile1, ftWrite1))
return false ;
FILETIME ftWrite2 ;
if ( ! GetFileLastWriteTime( sFile2, ftWrite2))
return false ;
nRes = CompareFileTime( &ftWrite1, &ftWrite2) ;
return true ;
}
//-----------------------------------------------------------------------------
string
GetFileExtension( const string& sFile)
{
// separo l'estensione dal resto
string sFileTitle ;
string sFileExt ;
SplitLast( sFile, ".", sFileTitle, sFileExt) ;
if ( sFileExt.find_first_of( "/\\") != string::npos) {
sFileTitle = sFile ;
sFileExt = "" ;
}
return sFileExt ;
}
//-----------------------------------------------------------------------------
string
ChangeFileExtension( const string& sFile, const string& sNewExt)
{
// separo l'estensione dal resto
string sFileTitle ;
string sFileExt ;
SplitLast( sFile, ".", sFileTitle, sFileExt) ;
if ( sFileExt.find_first_of( "/\\") != string::npos) {
sFileTitle = sFile ;
sFileExt = "" ;
}
// genero il nome con la nuova estensione
if ( sNewExt[0] == '.')
return ( sFileTitle + sNewExt) ;
else
return ( sFileTitle + "." + sNewExt) ;
}
//-----------------------------------------------------------------------------
bool
FileExtensionMatches( const string& sFile, const string& sExt)
{
// separo l'estensione dal resto
string sFileTitle ;
string sFileExt ;
SplitLast( sFile, ".", sFileTitle, sFileExt) ;
if ( sFileExt.find_first_of( "/\\") != string::npos) {
sFileTitle = sFile ;
sFileExt = "" ;
}
// confronto
return ( EqualNoCase( sFileExt, sExt)) ;
}
//-----------------------------------------------------------------------------
bool
FindFirstFileEgt( const string& sFileSpec, string& sFileName)
{
wstring sFileSpecW ;
wstring sFileW ;
_wfinddata_t c_file ;
intptr_t hFile ;
sFileSpecW = stringtoW( sFileSpec) ;
// cerco il primo file che soddisfa le specifiche
if ( ( hFile = _wfindfirst( sFileSpecW.c_str(), &c_file)) != -1L) {
do {
if ( c_file.attrib != _A_SUBDIR) {
sFileName = LPSTR( WtoA( c_file.name)) ;
_findclose( hFile) ;
errno = 0 ;
return true ;
}
} while( _wfindnext( hFile, &c_file) == 0) ;
_findclose( hFile) ;
errno = 0 ;
}
return false ;
}
//----------------------------------------------------------------------------
string
GetFileName( const string& sPath)
{
// cerco l'ultimo separatore
size_t iPos = sPath.find_last_of( "/\\") ;
// se non c'è, è solo il nome di un file
if ( iPos == string::npos)
return sPath ;
// ritorno la parte finale
return sPath.substr( iPos+1) ;
}
//----------------------------------------------------------------------------
string
GetFileTitleEgt( const string& sPath)
{
// cerco l'ultimo separatore
size_t iPos = sPath.find_last_of( "/\\") ;
// recupero il nome
string sName = (( iPos == string::npos) ? sPath : sPath.substr( iPos + 1)) ;
// tolgo l'eventuale estensione
size_t iPos2 = sName.find_last_of( ".") ;
return (( iPos2 == string::npos) ? sName : sName.substr( 0, iPos2)) ;
}
//----------------------------------------------------------------------------
string
GetDirectory( const string& sPath)
{
// cerco l'ultimo separatore
size_t iPos = sPath.find_last_of( "/\\") ;
// se non c'è, è solo il nome di un file
if ( iPos == string::npos)
return "" ;
// ritorno la parte iniziale
return sPath.substr( 0, iPos) ;
}
//----------------------------------------------------------------------------
bool
ExistsDirectory( const string& sDir)
{
// leggo gli attributi del direttorio
DWORD dwAttr = ::GetFileAttributesW( stringtoW( sDir)) ;
// se non esiste
if ( dwAttr == INVALID_FILE_ATTRIBUTES)
return false ;
// se è un direttorio
return ( (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0) ;
}
//-----------------------------------------------------------------------------
bool
CreateDirectoryEgt( const string& sDir)
{
wstring sDirW = stringtoW( sDir) ;
return ( ::CreateDirectoryW( sDirW.c_str(), NULL) != FALSE) ;
}
//-----------------------------------------------------------------------------
bool
EmptyDirectory( const string& sDir)
{
wstring sDirW = stringtoW( sDir) ;
wstring sFileW = sDirW + L"\\*.*" ;
// ciclo su tutti i file del direttorio
_wfinddata_t c_file ;
intptr_t hFile ;
if ( ( hFile = _wfindfirst( sFileW.c_str(), &c_file)) != -1L) {
do {
if ( c_file.attrib != _A_SUBDIR) {
sFileW = sDirW + L"\\" + c_file.name ;
::DeleteFileW( sFileW.c_str()) ;
}
} while( _wfindnext( hFile, &c_file) == 0) ;
_findclose( hFile) ;
errno = 0 ;
}
return true ;
}
//-----------------------------------------------------------------------------
bool
EraseDirectory( const string& sDir)
{
wstring sDirW = stringtoW( sDir) ;
return( ::RemoveDirectoryW( sDirW.c_str()) != FALSE) ;
}