-
Notifications
You must be signed in to change notification settings - Fork 191
feat: creating and removing empty directories #1011
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:directory
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.
+362
−5
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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,17 @@ | ||
! Illustrate the usage of make_directory | ||
program example_make_directory | ||
use stdlib_system, only: make_directory, is_directory | ||
use stdlib_error, only: state_type | ||
implicit none | ||
|
||
type(state_type) :: err | ||
|
||
call make_directory("test", err=err) | ||
|
||
if (err%error()) then | ||
print *, err%print() | ||
else | ||
print *, "directory created sucessfully" | ||
end if | ||
|
||
end program example_make_directory |
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,17 @@ | ||
! Illustrate the usage of remove_directory | ||
program example_remove_directory | ||
use stdlib_system, only: make_directory, is_directory, remove_directory | ||
use stdlib_error, only: state_type | ||
implicit none | ||
|
||
type(state_type) :: err | ||
|
||
call remove_directory("directory_to_be_removed", err) | ||
|
||
if (err%error()) then | ||
print *, err%print() | ||
else | ||
print *, "directory removed successfully" | ||
end if | ||
|
||
end program example_remove_directory |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ module stdlib_system | |
use, intrinsic :: iso_c_binding, only : c_int, c_long, c_ptr, c_null_ptr, c_int64_t, c_size_t, & | ||
c_f_pointer | ||
use stdlib_kinds, only: int64, dp, c_bool, c_char | ||
use stdlib_strings, only: to_c_char | ||
use stdlib_strings, only: to_c_char, to_string | ||
use stdlib_error, only: state_type, STDLIB_SUCCESS, STDLIB_FS_ERROR | ||
implicit none | ||
private | ||
|
@@ -100,6 +100,36 @@ module stdlib_system | |
!! | ||
public :: is_directory | ||
|
||
!! version: experimental | ||
!! | ||
!! Makes an empty directory. | ||
!! ([Specification](../page/specs/stdlib_system.html#make_directory)) | ||
!! | ||
!! ### Summary | ||
!! Creates an empty directory with particular permissions. | ||
!! | ||
!! ### Description | ||
!! This function makes an empty directory according to the path provided. | ||
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted | ||
!! appropriate error message is returned whenever any error occur. | ||
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. Capitalise "appropriate" to be consistent. (Same down below). |
||
!! | ||
public :: make_directory | ||
|
||
!! version: experimental | ||
!! | ||
!! Removes an empty directory. | ||
!! ([Specification](../page/specs/stdlib_system.html#remove_directory)) | ||
!! | ||
!! ### Summary | ||
!! Deletes an empty directory. | ||
!! | ||
!! ### Description | ||
!! This function deletes an empty directory according to the path provided. | ||
!! Relative paths as well as on Windows paths involving either `/` or `\` are accepted. | ||
!! appropriate error message is returned whenever any error occur. | ||
!! | ||
public :: remove_directory | ||
|
||
!! version: experimental | ||
!! | ||
!! Deletes a specified file from the filesystem. | ||
|
@@ -690,6 +720,96 @@ end function stdlib_is_directory | |
|
||
end function is_directory | ||
|
||
function c_get_strerror() result(str) | ||
character(len=:), allocatable :: str | ||
|
||
interface | ||
type(c_ptr) function strerror(len) bind(C, name='stdlib_strerror') | ||
import c_size_t, c_ptr | ||
implicit none | ||
integer(c_size_t), intent(out) :: len | ||
end function strerror | ||
end interface | ||
|
||
type(c_ptr) :: c_str_ptr | ||
integer(c_size_t) :: len, i | ||
character(kind=c_char), pointer :: c_str(:) | ||
|
||
c_str_ptr = strerror(len) | ||
|
||
call c_f_pointer(c_str_ptr, c_str, [len]) | ||
|
||
allocate(character(len=len) :: str) | ||
|
||
do concurrent (i=1:len) | ||
str(i:i) = c_str(i) | ||
end do | ||
end function c_get_strerror | ||
|
||
!! makes an empty directory | ||
subroutine make_directory(path, mode, err) | ||
character(len=*), intent(in) :: path | ||
integer, intent(in), optional :: mode | ||
type(state_type), optional, intent(out) :: err | ||
|
||
integer :: code | ||
type(state_type) :: err0 | ||
|
||
|
||
interface | ||
integer function stdlib_make_directory(cpath, cmode) bind(C, name='stdlib_make_directory') | ||
import c_char | ||
character(kind=c_char), intent(in) :: cpath(*) | ||
integer, intent(in) :: cmode | ||
end function stdlib_make_directory | ||
end interface | ||
|
||
if (is_windows() .and. present(mode)) then | ||
! _mkdir() doesn't have a `mode` argument | ||
err0 = state_type(STDLIB_FS_ERROR, "mode argument not present for Windows") | ||
call err0%handle(err) | ||
return | ||
end if | ||
|
||
code = stdlib_make_directory(to_c_char(trim(path)), mode) | ||
|
||
select case (code) | ||
case (0) | ||
return | ||
case default | ||
! error | ||
err0 = state_type(STDLIB_FS_ERROR, "code:", to_string(code)//',', c_get_strerror()) | ||
call err0%handle(err) | ||
end select | ||
end subroutine make_directory | ||
|
||
!! Removes an empty directory | ||
subroutine remove_directory(path, err) | ||
character(len=*), intent(in) :: path | ||
type(state_type), optional, intent(out) :: err | ||
|
||
integer :: code | ||
type(state_type) :: err0 | ||
|
||
interface | ||
integer function stdlib_remove_directory(cpath) bind(C, name='stdlib_remove_directory') | ||
import c_char | ||
character(kind=c_char), intent(in) :: cpath(*) | ||
end function stdlib_remove_directory | ||
end interface | ||
|
||
code = stdlib_remove_directory(to_c_char(trim(path))) | ||
|
||
select case (code) | ||
case (0) | ||
return | ||
case default | ||
! error | ||
err0 = state_type(STDLIB_FS_ERROR, "code:", to_string(code)//',', c_get_strerror()) | ||
call err0%handle(err) | ||
end select | ||
end subroutine remove_directory | ||
|
||
!> Returns the file path of the null device for the current operating system. | ||
!> | ||
!> Version: Helper function. | ||
|
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,46 @@ | ||
#include <stddef.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#ifdef _WIN32 | ||
#include <direct.h> | ||
#else | ||
#include <unistd.h> | ||
#endif /* ifdef _WIN32 */ | ||
|
||
char* stdlib_strerror(size_t* len){ | ||
char* err = strerror(errno); | ||
*len = strlen(err); | ||
return err; | ||
} | ||
|
||
int stdlib_make_directory(const char* path, mode_t mode){ | ||
int code; | ||
#ifdef _WIN32 | ||
code = _mkdir(path); | ||
#else | ||
code = mkdir(path, mode); | ||
#endif /* ifdef _WIN32 */ | ||
|
||
if (!code){ | ||
return 0; | ||
} | ||
|
||
return errno; | ||
} | ||
|
||
int stdlib_remove_directory(const char* path){ | ||
int code; | ||
#ifdef _WIN32 | ||
code = _rmdir(path); | ||
#else | ||
code = rmdir(path); | ||
#endif /* ifdef _WIN32 */ | ||
|
||
if (!code){ | ||
return 0; | ||
} | ||
|
||
return errno; | ||
} |
Oops, something went wrong.
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.
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.
Add period to end of sentence.