9

How can I get the platform-specific path separator (not directory separator) in C++, i.e. the separator that is needed to combine multiple paths in a list (e.g. PATH environment variable).

Under Linux, this would be :, under Windows ;.

In other words, I am looking for the C++-equivalent of Python's os.pathsep, Java's path.separator or PHP's PATH_SEPARATOR.

If Boost provides such a function that would be fine, as we are using it in our projects anyway. If not, I guess any other solution would be good.

All I could find (here and elsewhere) were either just ways to retrieve the directory separator (i.e. / vs. \) or related to other languages than C++.

5
  • possible duplicate of Obtain platform's path separator using Boost.Filesystem Commented May 9, 2012 at 10:25
  • 3
    @awoodland: Really? Did you read the question? Commented May 9, 2012 at 10:25
  • @Nick - Oops, sorry I read the phrase "path separator" a lot and remembered that question - definitely not a duplicate then Commented May 9, 2012 at 10:32
  • 2
    Is a preprocessor directive not sufficient? #ifdef _WIN32 const std::string os_pathsep(";") #else const std::string os_pathsep(":") #endif Commented May 9, 2012 at 10:36
  • Well, somehow yes, but I'd prefer not to have to bother with this in my own code at all (similar to not having to deal with whether the directory separator is / or `\`). Commented May 9, 2012 at 10:43

2 Answers 2

6

As per comment, a possibility would be to use the preprocessor:

#ifdef _WIN32
const std::string os_pathsep(";");
#else
const std::string os_pathsep(":");
#endif
Sign up to request clarification or add additional context in comments.

5 Comments

Agreed. We're talking about 5 lines of code, here. You could have written this 100 times in the time it took to google it and ask a question on SO. Not everything requires a highly-engineered, elegant and sublime solution.
Many platform dependent things can boil down to a few lines of code. Nevertheless, there are libraries (like the abovementionend APR, I was hoping for Boost as well) that provide this functionality and make sure that future platforms are properly taken care of as well without me taking care of this. I was just suprised that many other languages (or libraires for those) provide this, but C++ does not.
As I'm sure you're aware, these libraries are generally dependent on people dedicating their spare time to improvement. Why not have a go yourself and get something added to the boost::filesystem library?
@Nick Sure, I guess I'll ask on Boost mailing list next. Somehow, I guess there might be some reason (rather than an oversight) why this is not part of one of the libraries...Let's see
Well, obviously there is really no other choice (apart from @Nick suggestion of APR), so I guess I have to go with this solution. Thanks everyone.
4

The only portable way beyond the use of the preprocessor that I can find would be through the Apache Portable Runtime:

apr_filepath_list_merge and apr_filepath_list_split

You could have a look and see how these are implemented but I'm guessing it would just be a preprocessor definition.

2 Comments

Using the APR just for this would probably be overkill however...!
I just checked the code. APR has platform-dependent implementations (seperate source files) in which the appropriate character is hard-coded within the functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.