bool SendFile(const CString& sRemoteNick, const CString& sFileName) { CString sFullPath = CDir::ChangeDir(GetSavePath(), sFileName, CZNC::Get().GetHomePath()); CDCCSock* pSock = new CDCCSock(this, sRemoteNick, sFullPath); CFile* pFile = pSock->OpenFile(false); if (!pFile) { delete pSock; return false; } CString sLocalDCCIP = GetUser()->GetLocalDCCIP(); unsigned short uPort = CZNC::Get().GetManager().ListenRand( "DCC::LISTEN::" + sRemoteNick, sLocalDCCIP, false, SOMAXCONN, pSock, 120); if (GetUser()->GetNick().Equals(sRemoteNick)) { PutUser(":*[email protected] PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(sLocalDCCIP)) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } else { PutIRC("PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(sLocalDCCIP)) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } PutModule(t_f("Attempting to send [{1}] to [{2}].")( pFile->GetShortName(), sRemoteNick)); return true; }
void CDirWatcher::ReadIgnoreList() { CFile f; if(f.Open(CNewzflow::Instance()->settings->GetAppDataDir() + _T("watcher.dat"), GENERIC_READ, 0, OPEN_EXISTING)) { size_t size = (size_t)f.GetSize(); void* buffer = new char [size]; f.Read(buffer, size); f.Close(); CMemFile mf(buffer, size); if(mf.Read<unsigned>() != 'NFW!') { return; } unsigned version = mf.Read<unsigned>(); if(version != 1) { return; } unsigned count = mf.Read<unsigned>(); while(count != 0) { filesToIgnore.Add(mf.ReadString()); count--; } } }
CFile* CDCCSock::OpenFile(bool bWrite) { if ((m_pFile) || (m_sLocalFile.empty())) { m_pModule->PutModule(((bWrite) ? "DCC <- [" : "DCC -> [") + m_sRemoteNick + "][" + m_sLocalFile + "] - Unable to open file."); return NULL; } m_pFile = new CFile(m_sLocalFile); if (bWrite) { if (m_pFile->Exists()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - File already exists [" + m_sLocalFile + "]"); return NULL; } if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]"); return NULL; } } else { if (!m_pFile->IsReg()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Not a file [" + m_sLocalFile + "]"); return NULL; } if (!m_pFile->Open()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]"); return NULL; } // The DCC specs only allow file transfers with files smaller // than 4GiB (see ReadData()). unsigned long long uFileSize = m_pFile->GetSize(); if (uFileSize > (unsigned long long) 0xffffffff) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - File too large (>4 GiB) [" + m_sLocalFile + "]"); return NULL; } m_uFileSize = (unsigned long) uFileSize; } m_sFileName = m_pFile->GetShortName(); return m_pFile; }
void CHTTPSock::WriteFileGzipped(CFile& File) { char szBufIn[8192]; char szBufOut[8192]; off_t iFileSize = File.GetSize(), iFileReadTotal = 0; z_stream zStrm; int zFlush = Z_NO_FLUSH; int zStatus; if (!InitZlibStream(&zStrm, szBufIn)) { DEBUG("- Error initializing zlib!"); return; } do { ssize_t iFileRead = 0; if (zStrm.avail_in == 0) { // input buffer is empty, try to read more data from file. // if there is no more data, finish the stream. if (iFileReadTotal < iFileSize) { iFileRead = File.Read(szBufIn, sizeof(szBufIn)); if (iFileRead < 1) { // wtf happened? better quit compressing. iFileReadTotal = iFileSize; zFlush = Z_FINISH; } else { iFileReadTotal += iFileRead; zStrm.next_in = (Bytef*)szBufIn; zStrm.avail_in = iFileRead; } } else { zFlush = Z_FINISH; } } zStrm.next_out = (Bytef*)szBufOut; zStrm.avail_out = sizeof(szBufOut); zStatus = deflate(&zStrm, zFlush); if ((zStatus == Z_OK || zStatus == Z_STREAM_END) && zStrm.avail_out < sizeof(szBufOut)) { // there's data in the buffer: Write(szBufOut, sizeof(szBufOut) - zStrm.avail_out); } } while (zStatus == Z_OK); deflateEnd(&zStrm); }
bool CFile::IsEmpty_( const char *pcFileName ) { if( Exist_( pcFileName ) ) { CFile oFile; if( oFile.Open( pcFileName, FLAG_READ ) ) { const bool bIsEmpty( !oFile.GetSize() ); oFile.Close(); return bIsEmpty; } return false; } return true; }
bool CUser::SendFile(const CString& sRemoteNick, const CString& sFileName, const CString& sModuleName) { CString sFullPath = CDir::ChangeDir(GetDLPath(), sFileName, CZNC::Get().GetHomePath()); CDCCSock* pSock = new CDCCSock(this, sRemoteNick, sFullPath, sModuleName); CFile* pFile = pSock->OpenFile(false); if (!pFile) { delete pSock; return false; } unsigned short uPort = CZNC::Get().GetManager().ListenRand("DCC::LISTEN::" + sRemoteNick, GetLocalDCCIP(), false, SOMAXCONN, pSock, 120); if (GetNick().Equals(sRemoteNick)) { PutUser(":" + GetStatusPrefix() + "[email protected] PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(GetLocalDCCIP())) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } else { PutIRC("PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(GetLocalDCCIP())) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } PutModule(sModuleName, "DCC -> [" + sRemoteNick + "][" + pFile->GetShortName() + "] - Attempting Send."); return true; }
void CHTTPSock::WriteFileUncompressed(CFile& File) { char szBuf[4096]; off_t iLen = 0; ssize_t i = 0; off_t iSize = File.GetSize(); // while we haven't reached iSize and read() succeeds... while (iLen < iSize && (i = File.Read(szBuf, sizeof(szBuf))) > 0) { Write(szBuf, i); iLen += i; } if (i < 0) { DEBUG("- Error while reading file: " << strerror(errno)); } }
bool CDX11SourceTextureData2D::CreateD3DData(ID3D11Device* pkDevice) { D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; D3D11_TEXTURE2D_DESC desc; CFileSystem* pFileSystem = CCoreEngine::Get()->GetFileSystem(); CFile* pFile = pFileSystem->OpenFile(m_kCreateParams.m_sFileName, CFile::OM_Read); if (pFile == 0) return false; size_t stSize = pFile->GetSize(); char* acData = IntAlloc(char, stSize); if (pFile->ReadData(acData, stSize) != stSize) { IntFree(acData); pFileSystem->CloseFile(pFile); return false; } // Load the texture and initialize an ID3D11Texture2D object. D3DX11CreateTextureFromMemory( pkDevice, acData, stSize, NULL, NULL, (ID3D11Resource**) &m_d3dTexture2D, NULL ); // free the temporary read data and close the file again IntFree(acData); pFileSystem->CloseFile(pFile); // Get a texture description to determine the texture // format of the loaded texture. m_d3dTexture2D->GetDesc( &desc ); // Fill in the D3D11_SHADER_RESOURCE_VIEW_DESC structure. srvDesc.Format = desc.Format; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = desc.MipLevels; // Create the shader resource view. pkDevice->CreateShaderResourceView(m_d3dTexture2D, &srvDesc, &m_d3dShaderResourceView ); return true; }
CFile* CDCCSock::OpenFile(bool bWrite) { if ((m_pFile) || (m_sLocalFile.empty())) { if (m_bSend) { m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Unable to open file.")( m_sFileName, m_sRemoteNick)); } else { m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: Unable to open file.")( m_sFileName, m_sRemoteNick)); } return nullptr; } m_pFile = new CFile(m_sLocalFile); if (bWrite) { if (m_pFile->Exists()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: File already exists.")( m_sFileName, m_sRemoteNick)); return nullptr; } if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: Could not open file.")( m_sFileName, m_sRemoteNick)); return nullptr; } } else { if (!m_pFile->IsReg()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Not a file.")( m_sFileName, m_sRemoteNick)); return nullptr; } if (!m_pFile->Open()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Could not open file.")( m_sFileName, m_sRemoteNick)); return nullptr; } // The DCC specs only allow file transfers with files smaller // than 4GiB (see ReadData()). unsigned long long uFileSize = m_pFile->GetSize(); if (uFileSize > (unsigned long long)0xffffffffULL) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: File too large (>4 GiB).")( m_sFileName, m_sRemoteNick)); return nullptr; } m_uFileSize = uFileSize; } m_sFileName = m_pFile->GetShortName(); return m_pFile; }
int main3(int argc, char* argv[]) { //( 1 ) (( 3 ) 2 )(( 5 )4)( 6 ) //(\w+)://((\w+\.)*\w+)((/\w*)*)(/\w+\.\w+)? //^协议://网址(x.x...x)/路径(n个\字串)/网页文件(xxx.xxx) //\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。 /W与之相反 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]' //+ 匹配前面的子表达式一次或多次。( ) 标记一个子表达式的开始和结束位置 * 匹配前面的子表达式零次或多次 //? 匹配前面的子表达式零次或一次(等价于 {0,1}),或指明一个非贪婪限定符。 什么是非贪婪限定符 ? 。 // (\w+)://((\w+\.)*\w+)((/\w*)*)(/\w+\.\w+)? //const char *szReg = "(\\w+)://((\\w+\\.)*\\w+)((/\\w*)*)(/\\w+\\.\\w+)?"; //const char *szStr = "http://www.cppprog.com/2009/0112/48.html"; LPSTR pBuffer = NULL; //HTML文件读取 { char szFilename[MAX_PATH] = {"C:\\Lookup\\1.html"}; CFile f; if( f.Open(szFilename)==TRUE ) { FILETIME ftModified; f.GetFileTime(NULL, NULL, &ftModified); DWORD dwSize = f.GetSize(); pBuffer = (LPSTR)new CHAR[dwSize + 1]; f.Read(pBuffer, dwSize); pBuffer[dwSize] = '\0'; f.Close(); } } const char *szReg = "target=\"_blank\"\\>(.+?)\\</a\\>\\</td\\>"; //try { //字符串匹配 boost::regex reg( szReg ); bool r=boost::regex_match( pBuffer , reg); //boost::regex_error err; //cout<< err.what() <<endl; //assert(r); } //catch(boost::regex_error &err) //{ // cout<< err.what() <<endl; // cin.get(); // return 0; //} //catch(...) //{ // cout<<"error"<<endl; // cin.get(); // return 0; //} { //提取子串 boost::cmatch mat; boost::regex reg( szReg ); bool r=boost::regex_match( pBuffer, mat, reg); if(r) //如果匹配成功 { //显示所有子串 for(boost::cmatch::iterator itr=mat.begin(); itr!=mat.end(); ++itr) { // 指向子串对应首位置 指向子串对应尾位置 子串内容 cout << itr->first-pBuffer << ' ' << itr->second-pBuffer << ' ' << *itr << endl; } } //也可直接取指定位置信息 if(mat[4].matched) cout << "Path is" << mat[4] << endl; } delete [] pBuffer; cin.get(); return 0; }