From 9ac942dec743f18ad70508145e0fa87096aa6848 Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Fri, 27 Sep 2024 12:12:37 +0200 Subject: [PATCH 1/6] `loadtxt/savetxt`: do not require space after last entry --- src/stdlib_io.fypp | 47 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 7aceae2e2..542c31321 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -144,53 +144,28 @@ contains do i = 1, skiprows_ read(s, *) end do - - #:if 'real' in t1 + ! Default to format used for savetxt if fmt not specified. - fmt_ = optval(fmt, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") - - if ( fmt_ == '*' ) then - ! Use list directed read if user has specified fmt='*' - do i = 1, max_rows_ - read (s,*) d(i, :) - enddo - else - ! Otherwise pass default or user specified fmt string. - do i = 1, max_rows_ - read (s,fmt_) d(i, :) - enddo - endif + #:if 'real' in t1 + fmt_ = optval(fmt, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))") #:elif 'complex' in t1 - ! Default to format used for savetxt if fmt not specified. - fmt_ = optval(fmt, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") - if ( fmt_ == '*' ) then - ! Use list directed read if user has specified fmt='*' - do i = 1, max_rows_ - read (s,*) d(i, :) - enddo - else - ! Otherwise pass default or user specified fmt string. - do i = 1, max_rows_ - read (s,fmt_) d(i, :) - enddo - endif + fmt_ = optval(fmt, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))") #:else - ! Default to list directed for integer fmt_ = optval(fmt, "*") - ! Use list directed read if user has specified fmt='*' + #:endif + if ( fmt_ == '*' ) then + ! Use list directed read if user has specified fmt='*' do i = 1, max_rows_ read (s,*) d(i, :) enddo else - ! Otherwise pass default user specified fmt string. + ! Otherwise pass default or user specified fmt string. do i = 1, max_rows_ read (s,fmt_) d(i, :) enddo endif - #:endif - close(s) end subroutine loadtxt_${t1[0]}$${k1}$ @@ -222,11 +197,11 @@ contains s = open(filename, "w") do i = 1, size(d, 1) #:if 'real' in t1 - write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))") d(i, :) #:elif 'complex' in t1 - write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))") d(i, :) #:elif 'integer' in t1 - write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",:,1x))") d(i, :) #:else write(s, *) d(i, :) #:endif From 635e0b575c0da582dff817683775253ea790b77f Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 29 Sep 2024 09:21:16 +0200 Subject: [PATCH 2/6] simple error control: print OS message on error --- src/stdlib_io.fypp | 50 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 542c31321..6144864b5 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -120,7 +120,8 @@ contains !! ... !! integer :: s - integer :: nrow, ncol, i, skiprows_, max_rows_ + integer :: nrow, ncol, i, ios, skiprows_, max_rows_ + character(len=128) :: iomsg,msgout skiprows_ = max(optval(skiprows, 0), 0) max_rows_ = optval(max_rows, -1) @@ -142,7 +143,13 @@ contains allocate(d(max_rows_, ncol)) do i = 1, skiprows_ - read(s, *) + read(s, *, iostat=ios, iomsg=iomsg) + + if (ios/=0) then + write(msgout,1) trim(iomsg),i,trim(filename) + call error_stop(msg=trim(msgout)) + end if + end do ! Default to format used for savetxt if fmt not specified. @@ -157,16 +164,30 @@ contains if ( fmt_ == '*' ) then ! Use list directed read if user has specified fmt='*' do i = 1, max_rows_ - read (s,*) d(i, :) + read (s,*,iostat=ios,iomsg=iomsg) d(i, :) + + if (ios/=0) then + write(msgout,1) trim(iomsg),i,trim(filename) + call error_stop(msg=trim(msgout)) + end if + enddo else ! Otherwise pass default or user specified fmt string. do i = 1, max_rows_ - read (s,fmt_) d(i, :) + read (s,fmt_,iostat=ios,iomsg=iomsg) d(i, :) + + if (ios/=0) then + write(msgout,1) trim(iomsg),i,trim(filename) + call error_stop(msg=trim(msgout)) + end if + enddo endif close(s) + + 1 format('loadtxt: error <',a,'> reading line ',i0,' of ',a,'.') end subroutine loadtxt_${t1[0]}$${k1}$ #:endfor @@ -193,20 +214,31 @@ contains !!``` !! - integer :: s, i + integer :: s, i, ios + character(len=128) :: iomsg,msgout s = open(filename, "w") do i = 1, size(d, 1) #:if 'real' in t1 - write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))") d(i, :) + write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))", & #:elif 'complex' in t1 - write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))") d(i, :) + write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))", & #:elif 'integer' in t1 - write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",:,1x))") d(i, :) + write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",:,1x))", & #:else - write(s, *) d(i, :) + write(s, *, & #:endif + iostat=ios,iomsg=iomsg) d(i, :) + + if (ios/=0) then + write(msgout,1) trim(iomsg),i,trim(filename) + call error_stop(msg=trim(msgout)) + end if + end do close(s) + + 1 format('savetxt: error <',a,'> writing line ',i0,' of ',a,'.') + end subroutine savetxt_${t1[0]}$${k1}$ #:endfor From 15de15d951aa988229dcf15e4b7c767d0ab74b75 Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 6 Oct 2024 09:28:20 +0200 Subject: [PATCH 3/6] Update src/stdlib_io.fypp Co-authored-by: Jeremie Vandenplas --- src/stdlib_io.fypp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 6144864b5..65196d237 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -121,7 +121,7 @@ contains !! integer :: s integer :: nrow, ncol, i, ios, skiprows_, max_rows_ - character(len=128) :: iomsg,msgout + character(len=128) :: iomsg, msgout skiprows_ = max(optval(skiprows, 0), 0) max_rows_ = optval(max_rows, -1) From 6439a2cc2c6ed825e8d71071502c9aa9400a99c4 Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 6 Oct 2024 12:43:48 +0200 Subject: [PATCH 4/6] `call error_stop` -> `error stop` --- src/stdlib_io.fypp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 6144864b5..b32743569 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -9,7 +9,6 @@ module stdlib_io use, intrinsic :: iso_fortran_env, only : input_unit use stdlib_kinds, only: sp, dp, xdp, qp, & int8, int16, int32, int64 - use stdlib_error, only: error_stop use stdlib_optval, only: optval use stdlib_ascii, only: is_blank use stdlib_string_type, only : string_type @@ -147,7 +146,7 @@ contains if (ios/=0) then write(msgout,1) trim(iomsg),i,trim(filename) - call error_stop(msg=trim(msgout)) + error stop trim(msgout) end if end do @@ -168,7 +167,7 @@ contains if (ios/=0) then write(msgout,1) trim(iomsg),i,trim(filename) - call error_stop(msg=trim(msgout)) + error stop trim(msgout) end if enddo @@ -179,7 +178,7 @@ contains if (ios/=0) then write(msgout,1) trim(iomsg),i,trim(filename) - call error_stop(msg=trim(msgout)) + error stop trim(msgout) end if enddo @@ -231,7 +230,7 @@ contains if (ios/=0) then write(msgout,1) trim(iomsg),i,trim(filename) - call error_stop(msg=trim(msgout)) + error stop trim(msgout) end if end do @@ -367,7 +366,7 @@ contains position_='asis' status_='new' case default - call error_stop("Unsupported mode: "//mode_(1:2)) + error stop "Unsupported mode: "//mode_(1:2) end select select case (mode_(3:3)) @@ -376,7 +375,7 @@ contains case('b') form_='unformatted' case default - call error_stop("Unsupported mode: "//mode_(3:3)) + error stop "Unsupported mode: "//mode_(3:3) end select access_ = 'stream' @@ -422,9 +421,9 @@ contains else if (a(i:i) == ' ') then cycle else if(any(.not.lfirst)) then - call error_stop("Wrong mode: "//trim(a)) + error stop "Wrong mode: "//trim(a) else - call error_stop("Wrong character: "//a(i:i)) + error stop "Wrong character: "//a(i:i) endif end do @@ -473,7 +472,7 @@ contains if (present(iostat)) then iostat = stat else if (stat /= 0) then - call error_stop(trim(msg)) + error stop trim(msg) end if end subroutine getline_char From ab9fb9da3bbccaa9fe0f6b826f923866d2fda9f8 Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 6 Oct 2024 12:48:32 +0200 Subject: [PATCH 5/6] Update src/stdlib_io.fypp Co-authored-by: Jeremie Vandenplas --- src/stdlib_io.fypp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 65196d237..de5f3e4d5 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -173,7 +173,7 @@ contains enddo else - ! Otherwise pass default or user specified fmt string. + ! Otherwise pass default or user specified fmt string. do i = 1, max_rows_ read (s,fmt_,iostat=ios,iomsg=iomsg) d(i, :) From ddb81868e6f3fd6d7e75dc7398e4feda4058749e Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 6 Oct 2024 16:17:55 +0200 Subject: [PATCH 6/6] Update src/stdlib_io.fypp Co-authored-by: Jeremie Vandenplas --- src/stdlib_io.fypp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 6917f72fb..ac207a944 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -214,7 +214,7 @@ contains !! integer :: s, i, ios - character(len=128) :: iomsg,msgout + character(len=128) :: iomsg, msgout s = open(filename, "w") do i = 1, size(d, 1) #:if 'real' in t1