Skip to content

Commit 8df057f

Browse files
nmlgcslouken
authored andcommitted
iostream: Properly support the "x" mode for SDL_IOFromFile()
The "x" mode for `fopen()` (open file only if it doesn't exist) used to be a glibc-exclusive extension, but was later standardized in C11, and is now also implemented as part of every other widely-used libc: * musl: https://git.musl-libc.org/cgit/musl/tree/src/stdio/__fmodeflags.c?id=0ccaf0572e9cccda2cced0f7ee659af4c1c6679a * Android Bionic / OpenBSD: https://android.googlesource.com/platform/bionic/+/731631f300090436d7f5df80d50b6275c8c60a93/libc/upstream-openbsd/lib/libc/stdio/flags.c#86 * Apple / FreeBSD: https://github.com/apple-oss-distributions/Libc/blob/63976b830a836a22649b806fe62e8614fe3e5555/stdio/FreeBSD/flags.c#L91-L92 As a result, "x" has already been working on all our automatically tested platforms that implement `SDL_IOFromFile()` via `fopen()`. So all we'd be missing for proper support is a Windows implementation using `CREATE_NEW`, and the documentation that this mode exists and is intended to work.
1 parent 87e3250 commit 8df057f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

include/SDL3/SDL_iostream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,17 @@ typedef struct SDL_IOStream SDL_IOStream;
203203
* - "w": Create an empty file for writing. If a file with the same name
204204
* already exists its content is erased and the file is treated as a new
205205
* empty file.
206+
* - "wx": Create an empty file for writing. If a file with the same name
207+
* already exists, the call fails.
206208
* - "a": Append to a file. Writing operations append data at the end of the
207209
* file. The file is created if it does not exist.
208210
* - "r+": Open a file for update both reading and writing. The file must
209211
* exist.
210212
* - "w+": Create an empty file for both reading and writing. If a file with
211213
* the same name already exists its content is erased and the file is
212214
* treated as a new empty file.
215+
* - "w+x": Create an empty file for both reading and writing. If a file with
216+
* the same name already exists, the call fails.
213217
* - "a+": Open a file for reading and appending. All writing operations are
214218
* performed at the end of the file, protecting the previous content to be
215219
* overwritten. You can reposition (fseek, rewind) the internal pointer to

src/io/SDL_iostream.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,23 @@ static HANDLE SDLCALL windows_file_open(const char *filename, const char *mode)
9494

9595
// "r" = reading, file must exist
9696
// "w" = writing, truncate existing, file may not exist
97+
// "wx"= writing, file must not exist
9798
// "r+"= reading or writing, file must exist
9899
// "a" = writing, append file may not exist
99100
// "a+"= append + read, file may not exist
100101
// "w+" = read, write, truncate. file may not exist
102+
// "w+x"= read, write, file must not exist
101103

102104
must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
103105
truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
104106
r_right = (SDL_strchr(mode, '+') != NULL || must_exist) ? GENERIC_READ : 0;
105107
a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
106108
w_right = (a_mode || SDL_strchr(mode, '+') || truncate) ? GENERIC_WRITE : 0;
107109

110+
if (truncate && (SDL_strchr(mode, 'x') != NULL)) {
111+
truncate = CREATE_NEW;
112+
}
113+
108114
if (!r_right && !w_right) {
109115
return INVALID_HANDLE_VALUE; // inconsistent mode
110116
}

test/testautomation_iostream.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ static int SDLCALL iostrm_testFileWrite(void *arg)
615615
int result;
616616

617617
/* Write test. */
618-
rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
619-
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\") succeeded");
620-
SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
618+
rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+x");
619+
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+x\") succeeded");
620+
SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in exclusive write mode does not return NULL");
621621

622622
/* Bail out if NULL */
623623
if (rw == NULL) {
@@ -632,6 +632,11 @@ static int SDLCALL iostrm_testFileWrite(void *arg)
632632
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
633633
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
634634

635+
/* Exclusively opening an existing file should fail. */
636+
rw = SDL_IOFromFile(IOStreamWriteTestFilename, "wx");
637+
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"wx\") succeeded");
638+
SDLTest_AssertCheck(rw == NULL, "Verify opening existing file with SDL_IOFromFile in exclusive write mode returns NULL");
639+
635640
return TEST_COMPLETED;
636641
}
637642

0 commit comments

Comments
 (0)