本文整理汇总了C++中CAutoFile类的典型用法代码示例。如果您正苦于以下问题:C++ CAutoFile类的具体用法?C++ CAutoFile怎么用?C++ CAutoFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CAutoFile类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: fopen
bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE *file = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!filein)
return error("CAddrman::Read() : open failed");
// use file size to size memory buffer
int fileSize = boost::filesystem::file_size(pathAddr);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if ( dataSize < 0 ) dataSize = 0;
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (std::exception &e) {
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
}
filein.fclose();
CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if (hashIn != hashTmp)
return error("CAddrman::Read() : checksum mismatch; data corrupted");
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (pchMessageStart magic number) and
ssPeers >> FLATDATA(pchMsgTmp);
// verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return error("CAddrman::Read() : invalid network magic number");
// de-serialize address data into one CAddrMan object
ssPeers >> addr;
}
catch (std::exception &e) {
return error("CAddrman::Read() : I/O error or stream data corrupted");
}
return true;
}
示例2: fopen
bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE *file = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if(!filein)
return(error("CAddrDB::Read() : fopen(%s) failed", pathAddr.string().c_str()));
// use file size to size memory buffer
int fileSize = GetFilesize(filein);
int dataSize = fileSize - sizeof(uint256);
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch(std::exception &e) {
return(error("CAddrDB::Read() : I/O error"));
}
filein.fclose();
CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if(hashIn != hashTmp)
return(error("CAddrDB::Read() : checksum mismatch"));
// de-serialize address data
unsigned char pchMsgTmp[4];
try {
ssPeers >> FLATDATA(pchMsgTmp);
ssPeers >> addr;
}
catch(std::exception &e) {
return(error("CAddrDB::Read() #2 : I/O error"));
}
// finally, verify the network matches ours
if(memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return(error("CAddrDB::Read() : invalid network magic number"));
return(true);
}
示例3: RAND_bytes
bool CAddrDB::Write(const CAddrMan& addr)
{
// Generate random temporary filename
unsigned short randv = 0;
RAND_bytes((unsigned char *)&randv, sizeof(randv));
std::string tmpfn = strprintf("peers.dat.%04x", randv);
// serialize addresses, checksum data up to that point, then append csum
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
//header fix mincoin
unsigned char pchMessageStart[4];
GetMessageStart(pchMessageStart,false);
ssPeers << FLATDATA(pchMessageStart);
ssPeers << addr;
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash;
// open temp output file, and associate with CAutoFile
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CAddrman::Write() : open failed");
// Write and commit header, data
try {
fileout << ssPeers;
}
catch (std::exception &e) {
return error("CAddrman::Write() : I/O error");
}
FileCommit(fileout);
fileout.fclose();
// replace existing peers.dat, if any, with new peers.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("CAddrman::Write() : Rename-into-place failed");
return true;
}
示例4: InternalEnsurePipeOpen
bool CRemoteCacheLink::InternalEnsurePipeOpen ( CAutoFile& hPipe
, const CString& pipeName) const
{
if (hPipe)
return true;
int tryleft = 2;
while (!hPipe && tryleft--)
{
hPipe = CreateFile(
pipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
FILE_FLAG_OVERLAPPED, // default attributes
NULL); // no template file
if ((!hPipe) && (GetLastError() == ERROR_PIPE_BUSY))
{
// TGitCache is running but is busy connecting a different client.
// Do not give up immediately but wait for a few milliseconds until
// the server has created the next pipe instance
if (!WaitNamedPipe (pipeName, 50))
{
continue;
}
}
}
if (hPipe)
{
// The pipe connected; change to message-read mode.
DWORD dwMode;
dwMode = PIPE_READMODE_MESSAGE;
if(!SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL)) // don't set maximum time
{
CTraceToOutputDebugString::Instance()(__FUNCTION__ ": SetNamedPipeHandleState failed");
hPipe.CloseHandle();
}
}
return hPipe;
}
示例5: dumpbootstrap
Value dumpbootstrap(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 2)
throw runtime_error(
"dumpbootstrap \"destination\" \"blocks\"\n"
"\nCreates a bootstrap format block dump of the blockchain in destination, which can be a directory or a path with filename, up to the given block number.");
string strDest = params[0].get_str();
int nBlocks = params[1].get_int();
if (nBlocks < 0 || nBlocks > nBestHeight)
throw runtime_error("Block number out of range.");
boost::filesystem::path pathDest(strDest);
if (boost::filesystem::is_directory(pathDest))
pathDest /= "bootstrap.dat";
try {
FILE* file = fopen(pathDest.string().c_str(), "wb");
if (!file)
throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not open bootstrap file for writing.");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout)
throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not open bootstrap file for writing.");
for (int nHeight = 0; nHeight <= nBlocks; nHeight++)
{
CBlock block;
CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
block.ReadFromDisk(pblockindex, true);
fileout << FLATDATA(Params().MessageStart()) << fileout.GetSerializeSize(block) << block;
}
} catch(const boost::filesystem::filesystem_error &e) {
throw JSONRPCError(RPC_MISC_ERROR, "Error: Bootstrap dump failed!");
}
return Value::null;
}
示例6: RAND_bytes
bool CAddrDB::Write(const CAddrMan& addr)
{
// Generate random temporary filename Генерация случайного временного файла
unsigned short randv = 0;
RAND_bytes((unsigned char *)&randv, sizeof(randv));
std::string tmpfn = strprintf("peers.dat.%04x", randv);
// serialize addresses, checksum data up to that point, then append csum сериализации адреса, контрольная сумма данных до того момента, затем добавить CSUM
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
ssPeers << FLATDATA(Params().MessageStart());
ssPeers << addr;
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash;
// open temp output file, and associate with CAutoFile открытыть временный выходной файл и ассоциировать с CAutoFile
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CAddrman::Write() : open failed");
// Write and commit header, data запись и фиксация заголовка, данных
try {
fileout << ssPeers;
}
catch (std::exception &e) {
return error("CAddrman::Write() : I/O error");
}
FileCommit(fileout);
fileout.fclose();
// replace existing peers.dat, if any, with new peers.dat.XXXX заменить существующие peers.dat, если таковые имеются, новыми peers.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("CAddrman::Write() : Rename-into-place failed");
return true;
}
示例7: CAutoFile
return true;
}
uint256 CLotData::GetClottoFileHash() {
CAutoFile fileout = CAutoFile(OpenFile(0, true), SER_DISK, CLIENT_VERSION);
if (!fileout)
return error("CLotDB::GetClottoFileHash : open failed");
fseek(fileout, 0, SEEK_END); //move the point to the end of file
long filesize = ftell(fileout); //get the file size
if (filesize < 0)
return error("CLotDB::GetClottoFileHash : ftell failed");
rewind(fileout); //move the point to the start of file
vector<unsigned char> vchData;
vchData.resize(filesize);
try {
fileout.read((char *) &vchData[0], filesize);
} catch (std::exception &e) {
return error("CLotDB::GetClottoFileHash : read failed");
}
fileout.fclose();
示例8: while
void CDirectoryWatcher::WorkerThread()
{
DWORD numBytes;
CDirWatchInfo * pdi = NULL;
LPOVERLAPPED lpOverlapped;
WCHAR buf[READ_DIR_CHANGE_BUFFER_SIZE] = {0};
WCHAR * pFound = NULL;
while (m_bRunning)
{
CleanupWatchInfo();
if (watchedPaths.GetCount())
{
// Any incoming notifications?
pdi = NULL;
numBytes = 0;
InterlockedExchange(&m_bCleaned, FALSE);
if ((!m_hCompPort)
|| !GetQueuedCompletionStatus(m_hCompPort,
&numBytes,
(PULONG_PTR) &pdi,
&lpOverlapped,
600000 /*10 minutes*/))
{
// No. Still trying?
if (!m_bRunning)
return;
ATLTRACE(_T(": restarting watcher\n"));
m_hCompPort.CloseHandle();
// We must sync the whole section because other threads may
// receive "AddPath" calls that will delete the completion
// port *while* we are adding references to it .
AutoLocker lock(m_critSec);
// Clear the list of watched objects and recreate that list.
// This will also delete the old completion port
ClearInfoMap();
CleanupWatchInfo();
for (int i=0; i<watchedPaths.GetCount(); ++i)
{
CTGitPath watchedPath = watchedPaths[i];
CAutoFile hDir = CreateFile(watchedPath.GetWinPath(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | //required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME.
FILE_FLAG_OVERLAPPED,
NULL);
if (!hDir)
{
// this could happen if a watched folder has been removed/renamed
ATLTRACE(_T("CDirectoryWatcher: CreateFile failed. Can't watch directory %s\n"), watchedPaths[i].GetWinPath());
watchedPaths.RemovePath(watchedPath);
break;
}
DEV_BROADCAST_HANDLE NotificationFilter;
SecureZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
NotificationFilter.dbch_handle = hDir;
// RegisterDeviceNotification sends a message to the UI thread:
// make sure we *can* send it and that the UI thread isn't waiting on a lock
int numPaths = watchedPaths.GetCount();
size_t numWatch = watchInfoMap.size();
lock.Unlock();
NotificationFilter.dbch_hdevnotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
lock.Lock();
// since we released the lock to prevent a deadlock with the UI thread,
// it could happen that new paths were added to watch, or another thread
// could have cleared our info map.
// if that happened, we have to restart watching all paths again.
if ((numPaths != watchedPaths.GetCount()) || (numWatch != watchInfoMap.size()))
{
ClearInfoMap();
CleanupWatchInfo();
Sleep(200);
break;
}
CDirWatchInfo * pDirInfo = new CDirWatchInfo(hDir, watchedPath);
hDir.Detach(); // the new CDirWatchInfo object owns the handle now
pDirInfo->m_hDevNotify = NotificationFilter.dbch_hdevnotify;
HANDLE port = CreateIoCompletionPort(pDirInfo->m_hDir, m_hCompPort, (ULONG_PTR)pDirInfo, 0);
if (port == NULL)
{
ATLTRACE(_T("CDirectoryWatcher: CreateIoCompletionPort failed. Can't watch directory %s\n"), watchedPath.GetWinPath());
// we must close the directory handle to allow ClearInfoMap()
// to close the completion port properly
//.........这里部分代码省略.........
示例9: rSyncPath
bool SyncCommand::Execute()
{
bool bRet = false;
CRegString rSyncPath(L"Software\\TortoiseSVN\\SyncPath");
CTSVNPath syncPath = CTSVNPath(CString(rSyncPath));
CTSVNPath syncFolder = syncPath;
CRegDWORD regCount(L"Software\\TortoiseSVN\\SyncCounter");
CRegDWORD regSyncAuth(L"Software\\TortoiseSVN\\SyncAuth");
bool bSyncAuth = DWORD(regSyncAuth) != 0;
if (!cmdLinePath.IsEmpty())
syncPath = cmdLinePath;
if (syncPath.IsEmpty() && !parser.HasKey(L"askforpath"))
{
return false;
}
syncPath.AppendPathString(L"tsvnsync.tsex");
BOOL bWithLocals = FALSE;
if (parser.HasKey(L"askforpath"))
{
// ask for the path first, then for the password
// this is used for a manual import/export
CString path;
bool bGotPath = FileOpenSave(path, bWithLocals, !!parser.HasKey(L"load"), GetExplorerHWND());
if (bGotPath)
{
syncPath = CTSVNPath(path);
if (!parser.HasKey(L"load") && syncPath.GetFileExtension().IsEmpty())
syncPath.AppendRawString(L".tsex");
}
else
return false;
}
CSimpleIni iniFile;
iniFile.SetMultiLine(true);
SVNAuthData authData;
CAutoRegKey hMainKey;
RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\TortoiseSVN", 0, KEY_READ, hMainKey.GetPointer());
FILETIME filetime = { 0 };
RegQueryInfoKey(hMainKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &filetime);
bool bCloudIsNewer = false;
if (!parser.HasKey(L"save"))
{
// open the file in read mode
CAutoFile hFile = CreateFile(syncPath.GetWinPathString(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile.IsValid())
{
// load the file
LARGE_INTEGER fsize = { 0 };
if (GetFileSizeEx(hFile, &fsize))
{
auto filebuf = std::make_unique<char[]>(DWORD(fsize.QuadPart));
DWORD bytesread = 0;
if (ReadFile(hFile, filebuf.get(), DWORD(fsize.QuadPart), &bytesread, NULL))
{
// decrypt the file contents
std::string encrypted;
if (bytesread > 0)
encrypted = std::string(filebuf.get(), bytesread);
CRegString regPW(L"Software\\TortoiseSVN\\SyncPW");
CString password;
if (parser.HasKey(L"askforpath") && parser.HasKey(L"load"))
{
INT_PTR dlgret = 0;
bool bPasswordMatches = true;
do
{
bPasswordMatches = true;
CPasswordDlg passDlg(CWnd::FromHandle(GetExplorerHWND()));
passDlg.m_bForSave = !!parser.HasKey(L"save");
dlgret = passDlg.DoModal();
password = passDlg.m_sPW1;
if ((dlgret == IDOK) && (parser.HasKey(L"load")))
{
std::string passworda = CUnicodeUtils::StdGetUTF8((LPCWSTR)password);
std::string decrypted = CStringUtils::Decrypt(encrypted, passworda);
if ((decrypted.size() < 3) || (decrypted.substr(0, 3) != "***"))
{
bPasswordMatches = false;
}
}
} while ((dlgret == IDOK) && !bPasswordMatches);
if (dlgret != IDOK)
return false;
}
else
{
auto passwordbuf = CStringUtils::Decrypt(CString(regPW));
if (passwordbuf.get())
{
password = passwordbuf.get();
}
else
{
// password does not match or it couldn't be read from
// the registry!
//.........这里部分代码省略.........
示例10: CommandWaitThread
DWORD WINAPI CommandWaitThread(LPVOID lpvParam)
{
ATLTRACE("CommandWaitThread started\n");
bool * bRun = (bool *)lpvParam;
// The main loop creates an instance of the named pipe and
// then waits for a client to connect to it. When the client
// connects, a thread is created to handle communications
// with that client, and the loop is repeated.
DWORD dwThreadId;
BOOL fConnected;
CAutoFile hPipe;
while (*bRun)
{
hPipe = CreateNamedPipe(
GetCacheCommandPipeName(),
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
NMPWAIT_USE_DEFAULT_WAIT, // client time-out
NULL); // NULL DACL
if (!hPipe)
{
//OutputDebugStringA("TSVNCache: CreatePipe failed\n");
//DebugOutputLastError();
if (*bRun)
Sleep(200);
continue; // never leave the thread!
}
// Wait for the client to connect; if it succeeds,
// the function returns a nonzero value. If the function returns
// zero, GetLastError returns ERROR_PIPE_CONNECTED.
fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (fConnected)
{
// Create a thread for this client.
CAutoGeneralHandle hCommandThread = CreateThread(
NULL, // no security attribute
0, // default stack size
CommandThread,
(HANDLE) hPipe, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (!hCommandThread)
{
//OutputDebugStringA("TSVNCache: Could not create Command thread\n");
//DebugOutputLastError();
DisconnectNamedPipe(hPipe);
hPipe.CloseHandle();
// since we're now closing this thread, we also have to close the whole application!
// otherwise the thread is dead, but the app is still running, refusing new instances
// but no pipe will be available anymore.
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 1;
}
// detach the handle, since we passed it to the thread
hPipe.Detach();
}
else
{
// The client could not connect, so close the pipe.
//OutputDebugStringA("TSVNCache: ConnectNamedPipe failed\n");
//DebugOutputLastError();
hPipe.CloseHandle();
if (*bRun)
Sleep(200);
continue; // don't end the thread!
}
}
ATLTRACE("CommandWait thread exited\n");
return 0;
}
示例11: FreePictureData
bool CPicture::Load(tstring sFilePathName)
{
bool bResult = false;
bIsIcon = false;
lpIcons = NULL;
//CFile PictureFile;
//CFileException e;
FreePictureData(); // Important - Avoid Leaks...
// No-op if no file specified
if (sFilePathName.empty())
return true;
// Load & initialize the GDI+ library if available
HMODULE hGdiPlusLib = AtlLoadSystemLibraryUsingFullPath(_T("gdiplus.dll"));
if (hGdiPlusLib && GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) == Ok)
{
bHaveGDIPlus = true;
}
// Since we loaded the gdiplus.dll only to check if it's available, we
// can safely free the library here again - GdiplusStartup() loaded it too
// and reference counting will make sure that it stays loaded until GdiplusShutdown()
// is called.
FreeLibrary(hGdiPlusLib);
// Attempt to load using GDI+ if available
if (bHaveGDIPlus)
{
pBitmap = new Bitmap(sFilePathName.c_str(), FALSE);
GUID guid;
pBitmap->GetRawFormat(&guid);
if (pBitmap->GetLastStatus() != Ok)
{
delete pBitmap;
pBitmap = NULL;
}
// gdiplus only loads the first icon found in an icon file
// so we have to handle icon files ourselves :(
// Even though gdiplus can load icons, it can't load the new
// icons from Vista - in Vista, the icon format changed slightly.
// But the LoadIcon/LoadImage API still can load those icons,
// at least those dimensions which are also used on pre-Vista
// systems.
// For that reason, we don't rely on gdiplus telling us if
// the image format is "icon" or not, we also check the
// file extension for ".ico".
std::transform(sFilePathName.begin(), sFilePathName.end(), sFilePathName.begin(), ::tolower);
bIsIcon = (guid == ImageFormatIcon) || (wcsstr(sFilePathName.c_str(), L".ico") != NULL) || (wcsstr(sFilePathName.c_str(), L".cur") != NULL);
bIsTiff = (guid == ImageFormatTIFF) || (_tcsstr(sFilePathName.c_str(), _T(".tiff")) != NULL);
m_Name = sFilePathName;
if (bIsIcon)
{
// Icon file, get special treatment...
if (pBitmap)
{
// Cleanup first...
delete (pBitmap);
pBitmap = NULL;
bIsIcon = true;
}
CAutoFile hFile = CreateFile(sFilePathName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile)
{
BY_HANDLE_FILE_INFORMATION fileinfo;
if (GetFileInformationByHandle(hFile, &fileinfo))
{
lpIcons = new BYTE[fileinfo.nFileSizeLow];
DWORD readbytes;
if (ReadFile(hFile, lpIcons, fileinfo.nFileSizeLow, &readbytes, NULL))
{
// we have the icon. Now gather the information we need later
if (readbytes >= sizeof(ICONDIR))
{
// we are going to open same file second time so we have to close the file now
hFile.CloseHandle();
LPICONDIR lpIconDir = (LPICONDIR)lpIcons;
if ((lpIconDir->idCount) && ((lpIconDir->idCount * sizeof(ICONDIR)) <= fileinfo.nFileSizeLow))
{
try
{
bResult = false;
nCurrentIcon = 0;
hIcons = new HICON[lpIconDir->idCount];
// check that the pointers point to data that we just loaded
if (((BYTE*)lpIconDir->idEntries > (BYTE*)lpIconDir) &&
(((BYTE*)lpIconDir->idEntries) + (lpIconDir->idCount * sizeof(ICONDIRENTRY)) < ((BYTE*)lpIconDir) + fileinfo.nFileSizeLow))
{
m_Width = lpIconDir->idEntries[0].bWidth;
m_Height = lpIconDir->idEntries[0].bHeight;
bResult = true;
for (int i=0; i<lpIconDir->idCount; ++i)
{
hIcons[i] = (HICON)LoadImage(NULL, sFilePathName.c_str(), IMAGE_ICON,
lpIconDir->idEntries[i].bWidth,
//.........这里部分代码省略.........
示例12: GetTimeMillis
CMasternodeDB::ReadResult CMasternodeDB::Read(CMasternodeMan& mnodemanToLoad)
{
int64_t nStart = GetTimeMillis();
// open input file, and associate with CAutoFile
FILE *file = fopen(pathMN.string().c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!filein)
{
error("%s : Failed to open file %s", __func__, pathMN.string());
return FileError;
}
// use file size to size memory buffer
int fileSize = boost::filesystem::file_size(pathMN);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if (dataSize < 0)
dataSize = 0;
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (std::exception &e) {
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return HashReadError;
}
filein.fclose();
CDataStream ssMasternodes(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssMasternodes.begin(), ssMasternodes.end());
if (hashIn != hashTmp)
{
error("%s : Checksum mismatch, data corrupted", __func__);
return IncorrectHash;
}
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (network specific magic number) and ..
ssMasternodes >> FLATDATA(pchMsgTmp);
// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
{
error("%s : Invalid network magic number", __func__);
return IncorrectMagic;
}
// de-serialize address data into one CMnList object
ssMasternodes >> mnodemanToLoad;
}
catch (std::exception &e) {
mnodemanToLoad.Clear();
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return IncorrectFormat;
}
mnodemanToLoad.CheckAndRemove(); // clean out expired
LogPrintf("Loaded info from mncache.dat %dms\n", GetTimeMillis() - nStart);
LogPrintf(" %s\n", mnodemanToLoad.ToString());
return Ok;
}
示例13: while
void CPathWatcher::WorkerThread()
{
DWORD numBytes;
CDirWatchInfo * pdi = NULL;
LPOVERLAPPED lpOverlapped;
WCHAR buf[MAX_PATH*4] = {0};
while (m_bRunning)
{
if (watchedPaths.GetCount())
{
if (!GetQueuedCompletionStatus(m_hCompPort,
&numBytes,
(PULONG_PTR) &pdi,
&lpOverlapped,
INFINITE))
{
// Error retrieving changes
// Clear the list of watched objects and recreate that list
if (!m_bRunning)
return;
{
AutoLocker lock(m_critSec);
ClearInfoMap();
}
DWORD lasterr = GetLastError();
if ((m_hCompPort)&&(lasterr!=ERROR_SUCCESS)&&(lasterr!=ERROR_INVALID_HANDLE))
{
m_hCompPort.CloseHandle();
}
// Since we pass m_hCompPort to CreateIoCompletionPort, we
// have to set this to NULL to have that API create a new
// handle.
m_hCompPort = NULL;
for (int i=0; i<watchedPaths.GetCount(); ++i)
{
CAutoFile hDir = CreateFile(watchedPaths[i].GetWinPath(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | //required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME.
FILE_FLAG_OVERLAPPED,
NULL);
if (!hDir)
{
// this could happen if a watched folder has been removed/renamed
m_hCompPort.CloseHandle();
AutoLocker lock(m_critSec);
watchedPaths.RemovePath(watchedPaths[i]);
i--; if (i<0) i=0;
break;
}
CDirWatchInfo * pDirInfo = new CDirWatchInfo(hDir, watchedPaths[i]);
hDir.Detach(); // the new CDirWatchInfo object owns the handle now
m_hCompPort = CreateIoCompletionPort(pDirInfo->m_hDir, m_hCompPort, (ULONG_PTR)pDirInfo, 0);
if (m_hCompPort == NULL)
{
AutoLocker lock(m_critSec);
ClearInfoMap();
delete pDirInfo;
pDirInfo = NULL;
watchedPaths.RemovePath(watchedPaths[i]);
i--; if (i<0) i=0;
break;
}
if (!ReadDirectoryChangesW(pDirInfo->m_hDir,
pDirInfo->m_Buffer,
READ_DIR_CHANGE_BUFFER_SIZE,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
&numBytes,// not used
&pDirInfo->m_Overlapped,
NULL)) //no completion routine!
{
AutoLocker lock(m_critSec);
ClearInfoMap();
delete pDirInfo;
pDirInfo = NULL;
watchedPaths.RemovePath(watchedPaths[i]);
i--; if (i<0) i=0;
break;
}
AutoLocker lock(m_critSec);
watchInfoMap[pDirInfo->m_hDir] = pDirInfo;
ATLTRACE(_T("watching path %s\n"), pDirInfo->m_DirName.GetWinPath());
}
}
else
{
if (!m_bRunning)
return;
// NOTE: the longer this code takes to execute until ReadDirectoryChangesW
// is called again, the higher the chance that we miss some
// changes in the file system!
if (pdi)
{
if (numBytes == 0)
{
goto continuewatching;
//.........这里部分代码省略.........
示例14: RemoveAll
BOOL CFileTextLines::Load(const CString& sFilePath, int lengthHint /* = 0*/)
{
m_LineEndings = EOL_AUTOLINE;
m_UnicodeType = CFileTextLines::AUTOTYPE;
RemoveAll();
m_endings.clear();
if(lengthHint != 0)
{
Reserve(lengthHint);
}
if (PathIsDirectory(sFilePath))
{
m_sErrorString.Format(IDS_ERR_FILE_NOTAFILE, (LPCTSTR)sFilePath);
return FALSE;
}
if (!PathFileExists(sFilePath))
{
//file does not exist, so just return SUCCESS
return TRUE;
}
CAutoFile hFile = CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (!hFile)
{
SetErrorString();
return FALSE;
}
LARGE_INTEGER fsize;
if (!GetFileSizeEx(hFile, &fsize))
{
SetErrorString();
return false;
}
if (fsize.HighPart)
{
// file is way too big for us
m_sErrorString.LoadString(IDS_ERR_FILE_TOOBIG);
return FALSE;
}
// If new[] was done for type T delete[] must be called on a pointer of type T*,
// otherwise the behavior is undefined.
// +1 is to address possible truncation when integer division is done
wchar_t* pFileBuf = new wchar_t[fsize.LowPart/sizeof(wchar_t) + 1];
DWORD dwReadBytes = 0;
if (!ReadFile(hFile, pFileBuf, fsize.LowPart, &dwReadBytes, NULL))
{
delete [] pFileBuf;
SetErrorString();
return FALSE;
}
if (m_UnicodeType == CFileTextLines::AUTOTYPE)
{
m_UnicodeType = this->CheckUnicodeType(pFileBuf, dwReadBytes);
}
if (m_LineEndings == EOL_AUTOLINE)
{
m_LineEndings = CheckLineEndings(pFileBuf, min(10000, dwReadBytes));
}
hFile.CloseHandle();
if (m_UnicodeType == CFileTextLines::BINARY)
{
m_sErrorString.Format(IDS_ERR_FILE_BINARY, (LPCTSTR)sFilePath);
delete [] pFileBuf;
return FALSE;
}
// we may have to convert the file content
if ((m_UnicodeType == UTF8)||(m_UnicodeType == UTF8BOM))
{
int ret = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pFileBuf, dwReadBytes, NULL, 0);
wchar_t * pWideBuf = new wchar_t[ret];
int ret2 = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pFileBuf, dwReadBytes, pWideBuf, ret);
if (ret2 == ret)
{
delete [] pFileBuf;
pFileBuf = pWideBuf;
dwReadBytes = ret2;
} else
delete [] pWideBuf;
}
else if (m_UnicodeType == ASCII)
{
int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)pFileBuf, dwReadBytes, NULL, 0);
wchar_t * pWideBuf = new wchar_t[ret];
int ret2 = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)pFileBuf, dwReadBytes, pWideBuf, ret);
if (ret2 == ret)
{
delete [] pFileBuf;
pFileBuf = pWideBuf;
dwReadBytes = ret2;
}
else
delete [] pWideBuf;
}
// fill in the lines into the array
//.........这里部分代码省略.........