Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
VideoLAN
medialibrary
Commits
2d69edad
Commit
2d69edad
authored
Aug 25, 2016
by
Hugo Beauzée-Luyssen
Browse files
utils: Filename: Handle both backward & forward slashes on win32
parent
505f48f6
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/utils/Filename.cpp
View file @
2d69edad
...
...
@@ -53,21 +53,50 @@ std::string directory( const std::string& filePath )
{
auto
pos
=
filePath
.
find_last_of
(
DIR_SEPARATOR
);
if
(
pos
==
std
::
string
::
npos
)
#ifdef _WIN32
{
pos
=
filePath
.
find_last_of
(
'/'
);
if
(
pos
==
std
::
string
::
npos
)
return
{};
}
#else
return
{};
#endif
return
filePath
.
substr
(
0
,
pos
+
1
);
}
std
::
string
parentDirectory
(
const
std
::
string
&
path
)
{
auto
pos
=
path
.
find_last_of
(
DIR_SEPARATOR
);
#ifdef _WIN32
// If the \ appears right after the drive letter, or there's no backward /
if
(
(
pos
==
2
&&
path
[
1
]
==
':'
)
||
pos
==
std
::
string
::
npos
)
{
// Fall back to forward slashes
pos
=
path
.
find_last_of
(
'/'
);
}
#endif
if
(
pos
==
path
.
length
()
-
1
)
{
#ifdef _WIN32
auto
oldPos
=
pos
;
#endif
pos
=
path
.
find_last_of
(
DIR_SEPARATOR
,
pos
-
1
);
#ifdef _WIN32
if
(
(
pos
==
2
&&
path
[
1
]
==
':'
)
||
pos
==
std
::
string
::
npos
)
pos
=
path
.
find_last_of
(
'/'
,
oldPos
-
1
);
#endif
}
return
path
.
substr
(
0
,
pos
+
1
);
}
std
::
string
fileName
(
const
std
::
string
&
filePath
)
{
auto
pos
=
filePath
.
find_last_of
(
DIR_SEPARATOR
);
#ifdef _WIN32
if
(
pos
==
std
::
string
::
npos
)
pos
=
filePath
.
find_last_of
(
'/'
);
#endif
if
(
pos
==
std
::
string
::
npos
)
return
filePath
;
return
filePath
.
substr
(
pos
+
1
);
...
...
@@ -76,9 +105,17 @@ std::string fileName(const std::string& filePath)
std
::
string
firstFolder
(
const
std
::
string
&
path
)
{
size_t
offset
=
0
;
while
(
path
[
offset
]
==
DIR_SEPARATOR
)
while
(
path
[
offset
]
==
DIR_SEPARATOR
#ifdef _WIN32
||
path
[
offset
]
==
'/'
#endif
)
offset
++
;
auto
pos
=
path
.
find_first_of
(
DIR_SEPARATOR
,
offset
);
#ifdef _WIN32
if
(
pos
==
std
::
string
::
npos
)
pos
=
path
.
find_first_of
(
'/'
,
offset
);
#endif
if
(
pos
==
std
::
string
::
npos
)
return
{};
return
path
.
substr
(
offset
,
pos
-
offset
);
...
...
@@ -89,7 +126,11 @@ std::string removePath( const std::string& fullPath, const std::string& toRemove
if
(
toRemove
.
length
()
==
0
)
return
fullPath
;
auto
pos
=
fullPath
.
find
(
toRemove
)
+
toRemove
.
length
();
while
(
fullPath
[
pos
]
==
DIR_SEPARATOR
)
while
(
fullPath
[
pos
]
==
DIR_SEPARATOR
#ifdef _WIN32
||
fullPath
[
pos
]
==
'/'
#endif
)
pos
++
;
if
(
pos
>=
fullPath
.
length
()
)
return
{};
...
...
@@ -98,7 +139,11 @@ std::string removePath( const std::string& fullPath, const std::string& toRemove
std
::
string
&
toFolderPath
(
std
::
string
&
path
)
{
if
(
*
path
.
crbegin
()
!=
DIR_SEPARATOR
)
if
(
*
path
.
crbegin
()
!=
DIR_SEPARATOR
#ifdef _WIN32
&&
*
path
.
crbegin
()
!=
'/'
#endif
)
path
+=
DIR_SEPARATOR
;
return
path
;
}
...
...
@@ -106,7 +151,11 @@ std::string& toFolderPath( std::string& path )
std
::
string
toFolderPath
(
const
std
::
string
&
path
)
{
auto
p
=
path
;
if
(
*
p
.
crbegin
()
!=
DIR_SEPARATOR
)
if
(
*
p
.
crbegin
()
!=
DIR_SEPARATOR
#ifdef _WIN32
&&
*
p
.
crbegin
()
!=
'/'
#endif
)
p
+=
DIR_SEPARATOR
;
return
p
;
}
...
...
test/unittest/FsUtilsTests.cpp
View file @
2d69edad
...
...
@@ -85,4 +85,10 @@ TEST( FsUtils, parentFolder )
ASSERT_EQ
(
"/a/b/"
,
utils
::
file
::
parentDirectory
(
"/a/b/c/"
)
);
ASSERT_EQ
(
"/a/b/"
,
utils
::
file
::
parentDirectory
(
"/a/b/c"
)
);
ASSERT_EQ
(
""
,
utils
::
file
::
parentDirectory
(
""
)
);
#ifdef _WIN32
ASSERT_EQ
(
"C:
\\
a/b/"
,
utils
::
file
::
parentDirectory
(
"C:
\\
a/b/c"
)
);
ASSERT_EQ
(
"C:/a/b/"
,
utils
::
file
::
parentDirectory
(
"C:/a/b/c
\\
"
)
);
ASSERT_EQ
(
"C:
\\
a
\\
b
\\
"
,
utils
::
file
::
parentDirectory
(
"C:
\\
a
\\
b
\\
c
\\
"
)
);
ASSERT_EQ
(
"C:
\\
a
\\
b
\\
"
,
utils
::
file
::
parentDirectory
(
"C:
\\
a
\\
b
\\
c"
)
);
#endif
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment