-
Notifications
You must be signed in to change notification settings - Fork 191
stdlib_system
: essential path functionality
#999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wassup05
wants to merge
15
commits into
fortran-lang:master
Choose a base branch
from
wassup05:path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+600
−15
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
657b312
added interfaces and necessary build stuff
wassup05 f41dee4
implemented interfaces
wassup05 572ad46
example
wassup05 8a10f26
tests added
wassup05 1bd58b7
added specs
wassup05 ea71960
an edge case handled
wassup05 c561108
separated examples according to OS and functions
wassup05 272eb67
further split examples and fix some autodoc links issues due to wrong…
jalvesz 5147094
misslocation of source file
jalvesz 9e64094
fix example program names
jalvesz ca867f8
fix example_path_join.f90
wassup05 62bef00
Consistent naming
wassup05 1c87df9
windows test case for path with spaces
wassup05 45824b5
remove pre-processor parameters
wassup05 3501bc9
Merge branch 'master' of github.com:wassup05/stdlib into path
wassup05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
! Usage of base_name | ||
program example_path_base_name | ||
use stdlib_system, only: base_name, OS_TYPE, OS_WINDOWS | ||
character(len=:), allocatable :: p1 | ||
|
||
if(OS_TYPE() == OS_WINDOWS) then | ||
p1 = 'C:\Users' | ||
else | ||
p1 = '/home' | ||
endif | ||
|
||
print *, 'base name of '// p1 // ' -> ' // base_name(p1) | ||
! base name of C:\Users -> Users | ||
! OR | ||
! base name of /home -> home | ||
end program example_path_base_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
! Usage of dir_name | ||
program example_path_dir_name | ||
use stdlib_system, only: dir_name, OS_TYPE, OS_WINDOWS | ||
character(len=:), allocatable :: p1, head, tail | ||
|
||
if(OS_TYPE() == OS_WINDOWS) then | ||
p1 = 'C:\Users' ! C:\Users | ||
else | ||
p1 = '/home' ! /home | ||
endif | ||
|
||
print *, 'dir_name of '// p1 // ' -> ' // dir_name(p1) | ||
! dir_name of C:\Users -> C:\ | ||
! OR | ||
! dir_name of /home -> / | ||
end program example_path_dir_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
! Usage of join_path, operator(/) | ||
program example_path_join | ||
use stdlib_system, only: join_path, operator(/), OS_TYPE, OS_WINDOWS | ||
character(len=:), allocatable :: p1, p2, p3 | ||
character(len=20) :: parr(4) | ||
|
||
if(OS_TYPE() == OS_WINDOWS) then | ||
p1 = 'C:'/'Users'/'User1'/'Desktop' | ||
p2 = join_path('C:\Users\User1', 'Desktop') | ||
parr = [character(len=20) :: 'C:', 'Users', 'User1', 'Desktop'] | ||
p3 = join_path(parr) | ||
else | ||
p1 = ''/'home'/'User1'/'Desktop' | ||
p2 = join_path('/home/User1', 'Desktop') | ||
parr = [character(len=20) :: '', 'home', 'User1', 'Desktop'] | ||
p3 = join_path(parr) | ||
end if | ||
|
||
! (p1 == p2 == p3) = '/home/User1/Desktop' OR 'C:\Users\User1\Desktop' | ||
print *, p1 ! /home/User1/Desktop OR 'C:\Users\User1\Desktop' | ||
print *, "p1 == p2: ", p1 == p2 ! T | ||
print *, "p2 == p3: ", p2 == p3 ! T | ||
end program example_path_join |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
! Usage of split_path | ||
program example_path_split_path | ||
use stdlib_system, only: join_path, split_path, OS_TYPE, OS_WINDOWS | ||
character(len=:), allocatable :: p1, head, tail | ||
|
||
if(OS_TYPE() == OS_WINDOWS) then | ||
p1 = join_path('C:\Users\User1', 'Desktop') ! C:\Users\User1\Desktop | ||
else | ||
p1 = join_path('/home/User1', 'Desktop') ! /home/User1/Desktop | ||
endif | ||
|
||
call split_path(p1, head, tail) | ||
! head = /home/User1 OR C:\Users\User1, tail = Desktop | ||
print *, p1 // " -> " // head // " + " // tail | ||
! C:\Users\User1\Desktop -> C:\Users\User1 + Desktop | ||
! OR | ||
! /home/User1/Desktop -> /home/User1 + Desktop | ||
|
||
call split_path(head, p1, tail) | ||
! p1 = /home OR C:\Users, tail = User1 | ||
print *, head // " -> " // p1 // " + " // tail | ||
! C:\Users\User1 -> C:\Users + User1 | ||
! OR | ||
! /home/User1 -> /home + User1 | ||
end program example_path_split_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
submodule(stdlib_system) stdlib_system_path | ||
use stdlib_ascii, only: reverse | ||
use stdlib_strings, only: chomp, find, join | ||
contains | ||
module function join2(p1, p2) result(path) | ||
character(:), allocatable :: path | ||
character(*), intent(in) :: p1, p2 | ||
|
||
path = trim(p1) // path_sep() // trim(p2) | ||
end function join2 | ||
|
||
module function joinarr(p) result(path) | ||
character(:), allocatable :: path | ||
character(*), intent(in) :: p(:) | ||
|
||
path = join(p, path_sep()) | ||
end function joinarr | ||
|
||
module function join_op(p1, p2) result(path) | ||
character(:), allocatable :: path | ||
character(*), intent(in) :: p1, p2 | ||
|
||
path = join_path(p1, p2) | ||
end function join_op | ||
|
||
module subroutine split_path(p, head, tail) | ||
character(*), intent(in) :: p | ||
character(:), allocatable, intent(out) :: head, tail | ||
character(:), allocatable :: temp | ||
integer :: i | ||
character(len=1) :: sep | ||
sep = path_sep() | ||
|
||
! Empty string, return (.,'') | ||
if (trim(p) == '') then | ||
head = '.' | ||
tail = '' | ||
return | ||
end if | ||
|
||
! Remove trailing path separators | ||
temp = trim(chomp(trim(p), sep)) | ||
|
||
if (temp == '') then | ||
head = sep | ||
tail = '' | ||
return | ||
end if | ||
|
||
i = find(reverse(temp), sep) | ||
|
||
! if no `pathsep`, then it probably was a root dir like `C:\` | ||
if (i == 0) then | ||
head = temp // sep | ||
tail = '' | ||
return | ||
end if | ||
|
||
head = temp(:len(temp)-i) | ||
|
||
! child of a root directory | ||
if (find(head, sep) == 0) then | ||
head = head // sep | ||
end if | ||
|
||
tail = temp(len(temp)-i+2:) | ||
end subroutine split_path | ||
|
||
module function base_name(p) result(base) | ||
character(:), allocatable :: base, temp | ||
character(*), intent(in) :: p | ||
|
||
call split_path(p, temp, base) | ||
end function base_name | ||
|
||
module function dir_name(p) result(dir) | ||
character(:), allocatable :: dir, temp | ||
character(*), intent(in) :: p | ||
|
||
call split_path(p, dir, temp) | ||
end function dir_name | ||
end submodule stdlib_system_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ ADDTEST(filesystem) | |
ADDTEST(os) | ||
ADDTEST(sleep) | ||
ADDTEST(subprocess) | ||
ADDTEST(path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
module test_path | ||
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test | ||
use stdlib_system, only: join_path, operator(/), split_path, OS_TYPE, OS_WINDOWS | ||
implicit none | ||
contains | ||
!> Collect all exported unit tests | ||
subroutine collect_suite(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
new_unittest('test_join_path', test_join_path), & | ||
new_unittest('test_join_path_operator', test_join_path_op), & | ||
new_unittest('test_split_path', test_split_path) & | ||
] | ||
end subroutine collect_suite | ||
|
||
subroutine checkpath(error, funcname, expected, got) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=*), intent(in) :: funcname | ||
character(len=*), intent(in) :: expected | ||
character(len=:), allocatable :: got | ||
character(len=:), allocatable :: message | ||
|
||
message = "'"//funcname//"'"//" error: Expected '"// expected // "' but got '" // got // "'" | ||
call check(error, expected == got, message) | ||
|
||
end subroutine checkpath | ||
|
||
subroutine test_join_path(error) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=:), allocatable :: path | ||
character(len=20) :: paths(5) | ||
|
||
if (OS_TYPE() == OS_WINDOWS) then | ||
path = join_path('C:\Users', 'Alice') | ||
call checkpath(error, 'join_path', 'C:\Users\Alice', path) | ||
if (allocated(error)) return | ||
|
||
paths = [character(20) :: 'C:','Users','Bob','Pictures','2025'] | ||
path = join_path(paths) | ||
|
||
call checkpath(error, 'join_path', 'C:\Users\Bob\Pictures\2025', path) | ||
if (allocated(error)) return | ||
|
||
path = join_path('"C:\Users\John Doe"', 'Pictures\2025') ! path with spaces | ||
call checkpath(error, 'join_path', '"C:\Users\John Doe"\Pictures\2025', path) | ||
if (allocated(error)) return | ||
else | ||
path = join_path('/home', 'Alice') | ||
call checkpath(error, 'join_path', '/home/Alice', path) | ||
if (allocated(error)) return | ||
|
||
paths = [character(20) :: '','home','Bob','Pictures','2025'] | ||
path = join_path(paths) | ||
|
||
call checkpath(error, 'join_path', '/home/Bob/Pictures/2025', path) | ||
if (allocated(error)) return | ||
end if | ||
end subroutine test_join_path | ||
|
||
!> Test the operator | ||
subroutine test_join_path_op(error) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=:), allocatable :: path | ||
|
||
if (OS_TYPE() == OS_WINDOWS) then | ||
path = 'C:'/'Users'/'Alice'/'Desktop' | ||
call checkpath(error, 'join_path operator', 'C:\Users\Alice\Desktop', path) | ||
if (allocated(error)) return | ||
else | ||
path = ''/'home'/'Alice'/'.config' | ||
call checkpath(error, 'join_path operator', '/home/Alice/.config', path) | ||
if (allocated(error)) return | ||
end if | ||
end subroutine test_join_path_op | ||
|
||
subroutine test_split_path(error) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=:), allocatable :: head, tail | ||
|
||
call split_path('', head, tail) | ||
call checkpath(error, 'split_path-head', '.', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', '', tail) | ||
if (allocated(error)) return | ||
|
||
if (OS_TYPE() == OS_WINDOWS) then | ||
call split_path('\\\\', head, tail) | ||
call checkpath(error, 'split_path-head', '\', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', '', tail) | ||
if (allocated(error)) return | ||
|
||
call split_path('C:\', head, tail) | ||
call checkpath(error, 'split_path-head', 'C:\', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', '', tail) | ||
if (allocated(error)) return | ||
|
||
call split_path('C:\Users\Alice\\\\\', head, tail) | ||
call checkpath(error, 'split_path-head', 'C:\Users', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', 'Alice', tail) | ||
if (allocated(error)) return | ||
else | ||
call split_path('/////', head, tail) | ||
call checkpath(error, 'split_path-head', '/', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', '', tail) | ||
if (allocated(error)) return | ||
|
||
call split_path('/home/Alice/foo/bar.f90///', head, tail) | ||
call checkpath(error, 'split_path-head', '/home/Alice/foo', head) | ||
if (allocated(error)) return | ||
call checkpath(error, 'split_path-tail', 'bar.f90', tail) | ||
if (allocated(error)) return | ||
end if | ||
end subroutine test_split_path | ||
|
||
end module test_path | ||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_path, only : collect_suite | ||
|
||
implicit none | ||
|
||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("path", collect_suite) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program tester |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.