]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/Folders.cpp
Add missing C standard library headers
[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.hpp"
22
23 #include <cstring>
24 #include <cstdlib>
25 #include <cerrno>
26 #include <unistd.h>
27
28 #if PLATFORM_UNIX
29 #include <pwd.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #endif
33
34 #if _WIN32
35 #include <shlobj.h> // to get paths related functions
36 #include <windows.h>
37 #endif
38
39 const std::string Folders::dataDir = DATA_DIR;
40
41 std::string Folders::getScreenshotDir()
42 {
43     std::string screenshotDir = getUserDataPath() + "/Screenshots";
44     makeDirectory(screenshotDir);
45     return screenshotDir;
46 }
47
48 std::string Folders::getResourcePath(std::string filepath)
49 {
50     return dataDir + '/' + filepath;
51 }
52
53 std::string Folders::getUserDataPath()
54 {
55     std::string userDataPath;
56 #ifdef _WIN32
57     char path[MAX_PATH];
58     // %APPDATA% (%USERPROFILE%\Application Data)
59     if(SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_APPDATA, nullptr, 0, path))) {
60         userDataPath = std::string(path) + "/Lugaru/";
61     } else {
62         return dataDir;
63     }
64 #elif (defined(__APPLE__) && defined(__MACH__))
65     const char* homePath = getHomeDirectory();
66     if (homePath == NULL) {
67         userDataPath = ".";
68     } else {
69         userDataPath = std::string(homePath) + "/Library/Application Support/Lugaru";
70     }
71 #else // Linux
72     userDataPath = getGenericDirectory("XDG_DATA_HOME", ".local/share");
73 #endif
74     makeDirectory(userDataPath);
75     return userDataPath;
76 }
77
78 std::string Folders::getConfigFilePath()
79 {
80     std::string configFolder;
81 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
82     configFolder = getUserDataPath();
83 #else // Linux
84     configFolder = getGenericDirectory("XDG_CONFIG_HOME", ".config");
85     makeDirectory(configFolder);
86 #endif
87     return configFolder + "/config.txt";
88 }
89
90 #if PLATFORM_LINUX
91 /* Generic code for XDG ENVVAR test and fallback */
92 std::string Folders::getGenericDirectory(const char* ENVVAR, const std::string fallback) {
93     const char* path = getenv(ENVVAR);
94     std::string ret;
95     if ((path != NULL) && (strlen(path) != 0)) {
96         ret = std::string(path) + "/lugaru";
97     } else {
98         const char* homedir = getHomeDirectory();
99         if ((homedir != NULL) && (strlen(homedir) != 0)) {
100             ret = std::string(homedir) + '/' + fallback + "/lugaru";
101         } else {
102             ret = ".";
103         }
104     }
105     return ret;
106 }
107 #endif
108
109 #if PLATFORM_UNIX
110 const char* Folders::getHomeDirectory()
111 {
112     const char *homedir = getenv("HOME");
113     if (homedir != NULL)
114         return homedir;
115     struct passwd *pw = getpwuid(getuid());
116     if (pw != NULL)
117         return pw->pw_dir;
118     return NULL;
119 }
120 #endif
121
122 bool Folders::makeDirectory(std::string path) {
123 #ifdef _WIN32
124     int status = CreateDirectory(path.c_str(), NULL);
125     if (status != 0) {
126         return true;
127     } else if(GetLastError() == ERROR_ALREADY_EXISTS) {
128         return true;
129     } else {
130         return false;
131     }
132 #else
133     errno = 0;
134     int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
135     if (status == 0) {
136         return true;
137     } else if(errno == EEXIST) {
138         return true;
139     } else {
140         return false;
141     }
142 #endif
143 }
144
145 FILE* Folders::openMandatoryFile(std::string filename, const char* mode)
146 {
147     FILE* tfile = fopen(filename.c_str(), mode);
148     if (tfile == NULL) {
149         throw FileNotFoundException(filename);
150     }
151     return tfile;
152 }