1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22
23 #ifdef SDL_FILESYSTEM_WINDOWS
24
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 /* System dependent filesystem routines */
27
28 #include "../../core/windows/SDL_windows.h"
29 #include <shlobj.h>
30
31 #include "SDL_assert.h"
32 #include "SDL_error.h"
33 #include "SDL_stdinc.h"
34 #include "SDL_filesystem.h"
35
36 char *
SDL_GetBasePath(void)37 SDL_GetBasePath(void)
38 {
39 typedef DWORD (WINAPI *GetModuleFileNameExW_t)(HANDLE, HMODULE, LPWSTR, DWORD);
40 GetModuleFileNameExW_t pGetModuleFileNameExW;
41 DWORD buflen = 128;
42 WCHAR *path = NULL;
43 HANDLE psapi = LoadLibrary(L"psapi.dll");
44 char *retval = NULL;
45 DWORD len = 0;
46 int i;
47
48 if (!psapi) {
49 WIN_SetError("Couldn't load psapi.dll");
50 return NULL;
51 }
52
53 pGetModuleFileNameExW = (GetModuleFileNameExW_t)GetProcAddress(psapi, "GetModuleFileNameExW");
54 if (!pGetModuleFileNameExW) {
55 WIN_SetError("Couldn't find GetModuleFileNameExW");
56 FreeLibrary(psapi);
57 return NULL;
58 }
59
60 while (SDL_TRUE) {
61 void *ptr = SDL_realloc(path, buflen * sizeof (WCHAR));
62 if (!ptr) {
63 SDL_free(path);
64 FreeLibrary(psapi);
65 SDL_OutOfMemory();
66 return NULL;
67 }
68
69 path = (WCHAR *) ptr;
70
71 len = pGetModuleFileNameExW(GetCurrentProcess(), NULL, path, buflen);
72 if (len != buflen) {
73 break;
74 }
75
76 /* buffer too small? Try again. */
77 buflen *= 2;
78 }
79
80 FreeLibrary(psapi);
81
82 if (len == 0) {
83 SDL_free(path);
84 WIN_SetError("Couldn't locate our .exe");
85 return NULL;
86 }
87
88 for (i = len-1; i > 0; i--) {
89 if (path[i] == '\\') {
90 break;
91 }
92 }
93
94 SDL_assert(i > 0); /* Should have been an absolute path. */
95 path[i+1] = '\0'; /* chop off filename. */
96
97 retval = WIN_StringToUTF8(path);
98 SDL_free(path);
99
100 return retval;
101 }
102
103 char *
SDL_GetPrefPath(const char * org,const char * app)104 SDL_GetPrefPath(const char *org, const char *app)
105 {
106 /*
107 * Vista and later has a new API for this, but SHGetFolderPath works there,
108 * and apparently just wraps the new API. This is the new way to do it:
109 *
110 * SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
111 * NULL, &wszPath);
112 */
113
114 WCHAR path[MAX_PATH];
115 char *retval = NULL;
116 WCHAR* worg = NULL;
117 WCHAR* wapp = NULL;
118 size_t new_wpath_len = 0;
119 BOOL api_result = FALSE;
120
121 if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
122 WIN_SetError("Couldn't locate our prefpath");
123 return NULL;
124 }
125
126 worg = WIN_UTF8ToString(org);
127 if (worg == NULL) {
128 SDL_OutOfMemory();
129 return NULL;
130 }
131
132 wapp = WIN_UTF8ToString(app);
133 if (wapp == NULL) {
134 SDL_free(worg);
135 SDL_OutOfMemory();
136 return NULL;
137 }
138
139 new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
140
141 if ((new_wpath_len + 1) > MAX_PATH) {
142 SDL_free(worg);
143 SDL_free(wapp);
144 WIN_SetError("Path too long.");
145 return NULL;
146 }
147
148 lstrcatW(path, L"\\");
149 lstrcatW(path, worg);
150 SDL_free(worg);
151
152 api_result = CreateDirectoryW(path, NULL);
153 if (api_result == FALSE) {
154 if (GetLastError() != ERROR_ALREADY_EXISTS) {
155 SDL_free(wapp);
156 WIN_SetError("Couldn't create a prefpath.");
157 return NULL;
158 }
159 }
160
161 lstrcatW(path, L"\\");
162 lstrcatW(path, wapp);
163 SDL_free(wapp);
164
165 api_result = CreateDirectoryW(path, NULL);
166 if (api_result == FALSE) {
167 if (GetLastError() != ERROR_ALREADY_EXISTS) {
168 WIN_SetError("Couldn't create a prefpath.");
169 return NULL;
170 }
171 }
172
173 lstrcatW(path, L"\\");
174
175 retval = WIN_StringToUTF8(path);
176
177 return retval;
178 }
179
180 #endif /* SDL_FILESYSTEM_WINDOWS */
181
182 /* vi: set ts=4 sw=4 expandtab: */
183