Improve RISC OS implementations of SDL_GetBasePath and SDL_GetPrefPath
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
diff --git a/src/filesystem/riscos/SDL_sysfilesystem.c b/src/filesystem/riscos/SDL_sysfilesystem.c
index a16efff..125470b 100644
--- a/src/filesystem/riscos/SDL_sysfilesystem.c
+++ b/src/filesystem/riscos/SDL_sysfilesystem.c
@@ -25,28 +25,144 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* System dependent filesystem routines */
-#include <errno.h>
-#include <sys/stat.h>
+#include <kernel.h>
+#include <swis.h>
+#include <unixlib/local.h>
#include "SDL_error.h"
#include "SDL_stdinc.h"
#include "SDL_filesystem.h"
-#include "SDL_rwops.h"
+
+/* Wrapper around __unixify_std that uses SDL's memory allocators */
+static char *
+SDL_unixify_std(const char *ro_path, char *buffer, size_t buf_len, int filetype)
+{
+ const char *const in_buf = buffer; /* = NULL if we malloc the buffer. */
+
+ if (!buffer) {
+ /* This matches the logic in __unixify, with an additional byte for the
+ * extra path separator.
+ */
+ buf_len = SDL_strlen(ro_path) + 14 + 1;
+ buffer = SDL_malloc(buf_len);
+
+ if (!buffer) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+ }
+
+ if (!__unixify_std(ro_path, buffer, buf_len, filetype)) {
+ if (!in_buf)
+ SDL_free(buffer);
+
+ SDL_SetError("Could not convert '%s' to a Unix-style path", ro_path);
+ return NULL;
+ }
+
+ /* HACK: It's necessary to add an extra path separator here since SDL's API
+ * requires it, however paths with trailing separators aren't normally valid
+ * on RISC OS.
+ */
+ if (__get_riscosify_control() & __RISCOSIFY_NO_PROCESS)
+ SDL_strlcat(buffer, ".", buf_len);
+ else
+ SDL_strlcat(buffer, "/", buf_len);
+
+ return buffer;
+}
+
+static char *
+canonicalisePath(const char *path, const char *pathVar)
+{
+ _kernel_oserror *error;
+ _kernel_swi_regs regs;
+ char *buf;
+
+ regs.r[0] = 37;
+ regs.r[1] = (int)path;
+ regs.r[2] = 0;
+ regs.r[3] = (int)pathVar;
+ regs.r[4] = 0;
+ regs.r[5] = 0;
+ error = _kernel_swi(OS_FSControl, ®s, ®s);
+ if (error) {
+ SDL_SetError("Couldn't canonicalise path: %s", error->errmess);
+ return NULL;
+ }
+
+ regs.r[5] = 1 - regs.r[5];
+ buf = SDL_malloc(regs.r[5]);
+ if (!buf) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+ regs.r[2] = (int)buf;
+ error = _kernel_swi(OS_FSControl, ®s, ®s);
+ if (error) {
+ SDL_SetError("Couldn't canonicalise path: %s", error->errmess);
+ SDL_free(buf);
+ return NULL;
+ }
+
+ return buf;
+}
+
+static _kernel_oserror *
+createDirectoryRecursive(char *path)
+{
+ char *ptr = NULL;
+ _kernel_oserror *error;
+ _kernel_swi_regs regs;
+ regs.r[0] = 8;
+ regs.r[1] = (int)path;
+ regs.r[2] = 0;
+
+ for (ptr = path+1; *ptr; ptr++) {
+ if (*ptr == '.') {
+ *ptr = '\0';
+ error = _kernel_swi(OS_File, ®s, ®s);
+ *ptr = '.';
+ if (error != NULL)
+ return error;
+ }
+ }
+ return _kernel_swi(OS_File, ®s, ®s);
+}
char *
SDL_GetBasePath(void)
{
- SDL_Unsupported();
- return NULL;
+ _kernel_swi_regs regs;
+ _kernel_oserror *error;
+ char *canon, *ptr, *retval;
+
+ error = _kernel_swi(OS_GetEnv, ®s, ®s);
+ if (error) {
+ return NULL;
+ }
+
+ canon = canonicalisePath((const char *)regs.r[0], "Run$Path");
+ if (!canon) {
+ return NULL;
+ }
+
+ /* chop off filename. */
+ ptr = SDL_strrchr(canon, '.');
+ if (ptr != NULL)
+ *ptr = '\0';
+
+ retval = SDL_unixify_std(canon, NULL, 0, __RISCOSIFY_FILETYPE_NOTSPECIFIED);
+ SDL_free(canon);
+ return retval;
}
char *
SDL_GetPrefPath(const char *org, const char *app)
{
- const char *prefix = "/<Choices$Write>/";
- char *retval = NULL;
- char *ptr = NULL;
- size_t len = 0;
+ char *canon, *dir, *retval;
+ size_t len;
+ _kernel_oserror *error;
if (!app) {
SDL_InvalidParamError("app");
@@ -56,34 +172,36 @@ SDL_GetPrefPath(const char *org, const char *app)
org = "";
}
- len = SDL_strlen(prefix) + SDL_strlen(org) + SDL_strlen(app) + 3;
- retval = (char *) SDL_malloc(len);
- if (!retval) {
+ canon = canonicalisePath("<Choices$Write>", "Run$Path");
+ if (!canon) {
+ return NULL;
+ }
+
+ len = SDL_strlen(canon) + SDL_strlen(org) + SDL_strlen(app) + 4;
+ dir = (char *) SDL_malloc(len);
+ if (!dir) {
SDL_OutOfMemory();
+ free(canon);
return NULL;
}
if (*org) {
- SDL_snprintf(retval, len, "%s%s/%s/", prefix, org, app);
+ SDL_snprintf(dir, len, "%s.%s.%s", canon, org, app);
} else {
- SDL_snprintf(retval, len, "%s%s/", prefix, app);
+ SDL_snprintf(dir, len, "%s.%s", canon, app);
}
- for (ptr = retval+1; *ptr; ptr++) {
- if (*ptr == '/') {
- *ptr = '\0';
- if (mkdir(retval, 0700) != 0 && errno != EEXIST)
- goto error;
- *ptr = '/';
- }
- }
- if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
-error:
- SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
- SDL_free(retval);
+ SDL_free(canon);
+
+ error = createDirectoryRecursive(dir);
+ if (error != NULL) {
+ SDL_SetError("Couldn't create directory: %s", error->errmess);
+ SDL_free(dir);
return NULL;
}
+ retval = SDL_unixify_std(dir, NULL, 0, __RISCOSIFY_FILETYPE_NOTSPECIFIED);
+ SDL_free(dir);
return retval;
}