@@ -2,6 +2,7 @@ module stdlib_experimental_io
2
2
use stdlib_experimental_kinds, only: sp, dp, qp
3
3
use stdlib_experimental_error, only: error_stop
4
4
use stdlib_experimental_optval, only: optval
5
+ use stdlib_experimental_ascii, only: is_blank
5
6
implicit none
6
7
private
7
8
! Public API
@@ -231,16 +232,16 @@ integer function number_of_columns(s)
231
232
232
233
integer :: ios
233
234
character :: c
234
- logical :: lastwhite
235
+ logical :: lastblank
235
236
236
237
rewind(s)
237
238
number_of_columns = 0
238
- lastwhite = .true.
239
+ lastblank = .true.
239
240
do
240
241
read (s, ' (a)' , advance= ' no' , iostat= ios) c
241
242
if (ios /= 0 ) exit
242
- if (lastwhite .and. .not. whitechar (c)) number_of_columns = number_of_columns + 1
243
- lastwhite = whitechar (c)
243
+ if (lastblank .and. .not. is_blank (c)) number_of_columns = number_of_columns + 1
244
+ lastblank = is_blank (c)
244
245
end do
245
246
rewind(s)
246
247
@@ -265,17 +266,7 @@ integer function number_of_rows_numeric(s)
265
266
266
267
end function
267
268
268
- pure logical function whitechar(char) ! white character
269
- ! returns .true. if char is space (32) or tab (9), .false. otherwise
270
- character , intent (in ) :: char
271
- if (iachar (char) == 32 .or. iachar (char) == 9 ) then
272
- whitechar = .true.
273
- else
274
- whitechar = .false.
275
- end if
276
- end function
277
-
278
- integer function open (filename , mode ) result(u)
269
+ integer function open (filename , mode , iostat ) result(u)
279
270
! Open a file
280
271
!
281
272
! To open a file to read:
@@ -293,8 +284,10 @@ integer function open(filename, mode) result(u)
293
284
294
285
character (* ), intent (in ) :: filename
295
286
character (* ), intent (in ), optional :: mode
296
- integer :: io
297
- character (3 ):: mode_
287
+ integer , intent (out ), optional :: iostat
288
+
289
+ integer :: io_
290
+ character (3 ) :: mode_
298
291
character (:),allocatable :: action_, position_, status_, access_, form_
299
292
300
293
@@ -348,37 +341,51 @@ integer function open(filename, mode) result(u)
348
341
call error_stop(" Unsupported mode: " // mode_(3 :3 ))
349
342
end select
350
343
351
- open (newunit= u, file= filename, &
352
- action = action_, position = position_, status = status_, &
353
- access = access_, form = form_, &
354
- iostat = io)
344
+ if (present (iostat)) then
345
+ open (newunit= u, file= filename, &
346
+ action = action_, position = position_, status = status_, &
347
+ access = access_, form = form_, &
348
+ iostat = iostat)
349
+ else
350
+ open (newunit= u, file= filename, &
351
+ action = action_, position = position_, status = status_, &
352
+ access = access_, form = form_)
353
+ end if
355
354
356
355
end function
357
356
358
357
character (3 ) function parse_mode(mode) result(mode_)
359
358
character (* ), intent (in ) :: mode
360
359
361
- integer :: i
362
- character (:),allocatable :: a
360
+ integer :: i
361
+ character (:),allocatable :: a
362
+ logical :: lfirst(3 )
363
363
364
364
mode_ = ' r t'
365
365
366
366
if (len_trim (mode) == 0 ) return
367
367
a= trim (adjustl (mode))
368
368
369
+ lfirst = .true.
369
370
do i= 1 ,len (a)
370
- select case (a(i:i))
371
- case (' r' , ' w' , ' a' , ' x' )
371
+ if (lfirst(1 ) &
372
+ .and. (a(i:i) == ' r' .or. a(i:i) == ' w' .or. a(i:i) == ' a' .or. a(i:i) == ' x' ) &
373
+ ) then
372
374
mode_(1 :1 ) = a(i:i)
373
- case (' +' )
375
+ lfirst(1 )= .false.
376
+ else if (lfirst(2 ) .and. a(i:i) == ' +' ) then
374
377
mode_(2 :2 ) = a(i:i)
375
- case (' t' , ' b' )
378
+ lfirst(2 )= .false.
379
+ else if (lfirst(3 ) .and. (a(i:i) == ' t' .or. a(i:i) == ' b' )) then
376
380
mode_(3 :3 ) = a(i:i)
377
- case (' ' )
378
- cycle
379
- case default
381
+ lfirst(3 )= .false.
382
+ else if (a(i:i) == ' ' ) then
383
+ cycle
384
+ else if (any (.not. lfirst)) then
385
+ call error_stop(" Wrong mode: " // trim (a))
386
+ else
380
387
call error_stop(" Wrong character: " // a(i:i))
381
- end select
388
+ endif
382
389
end do
383
390
384
391
end function
0 commit comments