/** * @brief Create a temporary file with given prefix. * @param [in] prefix A prefix for temp file name. * @return Created temp file path. */ String TempFile::Create(LPCTSTR prefix) { String temp = env_GetTempPath(NULL); if (temp.empty()) { return TEXT(""); } String pref = prefix; if (pref.empty()) pref = TEXT("wmtmp"); temp = env_GetTempFileName(temp.c_str(), pref.c_str(), NULL); if (!temp.empty()) m_path = temp; return temp; }
/** * @brief Prune log file if it exceeds max given size. * @param [in] f Pointer to FILE structure. * @todo This is not safe function at all. We should check return values! */ void CLogFile::Prune(FILE *f) { TCHAR buf[8196] = {0}; DWORD amt; FILE *tf; String tempfile = env_GetTempFileName(_T("."), _T("LOG")); DeleteFile(tempfile.c_str()); if ((tf = _tfopen(tempfile.c_str(), _T("w"))) != NULL) { fseek(f, ftell(f) / 4, SEEK_SET); _fputts(_T("#### The log has been truncated due to size limits ####\n"), tf); while ((amt = fread(buf, 1, 8196, f)) > 0) fwrite(buf, amt, 1, tf); fclose(tf); fclose(f); DeleteFile(m_strLogPath.c_str()); MoveFile(tempfile.c_str(), m_strLogPath.c_str()); } }
// for OLECHAR files, transform to UTF8 for diffutils // TODO : convert Ansi to UTF8 if other file is unicode or uses a different codepage BOOL FileTransform_UCS2ToUTF8(String & filepath, BOOL bMayOverwrite) { String tempDir = env_GetTempPath(); if (tempDir.empty()) return FALSE; String tempFilepath = env_GetTempFileName(tempDir.c_str(), _T("_WM")); if (tempFilepath.empty()) return FALSE; // TODO : is it better with the BOM or without (just change the last argument) int nFileChanged = 0; BOOL bSuccess = OlecharToUTF8(filepath.c_str(), tempFilepath.c_str(), nFileChanged, FALSE); if (!bSuccess) return FALSE; if (nFileChanged) { // we do not overwrite so we delete the old file if (bMayOverwrite) { if (!::DeleteFile(filepath.c_str())) { LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"), filepath.c_str(), GetSysError(GetLastError()).c_str())); } } // and change the filepath if everything works filepath = tempFilepath; } else { if (!::DeleteFile(tempFilepath.c_str())) { LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"), tempFilepath.c_str(), GetSysError(GetLastError()).c_str())); } } return TRUE; }
BOOL FileTransform_NormalizeUnicode(String & filepath, BOOL bMayOverwrite) { String tempDir = env_GetTempPath(); if (tempDir.empty()) return FALSE; String tempFilepath = env_GetTempFileName(tempDir.c_str(), _T("_WM")); if (tempFilepath.empty()) return FALSE; int nFileChanged = 0; BOOL bSuccess = UnicodeFileToOlechar(filepath.c_str(), tempFilepath.c_str(), nFileChanged); if (!bSuccess) return FALSE; if (nFileChanged) { // we do not overwrite so we delete the old file if (bMayOverwrite) { if (!::DeleteFile(filepath.c_str())) { LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"), filepath.c_str(), GetSysError(GetLastError()).c_str())); } } // and change the filepath if everything works filepath = tempFilepath; } else { if (!::DeleteFile(tempFilepath.c_str())) { LogErrorString(Fmt(_T("DeleteFile(%s) failed: %s"), tempFilepath.c_str(), GetSysError(GetLastError()).c_str())); } } return TRUE; }
/** * @brief Create a temporary file from existing file's contents. * This function creates a temporary file to temp folder and copies * given file's contents to there. * @param [in] filepath Full path to existing file. * @param [in] prefix Prefix for the temporary filename. * @return Full path to the temporary file. */ String TempFile::CreateFromFile(LPCTSTR filepath, LPCTSTR prefix) { String temp = env_GetTempPath(NULL); if (temp.empty()) { return TEXT(""); } String pref = prefix; if (pref.empty()) pref = TEXT("wmtmp"); temp = env_GetTempFileName(temp.c_str(), pref.c_str(), NULL); if (!temp.empty()) { // Scratchpads don't have a file to copy. m_path = temp; if (::CopyFile(filepath, temp.c_str(), FALSE)) { ::SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL); } } return temp; }