Use std::replace() to replace chars in paths.
This commit is contained in:
parent
c3dfe1a356
commit
0485976c54
29
lib/path.cpp
29
lib/path.cpp
|
@ -16,26 +16,9 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
|
||||||
static std::string replace_chars(const std::string &str, char from, char to)
|
|
||||||
{
|
|
||||||
std::string modified(str);
|
|
||||||
size_t pos = modified.find(from);
|
|
||||||
if (pos!= std::string::npos)
|
|
||||||
{
|
|
||||||
std::string::iterator iter = modified.begin() + pos;
|
|
||||||
std::string::iterator end = modified.end();
|
|
||||||
while (iter != end)
|
|
||||||
{
|
|
||||||
if (*iter == from)
|
|
||||||
*iter = to;
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Path::toNativeSeparators(const std::string &path)
|
std::string Path::toNativeSeparators(const std::string &path)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
@ -45,12 +28,16 @@ std::string Path::toNativeSeparators(const std::string &path)
|
||||||
char separ = '\\';
|
char separ = '\\';
|
||||||
char native = '/';
|
char native = '/';
|
||||||
#endif
|
#endif
|
||||||
return replace_chars(path, separ, native);
|
std::string modified(path);
|
||||||
|
std::replace(modified.begin(), modified.end(), separ, native);
|
||||||
|
return modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Path::fromNativeSeparators(const std::string &path)
|
std::string Path::fromNativeSeparators(const std::string &path)
|
||||||
{
|
{
|
||||||
char nonnative = '\\';
|
char nonnative = '\\';
|
||||||
char internal = '/';
|
char newsepar = '/';
|
||||||
return replace_chars(path, nonnative, internal);
|
std::string modified(path);
|
||||||
|
std::replace(modified.begin(), modified.end(), nonnative, newsepar);
|
||||||
|
return modified;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue