Skip to content

Commit b2c115b

Browse files
committed
init
1 parent 5d00745 commit b2c115b

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ if(NOT WIN32) # FIXME doable via windows command
9090
add_test(NAME FortranTerminalIO COMMAND bash -c "./termio <<< 0")
9191
endif()
9292

93+
add_executable(special_char special_characters.f90)
94+
target_compile_options(special_char PRIVATE ${FFLAGS})
95+
96+
add_executable(expanduser expanduser.f90)
97+
target_compile_options(expanduser PRIVATE ${FFLAGS})
98+
9399
add_executable(precprob precision_problems.f90)
94100
target_compile_options(precprob PRIVATE ${FFLAGS})
95101
add_test(NAME FortranPrecision COMMAND precprob)

expanduser.f90

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
module ioutils
1+
module fsutils
22
implicit none
33
contains
44

5-
subroutine expanduser(expandhome, indir)
5+
function expanduser(indir)
66

7-
character(*), intent(in), optional :: indir
8-
character(:), allocatable, intent(out) :: expandhome
7+
character(:), allocatable :: expanduser
8+
character(*), intent(in) :: indir
99

10-
character :: filesep
11-
character(256) :: buf
1210
! -- resolve home directory as Fortran does not understand tilde
1311
! works for Linux, Mac, Windows and more
1412

15-
if (present(indir)) then
16-
if (indir(1:1) /= '~') then
17-
expandhome = indir
18-
return
19-
endif
13+
if (len(indir) < 1) then
14+
stop 'must provide path to expand'
15+
elseif (indir(1:1) /= '~') then
16+
expanduser = indir
17+
return
18+
elseif (len(indir) < 3) then
19+
expanduser = homedir()
20+
else
21+
expanduser = homedir() // indir(3:)
2022
endif
2123

24+
end function expanduser
25+
26+
27+
function homedir()
28+
29+
character(:), allocatable :: homedir
30+
character :: filesep
31+
character(256) :: buf
32+
2233
! assume MacOS/Linux/BSD/Cygwin/WSL
2334
filesep = '/'
2435
call get_environment_variable("HOME", buf)
@@ -28,34 +39,29 @@ subroutine expanduser(expandhome, indir)
2839
filesep = char(92)
2940
endif
3041

31-
expandhome = trim(buf) // filesep // indir(3:)
42+
homedir = trim(buf) // filesep
3243

44+
end function homedir
3345

34-
end subroutine expanduser
3546

36-
end module ioutils
47+
end module fsutils
3748

3849
!------ demo
3950

4051
program home
4152

42-
use ioutils
53+
use fsutils
4354
implicit none
4455
! explores what '~' means for paths in Fortran
4556
! Note: when testing, enclose argument in '~/test.txt' quotes or
4657
! shell will expand '~' before it gets to Fortran!
4758

48-
integer :: fid, ios
4959
character(:), allocatable :: expanded
5060
character(256) :: buf
5161

52-
call get_command_argument(1, buf, status=ios)
62+
call get_command_argument(1, buf)
5363

54-
if (ios==0) then
55-
call expanduser(expanded, trim(buf))
56-
else
57-
call expanduser(expanded)
58-
endif
64+
expanded = expanduser(trim(buf))
5965

6066
print '(A)', trim(buf)
6167
print '(A)', expanded

0 commit comments

Comments
 (0)