⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfile.cpp

📁 BCB下的高效开发包。包含大并发的网络库
💻 CPP
字号:
#include "QFile.h"
#include "../QSystem/QSystem.h"
#include "../QStdlib/QStdlib.h"
#include "Windows.h"
#include <vector>
#include <stdio.h>
//---------------------------------------------------------------------------
unsigned int GetFileLength(const std::string& FileName)
{
	WIN32_FIND_DATA Wfd;
        HANDLE Handle=FindFirstFile(FileName.c_str(),&Wfd);
        if(INVALID_HANDLE_VALUE==Handle)
                return 0xFFFFFFFF;
        unsigned long Ret=Wfd.nFileSizeLow;
        FindClose(Handle);
        if(Wfd.nFileSizeHigh > 0)
                -1;//文件过大, 必须使用扩展版本
        return Ret;
}
//---------------------------------------------------------------------------
__int64 QGetFileLengthEx(const std::string& File)
{
        WIN32_FIND_DATA df;

        HANDLE h = ::FindFirstFile(File.c_str(), &df);
        if (INVALID_HANDLE_VALUE == h)
                return -1;
        ::FindClose(h);
        return ((__int64)df.nFileSizeHigh << 32) + df.nFileSizeLow;
}
//---------------------------------------------------------------------------
bool IsFileExist(const std::string& FileName)
{
        WIN32_FIND_DATA df;
        HANDLE h = ::FindFirstFile(FileName.c_str(), &df);
        if (INVALID_HANDLE_VALUE == h)
                return false;
        ::FindClose(h);

        if (0 != (df.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        return false;
    return true;
}
//---------------------------------------------------------------------------
bool IsDirExist(const std::string& Dir)
{
    WIN32_FIND_DATA df;
    HANDLE h = ::FindFirstFile(Dir.c_str(), &df);
    if (INVALID_HANDLE_VALUE == h)
        return false;
    ::FindClose(h);
    return (0!=(df.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
}
//---------------------------------------------------------------------------
bool CreateTreeDir(const std::string& Path)
{
    std::vector<std::string> dirs = StrSeg(Path,"\\");
    if(dirs.size()<1)
        return false;
    std::string rootup;
    for(unsigned long i=0; i< dirs.size() - 1; ++i) {
        rootup += dirs[i];
        CreateDirectory(rootup.c_str(), 0);
        rootup += "\\";
    }
    rootup += dirs[dirs.size() - 1];
    return CreateDirectory(rootup.c_str(), 0) != 0;
}
//---------------------------------------------------------------------------
std::string QReadFile(const std::string& FileName,unsigned int MaxLen)
{
	DWORD dwRead;
        HANDLE hFile=CreateFile(FileName.c_str() ,GENERIC_READ, FILE_SHARE_READ
            ,0 ,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        if(INVALID_HANDLE_VALUE==hFile)
                return "";
        unsigned int LenToGet = GetFileLength(FileName);
        if(0xFFFFFFFF == LenToGet)
                return "";
        if(MaxLen>0)
                LenToGet = std::min<unsigned int>(MaxLen, LenToGet);
        std::string s(LenToGet, '\0');
        int Ret = ReadFile(hFile, &s[0], s.length(), &dwRead, NULL);
                CloseHandle(hFile);
        if(Ret) {
                s.resize(std::min(dwRead, (DWORD)s.size()));
                return s;
        }else{
                return "";
        }
}
//---------------------------------------------------------------------------
std::string QReadFile(const std::string& FileName)
{
        return QReadFile(FileName, 0xFFFFFFFF);
}
//---------------------------------------------------------------------------
bool QWriteFile(const std::string& FileName,const std::string& BinData,bool Append)
{
	SetFileAttributes(FileName.c_str(),FILE_ATTRIBUTE_NORMAL);
	if(!Append)
  		DeleteFile(FileName.c_str());
  	HANDLE hFile=CreateFile(FileName.c_str(),GENERIC_WRITE,0,0,OPEN_ALWAYS
    	,FILE_ATTRIBUTE_NORMAL,0);
 	if(hFile==INVALID_HANDLE_VALUE)
		return(false);
        if(Append)
                SetFilePointer(hFile,0,0,FILE_END);
	DWORD dwWrite;
	int Ret = WriteFile(hFile,BinData.data(),BinData.size(),&dwWrite,NULL);
	CloseHandle(hFile);
	return Ret && dwWrite == BinData.size();//返回正确且写入字节相等
}
//---------------------------------------------------------------------------
bool QDeleteFile(const std::string& File)
{
	SetFileAttributes(File.c_str(),FILE_ATTRIBUTE_NORMAL);
        return FALSE != ::DeleteFile(File.c_str());
}
//------------------------------------------------------------------------------
bool WriteLog(const std::string &Log, const std::string &FileName)
{
	static CriticalLock LogLock;//临界区
	std::string f = FileName;
	if(f=="")
                f = GetAppPath() + "Log.Log";
	LogLock.Lock();
	bool Ret = QWriteFile(f,QDateTime().DateTimeString()+"\t"+Log+"\r\n",true);
        LogLock.Free();
	return Ret;
}
//------------------------------------------------------------------------------
bool WriteLog(const std::string &Log)
{
        return WriteLog(Log, "");
}
//------------------------------------------------------------------------------
//没有经过严格审核的程序
std::string GetFileCompany(const std::string& FileName)
{
        try{
                std::vector<char> Company(256, '\0');
                int VersionLen= ::GetFileVersionInfoSize(const_cast<char*>(FileName.c_str()),NULL);//API自身不完善的const
                if(VersionLen <= 0)
                        return "-";
                std::vector<char> Buffer(VersionLen);
                if(Buffer.empty())
                        return "-";
                if (0==::GetFileVersionInfo(const_cast<char*>(FileName.c_str()), 0, VersionLen,&Buffer[0]))
                        return "-";
                // 为了方便访问。
                struct LANGANDCODEPAGE {
                        unsigned short wLanguage;
                        unsigned short wCodePage;
                } *lpTranslate;
                // Read the list of languages and code pages.
                UINT cbTranslate;
                if(0==::VerQueryValue(&Buffer[0],"\\VarFileInfo\\Translation",(LPVOID*)&lpTranslate,&cbTranslate))
                        return "-";
                UINT Bytes;
                char* lpBuffer = NULL;
                std::vector<char> SubBlock(Buffer.size());
                _snprintf(&SubBlock[0],SubBlock.size(),"\\StringFileInfo\\%04x%04x\\CompanyName",lpTranslate[0].wLanguage,lpTranslate[0].wCodePage);
                if(::VerQueryValue(&Buffer[0],&SubBlock[0],(LPVOID*)&lpBuffer,&Bytes))
                Company = std::vector<char>(lpBuffer, lpBuffer + Bytes);

                Company[Company.size()-1] = '\0';
                return std::string(&Company[0]);
        }catch(...){
                return "-";
        }
}
//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -