git @ Cat's Eye Technologies linapple / bbb4b25
Add a shim for eventual porting to SDL2 Added a shim.c and shim.h where we can stuff code that isn't ours, isn't supposed to be around forever, and which helps us port this mess to SDL2. Eventually this is where code from SDLCL will go. In the meantime, it contains the UNIX version of SDL_filesystem.c and the appropriate header content to match, modified only insofar as they need to be in order to compile in our source tree. Didn't bother to match style with the rest of the code for this pretty much on purpose as if SDL updates theirs before we stop using ours, I intend to pick up their changes verbatim. These functions aren't used yet—I'd have to look up where they need to be used and I have to leave for a bit, so if Chris or David wants to have a go at it now, they're welcome to. Otherwise I'll do something with it later tonight or tomorrow. T. Joseph Carter 3 years ago
3 changed file(s) with 346 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 // This file contains shim functions we're using temporarily before we port
1 // the whole codebase to SDL2
2
3 #ifndef __shim_h
4 #define __shim_h
5
6 /**
7 * \brief Get the path where the application resides.
8 *
9 * Get the "base path". This is the directory where the application was run
10 * from, which is probably the installation directory, and may or may not
11 * be the process's current working directory.
12 *
13 * This returns an absolute path in UTF-8 encoding, and is guaranteed to
14 * end with a path separator ('\\' on Windows, '/' most other places).
15 *
16 * The pointer returned by this function is owned by you. Please call
17 * SDL_free() on the pointer when you are done with it, or it will be a
18 * memory leak. This is not necessarily a fast call, though, so you should
19 * call this once near startup and save the string if you need it.
20 *
21 * Some platforms can't determine the application's path, and on other
22 * platforms, this might be meaningless. In such cases, this function will
23 * return NULL.
24 *
25 * \return String of base dir in UTF-8 encoding, or NULL on error.
26 *
27 * \sa SDL_GetPrefPath
28 */
29 extern DECLSPEC char *SDLCALL SDL_GetBasePath(void);
30
31 /**
32 * \brief Get the user-and-app-specific path where files can be written.
33 *
34 * Get the "pref dir". This is meant to be where users can write personal
35 * files (preferences and save games, etc) that are specific to your
36 * application. This directory is unique per user, per application.
37 *
38 * This function will decide the appropriate location in the native filesystem,
39 * create the directory if necessary, and return a string of the absolute
40 * path to the directory in UTF-8 encoding.
41 *
42 * On Windows, the string might look like:
43 * "C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\"
44 *
45 * On Linux, the string might look like:
46 * "/home/bob/.local/share/My Program Name/"
47 *
48 * On Mac OS X, the string might look like:
49 * "/Users/bob/Library/Application Support/My Program Name/"
50 *
51 * (etc.)
52 *
53 * You specify the name of your organization (if it's not a real organization,
54 * your name or an Internet domain you own might do) and the name of your
55 * application. These should be untranslated proper names.
56 *
57 * Both the org and app strings may become part of a directory name, so
58 * please follow these rules:
59 *
60 * - Try to use the same org string (including case-sensitivity) for
61 * all your applications that use this function.
62 * - Always use a unique app string for each one, and make sure it never
63 * changes for an app once you've decided on it.
64 * - Unicode characters are legal, as long as it's UTF-8 encoded, but...
65 * - ...only use letters, numbers, and spaces. Avoid punctuation like
66 * "Game Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
67 *
68 * This returns an absolute path in UTF-8 encoding, and is guaranteed to
69 * end with a path separator ('\\' on Windows, '/' most other places).
70 *
71 * The pointer returned by this function is owned by you. Please call
72 * SDL_free() on the pointer when you are done with it, or it will be a
73 * memory leak. This is not necessarily a fast call, though, so you should
74 * call this once near startup and save the string if you need it.
75 *
76 * You should assume the path returned by this function is the only safe
77 * place to write files (and that SDL_GetBasePath(), while it might be
78 * writable, or even the parent of the returned path, aren't where you
79 * should be writing things).
80 *
81 * Some platforms can't determine the pref path, and on other
82 * platforms, this might be meaningless. In such cases, this function will
83 * return NULL.
84 *
85 * \param org The name of your organization.
86 * \param app The name of your application.
87 * \return UTF-8 string of user dir in platform-dependent notation. NULL
88 * if there's a problem (creating directory failed, etc).
89 *
90 * \sa SDL_GetBasePath
91 */
92 extern DECLSPEC char *SDLCALL SDL_GetPrefPath(const char *org, const char *app);
93
94 #endif // __shim_h
7171
7272 //#include "Util_MemoryTextFile.h"
7373
74 // iK: This is to faciliate porting to SDL2
75 #include "shim.h"
0 // This file contains shim functions we're using temporarily before we port
1 // the whole codebase to SDL2
2
3 /*
4 Simple DirectMedia Layer
5 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22 */
23
24 // These come right from SDL2
25
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 /* System dependent filesystem routines */
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <limits.h>
36 #include <fcntl.h>
37
38 #if defined(__FREEBSD__) || defined(__OPENBSD__)
39 #include <sys/sysctl.h>
40 #endif
41
42 #include "SDL.h"
43
44 #define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
45
46 /* QNX's /proc/self/exefile is a text file and not a symlink. */
47 #if !defined(__QNXNTO__)
48 static char *
49 readSymLink(const char *path)
50 {
51 char *retval = NULL;
52 ssize_t len = 64;
53 ssize_t rc = -1;
54
55 while (1)
56 {
57 char *ptr = (char *) SDL_realloc(retval, (size_t) len);
58 if (ptr == NULL) {
59 SDL_OutOfMemory();
60 break;
61 }
62
63 retval = ptr;
64
65 rc = readlink(path, retval, len);
66 if (rc == -1) {
67 break; /* not a symlink, i/o error, etc. */
68 } else if (rc < len) {
69 retval[rc] = '\0'; /* readlink doesn't null-terminate. */
70 return retval; /* we're good to go. */
71 }
72
73 len *= 2; /* grow buffer, try again. */
74 }
75
76 SDL_free(retval);
77 return NULL;
78 }
79 #endif
80
81 char *
82 SDL_GetBasePath(void)
83 {
84 char *retval = NULL;
85
86 #if defined(__FREEBSD__)
87 char fullpath[PATH_MAX];
88 size_t buflen = sizeof (fullpath);
89 const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
90 if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
91 retval = SDL_strdup(fullpath);
92 if (!retval) {
93 SDL_OutOfMemory();
94 return NULL;
95 }
96 }
97 #endif
98 #if defined(__OPENBSD__)
99 char **retvalargs;
100 size_t len;
101 const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
102 if (sysctl(mib, 4, NULL, &len, NULL, 0) != -1) {
103 retvalargs = SDL_malloc(len);
104 if (!retvalargs) {
105 SDL_OutOfMemory();
106 return NULL;
107 }
108 sysctl(mib, 4, retvalargs, &len, NULL, 0);
109 retval = SDL_malloc(PATH_MAX + 1);
110 if (retval)
111 realpath(retvalargs[0], retval);
112
113 SDL_free(retvalargs);
114 }
115 #endif
116 #if defined(__SOLARIS__)
117 const char *path = getexecname();
118 if ((path != NULL) && (path[0] == '/')) { /* must be absolute path... */
119 retval = SDL_strdup(path);
120 if (!retval) {
121 SDL_OutOfMemory();
122 return NULL;
123 }
124 }
125 #endif
126
127 /* is a Linux-style /proc filesystem available? */
128 if (!retval && (access("/proc", F_OK) == 0)) {
129 /* !!! FIXME: after 2.0.6 ships, let's delete this code and just
130 use the /proc/%llu version. There's no reason to have
131 two copies of this plus all the #ifdefs. --ryan. */
132 #if defined(__FREEBSD__)
133 retval = readSymLink("/proc/curproc/file");
134 #elif defined(__NETBSD__)
135 retval = readSymLink("/proc/curproc/exe");
136 #elif defined(__QNXNTO__)
137 retval = SDL_LoadFile("/proc/self/exefile", NULL);
138 #else
139 retval = readSymLink("/proc/self/exe"); /* linux. */
140 if (retval == NULL) {
141 /* older kernels don't have /proc/self ... try PID version... */
142 char path[64];
143 const int rc = (int) SDL_snprintf(path, sizeof(path),
144 "/proc/%llu/exe",
145 (unsigned long long) getpid());
146 if ( (rc > 0) && (rc < sizeof(path)) ) {
147 retval = readSymLink(path);
148 }
149 }
150 #endif
151 }
152
153 /* If we had access to argv[0] here, we could check it for a path,
154 or troll through $PATH looking for it, too. */
155
156 if (retval != NULL) { /* chop off filename. */
157 char *ptr = SDL_strrchr(retval, '/');
158 if (ptr != NULL) {
159 *(ptr+1) = '\0';
160 } else { /* shouldn't happen, but just in case... */
161 SDL_free(retval);
162 retval = NULL;
163 }
164 }
165
166 if (retval != NULL) {
167 /* try to shrink buffer... */
168 char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
169 if (ptr != NULL)
170 retval = ptr; /* oh well if it failed. */
171 }
172
173 return retval;
174 }
175
176 char *
177 SDL_GetPrefPath(const char *org, const char *app)
178 {
179 /*
180 * We use XDG's base directory spec, even if you're not on Linux.
181 * This isn't strictly correct, but the results are relatively sane
182 * in any case.
183 *
184 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
185 */
186 const char *envr = SDL_getenv("XDG_DATA_HOME");
187 const char *append;
188 char *retval = NULL;
189 char *ptr = NULL;
190 size_t len = 0;
191
192 if (!app) {
193 SDL_InvalidParamError("app");
194 return NULL;
195 }
196 if (!org) {
197 org = "";
198 }
199
200 if (!envr) {
201 /* You end up with "$HOME/.local/share/Game Name 2" */
202 envr = SDL_getenv("HOME");
203 if (!envr) {
204 /* we could take heroic measures with /etc/passwd, but oh well. */
205 SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
206 return NULL;
207 }
208 append = "/.local/share/";
209 } else {
210 append = "/";
211 }
212
213 len = SDL_strlen(envr);
214 if (envr[len - 1] == '/')
215 append += 1;
216
217 len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
218 retval = (char *) SDL_malloc(len);
219 if (!retval) {
220 SDL_OutOfMemory();
221 return NULL;
222 }
223
224 if (*org) {
225 SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
226 } else {
227 SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
228 }
229
230 for (ptr = retval+1; *ptr; ptr++) {
231 if (*ptr == '/') {
232 *ptr = '\0';
233 if (mkdir(retval, 0700) != 0 && errno != EEXIST)
234 goto error;
235 *ptr = '/';
236 }
237 }
238 if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
239 error:
240 SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
241 SDL_free(retval);
242 return NULL;
243 }
244
245 return retval;
246 }
247
248 /* vi: set ts=4 sw=4 expandtab: */