Skip to content

Commit

Permalink
Added Mac OS X support. I just need to check it on a Mac
Browse files Browse the repository at this point in the history
sago007 committed Oct 26, 2015
1 parent dcb0fec commit 1eaaab7
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -14,6 +14,14 @@ else
BASE_CFLAGS+= -std=c++03
endif

#This is used to detect Mac because Mac needs CoreServices for linking
ifneq ($(OS),Windows_NT)
UNAME := $(shell uname -s)
ifeq ($(UNAME),Darwin)
BASE_LIBS+= -framework CoreServices
endif
endif

PROGRAMNAME=platform_folders

O_FILES=${PROGRAMNAME}.o sago/platform_folders.o
41 changes: 41 additions & 0 deletions sago/platform_folders.cpp
Original file line number Diff line number Diff line change
@@ -58,6 +58,22 @@ static std::string GetAppDataCommon() {
static std::string GetAppDataLocal() {
return GetWindowsFolder(CSIDL_LOCAL_APPDATA, "LocalAppData could not be found");
}
#elif defined(__APPLE__)
#include <CoreServices/CoreServices.h>

static std::string GetMacFolder(OSType folderType, const char* errorMsg) {
std::string ret;
FSRef ref;
char path[PATH_MAX];
OSStatus err = FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );
if (err != noErr) {
throw std::runtime_error(errorMsg);
}
FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );
ret = path;
return ret;
}

#else
#include <map>
#include <fstream>
@@ -147,6 +163,8 @@ namespace sago {
std::string getDataHome() {
#if defined(_WIN32)
return GetAppData();
#elif defined(__APPLE__)
return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
#else
return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
#endif
@@ -155,6 +173,8 @@ std::string getDataHome() {
std::string getConfigHome() {
#if defined(_WIN32)
return GetAppData();
#elif defined(__APPLE__)
return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
#else
return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
#endif
@@ -163,6 +183,8 @@ std::string getConfigHome() {
std::string getCacheDir() {
#if defined(_WIN32)
return GetAppDataLocal();
#elif defined(__APPLE__)
return GetMacFolder(kCachedDataFolderType, "Failed to find the Application Support Folder");
#else
return getLinuxFolderDefault("XDG_CONFIG_HOME", ".cache");
#endif
@@ -171,6 +193,7 @@ std::string getCacheDir() {
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
#if defined(_WIN32)
homes.push_back(GetAppDataCommon());
#elif defined(__APPLE__)
#else
appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
#endif
@@ -179,12 +202,14 @@ void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
#if defined(_WIN32)
homes.push_back(GetAppDataCommon());
#elif defined(__APPLE__)
#else
appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
#endif
}

#if defined(_WIN32)
#elif defined(__APPLE__)
#else
struct PlatformFolders::PlatformFoldersData {
std::map<std::string, std::string> folders;
@@ -226,6 +251,7 @@ static void PlatformFoldersFillData(std::map<std::string, std::string>& folders)

PlatformFolders::PlatformFolders() {
#if defined(_WIN32)
#elif defined(__APPLE__)
#else
this->data = new PlatformFolders::PlatformFoldersData();
try {
@@ -239,6 +265,7 @@ PlatformFolders::PlatformFolders() {

PlatformFolders::~PlatformFolders() {
#if defined(_WIN32)
#elif defined(__APPLE__)
#else
delete this->data;
#endif
@@ -247,6 +274,8 @@ PlatformFolders::~PlatformFolders() {
std::string PlatformFolders::getDocumentsFolder() const {
#if defined(_WIN32)
return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder");
#elif defined(__APPLE__)
return GetMacFolder(kDocumentsFolderType, "Failed to find Documents Folder");
#else
return data->folders["XDG_DOCUMENTS_DIR"];
#endif
@@ -255,6 +284,8 @@ std::string PlatformFolders::getDocumentsFolder() const {
std::string PlatformFolders::getDesktopFolder() const {
#if defined(_WIN32)
return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find Desktop folder");
#elif defined(__APPLE__)
return GetMacFolder(kDesktopFolderType, "Failed to find Desktop folder");
#else
return data->folders["XDG_DESKTOP_DIR"];
#endif
@@ -263,6 +294,8 @@ std::string PlatformFolders::getDesktopFolder() const {
std::string PlatformFolders::getPicturesFolder() const {
#if defined(_WIN32)
return GetWindowsFolder(CSIDL_MYPICTURES, "Failed to find My Pictures folder");
#elif defined(__APPLE__)
return GetMacFolder(kPictureDocumentsFolderType, "Failed to find Picture folder");
#else
return data->folders["XDG_PICTURES_DIR"];
#endif
@@ -272,6 +305,8 @@ std::string PlatformFolders::getDownloadFolder1() const {
#if defined(_WIN32)
//Pre Vista. Files was downloaded to the desktop
return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find My Downloads (Desktop) folder");
#elif defined(__APPLE__)
return GetMacFolder(kDownloadsFolderType, "Failed to find Download folder");
#else
return data->folders["XDG_DOWNLOAD_DIR"];
#endif
@@ -280,6 +315,8 @@ std::string PlatformFolders::getDownloadFolder1() const {
std::string PlatformFolders::getMusicFolder() const {
#if defined(_WIN32)
return GetWindowsFolder(CSIDL_MYMUSIC, "Failed to find My Music folder");
#elif defined(__APPLE__)
return GetMacFolder(kMusicDocumentsFolderType, "Failed to find Music folder");
#else
return data->folders["XDG_MUSIC_DIR"];
#endif
@@ -288,6 +325,8 @@ std::string PlatformFolders::getMusicFolder() const {
std::string PlatformFolders::getVideoFolder() const {
#if defined(_WIN32)
return GetWindowsFolder(CSIDL_MYVIDEO, "Failed to find My Video folder");
#elif defined(__APPLE__)
return GetMacFolder(kMovieDocumentsFolderType, "Failed to find Movie folder");
#else
return data->folders["XDG_VIDEOS_DIR"];
#endif
@@ -298,6 +337,8 @@ std::string PlatformFolders::getSaveGamesFolder1() const {
//A dedicated Save Games folder was not introduced until Vista. For XP and older save games are most often saved in a normal folder named "My Games".
//Data that should not be user accessible should be placed under GetDataHome() instead
return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder")+"\\My Games";
#elif defined(__APPLE__)
return GetMacFolder(kApplicationSupportFolderType, "Failed to find Application Support Folder");
#else
return getDataHome();
#endif

0 comments on commit 1eaaab7

Please sign in to comment.