git repos / PlatformFolders

blame: README.md

normal view · raw

80e52811 sum01 2018-05-04 17:14 1
# PlatformFolders [![Build Status](https://travis-ci.org/sago007/PlatformFolders.svg?branch=master)](https://travis-ci.org/sago007/PlatformFolders) [![AppVeyor](https://img.shields.io/appveyor/ci/sago007/PlatformFolders.svg?label=Windows)](https://ci.appveyor.com/project/sago007/platformfolders) [![license](https://img.shields.io/github/license/sago007/PlatformFolders.svg)](https://raw.githubusercontent.com/sago007/PlatformFolders/master/LICENSE) [![Join the chat at https://gitter.im/PlatformFolders/Lobby](https://badges.gitter.im/PlatformFolders/Lobby.svg)](https://gitter.im/PlatformFolders/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/729e36adcf5c4523bd136de1b33441cb)](https://www.codacy.com/app/github_43/PlatformFolders?utm_source=github.com&utm_medium=referral&utm_content=sago007/PlatformFolders&utm_campaign=Badge_Grade)
541df045 sum01 2018-04-20 03:21 2
0863f4b8 sum01 2018-04-26 21:26 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
a337b4fd sago007 2015-09-19 19:47 4
14f997e9 Poul Sander 2021-08-08 15:23 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/)
3b191916 sago007 2016-02-16 17:08 6
0863f4b8 sum01 2018-04-26 21:26 7
## Rationale
4f278f37 Poul Sander 2016-09-06 19:10 8
a337b4fd sago007 2015-09-19 19:47 9
There are a lot of platform abstraction libraries available. You can get graphics abstraction libraries, GUI abstraction libraries and file abstraction libraries.
a337b4fd sago007 2015-09-19 19:47 10
a337b4fd sago007 2015-09-19 19:47 11
But folder abstraction seems to be more difficult.
84975644 Poul Sander 2025-12-10 16:34 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.
a337b4fd sago007 2015-09-19 19:47 13
22fc4b25 Poul Sander 2021-07-07 20:26 14
[I have written a bit more about it here.](https://sago007.blogspot.dk/2015/10/abstraction-for-special-folders.html)
17b8cd4f Poul Sander 2015-10-26 18:07 15
463ec450 Poul Sander 2015-10-27 19:07 16
There are some alternatives that you might consider instead:
0863f4b8 sum01 2018-04-26 21:26 17
22fc4b25 Poul Sander 2021-07-07 20:26 18
* [QStandardPaths](https://doc.qt.io/qt-5/qstandardpaths.html)
0863f4b8 sum01 2018-04-26 21:26 19
* [glib](https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html)
463ec450 Poul Sander 2015-10-27 19:07 20
84975644 Poul Sander 2025-12-10 16:34 21
Both are probably 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.
463ec450 Poul Sander 2015-10-27 19:07 22
0863f4b8 sum01 2018-04-26 21:26 23
## Operating System Support
0863f4b8 sum01 2018-04-26 21:26 24
0863f4b8 sum01 2018-04-26 21:26 25
### Windows
0863f4b8 sum01 2018-04-26 21:26 26
66a26d0b sago007 2018-06-11 16:00 27
For Windows, the current version fetches the folders using SHGetKnownFolderPath. This requires Vista or newer.
0863f4b8 sum01 2018-04-26 21:26 28
0863f4b8 sum01 2018-04-26 21:26 29
### Linux
0863f4b8 sum01 2018-04-26 21:26 30
0863f4b8 sum01 2018-04-26 21:26 31
This library uses the [XDG user-dirs.](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
0863f4b8 sum01 2018-04-26 21:26 32
0863f4b8 sum01 2018-04-26 21:26 33
It should work on any Unix system that has the following headers available: `pwd.h`, `unistd.h`, and `sys/types.h`
0863f4b8 sum01 2018-04-26 21:26 34
0863f4b8 sum01 2018-04-26 21:26 35
### macOS
0863f4b8 sum01 2018-04-26 21:26 36
84975644 Poul Sander 2025-12-10 16:34 37
Version 4.0.0 and onwards use 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.
84975644 Poul Sander 2025-12-10 16:34 38
The versions 2.X and 3.X use the deprecated FSFindFolder, which requires the CoreServices framework during linking.
84975644 Poul Sander 2025-12-10 16:34 39
Version 1.X simply used the XDG specification.
0863f4b8 sum01 2018-04-26 21:26 40
0863f4b8 sum01 2018-04-26 21:26 41
## Usage
0863f4b8 sum01 2018-04-26 21:26 42
0863f4b8 sum01 2018-04-26 21:26 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.
0863f4b8 sum01 2018-04-26 21:26 44
0863f4b8 sum01 2018-04-26 21:26 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).
0863f4b8 sum01 2018-04-26 21:26 46
0863f4b8 sum01 2018-04-26 21:26 47
```cmake
f0488f27 sum01 2019-03-06 19:12 48
# Specifying a version is optional -- note it follows Semver
0863f4b8 sum01 2018-04-26 21:26 49
find_package(platform_folders 3.1.0 REQUIRED)
f0488f27 sum01 2019-03-06 19:12 50
# Which imports the linkable library "sago::platform_folders"
0863f4b8 sum01 2018-04-26 21:26 51
# Use it like so...
0863f4b8 sum01 2018-04-26 21:26 52
target_link_libraries(EXEORLIBNAME PRIVATE sago::platform_folders)
0863f4b8 sum01 2018-04-26 21:26 53
```
0863f4b8 sum01 2018-04-26 21:26 54
0863f4b8 sum01 2018-04-26 21:26 55
Alternatively, you can just copy the [sago](https://github.com/sago007/PlatformFolders/tree/master/sago) folder into your program and manually link everything.
ed93318e Poul Sander 2018-06-24 11:20 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.
6284bc96 sago007 2018-04-18 20:16 57
5b487573 sum01 2019-03-06 19:14 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.
5b487573 sum01 2019-03-06 19:14 59
0863f4b8 sum01 2018-04-26 21:26 60
### Building
6284bc96 sago007 2018-04-18 20:16 61
0863f4b8 sum01 2018-04-26 21:26 62
**Notes:**
a337b4fd sago007 2015-09-19 19:47 63
ed93318e Poul Sander 2018-06-24 11:20 64
* Until 4.0.0 macOS required the CoreServices framework during linking.
0863f4b8 sum01 2018-04-26 21:26 65
* If you don't want to install, remove the `--target install` command.
f16cdf4f sago007 2015-10-26 17:37 66
0863f4b8 sum01 2018-04-26 21:26 67
Linux/macOS:
a337b4fd sago007 2015-09-19 19:47 68
0863f4b8 sum01 2018-04-26 21:26 69
```
0863f4b8 sum01 2018-04-26 21:26 70
mkdir -p build && cd build
0863f4b8 sum01 2018-04-26 21:26 71
cmake -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release ..
0863f4b8 sum01 2018-04-26 21:26 72
sudo cmake --build . --target install
0863f4b8 sum01 2018-04-26 21:26 73
```
a337b4fd sago007 2015-09-19 19:47 74
0863f4b8 sum01 2018-04-26 21:26 75
Windows:
3984e19e sago007 2015-09-20 11:12 76
1cbbe99f Poul Sander 2018-04-17 16:13 77
```
0863f4b8 sum01 2018-04-26 21:26 78
mkdir build && cd build
0863f4b8 sum01 2018-04-26 21:26 79
cmake -DBUILD_TESTING=OFF ..
0863f4b8 sum01 2018-04-26 21:26 80
runas /user:Administrator "cmake --build . --config Release --target install"
1cbbe99f Poul Sander 2018-04-17 16:13 81
```
1cbbe99f Poul Sander 2018-04-17 16:13 82
0863f4b8 sum01 2018-04-26 21:26 83
## Example Usage
a337b4fd sago007 2015-09-19 19:47 84
a337b4fd sago007 2015-09-19 19:47 85
This sample program gets all folders from the system:
a337b4fd sago007 2015-09-19 19:47 86
0863f4b8 sum01 2018-04-26 21:26 87
```cpp
0863f4b8 sum01 2018-04-26 21:26 88
#include <sago/platform_folders.h>
0863f4b8 sum01 2018-04-26 21:26 89
#include <iostream>
0863f4b8 sum01 2018-04-26 21:26 90
#include <string>
a337b4fd sago007 2015-09-19 19:47 91
a337b4fd sago007 2015-09-19 19:47 92
int main()
a337b4fd sago007 2015-09-19 19:47 93
{
66a26d0b sago007 2018-06-11 16:00 94
	std::cout << "Config: " << sago::getConfigHome() << "\n";
66a26d0b sago007 2018-06-11 16:00 95
	std::cout << "Data: " << sago::getDataHome() << "\n";
46675c1a Poul Sander 2022-01-23 14:46 96
	std::cout << "State: " << sago::getStateDir() << "\n";
66a26d0b sago007 2018-06-11 16:00 97
	std::cout << "Cache: " << sago::getCacheDir() << "\n";
66a26d0b sago007 2018-06-11 16:00 98
	std::cout << "Documents: " << sago::getDocumentsFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 99
	std::cout << "Desktop: " << sago::getDesktopFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 100
	std::cout << "Pictures: " << sago::getPicturesFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 101
	std::cout << "Music: " << sago::getMusicFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 102
	std::cout << "Video: " << sago::getVideoFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 103
	std::cout << "Download: " << sago::getDownloadFolder() << "\n";
66a26d0b sago007 2018-06-11 16:00 104
	std::cout << "Save Games 1: " << sago::getSaveGamesFolder1() << "\n";
66a26d0b sago007 2018-06-11 16:00 105
	std::cout << "Save Games 2: " << sago::getSaveGamesFolder2() << "\n";
a337b4fd sago007 2015-09-19 19:47 106
	return 0;
a337b4fd sago007 2015-09-19 19:47 107
}
a337b4fd sago007 2015-09-19 19:47 108
```
a337b4fd sago007 2015-09-19 19:47 109
0863f4b8 sum01 2018-04-26 21:26 110
### Example Output
0863f4b8 sum01 2018-04-26 21:26 111
0863f4b8 sum01 2018-04-26 21:26 112
#### On Linux
0863f4b8 sum01 2018-04-26 21:26 113
a337b4fd sago007 2015-09-19 19:47 114
```
a337b4fd sago007 2015-09-19 19:47 115
Config: /home/poul/.config
a337b4fd sago007 2015-09-19 19:47 116
Data: /home/poul/.local/share
46675c1a Poul Sander 2022-01-23 14:46 117
State: /home/poul/.local/state
a337b4fd sago007 2015-09-19 19:47 118
Cache: /home/poul/.cache
a337b4fd sago007 2015-09-19 19:47 119
Documents: /home/poul/Dokumenter
a337b4fd sago007 2015-09-19 19:47 120
Desktop: /home/poul/Skrivebord
a337b4fd sago007 2015-09-19 19:47 121
Pictures: /home/poul/Billeder
a337b4fd sago007 2015-09-19 19:47 122
Music: /home/poul/Musik
a337b4fd sago007 2015-09-19 19:47 123
Video: /home/poul/Videoklip
a337b4fd sago007 2015-09-19 19:47 124
Download: /home/poul/Hentede filer
a337b4fd sago007 2015-09-19 19:47 125
Save Games 1: /home/poul/.local/share
66a26d0b sago007 2018-06-11 16:00 126
Save Games 2: /home/poul/.local/share
a337b4fd sago007 2015-09-19 19:47 127
```
a337b4fd sago007 2015-09-19 19:47 128
0863f4b8 sum01 2018-04-26 21:26 129
#### On Windows
0863f4b8 sum01 2018-04-26 21:26 130
63627d7a sago007 2015-09-20 18:08 131
```
63627d7a sago007 2015-09-20 18:08 132
Config: C:\users\poul\Application Data
63627d7a sago007 2015-09-20 18:08 133
Data: C:\users\poul\Application Data
46675c1a Poul Sander 2022-01-23 14:46 134
State: C:\users\poul\Local Settings\Application Data
63627d7a sago007 2015-09-20 18:08 135
Cache: C:\users\poul\Local Settings\Application Data
63627d7a sago007 2015-09-20 18:08 136
Documents: C:\users\poul\Mine dokumenter
63627d7a sago007 2015-09-20 18:08 137
Desktop: C:\users\poul\Skrivebord
63627d7a sago007 2015-09-20 18:08 138
Pictures: C:\users\poul\Mine Billeder
63627d7a sago007 2015-09-20 18:08 139
Music: C:\users\poul\Min Musik
63627d7a sago007 2015-09-20 18:08 140
Video: C:\users\poul\Mine Film
66a26d0b sago007 2018-06-11 16:00 141
Download: C:\users\poul\Downloads
63627d7a sago007 2015-09-20 18:08 142
Save Games 1: C:\users\poul\Mine dokumenter\My Games
66a26d0b sago007 2018-06-11 16:00 143
Save Games 2: C:\users\poul\Saved Games
63627d7a sago007 2015-09-20 18:08 144
```
63627d7a sago007 2015-09-20 18:08 145
0863f4b8 sum01 2018-04-26 21:26 146
#### On macOS
0863f4b8 sum01 2018-04-26 21:26 147
f16cdf4f sago007 2015-10-26 17:37 148
```
f16cdf4f sago007 2015-10-26 17:37 149
Config: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 150
Data: /Users/poul/Library/Application Support
46675c1a Poul Sander 2022-01-23 14:46 151
State: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 152
Cache: /Users/poul/Library/Caches
f16cdf4f sago007 2015-10-26 17:37 153
Documents: /Users/poul/Documents
f16cdf4f sago007 2015-10-26 17:37 154
Desktop: /Users/poul/Desktop
f16cdf4f sago007 2015-10-26 17:37 155
Pictures: /Users/poul/Pictures
f16cdf4f sago007 2015-10-26 17:37 156
Music: /Users/poul/Music
f16cdf4f sago007 2015-10-26 17:37 157
Video: /Users/poul/Movies
f16cdf4f sago007 2015-10-26 17:37 158
Download: /Users/poul/Downloads
f16cdf4f sago007 2015-10-26 17:37 159
Save Games 1: /Users/poul/Library/Application Support
66a26d0b sago007 2018-06-11 16:00 160
Save Games 2: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 161
```
f16cdf4f sago007 2015-10-26 17:37 162
0863f4b8 sum01 2018-04-26 21:26 163
## Compiler Compatibility
0863f4b8 sum01 2018-04-26 21:26 164
0863f4b8 sum01 2018-04-26 21:26 165
Versions up to 3.X.X should compile with any C++98 compiler.\
0863f4b8 sum01 2018-04-26 21:26 166
Versions from 4.0.0 and up require a C++11 compatible compiler.
1cbbe99f Poul Sander 2018-04-17 16:13 167
1cbbe99f Poul Sander 2018-04-17 16:13 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.
1cbbe99f Poul Sander 2018-04-17 16:13 169
0863f4b8 sum01 2018-04-26 21:26 170
## Encoding
0863f4b8 sum01 2018-04-26 21:26 171
0863f4b8 sum01 2018-04-26 21:26 172
From version 3.0, Windows always encodes to UTF-8, and this will be the default on almost any other system.
0863f4b8 sum01 2018-04-26 21:26 173
Before version 3.0, Windows was encoded in ANSI.
0863f4b8 sum01 2018-04-26 21:26 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.
63627d7a sago007 2015-09-20 18:08 175
84975644 Poul Sander 2025-12-10 16:34 176
# License
0863f4b8 sum01 2018-04-26 21:26 177
6284bc96 sago007 2018-04-18 20:16 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.
1970-01-01 00:00 179