| 1 | # PlatformFolders [](https://travis-ci.org/sago007/PlatformFolders) [](https://ci.appveyor.com/project/sago007/platformfolders) [](https://raw.githubusercontent.com/sago007/PlatformFolders/master/LICENSE) [](https://gitter.im/PlatformFolders/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://www.codacy.com/app/github_43/PlatformFolders?utm_source=github.com&utm_medium=referral&utm_content=sago007/PlatformFolders&utm_campaign=Badge_Grade) |
| 2 | |
| 3 | A C++ library to look for directories like `My Documents`, `~/.config`, `%APPDATA%`, etc. so that you do not need to write platform-specific code |
| 4 | |
| 5 | [Source code](https://github.com/sago007/PlatformFolders) • [Latest release](https://github.com/sago007/PlatformFolders/releases/latest) • [Doxygen documentation](https://sago007.github.io/PlatformFolders/html/doxygen/) |
| 6 | |
| 7 | ## Rationale |
| 8 | |
| 9 | There are a lot of platform abstraction libraries available. You can get graphics abstraction libraries, GUI abstraction libraries and file abstraction libraries. |
| 10 | |
| 11 | But folder abstraction seems to be more difficult. |
| 12 | My problem was that the code that found the place to save data was platform dependent. This cluttered my code and often I would not discover that it did not compile until moving it to the different platforms. |
| 13 | |
| 14 | [I have written a bit more about it here.](https://sago007.blogspot.dk/2015/10/abstraction-for-special-folders.html) |
| 15 | |
| 16 | There are some alternatives that you might consider instead: |
| 17 | |
| 18 | * [QStandardPaths](https://doc.qt.io/qt-5/qstandardpaths.html) |
| 19 | * [glib](https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html) |
| 20 | |
| 21 | Both are properly more mature than this library. However they are both parts of large frameworks and using them with libraries outside the framework may not be that simple. |
| 22 | |
| 23 | ## Operating System Support |
| 24 | |
| 25 | ### Windows |
| 26 | |
| 27 | For Windows, the current version fetches the folders using SHGetKnownFolderPath. This requires Vista or newer. |
| 28 | |
| 29 | ### Linux |
| 30 | |
| 31 | This library uses the [XDG user-dirs.](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) |
| 32 | |
| 33 | It should work on any Unix system that has the following headers available: `pwd.h`, `unistd.h`, and `sys/types.h` |
| 34 | |
| 35 | ### macOS |
| 36 | |
| 37 | Version 4.0.0 and forward uses hardcoded values for the directories on Mac OS X. Unlike the other operating systems the folders cannot be moved on a Mac and the translation is done in the UI. |
| 38 | The versions 2.X and 3.X uses the deprecated FSFindFolder, which requires the CoreServices framework during linking. |
| 39 | Version 1.X simple used the XDG specification. |
| 40 | |
| 41 | ## Usage |
| 42 | |
| 43 | This project should be compatible with things like [Cmake's ExternalProject_Add](https://cmake.org/cmake/help/latest/module/ExternalProject.html?highlight=externalproject_add#command:externalproject_add) if you wish to use it in your project. |
| 44 | |
| 45 | You can also follow the [build step](#building) below to install at a system level, and use [Cmake's find_package](https://cmake.org/cmake/help/latest/command/find_package.html). |
| 46 | |
| 47 | ```cmake |
| 48 | # Specifying a version is optional -- note it follows Semver |
| 49 | find_package(platform_folders 3.1.0 REQUIRED) |
| 50 | # Which imports the linkable library "sago::platform_folders" |
| 51 | # Use it like so... |
| 52 | target_link_libraries(EXEORLIBNAME PRIVATE sago::platform_folders) |
| 53 | ``` |
| 54 | |
| 55 | Alternatively, you can just copy the [sago](https://github.com/sago007/PlatformFolders/tree/master/sago) folder into your program and manually link everything. |
| 56 | If you use the last option and are using a library version from before 4.0.0: Remember to link to the CoreServices lib when compiling on Mac. This typically means passing "-framework CoreServices" during the linking phase. |
| 57 | |
| 58 | Note that if you build in-tree, you can link against the Cmake alias `sago::platform_folders` just like if you had used find_package. |
| 59 | |
| 60 | ### Building |
| 61 | |
| 62 | **Notes:** |
| 63 | |
| 64 | * Until 4.0.0 macOS required the CoreServices framework during linking. |
| 65 | * If you don't want to install, remove the `--target install` command. |
| 66 | |
| 67 | Linux/macOS: |
| 68 | |
| 69 | ``` |
| 70 | mkdir -p build && cd build |
| 71 | cmake -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release .. |
| 72 | sudo cmake --build . --target install |
| 73 | ``` |
| 74 | |
| 75 | Windows: |
| 76 | |
| 77 | ``` |
| 78 | mkdir build && cd build |
| 79 | cmake -DBUILD_TESTING=OFF .. |
| 80 | runas /user:Administrator "cmake --build . --config Release --target install" |
| 81 | ``` |
| 82 | |
| 83 | ## Example Usage |
| 84 | |
| 85 | This sample program gets all folders from the system: |
| 86 | |
| 87 | ```cpp |
| 88 | #include <sago/platform_folders.h> |
| 89 | #include <iostream> |
| 90 | #include <string> |
| 91 | |
| 92 | int main() |
| 93 | { |
| 94 | std::cout << "Config: " << sago::getConfigHome() << "\n"; |
| 95 | std::cout << "Data: " << sago::getDataHome() << "\n"; |
| 96 | std::cout << "State: " << sago::getStateDir() << "\n"; |
| 97 | std::cout << "Cache: " << sago::getCacheDir() << "\n"; |
| 98 | std::cout << "Documents: " << sago::getDocumentsFolder() << "\n"; |
| 99 | std::cout << "Desktop: " << sago::getDesktopFolder() << "\n"; |
| 100 | std::cout << "Pictures: " << sago::getPicturesFolder() << "\n"; |
| 101 | std::cout << "Music: " << sago::getMusicFolder() << "\n"; |
| 102 | std::cout << "Video: " << sago::getVideoFolder() << "\n"; |
| 103 | std::cout << "Download: " << sago::getDownloadFolder() << "\n"; |
| 104 | std::cout << "Save Games 1: " << sago::getSaveGamesFolder1() << "\n"; |
| 105 | std::cout << "Save Games 2: " << sago::getSaveGamesFolder2() << "\n"; |
| 106 | return 0; |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | ### Example Output |
| 111 | |
| 112 | #### On Linux |
| 113 | |
| 114 | ``` |
| 115 | Config: /home/poul/.config |
| 116 | Data: /home/poul/.local/share |
| 117 | State: /home/poul/.local/state |
| 118 | Cache: /home/poul/.cache |
| 119 | Documents: /home/poul/Dokumenter |
| 120 | Desktop: /home/poul/Skrivebord |
| 121 | Pictures: /home/poul/Billeder |
| 122 | Music: /home/poul/Musik |
| 123 | Video: /home/poul/Videoklip |
| 124 | Download: /home/poul/Hentede filer |
| 125 | Save Games 1: /home/poul/.local/share |
| 126 | Save Games 2: /home/poul/.local/share |
| 127 | ``` |
| 128 | |
| 129 | #### On Windows |
| 130 | |
| 131 | ``` |
| 132 | Config: C:\users\poul\Application Data |
| 133 | Data: C:\users\poul\Application Data |
| 134 | State: C:\users\poul\Local Settings\Application Data |
| 135 | Cache: C:\users\poul\Local Settings\Application Data |
| 136 | Documents: C:\users\poul\Mine dokumenter |
| 137 | Desktop: C:\users\poul\Skrivebord |
| 138 | Pictures: C:\users\poul\Mine Billeder |
| 139 | Music: C:\users\poul\Min Musik |
| 140 | Video: C:\users\poul\Mine Film |
| 141 | Download: C:\users\poul\Downloads |
| 142 | Save Games 1: C:\users\poul\Mine dokumenter\My Games |
| 143 | Save Games 2: C:\users\poul\Saved Games |
| 144 | ``` |
| 145 | |
| 146 | #### On macOS |
| 147 | |
| 148 | ``` |
| 149 | Config: /Users/poul/Library/Application Support |
| 150 | Data: /Users/poul/Library/Application Support |
| 151 | State: /Users/poul/Library/Application Support |
| 152 | Cache: /Users/poul/Library/Caches |
| 153 | Documents: /Users/poul/Documents |
| 154 | Desktop: /Users/poul/Desktop |
| 155 | Pictures: /Users/poul/Pictures |
| 156 | Music: /Users/poul/Music |
| 157 | Video: /Users/poul/Movies |
| 158 | Download: /Users/poul/Downloads |
| 159 | Save Games 1: /Users/poul/Library/Application Support |
| 160 | Save Games 2: /Users/poul/Library/Application Support |
| 161 | ``` |
| 162 | |
| 163 | ## Compiler Compatibility |
| 164 | |
| 165 | Versions up to 3.X.X should compile with any C++98 compiler.\ |
| 166 | Versions from 4.0.0 and up require a C++11 compatible compiler. |
| 167 | |
| 168 | The aim is to always support the default C++ compiler on the oldest supported version of Ubuntu. This is a very basic library and it is not supposed to force you to upgrade. |
| 169 | |
| 170 | ## Encoding |
| 171 | |
| 172 | From version 3.0, Windows always encodes to UTF-8, and this will be the default on almost any other system. |
| 173 | Before version 3.0, Windows was encoded in ANSI. |
| 174 | Although the user may use any characters they want, I recommend that the program should have only ASCII characters in the source code itself. |
| 175 | |
| 176 | # Licence |
| 177 | |
| 178 | Provided under the MIT license for the same reason XDG is licensed under it. So that you can quickly copy-paste the methods you need or just include the "sago"-folder. |