Hello MCMic,
MCMic {l Wrote}:Would you be able to do a clean C++ class file that could be used by all projects that intend to store data in user folder?
It would need to be multi-platform and have the name of the application in only one place.
I would use it for Genetic Invasion for sure.
Not sure it is really that special, I'll just post some code snippets of how meandmyshadow does it.
First of all the three different paths (under Linux):
//Under Windows there's just one userpath.
#ifdef WIN32
string userPath;
#else
//But on other platforms we make a difference between the userPath (config files) and the userDataPath (data files).
//Finally there's the path for cache data userCachePath.
string userPath,userDataPath,userCachePath;
#endif
Now we set the userpaths according to the
XDG Base Directory specification.
In your case just replace meandmyshadow with a string that you set before calling this method.
...
//TODO: Check if the userpath is empty before setting userPath???
//Check if the userPath is empty.
if(getUserPath().empty()){
#ifdef WIN32
//Get the userPath.
char s[1024];
SHGetSpecialFolderPathA(NULL,s,CSIDL_PERSONAL,1);
userPath=s;
userPath+="\\My Games\\meandmyshadow\\";
#else
//Temp variable that is used to prevent NULL assignement.
char* env;
//First get the $XDG_CONFIG_HOME env var.
env=getenv("XDG_CONFIG_HOME");
//If it's null set userPath to $HOME/.config/.
if(env!=NULL){
userPath=env;
}else{
userPath=getenv("HOME");
userPath+="/.config";
}
//And add meandmyshadow to it.
userPath+="/meandmyshadow/";
//Now get the $XDG_DATA_HOME env var.
env=getenv("XDG_DATA_HOME");
//If it's null set userDataPath to $HOME/.local/share.
if(env!=NULL){
userDataPath=env;
}else{
userDataPath=getenv("HOME");
userDataPath+="/.local/share";
}
//And add meandmyshadow to it.
userDataPath+="/meandmyshadow/";
//Now get the $XDG_CACHE_HOME env var.
env=getenv("XDG_CACHE_HOME");
//If it's null set userCachePath to $HOME/.cache.
if(env!=NULL){
userCachePath=env;
}else{
userCachePath=getenv("HOME");
userCachePath+="/.cache";
}
//And add meandmyshadow to it.
userCachePath+="/meandmyshadow/";
//Set env null.
env=NULL;
#endif
//Print the userPath.
cout<<"User preferences will be fetched from: "<<userPath<<endl;
#ifndef WIN32
//In case of a non-Windows computer show the user data path.
cout<<"User data will be fetched from: "<<userDataPath<<endl;
#endif
}
...
Now meandmyshadow will create the directories to make sure they exist before writing in them.
This isn't really relevant, just make sure it exists.
Here's the code for retrieving the userPath:
The advantage of a method is that you can give what type of userPath you want without making a difference between different OSes.
...
//Enum containing the different userPath types.
//NOTE: They are only needed for the non-Windows platform..
enum UserPaths{
//The userpath containing the config files.
//Default $HOME/.config/meandmyshadow/
USER_CONFIG,
//The userpath containing the user data.
//Default $HOME/.local/share/meandmyshadow/
USER_DATA,
//The userpath containing the temporary files.
//Default $HOME/.cache/meandmyshadow/
USER_CACHE
};
...
//Method for retrieving the userPath.
//type: The type of userpath to return, only used on non-Windows platforms.
//Returns: The userPath.
inline const std::string& getUserPath(int type=0){
#ifdef WIN32
return userPath;
#else
switch(type){
case USER_CONFIG:
return userPath;
break;
case USER_DATA:
return userDataPath;
break;
case USER_CACHE:
return userCachePath;
break;
default:
std::cerr<<"WARNING: Illegal userpath type, returning user config path."<<std::endl;
return userPath;
break;
}
#endif
}
...
Just realized that it isn't 100% XDG-compliant because it doesn't check the
$XDG_DATA_DIRS and
$XDG_CONFIG_DIRS environment variables.
But IMHO this isn't such a big problem.
Note that this code is only tested only works for Windows and Linux.
Other OSes will probably work, but haven't been tested.
As you can see it isn't that hard to implement, if you've got any questions feel free to ask.