Skip to content

Commit 9da3ea7

Browse files
committed
Add path_separator() with tests
1 parent eeeb634 commit 9da3ea7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/stdlib_io_filesystem.F90

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ module stdlib_io_filesystem
66
implicit none
77
private
88

9-
public :: temp_dir, is_windows, exists, list_dir, mkdir, rmdir, run
9+
public :: temp_dir, is_windows, exists, path_separator, list_dir, mkdir, rmdir, run
1010

1111
character(*), parameter :: temp_dir = 'temp'
12-
character(*), parameter :: listed_contents = temp_dir//'/listed_contents.txt'
1312

1413
contains
1514

@@ -34,6 +33,18 @@ logical function is_windows()
3433
is_windows = .false.
3534
end
3635

36+
!> Version: experimental
37+
!>
38+
!> Separator for paths.
39+
!> [Specification](../page/specs/stdlib_io.html#path_separator)
40+
character function path_separator()
41+
if (is_windows()) then
42+
path_separator = '\'
43+
else
44+
path_separator = '/'
45+
end if
46+
end
47+
3748
!> Version: experimental
3849
!>
3950
!> Whether a file or directory exists at the given path.
@@ -65,6 +76,7 @@ subroutine list_dir(dir, files, iostat, iomsg)
6576

6677
integer :: unit, stat
6778
character(len=256) :: line
79+
character(:), allocatable :: listed_contents
6880

6981
stat = 0
7082

@@ -77,6 +89,8 @@ subroutine list_dir(dir, files, iostat, iomsg)
7789
end if
7890
end if
7991

92+
listed_contents = temp_dir//path_separator()//'listed_contents.txt'
93+
8094
if (is_windows()) then
8195
call run('dir /b '//dir//' > '//listed_contents, stat)
8296
else

test/io/test_filesystem.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ subroutine collect_filesystem(testsuite)
2121
new_unittest("fs_file_not_exists", fs_file_not_exists, should_fail=.true.), &
2222
new_unittest("fs_file_exists", fs_file_exists), &
2323
new_unittest("fs_current_dir_exists", fs_current_dir_exists), &
24+
new_unittest("fs_path_separator", fs_path_separator), &
2425
new_unittest("fs_run_invalid_command", fs_run_invalid_command, should_fail=.true.), &
2526
new_unittest("fs_run_with_invalid_option", fs_run_with_invalid_option, should_fail=.true.), &
2627
new_unittest("fs_run_valid_command", fs_run_valid_command), &
@@ -80,6 +81,21 @@ subroutine fs_current_dir_exists(error)
8081
call check(error, is_existing, "Current directory should not fail.")
8182
end
8283

84+
subroutine fs_path_separator(error)
85+
type(error_type), allocatable, intent(out) :: error
86+
87+
character(*), parameter :: outer_dir = "path_separator_outer"
88+
character(*), parameter :: inner_dir = "path_separator_inner"
89+
90+
call rmdir(outer_dir)
91+
call check(error, .not. exists(outer_dir), "Directory should not exist.")
92+
call mkdir(outer_dir)
93+
call check(error, exists(outer_dir), "Outer directory should now exist.")
94+
call mkdir(outer_dir//path_separator()//inner_dir)
95+
call check(error, exists(outer_dir//path_separator()//inner_dir), "Inner directory should now exist.")
96+
call rmdir(outer_dir)
97+
end
98+
8399
subroutine fs_run_invalid_command(error)
84100
type(error_type), allocatable, intent(out) :: error
85101

0 commit comments

Comments
 (0)