CMT – Utils



CMT will require some utility functions, eg get the directory where I can write my temporary files, get the directory where I will keep the config files and so on.

For the regular windows developer, this might seem like a pointless task, because you can write stuff in the registry. I am not the regular windows developer and I’d rather keep away from the registry. And, I want to have the same format for the config files, both on Windows and on the other platforms (Linux or FreeBSD). 

There are some differences where a good programmer would store the config files. For windows, this should look like:

X:\Documents and Settings\%username%\Application Data\myapp\

and on Linux or FreeBSD, it should look like:

/home/%username%/.myapp/

Hopefully, wxWidgets provides as with a function to get the home directory for an user,

wxFileName::GetHomeDir

However, we still have a problem. On Windows, that function will return 

X:\Documents and Settings\%username%\

while on Linux/FreeBSD it will return

/home/%username%/

As you can see, the Linux/FreeBSD output is enough for my task, I can append .appname and I have the path to the directory where I can write my stuff.
On windows, what I get from this function is not enough. I still need to append “Application Data”, which on Windows installs with a different language than English, is translated too.

Msdn suggests using SHGetFolderPath with CSIDL_APPDATA parameter to get the full path to the Application Data folder for each user. And this works, from my experience with other windows projects, also for non english installs. 

Some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool CApplicationSettings::GetConfigDirectory(wxString &confdir)
{
	bool bRet = false;
	bRet = CApplicationSettings::GetApplicationDataPath(confdir);
	if (bRet)
	{
                if (confdir.Length() == 0) {
		     confdir = wxFileName::GetHomeDir();
                }
                confdir.append(wxFileName::GetPathSeparator());
	        confdir.append(wxT(".cmt"));
            	confdir.append(wxFileName::GetPathSeparator());
	}
	return bRet;
}
 
bool CApplicationSettings::GetApplicationDataPath(wxString &path)
{
	// _WIN64 is for 64 bit only, _WIN32 is always defined on Windows
	bool bRet = true;
        path = wxT("");
#ifdef _WIN32
	TCHAR szPath[MAX_PATH];
	BOOL ret = SHGetFolderPath(NULL,CSIDL_APPDATA , NULL, 0, szPath );
	// add checks
        if (!ret)
        {
              return false;
        }
	path.append(szPath);
#endif
	return bRet;
}

If the code runs on Linux/FreeBSD, GetApplicationDataPath will return true and the path will be empty. So, go on and use wxWidgets function to get the home dir, attach .cmt and return true.
If the code runs on Windows and there are any problems getting the App Data dir, the function will return false. If the function correctly got the App Data dir, we attach .cmt and return true.

Someone suggested to read the enviroment variables %APPDATA% for Windows and $HOME for Linux/FreeBSD. This solution seems simpler to implement and if I get enough time, I will research it to see if there are any drawbacks.

However, as this functionality is internal to the CApplicationSettings class, its users won’t be affected by a change at a latter time.

http://www.coada.net/wp-content/plugins/sociofluid/images/reddit_24.png http://www.coada.net/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://www.coada.net/wp-content/plugins/sociofluid/images/technorati_24.png http://www.coada.net/wp-content/plugins/sociofluid/images/google_24.png http://www.coada.net/wp-content/plugins/sociofluid/images/yahoobuzz_24.png