View Code
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include "time.h"
using namespace std;
void UTIL_LogPrint( const char *format, ... )
{
::OutputDebugString(format);
}
void UTIL_DeleteFile( const char *szFile )
{
if( unlink( szFile ) == 0 )
UTIL_LogPrint( "deleted \"%s\"\n", szFile );
else
{
#ifdef WIN32
UTIL_LogPrint( "error deleting \"%s\"\n", szFile );
#else
UTIL_LogPrint( "error deleting \"%s\" - %s\n", szFile, strerror( errno ) );
#endif
}
}
void UTIL_MakeFile( const char *szFile, string strContents )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "wb" ) ) == NULL )
{
UTIL_LogPrint( "warning - unable to open %s for writing\n", szFile );
return;
}
fwrite( (void *)strContents.c_str( ), sizeof( char ), strContents.size( ), pFile );
fclose( pFile );
}
bool UTIL_CheckFile( const char *szFile )
{
// check if file exists
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "r" ) ) == NULL )
return false;
fclose( pFile );
return true;
}
string UTIL_ReadFile( const char *szFile )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
{
UTIL_LogPrint( "warning - unable to open %s for reading\n", szFile );
return string( );
}
UTIL_LogPrint( "open %s for reading\n", szFile );
fseek( pFile, 0, SEEK_END );
unsigned long ulFileSize = ftell( pFile );
fseek( pFile, 0, SEEK_SET );
char *pData = (char *)malloc( sizeof( char ) * ulFileSize );
memset( pData, 0, sizeof( char ) * ulFileSize );
fread( (void *)pData, sizeof( char ), ulFileSize, pFile );
fclose( pFile );
string strFile( pData, ulFileSize );
free( pData );
return strFile;
}
void UTIL_MoveFile( const char *szFile, const char *szDest )
{
if( UTIL_CheckFile( szDest ) )
UTIL_LogPrint( "error archiving \"%s\" - destination file already exists\n", szDest );
else
UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );
// thanks MrMister
UTIL_DeleteFile( szFile );
}
void UTIL_CopyFile( const char *szFile, const char *szDest )
{
if( UTIL_CheckFile( szDest ) )
UTIL_LogPrint( "error archiving \"%s\" - destination file already exists\n", szDest );
else
UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );
}
string UTIL_GetLocalTimeString()//edited 不保证跨平台性
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char year[5];
char mon[3];
char day[3];
char hour[3];
char min[3];
char sec[3];
strftime(year,5,"%Y",timeinfo);
strftime(mon,3,"%m",timeinfo);
strftime(day,3,"%d",timeinfo);
strftime(hour,3,"%H",timeinfo);
strftime(min,3,"%M",timeinfo);
strftime(sec,3,"%S",timeinfo);
string timestr;
timestr.append(year);
timestr.append(mon);
timestr.append(day);
timestr.append(hour);
timestr.append(min);
timestr.append(sec);
return timestr;
}
void CreateDir(const char *DirPath)//edited 不保证跨平台性
{
CreateDirectory(DirPath,NULL);
}
#include <string.h>
#include <iostream>
#include "time.h"
using namespace std;
void UTIL_LogPrint( const char *format, ... )
{
::OutputDebugString(format);
}
void UTIL_DeleteFile( const char *szFile )
{
if( unlink( szFile ) == 0 )
UTIL_LogPrint( "deleted \"%s\"\n", szFile );
else
{
#ifdef WIN32
UTIL_LogPrint( "error deleting \"%s\"\n", szFile );
#else
UTIL_LogPrint( "error deleting \"%s\" - %s\n", szFile, strerror( errno ) );
#endif
}
}
void UTIL_MakeFile( const char *szFile, string strContents )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "wb" ) ) == NULL )
{
UTIL_LogPrint( "warning - unable to open %s for writing\n", szFile );
return;
}
fwrite( (void *)strContents.c_str( ), sizeof( char ), strContents.size( ), pFile );
fclose( pFile );
}
bool UTIL_CheckFile( const char *szFile )
{
// check if file exists
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "r" ) ) == NULL )
return false;
fclose( pFile );
return true;
}
string UTIL_ReadFile( const char *szFile )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
{
UTIL_LogPrint( "warning - unable to open %s for reading\n", szFile );
return string( );
}
UTIL_LogPrint( "open %s for reading\n", szFile );
fseek( pFile, 0, SEEK_END );
unsigned long ulFileSize = ftell( pFile );
fseek( pFile, 0, SEEK_SET );
char *pData = (char *)malloc( sizeof( char ) * ulFileSize );
memset( pData, 0, sizeof( char ) * ulFileSize );
fread( (void *)pData, sizeof( char ), ulFileSize, pFile );
fclose( pFile );
string strFile( pData, ulFileSize );
free( pData );
return strFile;
}
void UTIL_MoveFile( const char *szFile, const char *szDest )
{
if( UTIL_CheckFile( szDest ) )
UTIL_LogPrint( "error archiving \"%s\" - destination file already exists\n", szDest );
else
UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );
// thanks MrMister
UTIL_DeleteFile( szFile );
}
void UTIL_CopyFile( const char *szFile, const char *szDest )
{
if( UTIL_CheckFile( szDest ) )
UTIL_LogPrint( "error archiving \"%s\" - destination file already exists\n", szDest );
else
UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );
}
string UTIL_GetLocalTimeString()//edited 不保证跨平台性
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char year[5];
char mon[3];
char day[3];
char hour[3];
char min[3];
char sec[3];
strftime(year,5,"%Y",timeinfo);
strftime(mon,3,"%m",timeinfo);
strftime(day,3,"%d",timeinfo);
strftime(hour,3,"%H",timeinfo);
strftime(min,3,"%M",timeinfo);
strftime(sec,3,"%S",timeinfo);
string timestr;
timestr.append(year);
timestr.append(mon);
timestr.append(day);
timestr.append(hour);
timestr.append(min);
timestr.append(sec);
return timestr;
}
void CreateDir(const char *DirPath)//edited 不保证跨平台性
{
CreateDirectory(DirPath,NULL);
}