Skip to content

Commit 84fca8b

Browse files
committed
Obtain os from cmake and python script and make is_windows and path_separator parameters
1 parent 9119970 commit 84fca8b

File tree

6 files changed

+22
-81
lines changed

6 files changed

+22
-81
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ list(
6565
"-DPROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}"
6666
"-DPROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}"
6767
"-DPROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH}"
68+
"-DOS=\\\"${CMAKE_SYSTEM_NAME}\\\""
6869
"-I${PROJECT_SOURCE_DIR}/include"
6970
)
7071

config/fypp_deployment.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import platform
23
import fypp
34
import argparse
45
from joblib import Parallel, delayed
@@ -42,6 +43,7 @@ def pre_process_fypp(args):
4243
kwd.append("-DWITH_QP=True")
4344
if args.with_xdp:
4445
kwd.append("-DWITH_XDP=True")
46+
kwd.append("-DOS=\"{}\"".format(platform.system()))
4547

4648
optparser = fypp.get_option_parser()
4749
options, leftover = optparser.parse_args(args=kwd)

doc/specs/stdlib_io.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,42 +123,6 @@ Provides a text file called `filename` that contains the rank-2 `array`.
123123
{!example/io/example_savetxt.f90!}
124124
```
125125

126-
## `is_windows`
127-
128-
### Status
129-
130-
Experimental
131-
132-
### Description
133-
134-
Returns a logical value indicating whether the current operating system is Windows.
135-
136-
### Syntax
137-
138-
`is_windows = ` [[stdlib_io_filesystem(module):is_windows(function)]] `()`
139-
140-
### Return value
141-
142-
A logical value indicating whether the current operating system is Windows.
143-
144-
## `path_separator`
145-
146-
### Status
147-
148-
Experimental
149-
150-
### Description
151-
152-
Returns the path separator for the current operating system.
153-
154-
### Syntax
155-
156-
`separator = ` [[stdlib_io_filesystem(module):path_separator(function)]] `()`
157-
158-
### Return value
159-
160-
A character value containing the path separator for the current operating system.
161-
162126
## `exists`
163127

164128
### Status

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(fppFiles
1717
stdlib_hash_64bit_pengy.fypp
1818
stdlib_hash_64bit_spookyv2.fypp
1919
stdlib_io.fypp
20+
stdlib_io_filesystem.fypp
2021
stdlib_io_npy.fypp
2122
stdlib_io_npy_load.fypp
2223
stdlib_io_npy_save.fypp
@@ -109,7 +110,6 @@ set(SRC
109110
stdlib_hashmaps.f90
110111
stdlib_hashmap_chaining.f90
111112
stdlib_hashmap_open.f90
112-
stdlib_io_filesystem.F90
113113
stdlib_logger.f90
114114
stdlib_sorting_radix_sort.f90
115115
stdlib_system.F90

src/stdlib_io_filesystem.F90 renamed to src/stdlib_io_filesystem.fypp

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,24 @@
11
! SPDX-Identifier: MIT
22

3-
!> Interaction with the filesystem.
3+
!> Interaction with the filesystem.
44
module stdlib_io_filesystem
55
use stdlib_string_type, only: string_type
66
implicit none
77
private
88

99
public :: temp_dir, is_windows, exists, path_separator, list_dir, mkdir, rmdir, run
1010

11+
#: if OS == 'Windows'
12+
logical, parameter :: is_windows = .true.
13+
character, parameter :: path_separator = '\'
14+
#: else
15+
logical, parameter :: is_windows = .false.
16+
character, parameter :: path_separator = '/'
17+
#: endif
18+
1119
character(*), parameter :: temp_dir = 'temp'
1220

1321
contains
14-
15-
!> Version: experimental
16-
!>
17-
!> Whether the operating system is Windows.
18-
!> [Specification](../page/specs/stdlib_io.html#is_windows)
19-
logical function is_windows()
20-
character(len=255) :: value
21-
integer :: length, stat
22-
23-
call get_environment_variable('OSTYPE', value, length, stat)
24-
if (stat == 0 .and. length > 0 .and. (index(value, 'win') > 0 .or. index(value, 'msys') > 0)) then
25-
is_windows = .true.; return
26-
end if
27-
28-
call get_environment_variable('OS', value, length, stat)
29-
if (stat == 0 .and. length > 0 .and. index(value, 'Windows_NT') > 0) then
30-
is_windows = .true.; return
31-
end if
32-
33-
is_windows = .false.
34-
end function
35-
36-
!> Version: experimental
37-
!>
38-
!> Returns the path separator for the current operating system.
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 function
47-
4822
!> Version: experimental
4923
!>
5024
!> Whether a file or directory exists at the given path.
@@ -89,9 +63,9 @@ subroutine list_dir(dir, files, iostat, iomsg)
8963
end if
9064
end if
9165

92-
listed_contents = temp_dir//path_separator()//'listed_contents.txt'
66+
listed_contents = temp_dir//path_separator//'listed_contents.txt'
9367

94-
if (is_windows()) then
68+
if (is_windows) then
9569
call run('dir /b '//dir//' > '//listed_contents, stat)
9670
else
9771
call run('ls '//dir//' > '//listed_contents, stat)
@@ -127,7 +101,7 @@ subroutine mkdir(dir, iostat, iomsg)
127101
integer, optional, intent(out) :: iostat
128102
character(len=:), allocatable, optional, intent(out) :: iomsg
129103

130-
if (is_windows()) then
104+
if (is_windows) then
131105
call run('mkdir '//dir, iostat, iomsg)
132106
else
133107
call run('mkdir -p '//dir, iostat, iomsg)
@@ -141,7 +115,7 @@ subroutine mkdir(dir, iostat, iomsg)
141115
subroutine rmdir(dir)
142116
character(len=*), intent(in) :: dir
143117

144-
if (is_windows()) then
118+
if (is_windows) then
145119
call run('rmdir /s/q '//dir)
146120
else
147121
call run('rm -rf '//dir)

test/io/test_filesystem.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ subroutine fs_is_windows(error)
4141
integer :: length, stat
4242

4343
call get_environment_variable('HOMEDRIVE', value, length, stat)
44-
if (is_windows()) then
44+
if (is_windows) then
4545
call check(error, stat == 0 .and. length > 0, "Windows should be detected.")
4646
else
4747
call check(error, stat /= 0 .and. length == 0, "Windows should not be detected.")
@@ -91,8 +91,8 @@ subroutine fs_path_separator(error)
9191
call check(error, .not. exists(outer_dir), "Directory should not exist.")
9292
call mkdir(outer_dir)
9393
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.")
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.")
9696
call rmdir(outer_dir)
9797
end subroutine
9898

@@ -223,7 +223,7 @@ subroutine fs_list_dir_one_file_one_dir(error)
223223
call test_failed(error, "Creating file 1 in directory '"//temp_list_dir//"' failed."); return
224224
end if
225225

226-
if (is_windows()) then
226+
if (is_windows) then
227227
call mkdir(temp_list_dir//'\'//dir, stat)
228228
else
229229
call mkdir(temp_list_dir//'/'//dir, stat)
@@ -263,7 +263,7 @@ subroutine fs_rmdir_with_contents(error)
263263
call check(error, .not. exists(dir), "Directory should not exist.")
264264
call mkdir(dir)
265265
call check(error, exists(dir), "Directory should exist.")
266-
if (is_windows()) then
266+
if (is_windows) then
267267
call mkdir(dir//'\'//'another_dir')
268268
else
269269
call mkdir(dir//'/'//'another_dir')

0 commit comments

Comments
 (0)