tvector<tstring> ListDirectory(const tstring& sDirectory, bool bDirectories) { tvector<tstring> asResult; wchar_t szPath[MAX_PATH]; _swprintf(szPath, L"%s\\*", convert_to_wstring(sDirectory).c_str()); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(szPath, &fd); if (hFind != INVALID_HANDLE_VALUE) { int count = 0; do { if (!bDirectories && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; // Duh. if (wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0) continue; asResult.push_back(convert_from_wstring(fd.cFileName)); } while(FindNextFile(hFind, &fd)); FindClose(hFind); } return asResult; }
tstring GetAppDataDirectory(const tstring& sDirectory, const tstring& sFile) { size_t iSize; _wgetenv_s(&iSize, NULL, 0, L"APPDATA"); tstring sSuffix; sSuffix.append(sDirectory).append("\\").append(sFile); if (!iSize) return sSuffix; wchar_t* pszVar = (wchar_t*)malloc(iSize * sizeof(wchar_t)); if (!pszVar) return sSuffix; _wgetenv_s(&iSize, pszVar, iSize, L"APPDATA"); tstring sReturn = convert_from_wstring(pszVar); free(pszVar); CreateDirectory(convert_to_wstring(tstring(sReturn).append("\\").append(sDirectory)).c_str(), NULL); sReturn.append("\\").append(sSuffix); return sReturn; }
std::string directory::file() const { #ifdef TORRENT_WINDOWS #if TORRENT_USE_WSTRING return convert_from_wstring(m_fd.cFileName); #else return convert_from_native(m_fd.cFileName); #endif #else return convert_from_native(m_dirent.d_name); #endif }
tstring FindAbsolutePath(const tstring& sPath) { wchar_t szPath[MAX_PATH]; std::wstring swPath; if (!sPath.length()) swPath = L"."; else swPath = convert_to_wstring(sPath); GetFullPathName(swPath.c_str(), MAX_PATH, szPath, nullptr); return convert_from_wstring(szPath); }
std::string current_working_directory() { #ifdef TORRENT_WINDOWS #if TORRENT_USE_WSTRING wchar_t cwd[TORRENT_MAX_PATH]; _wgetcwd(cwd, sizeof(cwd) / sizeof(wchar_t)); #else char cwd[TORRENT_MAX_PATH]; _getcwd(cwd, sizeof(cwd)); #endif // TORRENT_USE_WSTRING #else char cwd[TORRENT_MAX_PATH]; if (getcwd(cwd, sizeof(cwd)) == 0) return "/"; #endif #if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING return convert_from_wstring(cwd); #else return convert_from_native(cwd); #endif }
void CreateMinidump(void* pInfo, tchar* pszDirectory) { #ifndef _DEBUG time_t currTime = ::time( NULL ); struct tm * pTime = ::localtime( &currTime ); wchar_t szModule[MAX_PATH]; ::GetModuleFileName( NULL, szModule, sizeof(szModule) / sizeof(tchar) ); wchar_t *pModule = wcsrchr( szModule, '.' ); if ( pModule ) *pModule = 0; pModule = wcsrchr( szModule, '\\' ); if ( pModule ) pModule++; else pModule = L"unknown"; wchar_t szFileName[MAX_PATH]; _snwprintf( szFileName, sizeof(szFileName) / sizeof(tchar), L"%s_%d.%.2d.%2d.%.2d.%.2d.%.2d_%d.mdmp", pModule, pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec, g_iMinidumpsWritten++ ); HANDLE hFile = CreateFile( convert_to_wstring(GetAppDataDirectory(pszDirectory, convert_from_wstring(szFileName))).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) { MINIDUMP_EXCEPTION_INFORMATION mdei; mdei.ThreadId = GetCurrentThreadId(); mdei.ExceptionPointers = (EXCEPTION_POINTERS*)pInfo; mdei.ClientPointers = FALSE; MINIDUMP_CALLBACK_INFORMATION mci; mci.CallbackRoutine = NULL; mci.CallbackParam = 0; MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory); BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, (pInfo != 0) ? &mdei : 0, 0, &mci ); if( rv ) { // Success... message to user? } CloseHandle( hFile ); } #endif }