forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also includes a minimal CMake build system. We can improve the build system in further PRs. Fixes fortran-lang#16.
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
cmake_minimum_required(VERSION 3.5.0 FATAL_ERROR) | ||
|
||
enable_language(Fortran) | ||
|
||
project(stdlib) | ||
|
||
enable_testing() | ||
|
||
add_subdirectory(src) |
This file contains 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,12 @@ | ||
set(SRC | ||
stdlib_types.f90 | ||
stdlib_io.f90 | ||
) | ||
|
||
add_library(fortran_stdlib ${SRC}) | ||
|
||
install(TARGETS fortran_stdlib | ||
RUNTIME DESTINATION bin | ||
ARCHIVE DESTINATION lib | ||
LIBRARY DESTINATION lib | ||
) |
This file contains 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,104 @@ | ||
module stdlib_io | ||
use stdlib_types | ||
implicit none | ||
private | ||
public loadtxt, savetxt | ||
|
||
contains | ||
|
||
subroutine loadtxt(filename, d) | ||
! Loads a 2D array from a text file. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
! Filename to load the array from | ||
character(len=*), intent(in) :: filename | ||
! The array 'd' will be automatically allocated with the correct dimensions | ||
real(dp), allocatable, intent(out) :: d(:, :) | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(dp), allocatable :: data(:, :) | ||
! call loadtxt("log.txt", data) ! 'data' will be automatically allocated | ||
! | ||
! Where 'log.txt' contains for example:: | ||
! | ||
! 1 2 3 | ||
! 2 4 6 | ||
! 8 9 10 | ||
! 11 12 13 | ||
! ... | ||
! | ||
character :: c | ||
integer :: s, ncol, nrow, ios, i | ||
logical :: lastwhite | ||
real(dp) :: r | ||
|
||
open(newunit=s, file=filename, status="old") | ||
|
||
! determine number of columns | ||
ncol = 0 | ||
lastwhite = .true. | ||
do | ||
read(s, '(a)', advance='no', iostat=ios) c | ||
if (ios /= 0) exit | ||
if (lastwhite .and. .not. whitechar(c)) ncol = ncol + 1 | ||
lastwhite = whitechar(c) | ||
end do | ||
|
||
rewind(s) | ||
|
||
! determine number or rows | ||
nrow = 0 | ||
do | ||
read(s, *, iostat=ios) r | ||
if (ios /= 0) exit | ||
nrow = nrow + 1 | ||
end do | ||
|
||
rewind(s) | ||
|
||
allocate(d(nrow, ncol)) | ||
do i = 1, nrow | ||
read(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
subroutine savetxt(filename, d) | ||
! Saves a 2D array into a textfile. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
character(len=*), intent(in) :: filename ! File to save the array to | ||
real(dp), intent(in) :: d(:, :) ! The 2D array to save | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! real(dp) :: data(3, 2) | ||
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
close(s) | ||
end subroutine | ||
|
||
|
||
logical function whitechar(char) ! white character | ||
! returns .true. if char is space (32) or tab (9), .false. otherwise | ||
character, intent(in) :: char | ||
if (iachar(char) == 32 .or. iachar(char) == 9) then | ||
whitechar = .true. | ||
else | ||
whitechar = .false. | ||
end if | ||
end function | ||
|
||
end module |
This file contains 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,10 @@ | ||
module stdlib_types | ||
implicit none | ||
private | ||
public sp, dp, qp | ||
|
||
integer, parameter :: sp=kind(0.), & ! single precision | ||
dp=kind(0.d0), & ! double precision | ||
qp=selected_real_kind(32) ! quadruple precision | ||
|
||
end module |