libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.52k stars 1.77k forks source link

Add SDL_basename and SDL_dirname #7915

Open madebr opened 1 year ago

madebr commented 1 year ago

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.

slouken commented 1 year ago

Yeah, that sounds reasonable.

slouken commented 3 months ago

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

madebr commented 3 months ago

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?

slouken commented 3 months ago

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 commented 3 months ago

What's the use case for these in SDL?

madebr commented 3 months ago

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.

icculus commented 3 months ago

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

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

madebr commented 3 months ago

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.

slouken commented 3 months ago

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 commented 3 months ago

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

madebr commented 2 months ago

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