Skip to content

Commit 8caa640

Browse files
committed
Deploying to stdlib-fpm from @ 206f84a 🚀
1 parent 892ba5c commit 8caa640

File tree

3 files changed

+210
-46
lines changed

3 files changed

+210
-46
lines changed

example/example_loadtxt.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ program example_loadtxt
33
implicit none
44
real, allocatable :: x(:, :)
55
call loadtxt('example.dat', x)
6+
7+
! Can also use list directed format if the default read fails.
8+
call loadtxt('example.dat', x, fmt='*')
69
end program example_loadtxt

src/stdlib_io.f90

Lines changed: 142 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module stdlib_io
8888

8989
contains
9090

91-
subroutine loadtxt_rsp(filename, d, skiprows, max_rows)
91+
subroutine loadtxt_rsp(filename, d, skiprows, max_rows, fmt)
9292
!! version: experimental
9393
!!
9494
!! Loads a 2D array from a text file.
@@ -107,6 +107,8 @@ subroutine loadtxt_rsp(filename, d, skiprows, max_rows)
107107
!! A value of zero results in no lines to be read.
108108
!! The default value is -1.
109109
integer, intent(in), optional :: max_rows
110+
character(len=*), intent(in), optional :: fmt
111+
character(len=:), allocatable :: fmt_
110112
!!
111113
!! Example
112114
!! -------
@@ -147,13 +149,25 @@ subroutine loadtxt_rsp(filename, d, skiprows, max_rows)
147149
read(s, *)
148150
end do
149151

150-
do i = 1, max_rows_
151-
read(s, "(*"//FMT_REAL_sp(1:len(FMT_REAL_sp)-1)//",1x))") d(i, :)
152-
end do
152+
! Default to format used for savetxt if fmt not specified.
153+
fmt_ = optval(fmt, "(*"//FMT_REAL_sp(1:len(FMT_REAL_sp)-1)//",1x))")
154+
155+
if ( fmt_ == '*' ) then
156+
! Use list directed read if user has specified fmt='*'
157+
do i = 1, max_rows_
158+
read (s,*) d(i, :)
159+
enddo
160+
else
161+
! Otherwise pass default or user specified fmt string.
162+
do i = 1, max_rows_
163+
read (s,fmt_) d(i, :)
164+
enddo
165+
endif
166+
153167
close(s)
154168

155169
end subroutine loadtxt_rsp
156-
subroutine loadtxt_rdp(filename, d, skiprows, max_rows)
170+
subroutine loadtxt_rdp(filename, d, skiprows, max_rows, fmt)
157171
!! version: experimental
158172
!!
159173
!! Loads a 2D array from a text file.
@@ -172,6 +186,8 @@ subroutine loadtxt_rdp(filename, d, skiprows, max_rows)
172186
!! A value of zero results in no lines to be read.
173187
!! The default value is -1.
174188
integer, intent(in), optional :: max_rows
189+
character(len=*), intent(in), optional :: fmt
190+
character(len=:), allocatable :: fmt_
175191
!!
176192
!! Example
177193
!! -------
@@ -212,13 +228,25 @@ subroutine loadtxt_rdp(filename, d, skiprows, max_rows)
212228
read(s, *)
213229
end do
214230

215-
do i = 1, max_rows_
216-
read(s, "(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))") d(i, :)
217-
end do
231+
! Default to format used for savetxt if fmt not specified.
232+
fmt_ = optval(fmt, "(*"//FMT_REAL_dp(1:len(FMT_REAL_dp)-1)//",1x))")
233+
234+
if ( fmt_ == '*' ) then
235+
! Use list directed read if user has specified fmt='*'
236+
do i = 1, max_rows_
237+
read (s,*) d(i, :)
238+
enddo
239+
else
240+
! Otherwise pass default or user specified fmt string.
241+
do i = 1, max_rows_
242+
read (s,fmt_) d(i, :)
243+
enddo
244+
endif
245+
218246
close(s)
219247

220248
end subroutine loadtxt_rdp
221-
subroutine loadtxt_iint8(filename, d, skiprows, max_rows)
249+
subroutine loadtxt_iint8(filename, d, skiprows, max_rows, fmt)
222250
!! version: experimental
223251
!!
224252
!! Loads a 2D array from a text file.
@@ -237,6 +265,8 @@ subroutine loadtxt_iint8(filename, d, skiprows, max_rows)
237265
!! A value of zero results in no lines to be read.
238266
!! The default value is -1.
239267
integer, intent(in), optional :: max_rows
268+
character(len=*), intent(in), optional :: fmt
269+
character(len=:), allocatable :: fmt_
240270
!!
241271
!! Example
242272
!! -------
@@ -277,13 +307,25 @@ subroutine loadtxt_iint8(filename, d, skiprows, max_rows)
277307
read(s, *)
278308
end do
279309

280-
do i = 1, max_rows_
281-
read(s, *) d(i, :)
282-
end do
310+
! Default to list directed for integer
311+
fmt_ = optval(fmt, "*")
312+
! Use list directed read if user has specified fmt='*'
313+
if ( fmt_ == '*' ) then
314+
do i = 1, max_rows_
315+
read (s,*) d(i, :)
316+
enddo
317+
else
318+
! Otherwise pass default user specified fmt string.
319+
do i = 1, max_rows_
320+
read (s,fmt_) d(i, :)
321+
enddo
322+
endif
323+
324+
283325
close(s)
284326

285327
end subroutine loadtxt_iint8
286-
subroutine loadtxt_iint16(filename, d, skiprows, max_rows)
328+
subroutine loadtxt_iint16(filename, d, skiprows, max_rows, fmt)
287329
!! version: experimental
288330
!!
289331
!! Loads a 2D array from a text file.
@@ -302,6 +344,8 @@ subroutine loadtxt_iint16(filename, d, skiprows, max_rows)
302344
!! A value of zero results in no lines to be read.
303345
!! The default value is -1.
304346
integer, intent(in), optional :: max_rows
347+
character(len=*), intent(in), optional :: fmt
348+
character(len=:), allocatable :: fmt_
305349
!!
306350
!! Example
307351
!! -------
@@ -342,13 +386,25 @@ subroutine loadtxt_iint16(filename, d, skiprows, max_rows)
342386
read(s, *)
343387
end do
344388

345-
do i = 1, max_rows_
346-
read(s, *) d(i, :)
347-
end do
389+
! Default to list directed for integer
390+
fmt_ = optval(fmt, "*")
391+
! Use list directed read if user has specified fmt='*'
392+
if ( fmt_ == '*' ) then
393+
do i = 1, max_rows_
394+
read (s,*) d(i, :)
395+
enddo
396+
else
397+
! Otherwise pass default user specified fmt string.
398+
do i = 1, max_rows_
399+
read (s,fmt_) d(i, :)
400+
enddo
401+
endif
402+
403+
348404
close(s)
349405

350406
end subroutine loadtxt_iint16
351-
subroutine loadtxt_iint32(filename, d, skiprows, max_rows)
407+
subroutine loadtxt_iint32(filename, d, skiprows, max_rows, fmt)
352408
!! version: experimental
353409
!!
354410
!! Loads a 2D array from a text file.
@@ -367,6 +423,8 @@ subroutine loadtxt_iint32(filename, d, skiprows, max_rows)
367423
!! A value of zero results in no lines to be read.
368424
!! The default value is -1.
369425
integer, intent(in), optional :: max_rows
426+
character(len=*), intent(in), optional :: fmt
427+
character(len=:), allocatable :: fmt_
370428
!!
371429
!! Example
372430
!! -------
@@ -407,13 +465,25 @@ subroutine loadtxt_iint32(filename, d, skiprows, max_rows)
407465
read(s, *)
408466
end do
409467

410-
do i = 1, max_rows_
411-
read(s, *) d(i, :)
412-
end do
468+
! Default to list directed for integer
469+
fmt_ = optval(fmt, "*")
470+
! Use list directed read if user has specified fmt='*'
471+
if ( fmt_ == '*' ) then
472+
do i = 1, max_rows_
473+
read (s,*) d(i, :)
474+
enddo
475+
else
476+
! Otherwise pass default user specified fmt string.
477+
do i = 1, max_rows_
478+
read (s,fmt_) d(i, :)
479+
enddo
480+
endif
481+
482+
413483
close(s)
414484

415485
end subroutine loadtxt_iint32
416-
subroutine loadtxt_iint64(filename, d, skiprows, max_rows)
486+
subroutine loadtxt_iint64(filename, d, skiprows, max_rows, fmt)
417487
!! version: experimental
418488
!!
419489
!! Loads a 2D array from a text file.
@@ -432,6 +502,8 @@ subroutine loadtxt_iint64(filename, d, skiprows, max_rows)
432502
!! A value of zero results in no lines to be read.
433503
!! The default value is -1.
434504
integer, intent(in), optional :: max_rows
505+
character(len=*), intent(in), optional :: fmt
506+
character(len=:), allocatable :: fmt_
435507
!!
436508
!! Example
437509
!! -------
@@ -472,13 +544,25 @@ subroutine loadtxt_iint64(filename, d, skiprows, max_rows)
472544
read(s, *)
473545
end do
474546

475-
do i = 1, max_rows_
476-
read(s, *) d(i, :)
477-
end do
547+
! Default to list directed for integer
548+
fmt_ = optval(fmt, "*")
549+
! Use list directed read if user has specified fmt='*'
550+
if ( fmt_ == '*' ) then
551+
do i = 1, max_rows_
552+
read (s,*) d(i, :)
553+
enddo
554+
else
555+
! Otherwise pass default user specified fmt string.
556+
do i = 1, max_rows_
557+
read (s,fmt_) d(i, :)
558+
enddo
559+
endif
560+
561+
478562
close(s)
479563

480564
end subroutine loadtxt_iint64
481-
subroutine loadtxt_csp(filename, d, skiprows, max_rows)
565+
subroutine loadtxt_csp(filename, d, skiprows, max_rows, fmt)
482566
!! version: experimental
483567
!!
484568
!! Loads a 2D array from a text file.
@@ -497,6 +581,8 @@ subroutine loadtxt_csp(filename, d, skiprows, max_rows)
497581
!! A value of zero results in no lines to be read.
498582
!! The default value is -1.
499583
integer, intent(in), optional :: max_rows
584+
character(len=*), intent(in), optional :: fmt
585+
character(len=:), allocatable :: fmt_
500586
!!
501587
!! Example
502588
!! -------
@@ -538,13 +624,24 @@ subroutine loadtxt_csp(filename, d, skiprows, max_rows)
538624
read(s, *)
539625
end do
540626

541-
do i = 1, max_rows_
542-
read(s, "(*"//FMT_COMPLEX_sp(1:len(FMT_COMPLEX_sp)-1)//",1x))") d(i, :)
543-
end do
627+
! Default to format used for savetxt if fmt not specified.
628+
fmt_ = optval(fmt, "(*"//FMT_COMPLEX_sp(1:len(FMT_COMPLEX_sp)-1)//",1x))")
629+
if ( fmt_ == '*' ) then
630+
! Use list directed read if user has specified fmt='*'
631+
do i = 1, max_rows_
632+
read (s,*) d(i, :)
633+
enddo
634+
else
635+
! Otherwise pass default or user specified fmt string.
636+
do i = 1, max_rows_
637+
read (s,fmt_) d(i, :)
638+
enddo
639+
endif
640+
544641
close(s)
545642

546643
end subroutine loadtxt_csp
547-
subroutine loadtxt_cdp(filename, d, skiprows, max_rows)
644+
subroutine loadtxt_cdp(filename, d, skiprows, max_rows, fmt)
548645
!! version: experimental
549646
!!
550647
!! Loads a 2D array from a text file.
@@ -563,6 +660,8 @@ subroutine loadtxt_cdp(filename, d, skiprows, max_rows)
563660
!! A value of zero results in no lines to be read.
564661
!! The default value is -1.
565662
integer, intent(in), optional :: max_rows
663+
character(len=*), intent(in), optional :: fmt
664+
character(len=:), allocatable :: fmt_
566665
!!
567666
!! Example
568667
!! -------
@@ -604,9 +703,20 @@ subroutine loadtxt_cdp(filename, d, skiprows, max_rows)
604703
read(s, *)
605704
end do
606705

607-
do i = 1, max_rows_
608-
read(s, "(*"//FMT_COMPLEX_dp(1:len(FMT_COMPLEX_dp)-1)//",1x))") d(i, :)
609-
end do
706+
! Default to format used for savetxt if fmt not specified.
707+
fmt_ = optval(fmt, "(*"//FMT_COMPLEX_dp(1:len(FMT_COMPLEX_dp)-1)//",1x))")
708+
if ( fmt_ == '*' ) then
709+
! Use list directed read if user has specified fmt='*'
710+
do i = 1, max_rows_
711+
read (s,*) d(i, :)
712+
enddo
713+
else
714+
! Otherwise pass default or user specified fmt string.
715+
do i = 1, max_rows_
716+
read (s,fmt_) d(i, :)
717+
enddo
718+
endif
719+
610720
close(s)
611721

612722
end subroutine loadtxt_cdp

0 commit comments

Comments
 (0)