SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Add SDL_basename and SDL_dirname

Open madebr opened this issue 2 years ago • 10 comments

I think these functions could be useful, with an implementation close to the posix standard.

It should probably deviate by also treating the Windows backslash as a path separator. The SDL implementation should probably not modify its arguments and return a new allocated buffer, or an optional buffer passed as input.

If the SDL implementation differs from the posix standard, this will also mean basename and dirname from the C library will never be used, even when configuring SDL with -DSDL_LIBC=ON.

madebr avatar Jul 03 '23 03:07 madebr

Yeah, that sounds reasonable.

slouken avatar Jul 03 '23 13:07 slouken

@madebr, did you want to take a pass on this?

slouken avatar Jun 04 '24 01:06 slouken

Sure. Looks like posix basename and dirname do in-string modifications, and return a pointer pointing somewhere in the input string. I suppose SDL should model the same behavior, and not return a newly allocated string?

madebr avatar Jun 04 '24 01:06 madebr

Maybe instead we want SDL_splitpath(const char *path, char **dir, char **base); SDL_free(dir); SDL_free(base);

@icculus, what do you think?

slouken avatar Jun 04 '24 15:06 slouken

What's the use case for these in SDL?

slouken avatar Jun 04 '24 15:06 slouken

basename is used in testapp.c of SDL_ttf for building the filename when saving to a bmp. In https://github.com/libsdl-org/SDL_ttf/pull/289, I added a poor man's non-compliant basename implementation, which made me wondering whether a proper implementation would be useful for SDL3.

madebr avatar Jun 04 '24 15:06 madebr

Huh, it's more complicated than I thought it would be:

https://github.com/libsdl-org/SDL_ttf/pull/289/files#diff-0df9edd5f1933fec30bed7a6bfdf378b255d827f553d3eb7168d8542c7af6d64R43-R82

icculus avatar Jun 04 '24 16:06 icculus

My plan was to implement https://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html and https://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html

Here are some free test cases: https://pubs.opengroup.org/onlinepubs/9699919799/functions/basename.html#tag_16_32_06_02

And do the necessary to support windows dos and UNC paths.

madebr avatar Jun 04 '24 16:06 madebr

My plan was to implement https://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html and https://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html

Here are some free test cases: https://pubs.opengroup.org/onlinepubs/9699919799/functions/basename.html#tag_16_32_06_02

And do the necessary to support windows dos and UNC paths.

Okay, sounds good.

slouken avatar Jun 04 '24 17:06 slouken

@madebr, just double checking, did you want this in for the SDL 3.0 ABI milestone?

slouken avatar Jun 29 '24 20:06 slouken

Getting into 3.0 (3.2 release) is not absolutely needed. I prefer a well-defined, well-tested API.

madebr avatar Jul 08 '24 18:07 madebr