-
Notifications
You must be signed in to change notification settings - Fork 195
feat: get_file_size
- gets the size of a file in bytes
#1029
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
8
commits into
fortran-lang:master
Choose a base branch
from
wassup05:file_size
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
8 commits
Select commit
Hold shift + click to select a range
8a36613
added fortran function
wassup05 f07e3b7
implemented C interface
wassup05 15982fa
added example
wassup05 157d63f
added tests
wassup05 a3ddc1d
added docs
wassup05 d047e1f
add err handling to example
wassup05 c58be5e
add err handling to `read` in tests
wassup05 fed3de5
make test work on windows
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,21 @@ | ||
program example_get_file_size | ||
use, intrinsic :: iso_fortran_env, only: int64 | ||
use stdlib_system, only: get_file_size | ||
use stdlib_error, only: state_type | ||
implicit none | ||
|
||
type(state_type) :: err | ||
integer(int64) :: size | ||
|
||
character(*), parameter :: path = "path/to/check" | ||
|
||
size = get_file_size(path, err) | ||
|
||
if (err%ok()) then | ||
print *, "Size in bytes = ", size | ||
else | ||
print *, "Error!: ", err%print() | ||
end if | ||
|
||
end program example_get_file_size | ||
|
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -182,6 +182,21 @@ module stdlib_system | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public :: set_cwd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! version: experimental | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! Gets the size (in bytes) of a file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! ([Specification](../page/specs/stdlib_system.html#get_file_size)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! ### Summary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! Gets the size (in bytes) of a file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! ### Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! This function gets the size (in bytes) of a file in the filesystem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! It follows symbolic links, gives an error if the symbolic link or the `path` points to a directory instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public :: get_file_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! version: experimental | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!! Deletes a specified file from the filesystem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1112,6 +1127,39 @@ end function stdlib_set_cwd | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end subroutine set_cwd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer(int64) function get_file_size(path, err) result(size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
character(*), intent(in) :: path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type(state_type), optional, intent(out) :: err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
interface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer function stdlib_get_file_size(path, size) bind(C) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import c_char, c_int64_t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
character(kind=c_char), intent(in) :: path(*) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer(c_int64_t), intent(out) :: size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end function stdlib_get_file_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end interface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type(state_type) :: err0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer :: code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
integer(c_int64_t) :: c_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (is_directory(path)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
err0 = FS_ERROR("Is a directory!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
call err0%handle(err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
code = stdlib_get_file_size(to_c_char(trim(path)), c_size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (code /= 0) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
err0 = FS_ERROR_CODE(code, c_get_strerror()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
call err0%handle(err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1142
to
+1158
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. This functionality can be achieved by Fortran intrinsics only. Perhaps that would be a good way to avoid cross-platform CI issues and avoid the C backend calls?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size = c_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end function get_file_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
!> 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
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.
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.
We have not used
get_*
patterns anywhere else so, this function may perhaps just befile_size
? The fact that it is a function already means we'reget_
ting stuff.