Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cwk_path_is_resolved and cwk_path_get_resolved #49

Open
RaphGL opened this issue May 23, 2024 · 4 comments
Open

Add cwk_path_is_resolved and cwk_path_get_resolved #49

RaphGL opened this issue May 23, 2024 · 4 comments

Comments

@RaphGL
Copy link

RaphGL commented May 23, 2024

A lot of times when working with paths in programs you might have stuff like ~/Documents/my_doc.pdf or $HOME/Documents/my_doc.pdf or some other variation. I want to propose adding:

// returns true if there's no variables or `~` to be resolved in the path segments
 bool cwk_path_is_resolved(const char *path);

// gives you the path with all  the variables and `~` expanded
size_t cwk_path_get_resolved(const char *base, const char *path, char *buffer, size_t buffer_size);
@RaphGL
Copy link
Author

RaphGL commented May 23, 2024

I might be willing to try and implement this myself and push a PR since I'm using the library on one of my projects. But I only have time to give it a try this weekend and I'm unfamiliar with the code base besides trying to read the library before writing this issue to see how feasible the addition would be.

@likle
Copy link
Owner

likle commented May 23, 2024

Hi @RaphGL! That's an interesting proposal, even though I think this might be more difficult to do than it seems, depending on the details. I believe tilde and variables are usually not part of the actual path.

On UNIX-like systems, as far as I know, variables such as $HOME and tilde are almost always resolved by the shell, and there can be much more to it than just a simple replace. There's wordexp on POSIX systems, which probably does more or less what you are looking for.

On Windows, the story is a little different. For one, I am not entirely sure whether there's a standard behavior for tilde expansion for the home directory. I don't think CMD supports it, while PowerShell might, but I am unsure about that and would have to check it myself. Variables are usually wrapped in % characters such as %APP_DATA%, and it seems like there are multiple APIs to resolve these, one of them being ExpandEnvironmentStrings.

cwalk currently does not have any real dependencies on operating system libraries, which I would prefer not to change. So wordexp and ExpandEnvironmentStrings are probably not going to make it into cwalk. Also, if there's going to be an implementation as such, it must definitely have a clear specification of what it actually does. I am not sure yet whether it is possible to do a good and simple implementation for this

@RaphGL
Copy link
Author

RaphGL commented May 23, 2024

cwalk currently does not have any real dependencies on operating system libraries, which I would prefer not to change.

I wasn't thinking in adding any external dependencies as well. My thought process was just to handle basic environment variable expansions, wordexp seems to also expand other things handled by shells like patterns and globbing which seems to be out of scope for this library anyway. As for windows, I don't develop in it so I would not have the best notion for how to properly handle it, someone would have to chime in and help properly support windows.

I think only basic environment variable expansions should be supported. What I had in mind was to just parse a segment and detect if they include a~, $VAR or a ${VAR} and expand it in place.

A common use case for this is passing a path to fopen since it will just literally read the path it will break paths that users expect to be valid if they're not passed to the program through a shell or something.

I am not entirely sure whether there's a standard behavior for tilde expansion for the home directory. I don't think CMD supports it, while PowerShell might, but I am unsure about that and would have to check it myself.

If memory serves me right powershell does expand tilde but cmd doesn't. Either way resolving paths wouldn't be done automatically, users would have to explicitly say they wanted it by calling cwk_path_get_resolved, so users would have to opt into having their tildes and variables expanded.

Either way, I can't promise it would be the simplest implementation even tho it theoretically sounds very easily achievable. So I'll let you decide how to move forward :)

@likle
Copy link
Owner

likle commented May 25, 2024

A simple tilde and env variable expansion sounds cool! Maybe even with flags so the user can specify what he wants.. I am just not sure yet how to handle this on windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants