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