1 Star 0 Fork 5

mSaLee/ZHLT_UPack

forked from ccc1/ZHLT_UPack 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
iostatic.h 16.05 KB
一键复制 编辑 原始数据 按行查看 历史
ccc1 提交于 2016-08-19 17:59 . .
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
#ifndef __IO_STATIC_H__
#define __IO_STATIC_H__
/*!
Author : dfhu
data: 2002-7
des: the baic io function wrap
*/
#include <string>
#include <vector>
#include <sys/stat.h>
#include <sys/types.h>
#include "iostatic_func.h"
#ifdef WIN32
#include <windows.h>
#include <direct.h>
#include "winver.h"
#include <tchar.h>
#include <io.h>
#define mkdir(a,b) mkdir(a)
#define S_IRUSR 0
#define S_IWUSR 1
#define S_IXUSR 2
#define S_IRGRP 4
#define S_IWGRP 8
#define S_IXGRP 16
#define S_IROTH 32
#define S_IWOTH 64
#define S_IXOTH 128
#endif
#pragma warning (disable : 4267 4018)
namespace IO_OP
{
inline bool Exist(const char *name)
{
struct stat buf;
if(stat(name,&buf)==0)
return true;
else
return false;
}
inline bool Exist(const std::string &name)
{
return Exist(name.c_str());
}
inline bool Rename( const char *oldName, const char *newName )
{
return rename(oldName, newName ) == 0;
}
namespace File
{
static inline bool IsFile( const char *name )
{
struct stat buf;
if(stat(name,&buf)==0)
if(S_IFREG&buf.st_mode)
return true;
return false;
}
static inline bool Delete(const char *name)
{
#ifdef WIN32
return DeleteFile(name)!=0;
#else
unlink(name);
#endif
}
static inline int GetFileLength( const char *name )
{
struct stat buf;
if(stat(name,&buf)==0)
return buf.st_size;
else
return 0;
}
static inline bool Delete(const std::string &name)
{
return Delete(name.c_str());
}
static inline bool DeleteAlways( const char *name )
{
_chmod( name, _S_IREAD | _S_IWRITE );
return Delete(name);
}
static inline
bool CopyF( const char *src, const char *dst, bool bFailIfExist )
{
#ifdef _WIN32
return (CopyFile( src, dst, bFailIfExist)!=0);
#else
return false;
#endif
}
};
namespace Path
{
static inline bool IsDir(const char *name)
{
struct stat buf;
if(stat(name,&buf)==0)
if(S_IFDIR&buf.st_mode)
return true;
return false;
}
static inline bool IsDir(const std::string &name)
{
return IsDir(name.c_str());
}
static inline void Remove(const char *name)
{
#ifdef WIN32
RemoveDirectory(name);
#else
unlink(name);
#endif
}
static inline void Remove(const std::string &sDir)
{
Remove(sDir.c_str());
}
static inline bool MakeDir(const char *sDir,int priv = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)
{
return mkdir(sDir,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)==0;
}
static inline bool MakeDir(const std::string &sDir,int priv = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)
{
return mkdir(sDir.c_str(),S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH)==0;
}
/* static bool MakeTree(string &sDir)
{
if(Exist(sDir.c_str()))
return true;
else
{
string sParentDir = sDir;
for(;;)
{
Parent(sParentDir);
if(!Valid(sParentDir))
break;
if(MakeTree(sParentDir))
break;
}
if(Valid(sParentDir))
return MakeDir(sDir);
else
return false;
}
}*/
static inline bool MakeTree(const std::string &sDir)
{
if(Exist(sDir.c_str()))
return true;
else
{
std::string sParentDir = sDir;
// for(;;)
{
if(!Valid( sDir ))
return false;
if( MakeDir( sDir ) )
return true;
Parent(sParentDir);
if(MakeTree(sParentDir))
return MakeDir(sDir);
else
return false;
}
}
}
static inline void AllSub(const std::string &sDir,std::vector<std::string> &sFileNameArray, bool bFullPathNameReturn=true)
{
#ifdef WIN32
std::string sFileName=sDir+"/*.*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName=t;
sFileNameArray.push_back(sFileName);
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
if( bFullPathNameReturn )
sFileName=sDir+'/'+name->d_name;
else
sFileName = name->d_name;
sFileNameArray.push_back(sFileName);
free(name);
}
free(namelist);
}
#endif
}
static inline void AllSubNoExtJudge(const std::string &sDir,std::vector<std::string> &sFileNameArray, bool bFullPathNameReturn)
{
AllSub( sDir, sFileNameArray, bFullPathNameReturn );
}
static inline void AllSubWithSearchSubDir( const char *sSearchDir, const char *sExt, std::vector<std::string> &sFileNameArray )
{
#ifdef WIN32
std::string sDir = sSearchDir;
NoEndDir( sDir );
std::string sFileName=sDir+"/*";//+sExt;
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
std::vector< std::string > sDirArray;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
char *t=FindFileData.cFileName;
if( (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) )
{
if( (t[0] == '.' && t[1] == 0)
|| (t[0] == '.' && t[1] == '.' && t[2] == 0) )
{
}
else
{
sFileName=sDir+'/'+t;
sDirArray.push_back( sFileName );
}
}
else
{
bool bExt = false;
if( !sExt )
bExt = true;
else
{
const char *ext = IO_OP::File::GetExt( t );
if( ext && (ext+1) )
{
if( stricmp(ext+1,sExt) == 0)
bExt = true;
}
}
if(bExt )
{
sFileName=sDir+'/'+t;
sFileNameArray.push_back(sFileName);
}
}
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
for( int i=0; i<sDirArray.size(); ++i )
AllSubWithSearchSubDir( sDirArray[i].c_str(), sExt, sFileNameArray );
#endif
}
static inline void AllSub(const std::string &sDir, const std::string &sExt, std::vector<std::string> &sFileNameArray, bool bFullPathNameReturn=true)
{
#ifdef WIN32
std::string sFileName=sDir+"/*."+sExt;
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName = t;
sFileNameArray.push_back(sFileName);
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
if( strstr(name->d_name, sExt.c_str()) == NULL )
{
if( bFullPathNameReturn )
sFileName=sDir+'/'+name->d_name;
else
sFileName = name->d_name;
sFileNameArray.push_back(sFileName);
}
free(name);
}
free(namelist);
}
#endif
}
static inline void AllSubWithExtJudge(const std::string &sDir,const std::string &sExt,std::vector<std::string> &sFileNameArray, bool bFullPathNameReturn)
{
AllSub(sDir, sExt, sFileNameArray, bFullPathNameReturn );
}
static inline int GetDirFileNumber( const std::string &sDir )
{
#ifdef WIN32
std::string sFileName=sDir+"/*.*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return 0;
int n = 0;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
if( !(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) )
++n;
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
return n;
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
return n;
#endif
}
static inline int GetDirFileNumber( const std::string &sDir, const std::string &sExt )
{
#ifdef WIN32
std::string sFileName=sDir+"/*."+sExt;
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return 0;
int nNum = 0;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
if( !(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) )
++nNum;
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
return nNum;
#else
int nNum = 0;
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
if( strstr(name->d_name, sExt.c_str()) == NULL )
{
nNum++;
}
free(name);
}
free(namelist);
}
return nNum;
#endif
}
static inline void AllSubDir(const std::string &sDir,std::vector<std::string> &sDirNameArray, bool bFullPathNameReturn=true)
{
#ifdef WIN32
std::string sFileName=sDir;
EndDir(sFileName);
sFileName += "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
if( (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) )
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName = t;
sDirNameArray.push_back(sFileName);
}
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
// if( strstr(name->d_name, sExt.c_str()) == NULL )
{
if( bFullPathNameReturn )
sFileName=sDir+'/'+name->d_name;
else
sFileName = name->d_name;
sDirNameArray.push_back(sFileName);
}
free(name);
}
free(namelist);
}
#endif
}
static inline void AllSubFile(const std::string &sDir,std::vector<std::string> &sFileNameArray, bool bFullPathNameReturn=true)
{
#ifdef WIN32
std::string sFileName=sDir;
EndDir(sFileName);
sFileName += "*.*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
if( !(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
&& (!(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DEVICE)) )
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName = t;
sFileNameArray.push_back(sFileName);
}
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
// if( strstr(name->d_name, sExt.c_str()) == NULL )
{
if( bFullPathNameReturn )
sFileName=sDir+'/'+name->d_name;
else
sFileName = name->d_name;
sFileNameArray.push_back(sFileName);
}
free(name);
}
free(namelist);
}
#endif
}
static inline void AllSubDirFile(const std::string &sDir,std::vector<std::string> &sDirNameArray, std::vector<std::string> &sFilenameArray, bool bFullPathNameReturn = true )
{
#ifdef WIN32
std::string sFileName=sDir;
EndDir(sFileName);
sFileName += "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind=FindFirstFile(sFileName.c_str(),&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
return;
BOOL bFind=TRUE;//FindNextFile(hFind,&FindFileData);
while(bFind)
{
if( (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) )
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName = t;
sDirNameArray.push_back(sFileName);
}
else
{
char *t=FindFileData.cFileName;
if( bFullPathNameReturn )
sFileName=sDir+'/'+t;
else
sFileName = t;
sFilenameArray.push_back(sFileName);
}
bFind=FindNextFile(hFind,&FindFileData);
}
FindClose(hFind);
#else
std::string sFileName;
struct dirent * *namelist;
int n=scandir(sDir.c_str(),&namelist,NULL,alphasort);
if(n>0)
{
while(n--)
{
struct dirent *name=namelist[n];
// if( strstr(name->d_name, sExt.c_str()) == NULL )
{
if( bFullPathNameReturn )
sFileName=sDir+'/'+name->d_name;
else
sFileName = name->d_name;
sDirNameArray.push_back(sFileName);
}
free(name);
}
free(namelist);
}
#endif
}
static inline void DelTree(const std::string &sDir)
{
std::vector<std::string> tmpArray;
AllSub(sDir,tmpArray);
std::string name;
_chmod( sDir.c_str(), _S_IREAD | _S_IWRITE );
for(int i = 0 ;i < tmpArray.size();i++)
{
name = tmpArray[i];
const char *pName = IO_OP::Path::GetName(name.c_str());
if( pName[0] == '.' && pName[1] == 0 )
continue;
if( pName[0] == '.' && pName[2] == 0 && pName[1] == '.' )
continue;
if(IsDir(name))
{
DelTree(name);
}
else
{
///
_chmod( name.c_str(), _S_IREAD | _S_IWRITE );
File::Delete(name);
}
}
Remove(sDir);
}
static inline std::string GetCurrentDir()
{
char dirBufL[266];
#if defined(_WIN32)
GetCurrentDirectory(256, dirBufL);
#else
getcwd(dirBufL, 256);
#endif
return std::string(dirBufL);
}
static inline void MakeAbsoluteDir( std::string &sDir )
{
if( sDir.size() > 1 )
{
std::string sSub = sDir.substr(0,2);
if( sSub == "./" || sSub == ".\\" )
{
std::string currentDir = IO_OP::Path::GetCurrentDir();
IO_OP::Path::EndDir( currentDir );
sDir.insert( 0, currentDir );
}
}
if( sDir.size() > 2 )
{
std::string sSub = sDir.substr(0,3);
if( sSub == "../" || sSub == "..\\" )
{
std::string currentDir = IO_OP::Path::GetCurrentDir();
IO_OP::Path::EndDir( currentDir );
sDir.insert( 0, currentDir );
}
}
}
static inline void MakeAbsoluteDir( std::string &sDir, const char *currentDir )
{
if( sDir.size() > 1 )
{
std::string sSub = sDir.substr(0,2);
if( sSub == "./" || sSub == ".\\" )
{
sDir.insert( 0, currentDir );
}
}
if( sDir.size() > 2 )
{
std::string sSub = sDir.substr(0,3);
if( sSub == "../" || sSub == "..\\" )
{
sDir.insert( 0, currentDir );
}
}
}
template< typename TFunctor, typename TParam1, typename TParam2 >
inline
void do_walkdir( const std::string &sDir, TParam1 &p1, TParam2 &p2, TFunctor func)
{
func( sDir, p1, p2 );
std::vector< std::string > dirArray;
std::string sCheckDir = sDir;
IO_OP::Path::NoEndDir( sCheckDir );
IO_OP::Path::AllSubDir( sCheckDir,dirArray, false );
std::string sDirWithEnd = sDir;
IO_OP::Path::EndDir( sDirWithEnd );
for( size_t i=0; i<dirArray.size(); ++i )
{
if( (dirArray[i] == ".")
|| (dirArray[i] == "..") )
{
}
else
{
do_walkdir( sDirWithEnd + dirArray[i], p1, p2,func );
}
}
}
};
};
#pragma warning (default : 4267 4018)
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mSaLee/ZHLT_UPack.git
git@gitee.com:mSaLee/ZHLT_UPack.git
mSaLee
ZHLT_UPack
ZHLT_UPack
master

搜索帮助