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

📄 warfile.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarFile.h"   // class implemented#ifndef WAR_LOG_H#   include "WarLog.h"#endif#ifndef WAR_FILE_DRIVER_FILE_H#   include "WarFileDriverFile.h"#endif#ifndef WAR_PERFMON_DEF_H#   include "WarPerfmonDef.h"#endif#ifndef WAR_ASCII_MAPPER_H#   include "WarAsciiMapper.h"#endif/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarFile::WarFile(): mpFile(NULL){    WAR_DB_PERFMON_INC(WAR_PRFDEBUG_NUM_FILES);}WarFile::~WarFile(){    try    {        if (IsOpen())            Close();    }    catch(WarException)    {    }        if (mpFile)        delete mpFile;        WAR_DB_PERFMON_DEC(WAR_PRFDEBUG_NUM_FILES);}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================WarFile::warfile_ref_t WarFile::Create(const war_syspath_t& filePath)    throw(WarException){    war_syspath_t my_path;    my_path << "file:///" << filePath.GetPath();    return CreateForURL(WarUrl(my_path));}WarFile::warfile_ref_t WarFile::CreateForURL(const WarUrl& fileUrl)    throw(WarException){    warfile_ref_t file_ref(new WarFile);    file_ref->CreateDriverForUrl(fileUrl);    WarLog debug_log(WARLOG_FILES, "WarFile::CreateForURL()");    if (debug_log)    {        debug_log << "Created file object for URL: '"            << fileUrl.GetUrl()            << "'"            << war_endl;    }    return file_ref;}void WarFile::Open(war_uint32_t openFlags)    throw(WarException){    WarLog debug_log(WARLOG_FILES, "WarFile::Open()");    if (debug_log)    {        debug_log << "Attempting to open '"            << mUrl.GetUrl()            << "' with flags "            << ExplainFags(openFlags)            << war_endl;    }    if (mDriver.IsEmpty())        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);    if (openFlags & F_TEXT)        mpFile = new WarAsciiMapper(mpFile);    mpFile->Open(mUrl, openFlags);}void WarFile::Close() throw(WarException){    WarLog debug_log(WARLOG_FILES, "WarFile::Close()");    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (debug_log)    {        debug_log << "Closing '"            << mUrl            << "' having flags "            << ExplainFags(GetFlags())            << war_endl;    }    mpFile->Close();    // Win32 will delete tmp files itself _if_ it was created    // by the same handel. So, we try to delete it anyway...    try    {        if ((GetFlags() & (F_TEMPORARY | F_WRITE))            == (F_TEMPORARY | F_WRITE))            mpFile->GetDriver().WarDeleteFile(mUrl);    }    catch (WarException& ex)    {        if (debug_log)        {            debug_log << "Failed to delete F_TEMPORARY file: \""                << mUrl                << "\". "                << ex.Explain()                << war_endl;        }    }}war_uint32_t WarFile::Read(war_cptr_t Buf, war_uint32_t Bytes)throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!CanRead())        WarThrow(WarError(WAR_ERR_NOT_OPEN_FOR_READ), NULL);    SyncBufferedIo();    return mpFile->Read(Buf, Bytes);}void WarFile::Write(war_ccptr_t Buf, war_uint32_t Bytes)throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!CanWrite())        WarThrow(WarError(WAR_ERR_NOT_OPEN_FOR_WRITE), NULL);    mpFile->Write(Buf, Bytes);}void WarFile::WriteWithCallback(war_transfer_buffer_ptr_t& outBuffer)        throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!CanWrite())        WarThrow(WarError(WAR_ERR_NOT_OPEN_FOR_WRITE), NULL);    mpFile->WriteWithCallback(outBuffer);}void WarFile::ReadWithCallback(war_transfer_buffer_ptr_t& inBuffer)        throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!CanRead())        WarThrow(WarError(WAR_ERR_NOT_OPEN_FOR_READ), NULL);    SyncBufferedIo();    mpFile->ReadWithCallback(inBuffer);}void WarFile::Flush() throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!CanWrite())        WarThrow(WarError(WAR_ERR_NOT_OPEN_FOR_WRITE), NULL);    mpFile->Flush();}war_flen_t WarFile::Seek(war_flen_t fileOffset,                          SeekModes seekMode)                         throw(WarException){    if (!mpFile || !IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    SyncBufferedIo();    return mpFile->Seek(fileOffset, seekMode);}war_filehandle_t WarFile::GetHandle() throw(WarException){    return mpFile->GetHandle();}//============================= ACCESS     ===================================bool WarFile::ReadLine(std::string& Str) throw(WarException){    war_cstr_t p = NULL;    war_ccstr_t pp = NULL;        if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);        if (!CanRead())        WarThrow(WarError(WAR_FERR_NOT_OPEN_FOR_READ), NULL);        while(true)    {        // Look for a newline        p = mBuffer.GetStartOfBuffer();        pp = mBuffer.GetEndOfBuffer();        while(p < pp)        {            if ((*p == '\r') && (p[1] == '\n'))            {                *p++ = 0;                goto done;                            }            else if (*p == '\n')                goto done;                        p++;        }                // Need more data        mBuffer.Reserve();        war_uint32_t BytesRead = mpFile->Read(mBuffer.GetEndOfBuffer(),            mBuffer.FreeCapacity());        mBuffer.PushData(NULL, BytesRead);                if (!BytesRead)        {            if (mBuffer.IsEmpty())                return false;                        break;        }    }    done:    if (p >= pp)    {        // Special case. End of physical buffer with no newline to zero...        mBuffer.Expand(1);        Str = mBuffer.GetStartOfBuffer();        mBuffer.Reset();    }    else    {        *p++ = 0;        war_ccstr_t StartOfBuffer = mBuffer.GetStartOfBuffer();        size_t BytesUsed = p - StartOfBuffer;        Str = mBuffer.PopData(BytesUsed);        mBuffer.Purge();    }        return true;}void WarFile::AddFrom(const char * from){    Write((war_ccptr_t)from, strlen(from));}void WarFile::AddFrom(const std::string& from){    Write((war_ccptr_t)from.c_str(), from.size());}void WarFile::AddFrom(const std::wstring& from){    Write((war_ccptr_t)from.c_str(), from.size() * sizeof(wchar_t));}void WarFile::AddFrom(const int from){    std::string buffer;    AddFrom(WarItoa(buffer, from));}void WarFile::AddFrom(const unsigned int from){    std::string buffer;    AddFrom(WarItoa(buffer, from));}//============================= INQUIRY    ===================================std::string WarFile::ExplainFags(war_uint32_t fileFlags) const{    if (mDriver.IsEmpty())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return mDriver->WarExplainFags(fileFlags);}bool WarFile::IsOpen() throw(WarException){    if (mDriver.IsEmpty() || !mpFile)        return false;    return mpFile->IsOpen();}bool WarFile::CanWrite() throw(WarException){    if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return (GetFlags() & F_WRITE) == F_WRITE;}bool WarFile::CanRead() throw(WarException){    if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return (GetFlags() & F_READ) == F_READ;}bool WarFile::IsEof() throw(WarException){   if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return mBuffer.IsEmpty() && mpFile->IsEof();}war_flen_t WarFile::GetPosition() throw(WarException){    if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return mpFile->GetPosition();}war_flen_t WarFile::GetLength() throw(WarException){    if (!IsOpen())        WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL);    return mpFile->GetLength();}const war_uint32_t WarFile::GetFlags() const         throw(WarException){    if (!mpFile)        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);    return mpFile->GetFlags();}bool WarFile::CanDoCallback() const{    if (!mpFile)        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);    return mpFile->CanDoCallback();}/////////////////////////////// PROTECTED  ///////////////////////////////////// Switch between buffered and unbufferedvoid WarFile::SyncBufferedIo(){    if (mBuffer.IsEmpty())        return;    // Binary seek back to offset    mpFile->Seek(mBuffer.GetLength() * -1, WarFile::WAR_SEEK_CURRENT);    mBuffer.Reset();}void WarFile::OnRead(const WarError& status,                      war_transfer_buffer_ptr_t& buffer){   }void WarFile::OnWritten(const WarError& status,                         war_transfer_buffer_ptr_t& buffer){}void WarFile::CreateDriverForUrl(const WarUrl& fileUrl)    throw(WarException){     WarFileDriver::driver_ptr_t file_driver =          WarFileEngine::GetEngine().GetDriver(fileUrl.GetDriverName().c_str());    mDriver = file_driver;    mpFile = file_driver->WarCreateNewFileObject(fileUrl);    mUrl = fileUrl;    mpFile->mpCompanion = this;}/////////////////////////////// PRIVATE    ///////////////////////////////////

⌨️ 快捷键说明

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