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

Extend pathtools cross-platform #337

Merged
merged 8 commits into from
May 28, 2023

Conversation

chris-hld
Copy link
Contributor

Introduce get_home_dir() and normalize_path().

@mgeier mgeier mentioned this pull request Mar 8, 2023
@@ -193,12 +193,14 @@ ssr::conf_struct ssr::configuration(int& argc, char* argv[])
// load system-wide config file (Linux et al.)
load_config_file("/etc/ssr.conf",conf);
// load user config file (Mac)
std::string filename = getenv("HOME");
std::string filename = pathtools::get_home_dir().string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to keep using a path here?
Then the following line could use / to append components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the cross-compiler doesn't like these implicit path to string conversions...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I wasn't clear.

I'm not talking about any implicit conversions (those are bad!), I'm talking about never using std::string at all.

get_home_dir() would return a path, which would then be manipulated as such, and in the very end, .c_str() would be called on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works, too. It seems that we still need an explicit string() conversion before c_str(), as path::c_str() is equivalent to native().c_str().
https://en.cppreference.com/w/cpp/filesystem/path/native
The native type on windows is w_string, and its pointer was not compatible to const char*.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's unfortunate. It's interesting how C++ reliably manages to have the least expected behavior by default ... but I guess string().c_str() is fine.

filename += "/Library/SoundScapeRenderer/ssr.conf";
filename = pathtools::normalize_path(filename);
load_config_file(filename.c_str(),conf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename could still be a path here, which also provides a .c_str() method.

src/pathtools.h Outdated
**/
inline fs::path get_home_dir()
{
fs::path p_home = fs::current_path().root_path();
Copy link
Member

@mgeier mgeier Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this fallback case ... why would we return root_path()?
This seems wrong to me.

I think we could take inspiration from root_path() and return path() as a fallback.

This way, the caller could check if there is a HOME directory or not.

We would of course also have to check this when we are using get_home_dir().

I guess for now we were assuming that $HOME is always available, which might be true in most cases, but we might as well add a check for this case while we are at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root_path is indeed not the best solution. How about current_path()? That's also easy to test and perhaps a better fallback. Say a file can also be located in the installation folder, and could then be found as a fallback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$HOME is actually not available on Windows!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also easy to test

I disagree. It seems dangerous, because the current path might change at any point.

and perhaps a better fallback.

I don't think the current path should be involved, it has nothing to do with the HOME directory.

If no HOME directory is defined, this function should basically return an error. If path() is returned, this would mean it didn't work and there is no HOME dir.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see! I updated to return path() now, in case where it fails

src/configuration.cpp Outdated Show resolved Hide resolved
src/configuration.cpp Outdated Show resolved Hide resolved
src/pathtools.h Outdated Show resolved Hide resolved
src/configuration.cpp Outdated Show resolved Hide resolved
@mgeier
Copy link
Member

mgeier commented May 10, 2023

@chris-hld What do you think about my latest comments?

To avoid confusion about what I meant, I have implemented them in ed03a85 and e31b1e5.

@mgeier mgeier merged commit 747044c into SoundScapeRenderer:master May 28, 2023
11 of 12 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants