]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/Folders.cpp
Rename DATADIR to DATA_DIR to avoid conflict
[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 #endif
32
33 const std::string Folders::dataDir = DATA_DIR;
34
35 std::string Folders::getScreenshotDir()
36 {
37     std::string screenshotDir = getUserDataPath() + "/Screenshots";
38     makeDirectory(screenshotDir);
39     return screenshotDir;
40 }
41
42 std::string Folders::getResourcePath(std::string filepath)
43 {
44     return dataDir + '/' + filepath;
45 }
46
47 std::string Folders::getUserDataPath()
48 {
49 #ifdef _WIN32
50     return dataDir;
51 #else
52     std::string userDataPath;
53 #if (defined(__APPLE__) && defined(__MACH__))
54     const char* homePath = getHomeDirectory();
55     if (homePath == NULL) {
56         userDataPath = ".";
57     } else {
58         userDataPath = std::string(homePath) + "/Library/Application Support/Lugaru";
59     }
60 #else
61     userDataPath = getGenericDirectory("XDG_DATA_HOME", ".local/share");
62 #endif
63     makeDirectory(userDataPath);
64     return userDataPath;
65 #endif
66 }
67
68 std::string Folders::getConfigFilePath()
69 {
70 #ifdef _WIN32
71     return dataDir + "/config.txt";
72 #else
73     std::string configFolder;
74 #if (defined(__APPLE__) && defined(__MACH__))
75     configFolder = getUserDataPath();
76 #else
77     configFolder = getGenericDirectory("XDG_CONFIG_HOME", ".config");
78 #endif
79     makeDirectory(configFolder);
80     return configFolder + "/config.txt";
81 #endif
82 }
83
84 #if PLATFORM_LINUX
85 /* Generic code for XDG ENVVAR test and fallback */
86 std::string Folders::getGenericDirectory(const char* ENVVAR, const std::string fallback) {
87     const char* path = getenv(ENVVAR);
88     std::string ret;
89     if((path != NULL) && (strlen(path) != 0)) {
90         ret = std::string(path) + "/lugaru";
91     } else {
92         path = getHomeDirectory();
93         if((path != NULL) && (strlen(path) != 0)) {
94             ret = std::string(path) + '/' + fallback + "/lugaru";
95         } else {
96             ret = ".";
97         }
98     }
99     return ret;
100 }
101 #endif
102
103 #if PLATFORM_UNIX
104 const char* Folders::getHomeDirectory()
105 {
106     const char *homedir = getenv("HOME");
107     if (homedir != NULL)
108         return homedir;
109     struct passwd *pw = getpwuid(getuid());
110     if (pw != NULL)
111         return pw->pw_dir;
112     return NULL;
113 }
114 #endif
115
116 bool Folders::makeDirectory(std::string path) {
117 #ifdef _WIN32
118     int status = CreateDirectory(path.c_str(), NULL);
119     if(status != 0) {
120         return true;
121     } else if(GetLastError() == ERROR_ALREADY_EXISTS) {
122         return true;
123     } else {
124         return false;
125     }
126 #else
127     errno = 0;
128     int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
129     if (status == 0) {
130         return true;
131     } else if(errno == EEXIST) {
132         return true;
133     } else {
134         return false;
135     }
136 #endif
137 }