diff --git a/README.md b/README.md index 003ceb4..a94587a 100644 --- a/README.md +++ b/README.md @@ -66,14 +66,13 @@ int main() cout << "Config: " << sago::getConfigHome() << "\n"; cout << "Data: " << sago::getDataHome() << "\n"; cout << "Cache: " << sago::getCacheDir() << "\n"; - sago::PlatformFolders p; - cout << "Documents: " << p.getDocumentsFolder() << "\n"; - cout << "Desktop: " << p.getDesktopFolder() << "\n"; - cout << "Pictures: " << p.getPicturesFolder() << "\n"; - cout << "Music: " << p.getMusicFolder() << "\n"; - cout << "Video: " << p.getVideoFolder() << "\n"; - cout << "Download: " << p.getDownloadFolder1() << "\n"; - cout << "Save Games 1: " << p.getSaveGamesFolder1() << "\n"; + cout << "Documents: " << sago::getDocumentsFolder() << "\n"; + cout << "Desktop: " << sago::getDesktopFolder() << "\n"; + cout << "Pictures: " << sago::getPicturesFolder() << "\n"; + cout << "Music: " << sago::getMusicFolder() << "\n"; + cout << "Video: " << sago::getVideoFolder() << "\n"; + cout << "Download: " << sago::getDownloadFolder1() << "\n"; + cout << "Save Games 1: " << sago::getSaveGamesFolder1() << "\n"; return 0; } ``` diff --git a/platform_folders.cpp b/platform_folders.cpp index bcfd00c..8741eaf 100644 --- a/platform_folders.cpp +++ b/platform_folders.cpp @@ -27,19 +27,17 @@ #include #include "sago/platform_folders.h" -int main() -{ +int main() { std::cout << "Config: " << sago::getConfigHome() << "\n"; std::cout << "Data: " << sago::getDataHome() << "\n"; std::cout << "Cache: " << sago::getCacheDir() << "\n"; - sago::PlatformFolders p; - std::cout << "Documents: " << p.getDocumentsFolder() << "\n"; - std::cout << "Desktop: " << p.getDesktopFolder() << "\n"; - std::cout << "Pictures: " << p.getPicturesFolder() << "\n"; - std::cout << "Music: " << p.getMusicFolder() << "\n"; - std::cout << "Video: " << p.getVideoFolder() << "\n"; - std::cout << "Download: " << p.getDownloadFolder1() << "\n"; - std::cout << "Save Games 1: " << p.getSaveGamesFolder1() << "\n"; + std::cout << "Documents: " << sago::getDocumentsFolder() << "\n"; + std::cout << "Desktop: " << sago::getDesktopFolder() << "\n"; + std::cout << "Pictures: " << sago::getPicturesFolder() << "\n"; + std::cout << "Music: " << sago::getMusicFolder() << "\n"; + std::cout << "Video: " << sago::getVideoFolder() << "\n"; + std::cout << "Download: " << sago::getDownloadFolder1() << "\n"; + std::cout << "Save Games 1: " << sago::getSaveGamesFolder1() << "\n"; std::vector extraData; sago::appendAdditionalDataDirectories(extraData); for (size_t i=0; i < extraData.size(); ++i) { diff --git a/sago/platform_folders.cpp b/sago/platform_folders.cpp index ddb63a9..ce77a1f 100644 --- a/sago/platform_folders.cpp +++ b/sago/platform_folders.cpp @@ -37,8 +37,7 @@ SOFTWARE. #include #include -static std::string win32_utf16_to_utf8(const wchar_t* wstr) -{ +static std::string win32_utf16_to_utf8(const wchar_t* wstr) { std::string res; // If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result. int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); @@ -59,8 +58,7 @@ static std::string win32_utf16_to_utf8(const wchar_t* wstr) static std::string GetWindowsFolder(int folderId, const char* errorMsg) { wchar_t szPath[MAX_PATH]; szPath[0] = 0; - if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) ) - { + if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) ) { throw std::runtime_error(errorMsg); } return win32_utf16_to_utf8(szPath); @@ -126,7 +124,7 @@ static std::string getHome() { res = homeEnv; return res; } - struct passwd *pw = getpwuid(uid); + struct passwd* pw = getpwuid(uid); if (!pw) { throw std::runtime_error("Unable to get passwd struct."); } @@ -275,7 +273,8 @@ PlatformFolders::PlatformFolders() { this->data = new PlatformFolders::PlatformFoldersData(); try { PlatformFoldersFillData(data->folders); - } catch (...) { + } + catch (...) { delete this->data; throw; } @@ -363,6 +362,33 @@ std::string PlatformFolders::getSaveGamesFolder1() const { #endif } +std::string getDesktopFolder() { + return PlatformFolders().getDesktopFolder(); +} + +std::string getDocumentsFolder() { + return PlatformFolders().getDocumentsFolder(); +} + +std::string getDownloadFolder1() { + return PlatformFolders().getDownloadFolder1(); +} + +std::string getPicturesFolder() { + return PlatformFolders().getPicturesFolder(); +} + +std::string getMusicFolder() { + return PlatformFolders().getMusicFolder(); +} + +std::string getVideoFolder() { + return PlatformFolders().getVideoFolder(); +} + +std::string getSaveGamesFolder1() { + return PlatformFolders().getSaveGamesFolder1(); +} } //namespace sago diff --git a/sago/platform_folders.h b/sago/platform_folders.h index 73fba85..824e725 100644 --- a/sago/platform_folders.h +++ b/sago/platform_folders.h @@ -48,6 +48,7 @@ namespace sago { * @return The base folder for storring program data. */ std::string getDataHome(); + /** * Retrives the base folder for storring config files. * You must add the program name yourself like this: @@ -59,6 +60,7 @@ std::string getDataHome(); * @return The base folder for storring config data. */ std::string getConfigHome(); + /** * Retrives the base folder for storring cache files. * You must add the program name yourself like this: @@ -70,6 +72,7 @@ std::string getConfigHome(); * @return The base folder for storring data that do not need to be backed up. */ std::string getCacheDir(); + /** * This will append extra folders that your program should be looking for data files in. * This does not normally include the path returned by GetDataHome(). @@ -87,6 +90,7 @@ std::string getCacheDir(); * @param homes A vector that extra folders will be appended to. */ void appendAdditionalDataDirectories(std::vector& homes); + /** * This will append extra folders that your program should be looking for config files in. * This does not normally include the path returned by GetConfigHome(). @@ -106,6 +110,59 @@ void appendAdditionalDataDirectories(std::vector& homes); void appendAdditionalConfigDirectories(std::vector& homes); /** + * The folder that represents the desktop. + * Normally you should try not to use this folder. + * @return Absolute path to the user's desktop + */ +std::string getDesktopFolder(); + +/** + * The folder to store user documents to + * @return Absolute path to the "Documents" folder + */ +std::string getDocumentsFolder(); + +/** + * The folder where files are downloaded. + * @note Windows: This version is XP compatible and returns the Desktop. Vista and later has a dedicated folder. + * @return Absolute path to the folder where files are downloaded to. + */ +std::string getDownloadFolder1(); + +/** + * The folder for storring the user's pictures. + * @return Absolute path to the "Picture" folder + */ +std::string getPicturesFolder(); + +/** + * The folder where music is stored + * @return Absolute path to the music folder + */ +std::string getMusicFolder(); + +/** + * The folder where video is stored + * @return Absolute path to the video folder + */ +std::string getVideoFolder(); + +/** + * The base folder for storring saved games. + * You must add the program name to it like this: + * @code{.cpp} + * PlatformFolders pf; + * string saved_games_folder = pf.getSaveGamesFolder1()+"/My Program Name/"; + * @endcode + * @note Windows: This is an XP compatible version and returns the path to "My Games" in Documents. Vista and later has an official folder. + * @note Linux: XDF does not define a folder for saved games. This will just return the same as GetDataHome() + * @return The folder base folder for storring save games. + */ +std::string getSaveGamesFolder1(); + +#ifndef DOXYGEN_SHOULD_SKIP_THIS + +/** * This class contains methods for finding the system depended special folders. * For Windows these folders are either by convention or given by CSIDL. * For Linux XDG convention is used. @@ -166,10 +223,13 @@ private: #elif defined(__APPLE__) #else struct PlatformFoldersData; - PlatformFoldersData *data; + PlatformFoldersData* data; #endif }; +#endif // skip doxygen + + } //namespace sago #endif /* PLATFORM_FOLDERS_H */