Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
066087b3
Commit
066087b3
authored
Oct 25, 2019
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update create_dir
parent
e9d42e05
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
44 deletions
+55
-44
include/spdlog/details/os-inl.h
include/spdlog/details/os-inl.h
+21
-22
tests/test_create_dir.cpp
tests/test_create_dir.cpp
+34
-22
No files found.
include/spdlog/details/os-inl.h
View file @
066087b3
...
...
@@ -485,27 +485,30 @@ SPDLOG_INLINE bool create_dir(filename_t path)
{
return
true
;
}
using
char_type
=
filename_t
::
value_type
;
std
::
basic_istringstream
<
char_type
>
istream
(
path
);
filename_t
token
;
filename_t
cur_dir
;
#ifdef _WIN32
// support forward slash in windows
std
::
replace
(
path
.
begin
(),
path
.
end
(),
'/'
,
folder_sep
);
#endif
while
(
std
::
getline
(
istream
,
token
,
folder_sep
))
{
if
(
!
token
.
empty
())
{
cur_dir
+=
token
;
if
(
!
file_exists
(
cur_dir
)
&&
!
mkdir_
(
cur_dir
))
{
return
false
;
}
}
cur_dir
+=
folder_sep
;
}
#endif
size_t
search_offset
=
0
;
do
{
auto
token_pos
=
path
.
find
(
folder_sep
,
search_offset
);
// treat the entire path as a folder if no folder separator not found
if
(
token_pos
==
filename_t
::
npos
)
{
token_pos
=
path
.
size
();
}
auto
subdir
=
path
.
substr
(
0
,
token_pos
);
if
(
!
subdir
.
empty
()
&&
!
file_exists
(
subdir
)
&&
!
mkdir_
(
subdir
))
{
return
false
;
// return error if failed creating dir
}
search_offset
=
token_pos
+
1
;
}
while
(
search_offset
<
path
.
size
());
return
true
;
}
...
...
@@ -517,10 +520,6 @@ SPDLOG_INLINE bool create_dir(filename_t path)
// "abc///" => "abc//"
SPDLOG_INLINE
filename_t
dir_name
(
filename_t
path
)
{
#ifdef _WIN32
// support forward slash in windows
std
::
replace
(
path
.
begin
(),
path
.
end
(),
'/'
,
folder_sep
);
#endif
auto
pos
=
path
.
find_last_of
(
folder_sep
);
return
pos
!=
filename_t
::
npos
?
path
.
substr
(
0
,
pos
)
:
filename_t
{};
}
...
...
tests/test_create_dir.cpp
View file @
066087b3
...
...
@@ -8,6 +8,7 @@ using spdlog::details::os::file_exists;
void
test_create_dir
(
const
char
*
path
,
const
char
*
normalized_path
)
{
printf
(
"Test Create dir %s
\n
"
,
path
);
auto
rv
=
create_dir
(
path
);
REQUIRE
(
rv
==
true
);
REQUIRE
(
file_exists
(
normalized_path
));
...
...
@@ -18,11 +19,23 @@ void test_create_dir(const char *path, const char *normalized_path)
TEST_CASE
(
"create_dir"
,
"[create_dir]"
)
{
prepare_logdir
();
test_create_dir
(
"test_logs/dir1/dir1"
,
"test_logs/dir1/dir1"
);
test_create_dir
(
"test_logs/dir1///dir2"
,
"test_logs/dir1/dir2"
);
test_create_dir
(
"./test_logs/dir1/dir3"
,
"test_logs/dir1/dir3"
);
test_create_dir
(
"test_logs/../test_logs/dir1/dir4"
,
"test_logs/dir1/dir4"
);
test_create_dir
(
"./test_logs/dir1/dir2/dir99/../dir23"
,
"test_logs/dir1/dir2/dir23"
);
#ifdef WIN32
test_create_dir
(
"test_logs/dir1/dir1"
,
"test_logs
\\
dir1
\\
dir1"
);
test_create_dir
(
"test_logs/dir1/dir1"
,
"test_logs
\\
dir1
\\
dir1"
);
//test existing
test_create_dir
(
"test_logs/dir1///dir2//"
,
"test_logs
\\
dir1
\\
dir2"
);
test_create_dir
(
"./test_logs/dir1/dir3"
,
"test_logs
\\
dir1
\\
dir3"
);
test_create_dir
(
"test_logs/../test_logs/dir1/dir4"
,
"test_logs
\\
dir1
\\
dir4"
);
// test backslash
test_create_dir
(
"test_logs
\\
dir1
\\
dir222"
,
"test_logs
\\
dir1
\\
dir222"
);
test_create_dir
(
"test_logs
\\
dir1
\\
dir223
\\
"
,
"test_logs
\\
dir1
\\
dir223
\\
"
);
test_create_dir
(
".
\\
test_logs
\\
dir1
\\
dir2
\\
dir99
\\
..
\\
dir23"
,
"test_logs
\\
dir1
\\
dir2
\\
dir23"
);
#else
test_create_dir
(
"test_logs/dir1/dir1"
,
"test_logs/dir1/dir1"
);
test_create_dir
(
"test_logs/dir1/dir1"
,
"test_logs/dir1/dir1"
);
// test existing
test_create_dir
(
"test_logs/dir1///dir2"
,
"test_logs/dir1/dir2"
);
test_create_dir
(
"./test_logs/dir1/dir3"
,
"test_logs/dir1/dir3"
);
test_create_dir
(
"test_logs/../test_logs/dir1/dir4"
,
"test_logs/dir1/dir4"
);
#endif
}
TEST_CASE
(
"dir_name"
,
"[create_dir]"
)
...
...
@@ -30,29 +43,28 @@ TEST_CASE("dir_name", "[create_dir]")
using
spdlog
::
details
::
os
::
dir_name
;
REQUIRE
(
dir_name
(
""
).
empty
());
REQUIRE
(
dir_name
(
"dir"
).
empty
());
REQUIRE
(
dir_name
(
"dir/"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir///"
)
==
"dir//"
);
REQUIRE
(
dir_name
(
"dir/file"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir/file.txt"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir/file.txt/"
)
==
"dir/file.txt"
);
REQUIRE
(
dir_name
(
"/dir/file.txt"
)
==
"/dir"
);
REQUIRE
(
dir_name
(
"//dir/file.txt"
)
==
"//dir"
);
REQUIRE
(
dir_name
(
"//dir/file.txt"
)
==
"//dir"
);
REQUIRE
(
dir_name
(
"../file.txt"
)
==
".."
);
REQUIRE
(
dir_name
(
"./file.txt"
)
==
"."
);
#ifdef WIN32
REQUIRE
(
dir_name
(
R"(dir\)"
)
==
"dir"
);
REQUIRE
(
dir_name
(
R"(dir\\\)"
)
==
"dir//
"
);
REQUIRE
(
dir_name
(
R"(dir\\\)"
)
==
R"(dir\\)
"
);
REQUIRE
(
dir_name
(
R"(dir\file)"
)
==
"dir"
);
REQUIRE
(
dir_name
(
R"(dir\file.txt)"
)
==
"dir"
);
REQUIRE
(
dir_name
(
R"(dir\file.txt\)"
)
==
"dir/file.txt"
);
REQUIRE
(
dir_name
(
R"(\dir\file.txt)"
)
==
"/dir"
);
REQUIRE
(
dir_name
(
R"(\\dir\file.txt)"
)
==
"//dir"
);
REQUIRE
(
dir_name
(
R"(\\dir\file.txt)"
)
==
"//dir"
);
REQUIRE
(
dir_name
(
R"(dir\file.txt\)"
)
==
R"(dir\file.txt)"
);
REQUIRE
(
dir_name
(
R"(\dir\file.txt)"
)
==
R"(\dir)"
);
REQUIRE
(
dir_name
(
R"(\\dir\file.txt)"
)
==
R"(\\dir)"
);
REQUIRE
(
dir_name
(
R"(..\file.txt)"
)
==
".."
);
REQUIRE
(
dir_name
(
R"(.\file.txt)"
)
==
"."
);
REQUIRE
(
dir_name
(
R"(c:\\a\b\c\d\file.txt)"
)
==
"c://a/b/c/d"
);
//REQUIRE(dir_name(R"(c:\\a)") == "c://");
REQUIRE
(
dir_name
(
R"(c:\\a\b\c\d\file.txt)"
)
==
R"(c:\\a\b\c\d)"
);
#else
REQUIRE
(
dir_name
(
"dir/"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir///"
)
==
"dir//"
);
REQUIRE
(
dir_name
(
"dir/file"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir/file.txt"
)
==
"dir"
);
REQUIRE
(
dir_name
(
"dir/file.txt/"
)
==
"dir/file.txt"
);
REQUIRE
(
dir_name
(
"/dir/file.txt"
)
==
"/dir"
);
REQUIRE
(
dir_name
(
"//dir/file.txt"
)
==
"//dir"
);
REQUIRE
(
dir_name
(
"../file.txt"
)
==
".."
);
REQUIRE
(
dir_name
(
"./file.txt"
)
==
"."
);
#endif
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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