git repos / PlatformFolders

blame: README.md

normal view · raw

5ce952c2 Poul Sander 2015-10-26 17:50 1
# PlatformFolders [![Build Status](https://travis-ci.org/sago007/PlatformFolders.svg?branch=master)](https://travis-ci.org/sago007/PlatformFolders)
f16cdf4f sago007 2015-10-26 17:37 2
A self contained C++ abstraction library so that you do not need to have Linux, Windows and Mac OS X specific code to look for special directories
a337b4fd sago007 2015-09-19 19:47 3
3b191916 sago007 2016-02-16 17:08 4
Can be found at: https://github.com/sago007/PlatformFolders
3b191916 sago007 2016-02-16 17:08 5
a337b4fd sago007 2015-09-19 19:47 6
# Rationale
a337b4fd sago007 2015-09-19 19:47 7
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 8
a337b4fd sago007 2015-09-19 19:47 9
But folder abstraction seems to be more difficult.
a337b4fd sago007 2015-09-19 19:47 10
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 11
17b8cd4f Poul Sander 2015-10-26 18:07 12
I have written a bit more about it here: http://sago007.blogspot.dk/2015/10/abstraction-for-special-folders.html
17b8cd4f Poul Sander 2015-10-26 18:07 13
463ec450 Poul Sander 2015-10-27 19:07 14
There are some alternatives that you might consider instead:
463ec450 Poul Sander 2015-10-27 19:07 15
  * QStandardPaths - http://doc.qt.io/qt-5/qstandardpaths.html 
463ec450 Poul Sander 2015-10-27 19:07 16
  * glib - https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html
463ec450 Poul Sander 2015-10-27 19:07 17
463ec450 Poul Sander 2015-10-27 19:07 18
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. 
463ec450 Poul Sander 2015-10-27 19:07 19
a337b4fd sago007 2015-09-19 19:47 20
# Windows support
a337b4fd sago007 2015-09-19 19:47 21
For Windows the folders are fetched using SHGetFolderPath.
a337b4fd sago007 2015-09-19 19:47 22
The amount of supported folders differ from Windows version and this library targets XP and newer... and I'll drop XP support very soon. 
a337b4fd sago007 2015-09-19 19:47 23
Currently "Save Games" and "Downloads" should not be used as they are undefined on XP. 
a337b4fd sago007 2015-09-19 19:47 24
f16cdf4f sago007 2015-10-26 17:37 25
a337b4fd sago007 2015-09-19 19:47 26
# Linux support
a337b4fd sago007 2015-09-19 19:47 27
In Linux a lot of these folders are not official defined. However this library uses XDG user dirs.
a337b4fd sago007 2015-09-19 19:47 28
a337b4fd sago007 2015-09-19 19:47 29
# Mac OS X support
f16cdf4f sago007 2015-10-26 17:37 30
Uses the deprecated FSFindFolder (there is no C++ alternative). It requires "-framework CoreServices" during linking.
a337b4fd sago007 2015-09-19 19:47 31
3984e19e sago007 2015-09-20 11:12 32
# Usage
3984e19e sago007 2015-09-20 11:12 33
Copy "sago/platform_files.cpp" and "sago/platform_fildes.h" to your program and make sure that the cpp file is compiled and linked.
3984e19e sago007 2015-09-20 11:12 34
a337b4fd sago007 2015-09-19 19:47 35
# Hello World
a337b4fd sago007 2015-09-19 19:47 36
a337b4fd sago007 2015-09-19 19:47 37
This sample program gets all folders from the system:
a337b4fd sago007 2015-09-19 19:47 38
```
a337b4fd sago007 2015-09-19 19:47 39
#include <iostream>
a337b4fd sago007 2015-09-19 19:47 40
#include "sago/platform_folders.h"
a337b4fd sago007 2015-09-19 19:47 41
a337b4fd sago007 2015-09-19 19:47 42
using namespace std;
a337b4fd sago007 2015-09-19 19:47 43
using namespace sago;
a337b4fd sago007 2015-09-19 19:47 44
a337b4fd sago007 2015-09-19 19:47 45
int main()
a337b4fd sago007 2015-09-19 19:47 46
{
a337b4fd sago007 2015-09-19 19:47 47
	cout << "Config: " << getConfigHome() << endl;
a337b4fd sago007 2015-09-19 19:47 48
	cout << "Data: " << getDataHome() << endl;
a337b4fd sago007 2015-09-19 19:47 49
	cout << "Cache: " << getCacheDir() << endl;
a337b4fd sago007 2015-09-19 19:47 50
	PlatformFolders p;
a337b4fd sago007 2015-09-19 19:47 51
	cout << "Documents: " << p.getDocumentsFolder() << endl;
a337b4fd sago007 2015-09-19 19:47 52
	cout << "Desktop: " << p.getDesktopFolder() << endl;
a337b4fd sago007 2015-09-19 19:47 53
	cout << "Pictures: " << p.getPicturesFolder() << endl;
a337b4fd sago007 2015-09-19 19:47 54
	cout << "Music: " << p.getMusicFolder() << endl;
a337b4fd sago007 2015-09-19 19:47 55
	cout << "Video: " << p.getVideoFolder() << endl;
a337b4fd sago007 2015-09-19 19:47 56
	cout << "Download: " << p.getDownloadFolder1() << endl;
a337b4fd sago007 2015-09-19 19:47 57
	cout << "Save Games 1: " << p.getSaveGamesFolder1() << endl;
a337b4fd sago007 2015-09-19 19:47 58
	return 0;
a337b4fd sago007 2015-09-19 19:47 59
}
a337b4fd sago007 2015-09-19 19:47 60
```
a337b4fd sago007 2015-09-19 19:47 61
a337b4fd sago007 2015-09-19 19:47 62
The output on Linux would look like this:
a337b4fd sago007 2015-09-19 19:47 63
```
a337b4fd sago007 2015-09-19 19:47 64
Config: /home/poul/.config
a337b4fd sago007 2015-09-19 19:47 65
Data: /home/poul/.local/share
a337b4fd sago007 2015-09-19 19:47 66
Cache: /home/poul/.cache
a337b4fd sago007 2015-09-19 19:47 67
Documents: /home/poul/Dokumenter
a337b4fd sago007 2015-09-19 19:47 68
Desktop: /home/poul/Skrivebord
a337b4fd sago007 2015-09-19 19:47 69
Pictures: /home/poul/Billeder
a337b4fd sago007 2015-09-19 19:47 70
Music: /home/poul/Musik
a337b4fd sago007 2015-09-19 19:47 71
Video: /home/poul/Videoklip
a337b4fd sago007 2015-09-19 19:47 72
Download: /home/poul/Hentede filer
a337b4fd sago007 2015-09-19 19:47 73
Save Games 1: /home/poul/.local/share
a337b4fd sago007 2015-09-19 19:47 74
```
a337b4fd sago007 2015-09-19 19:47 75
63627d7a sago007 2015-09-20 18:08 76
On Windows it could be:
63627d7a sago007 2015-09-20 18:08 77
```
63627d7a sago007 2015-09-20 18:08 78
Config: C:\users\poul\Application Data
63627d7a sago007 2015-09-20 18:08 79
Data: C:\users\poul\Application Data
63627d7a sago007 2015-09-20 18:08 80
Cache: C:\users\poul\Local Settings\Application Data
63627d7a sago007 2015-09-20 18:08 81
Documents: C:\users\poul\Mine dokumenter
63627d7a sago007 2015-09-20 18:08 82
Desktop: C:\users\poul\Skrivebord
63627d7a sago007 2015-09-20 18:08 83
Pictures: C:\users\poul\Mine Billeder
63627d7a sago007 2015-09-20 18:08 84
Music: C:\users\poul\Min Musik
63627d7a sago007 2015-09-20 18:08 85
Video: C:\users\poul\Mine Film
63627d7a sago007 2015-09-20 18:08 86
Download: C:\users\poul\Skrivebord
63627d7a sago007 2015-09-20 18:08 87
Save Games 1: C:\users\poul\Mine dokumenter\My Games
63627d7a sago007 2015-09-20 18:08 88
```
63627d7a sago007 2015-09-20 18:08 89
f16cdf4f sago007 2015-10-26 17:37 90
On Mac OS X it could be:
f16cdf4f sago007 2015-10-26 17:37 91
```
f16cdf4f sago007 2015-10-26 17:37 92
Config: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 93
Data: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 94
Cache: /Users/poul/Library/Caches
f16cdf4f sago007 2015-10-26 17:37 95
Documents: /Users/poul/Documents
f16cdf4f sago007 2015-10-26 17:37 96
Desktop: /Users/poul/Desktop
f16cdf4f sago007 2015-10-26 17:37 97
Pictures: /Users/poul/Pictures
f16cdf4f sago007 2015-10-26 17:37 98
Music: /Users/poul/Music
f16cdf4f sago007 2015-10-26 17:37 99
Video: /Users/poul/Movies
f16cdf4f sago007 2015-10-26 17:37 100
Download: /Users/poul/Downloads
f16cdf4f sago007 2015-10-26 17:37 101
Save Games 1: /Users/poul/Library/Application Support
f16cdf4f sago007 2015-10-26 17:37 102
```
f16cdf4f sago007 2015-10-26 17:37 103
63627d7a sago007 2015-09-20 18:08 104
# Encoding
63627d7a sago007 2015-09-20 18:08 105
For Windows ANSI encoding is always used. Microsoft's implementation of "Unicode" is simply not compatible with platform independent code.
63627d7a sago007 2015-09-20 18:08 106
For all other systems the local encoding is used. For most systems this is UTF-8.
63627d7a sago007 2015-09-20 18:08 107
Generally you should only append simple ASCII chars to the paths. 
63627d7a sago007 2015-09-20 18:08 108
a337b4fd sago007 2015-09-19 19:47 109
# Licence 
a337b4fd sago007 2015-09-19 19:47 110
Provided under the MIT license for the same reason XDG is licenced under it. So that you can quickly copy-paste the methods you need or just include the "sago"-folder.
1970-01-01 00:00 111