// TempPath.h #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #ifndef TempPathh #define TempPathh #include "LocalPath.h" // Simple class to delete a file when this goes out of scope: class CAutoDeletePath : public CString { bool AutoDelete; public: CAutoDeletePath(bool AutoDelete=true) : AutoDelete(AutoDelete) {} CAutoDeletePath(const CString& Path, bool AutoDelete=true) : CString(Path), AutoDelete(AutoDelete) {} virtual ~CAutoDeletePath() {try{if(AutoDelete && !IsEmpty()) DeleteFile(*this);}catch(...){ASSERT(0);}} void SetAutoDelete(bool Auto=true) {AutoDelete=Auto;} bool GetAutoDelete() const {return AutoDelete;} }; /* Creates a unique Temporary File Name from a Template. The printf-style Template should use an integer parameter (%04d): Save(CTempPath("PXBF%04d.X_B")); This would produce a file-name like: "C:\DOCUME~1\rjm\LOCALS~1\Temp\PXBF0021.X_B" You MUST provide a template to describe the WHOLE File Name, or it can't know if it's generating a duplicate! */ struct CTempPath : public CAutoDeletePath, CLocalPath { CTempPath(bool AutoDelete=false) : CAutoDeletePath(AutoDelete) {} CTempPath(const char* Template, bool AutoDelete=false) : CAutoDeletePath(MakeTempPath(Template),AutoDelete) {ASSERT(CString(Template).Find(':')==-1);} virtual ~CTempPath() {} const CString& operator=(const CString& stringSrc) {return CString::operator=(stringSrc);} const CString& operator=(const char* stringSrc) {return CString::operator=(stringSrc);} static CString MakeTempPath(const char* Template) { // Sets this to the name of a temporary file CString Root(GetTempFolder()); // Form a temporary file name CString Path; int i=0; do { Path.Format(Template, ++i); // Will Hang if Template doesn't change with i Path=(Root + Path); } while(GetFileAttributes(Path)!=-1); return Path; } static bool ValidateTempFolder() { // Make sure a temporary folder exists and is writable: if(GetTempPath(0,0)==0) return false; CTempPath TempPath("CTempPathTest%04d.txt",true); CFile File; if(File.Open(TempPath,CFile::modeCreate|CFile::modeReadWrite)) {File.Close(); return true;} else return false; } }; #endif // ndef TempPathh