@@ -59,13 +59,15 @@ module stdlib_io
59
59
!> Format string for quadruple precision real numbers
60
60
FMT_REAL_QP = '(es44.35e4)', &
61
61
!> Format string for single precision complex numbers
62
- FMT_COMPLEX_SP = '(es15.8e2 ,1x,es15.8e2 )', &
62
+ FMT_COMPLEX_SP = '(es15.08e2 ,1x,es15.08e2 )', &
63
63
!> Format string for double precision complex numbers
64
64
FMT_COMPLEX_DP = '(es24.16e3,1x,es24.16e3)', &
65
65
!> Format string for extended double precision complex numbers
66
66
FMT_COMPLEX_XDP = '(es26.18e3,1x,es26.18e3)', &
67
67
!> Format string for quadruple precision complex numbers
68
68
FMT_COMPLEX_QP = '(es44.35e4,1x,es44.35e4)'
69
+ !> Default delimiter for loadtxt, savetxt and number_of_columns
70
+ character(len=1), parameter :: delimiter_default = " "
69
71
70
72
public :: FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_REAL_XDP, FMT_REAL_QP
71
73
public :: FMT_COMPLEX_SP, FMT_COMPLEX_DP, FMT_COMPLEX_XDP, FMT_COMPLEX_QP
@@ -143,9 +145,9 @@ contains
143
145
!! 11 12 13
144
146
!! ...
145
147
!!
146
- character(len=1), parameter :: delimiter_default = " "
147
148
integer :: s
148
- integer :: nrow, ncol, i, ios, skiprows_, max_rows_
149
+ integer :: nrow, ncol, i, j, ios, skiprows_, max_rows_, istart, iend
150
+ character(len=:), allocatable :: line, iomsg_
149
151
character(len=1024) :: iomsg, msgout
150
152
151
153
skiprows_ = max(optval(skiprows, 0), 0)
@@ -163,10 +165,11 @@ contains
163
165
ncol = 0
164
166
if ( skiprows_ < nrow ) ncol = number_of_columns(s, skiprows=skiprows_, delimiter=delimiter_)
165
167
#:if 'complex' in t1
166
- if (is_blank(delimiter_)) ncol = ncol / 2
168
+ ncol = ncol / 2
167
169
#:endif
168
170
169
171
allocate(d(max_rows_, ncol))
172
+ if (max_rows_ == 0 .or. ncol == 0) return
170
173
171
174
do i = 1, skiprows_
172
175
read(s, *, iostat=ios, iomsg=iomsg)
@@ -190,15 +193,44 @@ contains
190
193
191
194
if ( fmt_ == '*' ) then
192
195
! Use list directed read if user has specified fmt='*'
193
- do i = 1, max_rows_
194
- read (s,*,iostat=ios,iomsg=iomsg) d(i, :)
195
-
196
- if (ios/=0) then
197
- write(msgout,2) trim(iomsg),size(d,2),i,trim(filename)
198
- call error_stop(msg=trim(msgout))
199
- end if
200
-
201
- enddo
196
+ if (is_blank(delimiter_) .or. delimiter_ == ",") then
197
+ do i = 1, max_rows_
198
+ read (s,*,iostat=ios,iomsg=iomsg) d(i, :)
199
+
200
+ if (ios/=0) then
201
+ write(msgout,2) trim(iomsg),size(d,2),i,trim(filename)
202
+ call error_stop(msg=trim(msgout))
203
+ end if
204
+
205
+ enddo
206
+ ! Otherwise read each value separately
207
+ else
208
+ do i = 1, max_rows_
209
+ call get_line(s, line, ios, iomsg_)
210
+ if (ios/=0) then
211
+ write(msgout,2) trim(iomsg_),size(d,2),i,trim(filename)
212
+ call error_stop(msg=trim(msgout))
213
+ end if
214
+
215
+ istart = 0
216
+ do j = 1, ncol - 1
217
+ iend = index(line(istart+1:), delimiter_)
218
+ read (line(istart+1:istart+iend-1),*,iostat=ios,iomsg=iomsg) d(i, j)
219
+ if (ios/=0) then
220
+ write(msgout,2) trim(iomsg),size(d,2),i,trim(filename)
221
+ call error_stop(msg=trim(msgout))
222
+ end if
223
+ istart = istart + iend
224
+ end do
225
+
226
+ read (line(istart+1:),*,iostat=ios,iomsg=iomsg) d(i, ncol)
227
+ if (ios/=0) then
228
+ write(msgout,2) trim(iomsg),size(d,2),i,trim(filename)
229
+ call error_stop(msg=trim(msgout))
230
+ end if
231
+
232
+ enddo
233
+ end if
202
234
else
203
235
! Otherwise pass default or user specified fmt string.
204
236
do i = 1, max_rows_
@@ -241,7 +273,6 @@ contains
241
273
!! call savetxt("log.txt", data)
242
274
!!```
243
275
!!
244
- character(len=1), parameter :: delimiter_default = " "
245
276
integer :: s, i, ios
246
277
character(len=1) :: delimiter_
247
278
character(len=3) :: delim_str
@@ -253,7 +284,7 @@ contains
253
284
#:if 'real' in t1
254
285
fmt_ = "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,"//delim_str//"))"
255
286
#:elif 'complex' in t1
256
- fmt_ = "(*"//FMT_COMPLEX_${k1}$(1:len( FMT_COMPLEX_${k1}$)-1 )//",:,"//delim_str//"))"
287
+ fmt_ = "(*"//FMT_COMPLEX_${k1}$(1:11)//delim_str// FMT_COMPLEX_${k1}$(14:23 )//",:,"//delim_str//"))"
257
288
#:elif 'integer' in t1
258
289
fmt_ = "(*"//FMT_INT(1:len(FMT_INT)-1)//",:,"//delim_str//"))"
259
290
#:endif
@@ -289,7 +320,6 @@ contains
289
320
integer, intent(in), optional :: skiprows
290
321
character(len=1), intent(in), optional :: delimiter
291
322
292
- character(len=1), parameter :: delimiter_default = " "
293
323
integer :: ios, skiprows_, i
294
324
character :: c
295
325
character(len=:), allocatable :: line
0 commit comments