@@ -2,8 +2,8 @@ module fortran_subprocess
2
2
use iso_c_binding
3
3
use iso_fortran_env, only: int64, real64
4
4
use stdlib_system
5
- use stdlib_io, only: getfile
6
5
use stdlib_strings, only: to_c_string
6
+ use stdlib_linalg_state, only: linalg_state_type, LINALG_ERROR, linalg_error_handling
7
7
implicit none
8
8
public
9
9
@@ -417,5 +417,100 @@ function scratch_name(prefix) result(temp_filename)
417
417
418
418
end function scratch_name
419
419
420
+ ! > Helper function.
421
+ ! > Reads a whole ASCII file and loads its contents into an allocatable character string..
422
+ ! > The function handles error states and optionally deletes the file after reading.
423
+ ! > Temporarily uses `linalg_state_type` in lieu of the generalized `state_type`.
424
+ ! >
425
+ ! > Version: to be replaced after `getfile` is standardized in `stdlib_io`.
426
+ function getfile (fileName ,err ,delete ) result(file)
427
+ ! > Input file name
428
+ character (* ), intent (in ) :: fileName
429
+ ! > [optional] State return flag. On error, if not requested, the code will stop.
430
+ type (linalg_state_type), optional , intent (out ) :: err
431
+ ! > [optional] Delete file after reading? Default: do not delete
432
+ logical , optional , intent (in ) :: delete
433
+ ! > Return as an allocatable string
434
+ character (:), allocatable :: file
435
+
436
+ ! Local variables
437
+ type (linalg_state_type) :: err0
438
+ character (len= :), allocatable :: fileString
439
+ character (len= 512 ) :: iomsg
440
+ integer :: lun,iostat
441
+ integer (int64) :: errpos,fileSize
442
+ logical :: is_present,want_deleted
443
+
444
+ ! Initializations
445
+ file = " "
446
+
447
+ ! > Check if the file should be deleted after reading
448
+ if (present (delete)) then
449
+ want_deleted = delete
450
+ else
451
+ want_deleted = .false.
452
+ end if
453
+
454
+ ! > Check file existing
455
+ inquire (file= fileName, exist= is_present)
456
+ if (.not. is_present) then
457
+ err0 = linalg_state_type(' getfile' ,LINALG_ERROR,' File not present:' ,fileName)
458
+ call linalg_error_handling(err0,err)
459
+ return
460
+ end if
461
+
462
+ ! > Retrieve file size
463
+ inquire (file= fileName,size= fileSize)
464
+
465
+ invalid_size: if (fileSize< 0 ) then
466
+
467
+ err0 = linalg_state_type(' getfile' ,LINALG_ERROR,fileName,' has invalid size=' ,fileSize)
468
+ call linalg_error_handling(err0,err)
469
+ return
470
+
471
+ endif invalid_size
472
+
473
+ ! Read file
474
+ open (newunit= lun,file= fileName, &
475
+ form= ' unformatted' ,action= ' read' ,access= ' stream' ,status= ' old' , &
476
+ iostat= iostat,iomsg= iomsg)
477
+
478
+ if (iostat/= 0 ) then
479
+ err0 = linalg_state_type(' getfile' ,LINALG_ERROR,' Cannot open' ,fileName,' for read:' ,iomsg)
480
+ call linalg_error_handling(err0,err)
481
+ return
482
+ end if
483
+
484
+ allocate (character (len= fileSize) :: fileString)
485
+
486
+ read_data: if (fileSize> 0 ) then
487
+
488
+ read (lun, pos= 1 , iostat= iostat, iomsg= iomsg) fileString
489
+
490
+ ! Read error
491
+ if (iostat/= 0 ) then
492
+
493
+ inquire (unit= lun,pos= errpos)
494
+ err0 = linalg_state_type(' getfile' ,LINALG_ERROR,iomsg,' (' ,fileName,' at byte' ,errpos,' )' )
495
+ call linalg_error_handling(err0,err)
496
+ return
497
+
498
+ endif
499
+
500
+ end if read_data
501
+
502
+ if (want_deleted) then
503
+ close (lun,iostat= iostat,status= ' delete' )
504
+ if (iostat/= 0 ) err0 = linalg_state_type(' getfile' ,LINALG_ERROR,' Cannot delete' ,fileName,' after reading' )
505
+ else
506
+ close (lun,iostat= iostat)
507
+ if (iostat/= 0 ) err0 = linalg_state_type(' getfile' ,LINALG_ERROR,' Cannot close' ,fileName,' after reading' )
508
+ endif
509
+
510
+ ! Process output
511
+ call move_alloc(from= fileString,to = file)
512
+ call linalg_error_handling(err0,err)
513
+
514
+ end function getfile
420
515
421
516
end module fortran_subprocess
0 commit comments