CString extension for directory names

 Download Source Code


Profound programming rule learned the hard way: Never assume anything. So I got pretty tired of always checking a path in a CString to ensure that it terminated in a backslash before adding any additional names. I wrote the CDirectoryString class to handle this for me, and as I was writing an installation program at the time, I added some macro's for obtaining current context directory names.

The macros:

[windowsdir] - the windows directory
[systemdir] - the windows system directory
[currentdir] - the current directory
[datedir] - a directory name based on the current date

The CDirectoryString class will always check your termination and append the backslash if its missing. If you include one or more of the macros it will translate them during assigment or concatenation.

Also included are four public member functions:

void GetWindowsDirectory();
void GetSystemDirectory();
void GetCurrentDirectory();
void GetDateDirectory();
Calling either of these functions will REPLACE the current string contents with the content described by the function name.

Example:
CDirectoryString strTarget="[systemdir]";
printf(strTarget);

//output:
c:\windows\system\

another example:
CDirectoryString strTarget="c:\\outbox\\sessions[datedir]\\incoming";
printf(strTarget);

//output:
d:\outbox\sessions\08061998\incoming\

last but not least:
CDirectoryString strTarget;
strTarget.GetCurrentDirectory();
printf(strTarget);

//output:
c:\Program Files\

If you have any changes, new macros or perhaps I missed something, please email me. The source code:

DirectoryString.h

class CDirectoryString : public CString  
{
	public:
		CDirectoryString();
		CDirectoryString(const CString& stringSrc);
		CDirectoryString(LPCTSTR lpsz);
		CDirectoryString(LPCWSTR lpsz);
		virtual ~CDirectoryString();
		const CString& operator=(const CString& stringSrc);
		const CString& operator=(LPCTSTR lpsz);
		const CString& operator+=(LPCTSTR lpsz);
		const CString& operator+=(TCHAR ch);
		const CString& operator+=(const CString& string);
		void GetWindowsDirectory();
		void GetSystemDirectory();
		void GetCurrentDirectory();
		void GetDateDirectory();
		void Fixate();
		void ProcessMacros(const char* szSrc);
	protected:
		void ExpandMacro(char* pchMacroString);
		int GetMacroIndex(char* pchMacroString);
		void Concat(const char* pchString);
		void GetDateString(char* pchBuffer);
};
DirectoryString.cpp

CDirectoryString::CDirectoryString()
{
}

CDirectoryString::CDirectoryString(const CString& stringSrc)
{
	ProcessMacros(stringSrc);
}

CDirectoryString::CDirectoryString(LPCTSTR lpsz)
{
	ProcessMacros(lpsz);
}

CDirectoryString::CDirectoryString(LPCWSTR lpsz)
{
	CString::CString(lpsz);
	ProcessMacros(m_pchData);
}

CDirectoryString::~CDirectoryString()
{
}

const CString& CDirectoryString::operator=(const CString& stringSrc)
{	
	ProcessMacros(stringSrc);
	return *this;
}

const CString& CDirectoryString::operator=(LPCTSTR lpsz)
{	
	ProcessMacros(lpsz);
	return *this;
}

const CString& CDirectoryString::operator+=(LPCTSTR lpsz)
{
	*(CString*)this+=(lpsz);
	ProcessMacros(m_pchData);
	return *this;
}

const CString& CDirectoryString::operator+=(TCHAR ch)
{
	*(CString*)this+=(ch);
	ProcessMacros(m_pchData);
	return *this;
}

const CString& CDirectoryString::operator+=(const CString& string)
{
	*(CString*)this+=(string);
	ProcessMacros(m_pchData);
	return *this;
}

void CDirectoryString::ProcessMacros(const char* szSrc)
{
	if(!szSrc)
	{
		return;
	}
	int nOriginalLength=strlen(szSrc);

	char* pchOriginal=new char[nOriginalLength+1];
	strcpy(pchOriginal,szSrc);
	Release();

	char* pchAnchor=pchOriginal;
	char* pchLead=strchr(pchOriginal,'[');
	while(pchLead)
	{
		if(pchLead!=pchAnchor)
		{
			pchLead[0]=0;
			Concat(pchAnchor);
			pchLead[0]='[';
		}
		pchAnchor=pchLead;
		//scan through to the end of macro
		pchLead=strchr(pchLead,']');
		if(pchLead)
		{
			pchLead[0]=0;
			pchAnchor++;
			ExpandMacro(pchAnchor);
			pchLead[0]=']';
			pchAnchor=pchLead;
			pchAnchor++;
			pchLead=strchr(pchLead,'[');
		}
		else
		{
			break;
		}
	}
	if(strlen(pchAnchor))
	{
		Concat(pchAnchor);
	}
	delete[] pchOriginal;
	Fixate();
}

void CDirectoryString::ExpandMacro(char* pchMacroString)
{
	if(pchMacroString)
	{
		char szTemp[MAX_PATH];
		switch(GetMacroIndex(pchMacroString))
		{
			case 0:
				::GetWindowsDirectory(szTemp,MAX_PATH);
				break;
			case 1:
				::GetSystemDirectory(szTemp,MAX_PATH);
				break;
			case 2:
				::GetCurrentDirectory(MAX_PATH,szTemp);
				break;
			case 3:
				GetDateString(szTemp);
				break;
			default:
				//unknown macro, return
				return;
		}
		if(szTemp[strlen(szTemp)-1]!=92)
		{
			strcat(szTemp,"\\");
		}
		Concat(szTemp);
	}
}

void CDirectoryString::Concat(const char* pchString)
{
	CStringData* pOldData = GetData();
	ConcatCopy(GetData()->nDataLength, m_pchData, strlen(pchString),pchString);
	ASSERT(pOldData != NULL);
	CString::Release(pOldData);
}

void CDirectoryString::GetWindowsDirectory()
{
	char szTemp[MAX_PATH];
	::GetWindowsDirectory(szTemp,MAX_PATH);
	*this=szTemp;
	Fixate();
}

void CDirectoryString::GetSystemDirectory()
{
	char szTemp[MAX_PATH];
	::GetSystemDirectory(szTemp,MAX_PATH);
	*this=szTemp;
	Fixate();
}

void CDirectoryString::GetCurrentDirectory()
{
	char szTemp[MAX_PATH];
	::GetCurrentDirectory(MAX_PATH,szTemp);
	*this=szTemp;
	Fixate();
}

void CDirectoryString::GetDateDirectory()
{
	char szTemp[MAX_PATH];
	GetDateString(szTemp);
	*this=szTemp;
	Fixate();
}

void CDirectoryString::GetDateString(char* pchBuffer)
{
	if(!pchBuffer)
	{
		return;
	}
	char szTemp[5];
	SYSTEMTIME now;
	GetLocalTime(&now);
	sprintf(pchBuffer,"%02d",now.wDay);
	sprintf(szTemp,"%02d",now.wMonth);
	strcat(pchBuffer,szTemp);
	sprintf(szTemp,"%04d",now.wYear);
	strcat(pchBuffer,szTemp);
}

void CDirectoryString::Fixate()
{
	if(m_pchData[GetLength()-1]!=92)
	{
		*this+="\\";	
	}
}

int CDirectoryString::GetMacroIndex(char* pchMacroString)
{
	if(!pchMacroString)
	{
		return -1;
	}
	if(strcmp(pchMacroString,"windowsdir")==0)
	{
		return 0;
	}
	if(strcmp(pchMacroString,"systemdir")==0)
	{
		return 1;
	}
	if(strcmp(pchMacroString,"currentdir")==0)
	{
		return 2;
	}
	if(strcmp(pchMacroString,"datedir")==0)
	{
		return 3;
	}
	return -1;
}

Last updated: 8 July 1998