Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between path::string() and path::generic_string() in boost?

Tags:

c++

boost

What is the difference between boost::path::string() and boost::path::generic_string(), and when should I use each of them?

like image 986
mans Avatar asked Oct 03 '14 12:10

mans


2 Answers

This is clearly stated in the documentation; you need only read the documentation to gain knowledge and understanding. Please get into the habit of doing that, starting from now.

boost::path::string

Returns a std::string in the native pathname format.

boost::path::generic_string

Returns a std::string in the generic pathname format.

When to use each of them

Well, that's up to you, and depends on your needs! The following quotation, again from the documentation, may help…

[Note: For ISO/IEC 9945, no conversion occurs, since the native format and generic format are the same. For Windows, backslashes are converted to slashes --end note]

In day-to-day use, you can effectively say:

  • On Windows, native format has backslashes and generic format has slashes;
  • On Linux, both formats have slashes.
like image 90
Lightness Races in Orbit Avatar answered Sep 21 '22 14:09

Lightness Races in Orbit


Reading your mind, you are programming on a Windows system.

On your system, as far as boost can tell, the preferred separator between path elements is \. However, / is an acceptable separator.

The constructor to boost::fs::path docs state:

[Note: For ISO/IEC 9945 and Windows implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. --end note]

Note the clause about Windows implementations -- the generic format (with / separators) is already acceptable, so no conversion is done.

Then when you invoke t/fn the appends or / or /= operator is used. It states:

[Note: For ISO/IEC 9945-like implementations, including Unix variants, Linux, and Mac OS X, the preferred directory separator is a single forward slash.

For Windows-like implementations, including Cygwin and MinGW, the preferred directory separator is a single backslash.--end note]

And the preferred separator is \ on windows systems.

So at construction, no conversion from generic to system occurs -- but on appending with operator/ or similar, it is.

This results in your string looking ugly.

If you want to fix the problem, you could iterate over your 'malformed' path using begin and end, and store/append the elements into a new path using operator/.

boost::fs::path fix( boost::fs::path in ) {
  boost::fs::path retval;
  for ( auto element : in ) {
    if (retval.empty())
      retval = in;
    else
      retval /= in;
  }
  return retval;
}

which if I read the docs right will take your mixed-slash path and generate a clean one.

If you are stuck in C++03 iterate over in using in.begin() and in.end() and boost::fs::path::iterator.

like image 32
Yakk - Adam Nevraumont Avatar answered Sep 20 '22 14:09

Yakk - Adam Nevraumont