git repos / PlatformFolders

blame: README.md

normal view · raw

dff1dd87 sum01 2018-04-26 21:26 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)
d0a145ba sago007 2018-04-18 18:32 2
dff1dd87 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
d0a145ba sago007 2018-04-18 18:32 4
dff1dd87 sum01 2018-04-26 21:26 5
[Source code](https://github.com/sago007/PlatformFolders) • [Latest release](https://github.com/sago007/PlatformFolders/releases/latest) • [Doxygen documentation](http://sago007.github.io/PlatformFolders/html/doxygen/)
dff1dd87 sum01 2018-04-26 21:26 6
dff1dd87 sum01 2018-04-26 21:26 7
## Rationale
d0a145ba sago007 2018-04-18 18:32 8
d0a145ba sago007 2018-04-18 18:32 9
There are a lot of platform abstraction libraries available. You can get graphics abstraction libraries, GUI abstraction libraries and file abstraction libraries.
d0a145ba sago007 2018-04-18 18:32 10
d0a145ba sago007 2018-04-18 18:32 11
But folder abstraction seems to be more difficult.
d0a145ba sago007 2018-04-18 18:32 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.
d0a145ba sago007 2018-04-18 18:32 13
dff1dd87 sum01 2018-04-26 21:26 14
[I have written a bit more about it here.](http://sago007.blogspot.dk/2015/10/abstraction-for-special-folders.html)
d0a145ba sago007 2018-04-18 18:32 15
d0a145ba sago007 2018-04-18 18:32 16
There are some alternatives that you might consider instead:
dff1dd87 sum01 2018-04-26 21:26 17
dff1dd87 sum01 2018-04-26 21:26 18
* [QStandardPaths](http://doc.qt.io/qt-5/qstandardpaths.html)
dff1dd87 sum01 2018-04-26 21:26 19
* [glib](https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html)
d0a145ba sago007 2018-04-18 18:32 20
d0a145ba sago007 2018-04-18 18:32 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.
d0a145ba sago007 2018-04-18 18:32 22
dff1dd87 sum01 2018-04-26 21:26 23
## Operating System Support
23c151bc sago007 2018-04-18 19:03 24
dff1dd87 sum01 2018-04-26 21:26 25
### Windows
23c151bc sago007 2018-04-18 19:03 26
dff1dd87 sum01 2018-04-26 21:26 27
For Windows, the folders are fetched using SHGetFolderPath.
23c151bc sago007 2018-04-18 19:03 28
dff1dd87 sum01 2018-04-26 21:26 29
The amount of supported folders differ from Windows version and this library targets XP and newer.\
dff1dd87 sum01 2018-04-26 21:26 30
XP support will be dropped very soon.
d0a145ba sago007 2018-04-18 18:32 31
dff1dd87 sum01 2018-04-26 21:26 32
"Save Games" and "Downloads" should not be used, as they are undefined on XP.
d0a145ba sago007 2018-04-18 18:32 33
dff1dd87 sum01 2018-04-26 21:26 34
### Linux
d0a145ba sago007 2018-04-18 18:32 35
dff1dd87 sum01 2018-04-26 21:26 36
This library uses the [XDG user-dirs.](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
d0a145ba sago007 2018-04-18 18:32 37
dff1dd87 sum01 2018-04-26 21:26 38
It should work on any Unix system that has the following headers available: `pwd.h`, `unistd.h`, and `sys/types.h`
d0a145ba sago007 2018-04-18 18:32 39
dff1dd87 sum01 2018-04-26 21:26 40
### macOS
d0a145ba sago007 2018-04-18 18:32 41
dff1dd87 sum01 2018-04-26 21:26 42
Uses the deprecated FSFindFolder (there is no C++ alternative), which requires the CoreServices framework during linking.
dff1dd87 sum01 2018-04-26 21:26 43
dff1dd87 sum01 2018-04-26 21:26 44
## Usage
dff1dd87 sum01 2018-04-26 21:26 45
dff1dd87 sum01 2018-04-26 21:26 46
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.
dff1dd87 sum01 2018-04-26 21:26 47
dff1dd87 sum01 2018-04-26 21:26 48
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).
dff1dd87 sum01 2018-04-26 21:26 49
dff1dd87 sum01 2018-04-26 21:26 50
```cmake
dff1dd87 sum01 2018-04-26 21:26 51
# Specifying a version is optional -- note it follows by Semver
dff1dd87 sum01 2018-04-26 21:26 52
find_package(platform_folders 3.1.0 REQUIRED)
dff1dd87 sum01 2018-04-26 21:26 53
# Which creates the IMPORTED lib "sago::platform_folders"
dff1dd87 sum01 2018-04-26 21:26 54
# Use it like so...
dff1dd87 sum01 2018-04-26 21:26 55
target_link_libraries(EXEORLIBNAME PRIVATE sago::platform_folders)
d0a145ba sago007 2018-04-18 18:32 56
```
d0a145ba sago007 2018-04-18 18:32 57
dff1dd87 sum01 2018-04-26 21:26 58
Alternatively, you can just copy the [sago](https://github.com/sago007/PlatformFolders/tree/master/sago) folder into your program and manually link everything.
dff1dd87 sum01 2018-04-26 21:26 59
dff1dd87 sum01 2018-04-26 21:26 60
### Building
dff1dd87 sum01 2018-04-26 21:26 61
dff1dd87 sum01 2018-04-26 21:26 62
**Notes:**
dff1dd87 sum01 2018-04-26 21:26 63
dff1dd87 sum01 2018-04-26 21:26 64
* macOS requires the CoreServices framework during linking.
dff1dd87 sum01 2018-04-26 21:26 65
* If you don't want to install, remove the `--target install` command.
dff1dd87 sum01 2018-04-26 21:26 66
dff1dd87 sum01 2018-04-26 21:26 67
Linux/macOS:
dff1dd87 sum01 2018-04-26 21:26 68
dff1dd87 sum01 2018-04-26 21:26 69
```
dff1dd87 sum01 2018-04-26 21:26 70
mkdir -p build && cd build
dff1dd87 sum01 2018-04-26 21:26 71
cmake -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release ..
dff1dd87 sum01 2018-04-26 21:26 72
sudo cmake --build . --target install
dff1dd87 sum01 2018-04-26 21:26 73
```
dff1dd87 sum01 2018-04-26 21:26 74
dff1dd87 sum01 2018-04-26 21:26 75
Windows:
dff1dd87 sum01 2018-04-26 21:26 76
dff1dd87 sum01 2018-04-26 21:26 77
```
dff1dd87 sum01 2018-04-26 21:26 78
mkdir build && cd build
dff1dd87 sum01 2018-04-26 21:26 79
cmake -DBUILD_TESTING=OFF ..
dff1dd87 sum01 2018-04-26 21:26 80
runas /user:Administrator "cmake --build . --config Release --target install"
dff1dd87 sum01 2018-04-26 21:26 81
```
dff1dd87 sum01 2018-04-26 21:26 82
dff1dd87 sum01 2018-04-26 21:26 83
## Example Usage
dff1dd87 sum01 2018-04-26 21:26 84
dff1dd87 sum01 2018-04-26 21:26 85
This sample program gets all folders from the system:
dff1dd87 sum01 2018-04-26 21:26 86
dff1dd87 sum01 2018-04-26 21:26 87
```cpp
dff1dd87 sum01 2018-04-26 21:26 88
#include <sago/platform_folders.h>
dff1dd87 sum01 2018-04-26 21:26 89
#include <iostream>
dff1dd87 sum01 2018-04-26 21:26 90
#include <string>
d0a145ba sago007 2018-04-18 18:32 91
d0a145ba sago007 2018-04-18 18:32 92
int main()
d0a145ba sago007 2018-04-18 18:32 93
{
dff1dd87 sum01 2018-04-26 21:26 94
	using std::cout;
dff1dd87 sum01 2018-04-26 21:26 95
dff1dd87 sum01 2018-04-26 21:26 96
	cout << "Config: " << sago::getConfigHome() << '\n';
dff1dd87 sum01 2018-04-26 21:26 97
	cout << "Data: " << sago::getDataHome() << '\n';
dff1dd87 sum01 2018-04-26 21:26 98
	cout << "Cache: " << sago::getCacheDir() << '\n';
d0a145ba sago007 2018-04-18 18:32 99
	sago::PlatformFolders p;
d0a145ba sago007 2018-04-18 18:32 100
	cout << "Documents: " << p.getDocumentsFolder() << "\n";
d0a145ba sago007 2018-04-18 18:32 101
	cout << "Desktop: " << p.getDesktopFolder() << "\n";
d0a145ba sago007 2018-04-18 18:32 102
	cout << "Pictures: " << p.getPicturesFolder() << "\n";
d0a145ba sago007 2018-04-18 18:32 103
	cout << "Music: " << p.getMusicFolder() << "\n";
d0a145ba sago007 2018-04-18 18:32 104
	cout << "Video: " << p.getVideoFolder() << "\n";
d0a145ba sago007 2018-04-18 18:32 105
	cout << "Download: " << p.getDownloadFolder1() << "\n";
d0a145ba sago007 2018-04-18 18:32 106
	cout << "Save Games 1: " << p.getSaveGamesFolder1() << "\n";
d0a145ba sago007 2018-04-18 18:32 107
	return 0;
d0a145ba sago007 2018-04-18 18:32 108
}
d0a145ba sago007 2018-04-18 18:32 109
```
d0a145ba sago007 2018-04-18 18:32 110
dff1dd87 sum01 2018-04-26 21:26 111
### Example Output
dff1dd87 sum01 2018-04-26 21:26 112
dff1dd87 sum01 2018-04-26 21:26 113
#### On Linux
dff1dd87 sum01 2018-04-26 21:26 114
d0a145ba sago007 2018-04-18 18:32 115
```
d0a145ba sago007 2018-04-18 18:32 116
Config: /home/poul/.config
d0a145ba sago007 2018-04-18 18:32 117
Data: /home/poul/.local/share
d0a145ba sago007 2018-04-18 18:32 118
Cache: /home/poul/.cache
d0a145ba sago007 2018-04-18 18:32 119
Documents: /home/poul/Dokumenter
d0a145ba sago007 2018-04-18 18:32 120
Desktop: /home/poul/Skrivebord
d0a145ba sago007 2018-04-18 18:32 121
Pictures: /home/poul/Billeder
d0a145ba sago007 2018-04-18 18:32 122
Music: /home/poul/Musik
d0a145ba sago007 2018-04-18 18:32 123
Video: /home/poul/Videoklip
d0a145ba sago007 2018-04-18 18:32 124
Download: /home/poul/Hentede filer
d0a145ba sago007 2018-04-18 18:32 125
Save Games 1: /home/poul/.local/share
d0a145ba sago007 2018-04-18 18:32 126
```
d0a145ba sago007 2018-04-18 18:32 127
dff1dd87 sum01 2018-04-26 21:26 128
#### On Windows
dff1dd87 sum01 2018-04-26 21:26 129
d0a145ba sago007 2018-04-18 18:32 130
```
d0a145ba sago007 2018-04-18 18:32 131
Config: C:\users\poul\Application Data
d0a145ba sago007 2018-04-18 18:32 132
Data: C:\users\poul\Application Data
d0a145ba sago007 2018-04-18 18:32 133
Cache: C:\users\poul\Local Settings\Application Data
d0a145ba sago007 2018-04-18 18:32 134
Documents: C:\users\poul\Mine dokumenter
d0a145ba sago007 2018-04-18 18:32 135
Desktop: C:\users\poul\Skrivebord
d0a145ba sago007 2018-04-18 18:32 136
Pictures: C:\users\poul\Mine Billeder
d0a145ba sago007 2018-04-18 18:32 137
Music: C:\users\poul\Min Musik
d0a145ba sago007 2018-04-18 18:32 138
Video: C:\users\poul\Mine Film
d0a145ba sago007 2018-04-18 18:32 139
Download: C:\users\poul\Skrivebord
d0a145ba sago007 2018-04-18 18:32 140
Save Games 1: C:\users\poul\Mine dokumenter\My Games
d0a145ba sago007 2018-04-18 18:32 141
```
d0a145ba sago007 2018-04-18 18:32 142
dff1dd87 sum01 2018-04-26 21:26 143
#### On macOS
dff1dd87 sum01 2018-04-26 21:26 144
d0a145ba sago007 2018-04-18 18:32 145
```
d0a145ba sago007 2018-04-18 18:32 146
Config: /Users/poul/Library/Application Support
d0a145ba sago007 2018-04-18 18:32 147
Data: /Users/poul/Library/Application Support
d0a145ba sago007 2018-04-18 18:32 148
Cache: /Users/poul/Library/Caches
d0a145ba sago007 2018-04-18 18:32 149
Documents: /Users/poul/Documents
d0a145ba sago007 2018-04-18 18:32 150
Desktop: /Users/poul/Desktop
d0a145ba sago007 2018-04-18 18:32 151
Pictures: /Users/poul/Pictures
d0a145ba sago007 2018-04-18 18:32 152
Music: /Users/poul/Music
d0a145ba sago007 2018-04-18 18:32 153
Video: /Users/poul/Movies
d0a145ba sago007 2018-04-18 18:32 154
Download: /Users/poul/Downloads
d0a145ba sago007 2018-04-18 18:32 155
Save Games 1: /Users/poul/Library/Application Support
d0a145ba sago007 2018-04-18 18:32 156
```
d0a145ba sago007 2018-04-18 18:32 157
dff1dd87 sum01 2018-04-26 21:26 158
## Compiler Compatibility
dff1dd87 sum01 2018-04-26 21:26 159
dff1dd87 sum01 2018-04-26 21:26 160
Versions up to 3.X.X should compile with any C++98 compiler.\
dff1dd87 sum01 2018-04-26 21:26 161
Versions from 4.0.0 and up require a C++11 compatible compiler.
dff1dd87 sum01 2018-04-26 21:26 162
dff1dd87 sum01 2018-04-26 21:26 163
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.
dff1dd87 sum01 2018-04-26 21:26 164
dff1dd87 sum01 2018-04-26 21:26 165
## Encoding
dff1dd87 sum01 2018-04-26 21:26 166
dff1dd87 sum01 2018-04-26 21:26 167
From version 3.0, Windows always encodes to UTF-8, and this will be the default on almost any other system.
dff1dd87 sum01 2018-04-26 21:26 168
Before version 3.0, Windows was encoded in ANSI.
dff1dd87 sum01 2018-04-26 21:26 169
Although the user may use any characters they want, I recommend that the program should have only ASCII characters in the source code itself.
d0a145ba sago007 2018-04-18 18:32 170
d0a145ba sago007 2018-04-18 18:32 171
# Licence
dff1dd87 sum01 2018-04-26 21:26 172
dff1dd87 sum01 2018-04-26 21:26 173
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 174