]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/Folders.cpp
Trying to add implementations for MacOS and Windows
[lugaru.git] / Source / Utils / Folders.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Folders.h"
22 #include <cstring>
23 #include <unistd.h>
24 #if PLATFORM_UNIX
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28 #endif
29 #if _WIN32
30 #include <Windows.h>
31 #include <WinBase.h>
32 #endif
33
34 const std::string Folders::dataDir = DATADIR;
35
36 std::string Folders::getScreenshotDir()
37 {
38     std::string screenshotDir = getUserDataPath() + "/Screenshots";
39     makeDirectory(screenshotDir);
40     return screenshotDir;
41 }
42
43 std::string Folders::getResourcePath(std::string filepath)
44 {
45     return dataDir + '/' + filepath;
46 }
47
48 std::string Folders::getUserDataPath()
49 {
50 #ifdef _WIN32
51     return dataDir;
52 #else
53     std::string userDataPath;
54 #if (defined(__APPLE__) && defined(__MACH__))
55     const char* homePath = getHomeDirectory();
56     if (homePath == NULL) {
57         userDataPath = ".";
58     } else {
59         userDataPath = std::string(homePath) + "/Library/Application Support/Lugaru";
60     }
61 #else
62     userDataPath = getGenericDirectory("XDG_DATA_HOME", ".local/share");
63 #endif
64     makeDirectory(userDataPath);
65     return userDataPath;
66 #endif
67 }
68
69 std::string Folders::getConfigFilePath()
70 {
71 #ifdef _WIN32
72     return dataDir + "/config.txt";
73 #else
74     std::string configFolder;
75 #if (defined(__APPLE__) && defined(__MACH__))
76     configFolder = getUserDataPath();
77 #else
78     configFolder = getGenericDirectory("XDG_CONFIG_HOME", ".config");
79 #endif
80     makeDirectory(configFolder);
81     return configFolder + "/config.txt";
82 #endif
83 }
84
85 #if PLATFORM_LINUX
86 /* Generic code for XDG ENVVAR test and fallback */
87 std::string Folders::getGenericDirectory(const char* ENVVAR, const std::string fallback) {
88     const char* path = getenv(ENVVAR);
89     std::string ret;
90     if((path != NULL) && (strlen(path) != 0)) {
91         ret = std::string(path) + "/lugaru";
92     } else {
93         path = getHomeDirectory();
94         if((path != NULL) && (strlen(path) != 0)) {
95             ret = std::string(path) + '/' + fallback + "/lugaru";
96         } else {
97             ret = ".";
98         }
99     }
100     return ret;
101 }
102 #endif
103
104 #if PLATFORM_UNIX
105 const char* Folders::getHomeDirectory()
106 {
107     const char *homedir = getenv("HOME");
108     if (homedir != NULL)
109         return homedir;
110     struct passwd *pw = getpwuid(getuid());
111     if (pw != NULL)
112         return pw->pw_dir;
113     return NULL;
114 }
115 #endif
116
117 bool Folders::makeDirectory(std::string path) {
118 #ifdef _WIN32
119     int status = CreateDirectory(path.c_str(), NULL);
120     if(status != 0) {
121         return true;
122     } else if(GetLastError() == ERROR_ALREADY_EXISTS) {
123         return true;
124     } else {
125         return false;
126     }
127 #else
128     errno = 0;
129     int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
130     if (status == 0) {
131         return true;
132     } else if(errno == EEXIST) {
133         return true;
134     } else {
135         return false;
136     }
137 #endif
138 }