Skip to content

Commit

Permalink
Using winapi to get Application Data and Documents paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Jan 1, 2011
1 parent 73ace47 commit e1e813e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Utility/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <sys/stat.h>
#include <algorithm>

#ifdef _WIN32
#include <shlobj.h>
#endif

#include "utilities.h"

using namespace std;
Expand Down Expand Up @@ -97,20 +101,34 @@ bool Directory::fileExists(const std::string& filename) {
}

string Directory::home() {
/** @todo @c VERSION-0.1 Fix for WIN32 */
#ifndef _WIN32
char* h = getenv("HOME");
if(!h) return "";
else return h;
#else
/** @bug Doesn't work at all */
TCHAR h[MAX_PATH];
if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, h)))
return "";
#endif

return h;
}

string Directory::configurationDir(const std::string& applicationName, bool createIfNotExists) {
/** @todo @c VERSION-0.1 Fix for WIN32 -- it's $ENV{AppData} in CMake */
#ifndef _WIN32
string h = home();
if(h.empty()) return "";
string dir = join(h, '.' + lowercase(applicationName));
#else
TCHAR path[MAX_PATH];
if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path)))
return "";
string appdata = path;
if(appdata.empty()) return "";
string dir = join(appdata, applicationName);
#endif

string dir = join(home(), '.' + lowercase(applicationName));
if(createIfNotExists) mkpath(dir);

return dir;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Utility/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class UTILITY_EXPORT Directory: public std::vector<std::string> {
* @brief Get application configuration dir
* @param name Application name
* @param createIfNotExists Create the directory, if not exists already
*
* On Unix, the configuration dir is <tt>~/.<em>name</em></tt> (name is
* lowercased), on Windows the configuration dir is somewhere in
* <tt>C:/Document and Settings/user/Application Data/<em>name</em></tt>
* (name is left as is).
*/
static std::string configurationDir(const std::string& name, bool createIfNotExists = true);

Expand Down

0 comments on commit e1e813e

Please sign in to comment.