-
Notifications
You must be signed in to change notification settings - Fork 192
feat: path functions is_abs
and abs_path
#1027
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
6
commits into
fortran-lang:master
Choose a base branch
from
wassup05:abs_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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8c8b81
added interfaces
wassup05 522129f
added functions of the interfaces
wassup05 04b1bed
added tests
wassup05 be3d5b5
added example
wassup05 53a4368
added docs
wassup05 62e1efc
`is_abs` -> `is_abs_path` and small doc changes
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,29 @@ | ||
! Illustrate the usage of `abs_path`, `is_abs_path` | ||
program example_path_abs | ||
use stdlib_system, only: abs_path, is_abs_path | ||
use stdlib_error, only: state_type | ||
implicit none | ||
|
||
character(*), parameter :: path = "path/to/check" | ||
character(:), allocatable :: absolute_path | ||
type(state_type) :: err | ||
|
||
if (is_abs_path(path)) then | ||
print *, "Path is absolute!" | ||
! terminate the program since path is already absolute | ||
stop | ||
else | ||
print *, "Path is not absolute!" | ||
end if | ||
|
||
! get the absolute path | ||
absolute_path = abs_path(path, err) | ||
|
||
if (err%error()) then | ||
! there was an error! print it | ||
print *, "error converting to absolute path: " // err%print() | ||
else | ||
print *, "absolute path => " // absolute_path | ||
end if | ||
end program example_path_abs | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
submodule(stdlib_system) stdlib_system_path | ||
use stdlib_ascii, only: reverse | ||
use stdlib_strings, only: chomp, join | ||
use stdlib_strings, only: chomp, join, starts_with | ||
use stdlib_string_type, only: string_type, char, move | ||
contains | ||
module function join2_char_char(p1, p2) result(path) | ||
|
@@ -167,4 +167,58 @@ module function dir_name_string(p) result(dir) | |
|
||
call split_path(p, dir, temp) | ||
end function dir_name_string | ||
|
||
module logical function is_abs_path_char(p) | ||
character(len=*), intent(in) :: p | ||
character(len=1) :: sep | ||
|
||
sep = path_sep() | ||
|
||
if (sep == '/') then | ||
! should start with '/' | ||
is_abs_path_char = starts_with(p, sep) | ||
else | ||
! should be either an UNC path like '\\server\host...' | ||
! or should be starting with a drive letter like 'C:\Users\...' | ||
is_abs_path_char = starts_with(p(2:), ':\') .or. starts_with(p, '\\') | ||
end if | ||
end function is_abs_path_char | ||
|
||
module logical function is_abs_path_string(p) | ||
type(string_type), intent(in) :: p | ||
|
||
is_abs_path_string = is_abs_path(char(p)) | ||
end function is_abs_path_string | ||
|
||
module function abs_path_char(p, err) result(abs_p) | ||
character(len=*), intent(in) :: p | ||
type(state_type), optional, intent(out) :: err | ||
character(len=:), allocatable :: abs_p | ||
|
||
type(state_type) :: err0 | ||
character(:), allocatable :: cwd | ||
|
||
! get the current working directory | ||
call get_cwd(cwd, err0) | ||
|
||
if (err0%error()) then | ||
abs_p = '' | ||
call err0%handle(err) | ||
end if | ||
|
||
! join the cwd and path | ||
abs_p = cwd / p | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if |
||
end function abs_path_char | ||
|
||
module function abs_path_string(p, err) result(abs_p) | ||
type(string_type), intent(in) :: p | ||
type(state_type), optional, intent(out) :: err | ||
type(string_type) :: abs_p | ||
|
||
character(len=:), allocatable :: res | ||
|
||
res = abs_path(char(p), err) | ||
|
||
call move(res, abs_p) | ||
end function abs_path_string | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
is_abs_path
function only tests the form of a path specification, not the fact that it must actually exist in the filesystem. So I wonder if giving these functions the same name asis_file
,is_directory
etc., that instead test the actual filesystem, wouldn't be confusing for some. Also, do other library implementations only check the form, or do they require the actual path to exist?