-
Notifications
You must be signed in to change notification settings - Fork 193
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
Changes from 6 commits
657b312
f41dee4
572ad46
8a10f26
1bd58b7
ea71960
c561108
272eb67
5147094
9e64094
ca867f8
62bef00
1c87df9
45824b5
3501bc9
ee40f44
e6add70
616040d
812b5ed
a21a48b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -532,3 +532,180 @@ The file is removed from the filesystem if the operation is successful. If the o | |||||||||||||||||
```fortran | ||||||||||||||||||
{!example/system/example_delete_file.f90!} | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
## `joinpath` - Joins the provided paths according to the OS | ||||||||||||||||||
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
### Status | ||||||||||||||||||
|
||||||||||||||||||
Experimental | ||||||||||||||||||
|
||||||||||||||||||
### Description | ||||||||||||||||||
|
||||||||||||||||||
This interface joins the paths provided to it according to the platform specific path-separator. | ||||||||||||||||||
i.e `\` for windows and `/` for others | ||||||||||||||||||
|
||||||||||||||||||
### Syntax | ||||||||||||||||||
|
||||||||||||||||||
`res = [[stdlib_system(module):joinpath(interface)]] (p1, p2)` | ||||||||||||||||||
`res = [[stdlib_system(module):joinpath(interface)]] (p)` | ||||||||||||||||||
|
||||||||||||||||||
### Class | ||||||||||||||||||
Pure function | ||||||||||||||||||
|
||||||||||||||||||
### Arguments | ||||||||||||||||||
|
||||||||||||||||||
`p1, p2`: Shall be a character string. It is an `intent(in)` argument. | ||||||||||||||||||
|
!> Moves the allocated character scalar from 'from' to 'to' | |
!> [Specifications](../page/specs/stdlib_string_type.html#move) | |
interface move | |
module procedure :: move_string_string | |
module procedure :: move_string_char | |
module procedure :: move_char_string | |
module procedure :: move_char_char | |
end interface move |
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.
I have added the relevant changes... But it doesn't use move
, it uses assignment(=)
and char
, Let me know what you think of it.
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.
It works @wassup05, however, there are two copies: string->char
for the input arguments, and then char -> string
on the assignment. Using move
would at least avoid incurring the last copy. Just do:
character(:), allocatable :: join_char
join_char = join_path(char(a),char(b))
call move(from=join_char,to=...result variable...)
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.
Because the internal string_type
representation is just an allocatable character variable, I would also support (to be discussed with the community as a separate PR) the implementation of a tiny zero-copy "view" function for the string type, see here:
!> Zero-copy view of the string as a character array
pure function view(string) result(char_array)
type(string_type), intent(in), target :: string
character(:), pointer :: char_array
if (allocated(string%raw)) then
char_array => string%raw
else
nullify(char_array)
end if
end function view
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
wassup05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ ADD_EXAMPLE(process_5) | |
ADD_EXAMPLE(process_6) | ||
ADD_EXAMPLE(process_7) | ||
ADD_EXAMPLE(sleep) | ||
ADD_EXAMPLE(path_1) |
jalvesz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
program example_path | ||
use stdlib_system, only: joinpath, operator(/), splitpath, ISWIN, dirname, basename | ||
character(len=:), allocatable :: p1, p2, head, tail | ||
character(len=20) :: parr(4) | ||
|
||
if (ISWIN) then | ||
p1 = 'C:'/'Users'/'User1'/'Desktop' | ||
parr = [character(len=20) :: 'C:', 'Users', 'User1', 'Desktop'] | ||
p2 = joinpath(parr) | ||
|
||
! p1 == p2 = 'C:\Users\User1\Desktop' | ||
print *, p1 | ||
print *, "p1 == p2: ", p1 == p2 | ||
|
||
call splitpath(p1, head, tail) | ||
print *, p1 // " -> " // head // " + " // tail | ||
|
||
call splitpath(head, p1, tail) | ||
print *, head // " -> " // p1 // " + " // tail | ||
|
||
print *, 'dirname of '// p1 // ' -> ' // dirname(p1) | ||
print *, 'basename of '// p1 // ' -> ' // basename(p1) | ||
else | ||
p1 = ''/'home'/'User1'/'Desktop' | ||
parr = [character(len=20) :: '', 'home', 'User1', 'Desktop'] | ||
p2 = joinpath(parr) | ||
|
||
! p1 == p2 = '/home/User1/Desktop' | ||
print *, p1 | ||
print *, "p1 == p2: ", p1 == p2 | ||
|
||
call splitpath(p1, head, tail) | ||
print *, p1 // " -> " // head // " + " // tail | ||
|
||
call splitpath(head, p1, tail) | ||
print *, head // " -> " // p1 // " + " // tail | ||
|
||
print *, 'dirname of '// p1 // ' -> ' // dirname(p1) | ||
print *, 'basename of '// p1 // ' -> ' // basename(p1) | ||
end if | ||
end program example_path |
Uh oh!
There was an error while loading. Please reload this page.