-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFileUtils.f90
1572 lines (1277 loc) · 43.5 KB
/
FileUtils.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module FileUtils
use MpiUtils
use MiscUtils
use StringUtils
use, intrinsic :: ISO_FORTRAN_ENV, only : INT64
implicit none
!Utils using F2008 features
private
integer, parameter :: File_size_int = KIND(INT64)
Type :: TFileStream
integer :: unit = 0
logical :: ReadOnly = .true.
character(LEN=:), allocatable :: mode
character(LEN=:), allocatable :: access !sequential (fortran or text) or stream (binary, with file position)
character(LEN=:), allocatable :: FileName
contains
procedure :: SetDefaultModes
procedure :: Open => TFileStream_Open
procedure :: OpenFile => TFileStream_OpenFile
procedure :: Flush => TFileStream_Flush
procedure :: CreateFile
procedure :: CreateOpenFile
procedure :: Position => TFileStream_Position
procedure :: Size => TFileStream_Size
procedure :: Close => TFileStream_Close
procedure :: Rewind => TFileStream_Rewind
procedure :: Error
procedure :: Opened
procedure :: WriteTrim
procedure :: CheckOpen
procedure, private :: ReadStringFunc
procedure, private :: ReadStringSub
procedure, private :: ReadItemSub
procedure, private :: ReadItemFunc
procedure, private :: ReadItems
procedure, private :: ReadArray
procedure, private :: ReadArray2
procedure, private :: ReadArray2Func
procedure, private :: ReadArrayFunc
procedure, private :: WriteItemSub
procedure, private :: WriteArray
procedure, private :: WriteOneAndArray
procedure, private :: WriteArray2
procedure, private :: WriteItems
procedure, private :: WriteSizedArray1
procedure, private :: WriteSizedArray2
procedure, private :: ReadSizedArray_R
procedure, private :: ReadSizedArray_D
procedure, private :: ReadSizedArray_I
procedure, private :: ReadSizedArray2_I
procedure, private :: ReadSizedArray2_D
procedure, private :: ReadSizedArray2_R
generic :: Write => WriteItems, WriteArray, WriteArray2, WriteOneAndArray
generic :: Read => ReadItems, ReadArray, ReadArray2
generic :: ReadItem => ReadItemFunc, ReadArrayFunc, ReadArray2Func
generic :: ReadString => ReadStringSub
generic :: ReadStringItem => ReadStringFunc
generic :: WriteSizedArray => WriteSizedArray1,WriteSizedArray2
generic :: ReadSizedArray => ReadSizedArray_R,ReadSizedArray_D,ReadSizedArray_I, &
& ReadSizedArray2_I, ReadSizedArray2_R, ReadSizedArray2_D
final :: TFileStream_Free
end type
Type, extends(TFileStream) :: TBinaryFile
end type
Type, extends(TFileStream) :: TSequentialFile
contains
procedure :: SetDefaultModes => TSequentialFile_SetDefaultModes
end type TSequentialFile
Type, extends(TSequentialFile) :: TTextFile
character(LEN=20) :: RealFormat = '(*(E17.7))'
character(LEN=20) :: IntegerFormat = '(*(I10))'
logical :: AdvanceDefault = .true.
contains
procedure :: SetDefaultModes => TTextFile_SetDefaultModes
procedure :: ReadLine
procedure :: ReadLineSkipEmptyAndComments
procedure :: ReadNextContentLine !as above, but opens and closes file
procedure :: NewLine
procedure :: SkipLines
procedure :: Lines
procedure :: Columns
procedure :: WriteLeftAligned
procedure :: WriteItemTxt
procedure :: WriteArrayTxt
procedure :: WriteInLineItems
procedure :: WriteInLineTrim
procedure :: WriteFormat => File_WriteFormat
procedure :: WriteTrim => WriteTrimTxt
procedure, private :: ReadStringSub => ReadStringTxt
procedure, private :: ReadItemTxt
procedure, private :: ReadArrayTxt
procedure, private :: WriteInLineItem
procedure, private :: WriteInLineArray
procedure, private :: DefaultAdvance
procedure, private :: WriteItem => TTextFile_WriteItem
procedure, private :: WriteArray => TTextFile_WriteArray
procedure, private :: WriteOneAndArray => TTextFile_WriteOneAndArray
procedure, private :: WriteArray2 => WriteArray2Txt
procedure, private :: ReadItemSub => ReadItemTxt
procedure, private :: ReadArray => ReadArrayTxt
procedure, private :: ReadArray2 => ReadArray2Txt
procedure, private :: WriteItems => WriteItemsTxt
generic :: WriteInLine => WriteInLineItem, WriteInLineArray
end type
!Functions on filenames and text
!File instance below acts as a namespace
Type TFile
contains
procedure, nopass :: TxtNumberColumns
procedure, nopass :: TxtColumns
procedure, nopass :: TxtFileColumns
procedure, nopass :: TxtFileLines
procedure, nopass :: LastTopComment
procedure, nopass :: TopCommentLine
procedure, nopass :: LastLine => LastFileLine
procedure, nopass :: Size => FileSize
procedure, nopass :: Exists => FileExists
procedure, nopass :: ExtractName => ExtractFileName
procedure, nopass :: ExtractPath => ExtractFilePath
procedure, nopass :: Join => File_Join
procedure, nopass :: ChangeExt => ChangeFileExt
procedure, nopass :: CheckTrailingSlash
procedure, nopass :: IsFullPath
procedure, nopass :: ExtractExt => ExtractFileExt
procedure, nopass :: Delete => DeleteFile
procedure, nopass :: ReadTextMatrix
procedure, nopass :: ReadTextVector
procedure, nopass :: WriteTextMatrix
procedure, nopass :: WriteTextVector
procedure, nopass :: LoadTxt_1D
procedure, nopass :: LoadTxt_2D
procedure, nopass :: LoadTxt_int_1D
procedure, nopass :: LoadTxt_int_2D
procedure, nopass :: OpenTextFile
procedure, nopass :: CreateTextFile
procedure, nopass :: CharIsSlash
generic :: LoadTxt => LoadTxt_2D, LoadTxt_1D, LoadTxt_int_1D, LoadTxt_int_2D
generic :: SaveTxt => WriteTextMatrix, WriteTextVector
end type
type(TFile), public, save :: File
public TFileStream, TBinaryFile, TTextFile, TFile, File_size_int
contains
function FileExists(aname)
character(LEN=*), intent(IN) :: aname
logical FileExists
inquire(file=aname, exist = FileExists)
end function FileExists
function FileSize(name) result(fsize)
integer(file_size_int) fsize
character(LEN=*), intent(in)::name
inquire(file=name, size=fsize)
end function FileSize
function TxtNumberColumns(InLine) result(n)
character(LEN=*) :: InLine
integer n,i
logical isNum
n=0
isNum=.false.
do i=1, len_trim(InLIne)
if (verify(InLine(i:i),'-+eE.0123456789') == 0) then
if (.not. IsNum) n=n+1
IsNum=.true.
else
IsNum=.false.
end if
end do
end function TxtNumberColumns
function TxtColumns(InLine) result(n)
character(LEN=*) :: InLine
integer n,i
logical isNum
n=0
isNum=.false.
do i=1, len_trim(InLine)
if (InLine(i:i) > char(32)) then
if (.not. IsNum) n=n+1
IsNum=.true.
else
IsNum=.false.
end if
end do
end function TxtColumns
subroutine SetDefaultModes(this)
class(TFileStream) :: this
if (.not. allocated(this%mode)) this%mode = 'unformatted'
if (.not. allocated(this%access)) this%access = 'stream'
end subroutine SetDefaultModes
subroutine TSequentialFile_SetDefaultModes(this)
class(TSequentialFile) :: this
if (.not. allocated(this%access)) this%access= 'sequential'
call this%TFileStream%SetDefaultModes
end subroutine TSequentialFile_SetDefaultModes
subroutine TTextFile_SetDefaultModes(this)
class(TTextFile) :: this
if (.not. allocated(this%mode)) this%mode = 'formatted'
call this%TSequentialFile%SetDefaultModes
end subroutine TTextFile_SetDefaultModes
subroutine CheckOpen(this, forWrite)
class(TFileStream) :: this
logical, intent(in), optional :: forWrite
if (this%unit/=0) return
if (DefaultFalse(forWrite) .and. this%ReadOnly) then
call this%Error('File not open for write')
else
call this%Error('File not opened')
end if
end subroutine
function Opened(this)
class(TFileStream) :: this
logical Opened
Opened = this%unit /=0
end function Opened
subroutine Error(this, msg, errormsg)
class(TFileStream) :: this
character(LEN=*), intent(IN) :: msg
character(LEN=*), intent(IN), optional :: errormsg
character(LEN=:), allocatable :: Filename
if (.not. allocated(this%FileName)) then
FileName = '(no filename set)'
else
FileName = this%FileName
end if
if (present(errormsg)) then
call MpiStop(trim(errormsg)//' : '//FileName )
else
call MpiStop(trim(msg)//' : '// FileName )
end if
end subroutine
subroutine TFileStream_Close(this, del)
class(TFileStream) :: this
logical, optional :: del
if (this%unit/=0) then
if (DefaultFalse(del)) then
close(this%unit, status='DELETE')
else
close(this%unit)
end if
end if
this%unit=0
end subroutine TFileStream_Close
subroutine TFileStream_Free(this)
Type(TFileStream) :: this
call this%Close()
end subroutine TFileStream_Free
subroutine TFileStream_Flush(this)
class(TFileStream) :: this
call this%CheckOpen
flush(this%unit)
end subroutine TFileStream_Flush
subroutine TFileStream_Rewind(this)
class(TFileStream) :: this
if (this%Opened()) rewind(this%unit)
end subroutine TFileStream_Rewind
function TFileStream_Position(this)
class(TFileStream) :: this
integer(file_size_int) TFileStream_Position
call this%CheckOpen
if (this%access /= 'stream') call this%Error('Position requires access=stream')
inquire(this%unit, pos=TFileStream_Position)
end function TFileStream_Position
function TFileStream_Size(this)
class(TFileStream) :: this
integer(file_size_int) TFileStream_Size
if (this%Opened()) then
inquire(this%unit, size=TFileStream_Size)
else if (allocated(this%FileName)) then
TFileStream_Size = File%Size(this%FileName)
else
call this%Error('File not defined for size')
end if
end function TFileStream_Size
subroutine TFileStream_Open(this, aname, errormsg, status)
class(TFileStream) :: this
character(LEN=*), intent(IN) :: aname
character(LEN=*), intent(IN), optional :: errormsg
integer, intent(out), optional :: status
call this%OpenFile(aname,errormsg=errormsg, status=status)
end subroutine TFileStream_Open
subroutine TFileStream_OpenFile(this, aname, mode, errormsg, forwrite, append, status)
class(TFileStream) :: this
character(LEN=*), intent(IN) :: aname
character(LEN=*), intent(IN), optional :: mode
character(LEN=*), intent(IN), optional :: errormsg
integer, intent(out), optional :: status
logical, intent(in), optional :: forwrite, append
character(LEN=:), allocatable :: amode, state, action, pos
integer out_status
call this%Close()
call this%SetDefaultModes()
this%FileName = trim(aname)
amode = PresentDefault(this%mode, mode)
if (DefaultFalse(forwrite)) then
state = 'replace'
action = 'readwrite'
this%ReadOnly = .false.
else
state = 'old'
action = 'read'
this%ReadOnly = .true.
end if
if (DefaultFalse(append) .and. FileExists(aname)) then
pos = 'append'
state = 'old'
action = 'readwrite'
this%ReadOnly = .false.
else
pos='asis'
end if
open(file=aname,form=amode,status=state, action=action, newunit=this%unit, &
& iostat=out_status, position =pos, access=this%access)
if (present(status)) then
status=out_status
if (out_status/=0) this%unit = 0
else
if (out_status/=0) then
if (state == 'replace') then
call this%Error('Error creating file', errormsg)
else
call this%Error('File not found', errormsg)
end if
this%unit = 0
end if
end if
end subroutine TFileStream_OpenFile
subroutine CreateFile(this,aname, errormsg)
class(TFileStream) :: this
character(LEN=*), intent(IN) :: aname
character(LEN=*), intent(IN), optional :: errormsg
call this%OpenFile(aname, errormsg=errormsg, forwrite =.true.)
end subroutine CreateFile
subroutine CreateOpenFile(this, aname, append, errormsg)
class(TFileStream) :: this
character(LEN=*), intent(IN) :: aname
logical, optional, intent(in) :: append
character(LEN=*), intent(IN), optional :: errormsg
call this%OpenFile(aname, forwrite =.true., append=append, errormsg=errormsg)
end subroutine CreateOpenFile
subroutine ReadStringSub(this, S, OK)
class(TFileStream) :: this
character(LEN=:), allocatable :: S
logical, optional :: OK
logical isOK
integer i, status
call this%CheckOpen
read(this%unit, iostat=status) i
isOK = status==0
if (isOK) then
if (allocated(S)) deallocate(S)
allocate(character(LEN=i)::S)
read(this%unit, iostat=status) S
isOK = status==0
end if
if (present(OK)) OK = isOK
end subroutine ReadStringSub
function ReadStringFunc(this) result(S)
class(TFileStream) :: this
character(LEN=:), allocatable :: S
call this%ReadString(S)
end function ReadStringFunc
subroutine ReadItemSub(this, R, OK)
class(TFileStream) :: this
class(*), intent(out) :: R
logical, optional :: OK
logical res
integer status
call this%CheckOpen
select type(R)
type is (real)
read(this%unit, iostat=status) R
type is (double precision)
read(this%unit, iostat=status) R
type is (integer)
read(this%unit, iostat=status) R
type is (logical)
read(this%unit, iostat=status) R
type is (character(LEN=*))
read(this%unit, iostat=status) R
class default
call this%Error('Unknown type to read')
end select
res = status==0
if (status/=0 .and. (.not. IS_IOSTAT_END(status) .or. .not. present(OK))) &
& call this%Error('Error reading item')
if (present(OK)) OK = res
end subroutine ReadItemSub
function ReadItemFunc(this, R) result(res)
class(TFileStream) :: this
class(*), intent(out) :: R
logical :: res
call this%ReadItemSub(R, res)
end function ReadItemFunc
subroutine ReadArray(this, R, n, OK)
class(TFileStream) :: this
class(*) :: R(1:)
integer, intent(in), optional :: n
logical, optional :: OK
integer status
call this%CheckOpen
select type(R)
type is (real)
read(this%unit, iostat=status) R(1:PresentDefault(size(R),n))
type is (double precision)
read(this%unit, iostat=status) R(1:PresentDefault(size(R),n))
type is (integer)
read(this%unit, iostat=status) R(1:PresentDefault(size(R),n))
type is (logical)
read(this%unit, iostat=status) R(1:PresentDefault(size(R),n))
class default
call this%Error('Unknown type to read')
end select
if (status/=0 .and. (.not. IS_IOSTAT_END(status) .or. .not. present(OK))) &
& call this%Error('Error reading item')
if (present(OK)) OK = status==0
end subroutine ReadArray
function ReadArrayFunc(this, R, n) result(res)
class(TFileStream) :: this
class(*) :: R(1:)
integer, intent(in), optional :: n
logical :: res
call this%ReadArray(R,n,res)
end function ReadArrayFunc
function ReadArray2Func(this, R) result(res)
class(TFileStream) :: this
class(*) :: R(:,:)
logical :: res
call this%ReadArray2(R,res)
end function ReadArray2Func
subroutine ReadArray2(this, R, OK)
class(TFileStream) :: this
class(*) :: R(:,:)
logical, optional :: OK
integer status
call this%CheckOpen
select type(R)
type is (real)
read(this%unit, iostat=status) R
type is (double precision)
read(this%unit, iostat=status) R
type is (integer)
read(this%unit, iostat=status) R
type is (logical)
read(this%unit, iostat=status) R
class default
call this%Error('Unknown type to read')
end select
if (status/=0 .and. (.not. IS_IOSTAT_END(status) .or. .not. present(OK))) &
& call this%Error('Error reading item')
if (present(OK)) OK = status==0
end subroutine ReadArray2
subroutine ReadSizedArray_R(this, R)
class(TFileStream) :: this
Real, allocatable :: R(:)
integer sz
call this%Read(sz)
if (allocated(R)) deallocate(R)
allocate(R(sz))
call this%ReadArray(R)
end subroutine ReadSizedArray_R
subroutine ReadSizedArray_D(this, R)
class(TFileStream) :: this
double precision, allocatable :: R(:)
integer sz
call this%Read(sz)
if (allocated(R)) deallocate(R)
allocate(R(sz))
call this%ReadArray(R)
end subroutine ReadSizedArray_D
subroutine ReadSizedArray_I(this, R)
class(TFileStream) :: this
integer, allocatable :: R(:)
integer sz
call this%Read(sz)
if (allocated(R)) deallocate(R)
allocate(R(sz))
call this%ReadArray(R)
end subroutine ReadSizedArray_I
subroutine ReadSizedArray2_R(this, R)
class(TFileStream) :: this
real, allocatable :: R(:,:)
integer sz1, sz2
call this%Read(sz1)
call this%Read(sz2)
if (allocated(R)) deallocate(R)
allocate(R(sz1,sz2))
call this%ReadArray2(R)
end subroutine ReadSizedArray2_R
subroutine ReadSizedArray2_D(this, R)
class(TFileStream) :: this
double precision, allocatable :: R(:,:)
integer sz1, sz2
call this%Read(sz1)
call this%Read(sz2)
if (allocated(R)) deallocate(R)
allocate(R(sz1,sz2))
call this%ReadArray2(R)
end subroutine ReadSizedArray2_D
subroutine ReadSizedArray2_I(this, R)
class(TFileStream) :: this
integer, allocatable :: R(:,:)
integer sz1, sz2
call this%Read(sz1)
call this%Read(sz2)
if (allocated(R)) deallocate(R)
allocate(R(sz1,sz2))
call this%ReadArray2(R)
end subroutine ReadSizedArray2_I
subroutine WriteItemSub(this, R)
class(TFileStream) :: this
class(*), intent(in) :: R
call this%CheckOpen(.true.)
select type(R)
type is (real)
Write(this%unit) R
type is (double precision)
Write(this%unit) R
type is (integer)
Write(this%unit) R
type is (logical)
Write(this%unit) R
type is (character(LEN=*))
Write(this%unit) len(R)
Write(this%unit) R
class default
call this%Error('Unknown type to Write')
end select
end subroutine WriteItemSub
subroutine WriteTrim(this, S)
class(TFileStream) :: this
character(LEN=*), intent(in) :: S
call this%WriteItemSub(trim(S))
end subroutine WriteTrim
subroutine WriteSizedArray1(this, R, n)
class(TFileStream) :: this
class(*), intent(in) :: R(1:)
integer, intent(in), optional :: n
integer sz
sz=PresentDefault(size(R),n)
call this%Write(sz)
call this%WriteArray(R,n)
end subroutine WriteSizedArray1
subroutine WriteSizedArray2(this, R)
class(TFileStream) :: this
class(*), intent(in) :: R(:,:)
call this%Write(size(R,dim=1))
call this%Write(size(R,dim=2))
call this%WriteArray2(R)
end subroutine WriteSizedArray2
subroutine WriteOneAndArray(this, I, R)
class(TFileStream) :: this
class(*), intent(in) :: I
class(*), intent(in) :: R(:)
call this%WriteItemSub(I)
call this%WriteArray(R)
end subroutine WriteOneAndArray
subroutine WriteArray(this, R, n)
class(TFileStream) :: this
class(*), intent(in) :: R(1:)
integer, intent(in), optional :: n
integer sz
sz=PresentDefault(size(R),n)
call this%CheckOpen(.true.)
select type(R)
type is (real)
Write(this%unit) R(1:sz)
type is (double precision)
Write(this%unit) R(1:sz)
type is (integer)
Write(this%unit) R(1:sz)
type is (logical)
Write(this%unit) R(1:sz)
class default
call this%Error('Unknown type to Write')
end select
end subroutine WriteArray
subroutine WriteArray2(this, R)
class(TFileStream) :: this
class(*), intent(in) :: R(:,:)
call this%CheckOpen(.true.)
select type(R)
type is (real)
Write(this%unit) R
type is (double precision)
Write(this%unit) R
type is (integer)
Write(this%unit) R
type is (logical)
Write(this%unit) R
class default
call this%Error('Unknown type to Write')
end select
end subroutine WriteArray2
subroutine WriteItems(this, S1, S2,S3,S4,S5,S6)
class(TFileStream) :: this
class(*), intent(in) :: S1
class(*), intent(in), optional :: S2, S3,S4,S5,S6
call this%WriteItemSub(S1)
if (present(S2)) call this%WriteItemSub(S2)
if (present(S3)) call this%WriteItemSub(S3)
if (present(S4)) call this%WriteItemSub(S4)
if (present(S5)) call this%WriteItemSub(S5)
if (present(S6)) call this%WriteItemSub(S6)
end subroutine WriteItems
subroutine ReadItems(this, S1, S2,S3,S4,S5,S6,OK)
class(TFileStream) :: this
class(*) S1
class(*), optional :: S2,S3,S4,S5,S6
logical, optional :: OK
call this%ReadItemSub(S1,OK)
if (present(S2) .and. DefaultTrue(OK)) call this%ReadItemSub(S2,OK)
if (present(S3) .and. DefaultTrue(OK)) call this%ReadItemSub(S3,OK)
if (present(S4) .and. DefaultTrue(OK)) call this%ReadItemSub(S4,OK)
if (present(S5) .and. DefaultTrue(OK)) call this%ReadItemSub(S5,OK)
if (present(S6) .and. DefaultTrue(OK)) call this%ReadItemSub(S6,OK)
end subroutine ReadItems
!Text unformatted files
function ReadLine(this, InLine, trimmed) result(OK)
class(TTextFile) :: this
character(LEN=:), allocatable, optional :: InLine
logical, intent(in), optional :: trimmed
integer, parameter :: line_buf_len= 1024*4
character(LEN=line_buf_len) :: InS
logical :: OK, set
integer status, size
call this%CheckOpen
OK = .false.
set = .true.
do
read (this%unit,'(a)',advance='NO',iostat=status, size=size) InS
OK = .not. IS_IOSTAT_END(status)
if (.not. OK) return
if (present(InLine)) then
if (set) then
InLine = InS(1:size)
set=.false.
else
InLine = InLine // InS(1:size)
end if
end if
if (IS_IOSTAT_EOR(status)) exit
end do
if (present(trimmed) .and. present(InLine)) then
if (trimmed) InLine = trim(adjustl(InLine))
end if
end function ReadLine
function ReadLineSkipEmptyAndComments(this, InLine, comment) result(OK)
class(TTextFile) :: this
logical :: OK
character(LEN=:), allocatable :: InLine
character(LEN=:), allocatable, optional, intent(inout) :: comment
if (present(comment)) then
if (.not. allocated(comment)) comment=''
end if
do
OK = this%ReadLine(InLine, trimmed=.true.)
if (.not. OK) return
if (InLine=='') cycle
if (InLine(1:1)/='#') then
return
else
if (present(comment)) comment = trim(InLine(2:))
end if
end do
end function ReadLineSkipEmptyAndComments
function ReadNextContentLine(this, filename, InLine) result(OK)
class(TTextFile) :: this
character(LEN=*), intent(in) :: filename
character(LEN=:), intent(out), allocatable :: InLine
logical :: OK
if (.not. this%Opened()) call this%Open(filename)
OK = this%ReadLineSkipEmptyAndComments(InLine)
if (.not. OK) call this%Close()
end function ReadNextContentLine
function SkipLines(this, n) result(OK)
class(TTextFile) :: this
integer, intent(in) :: n
logical OK
integer ix
do ix = 1, n
if (.not. this%ReadLine()) then
OK = .false.
return
end if
end do
OK = .true.
end function SkipLines
function Columns(this) result(n)
class(TTextFile) :: this
integer n
character(LEN=:), allocatable :: InLine
if (this%ReadLineSkipEmptyAndComments(InLine)) then
n = File%TxtNumberColumns(InLine)
else
n=0
end if
call this%Rewind()
end function Columns
function Lines(this, nocomments) result(n)
class(TTextFile) :: this
logical, intent(in), optional :: nocomments
integer n
character(LEN=:), allocatable :: InLine
n=0
if (DefaultTrue(nocomments)) then
do while (this%ReadLineSkipEmptyAndComments(InLine))
n = n+1
end do
else
do while (this%ReadLine())
n = n+1
end do
end if
call this%Rewind()
end function Lines
function DefaultAdvance(this,advance)
class(TTextFile) :: this
logical, intent(in), optional :: advance
character(3) :: DefaultAdvance
if (PresentDefault(this%AdvanceDefault,advance)) then
DefaultAdvance='YES'
else
DefaultAdvance='NO'
end if
end function
subroutine NewLine(this)
class(TTextFile) :: this
call this%WriteItemTxt('')
end subroutine
subroutine WriteLeftAligned(this, Form, str)
class(TTextFile) :: this
character(LEN=*) str, Form
Character(LEN=max(len(str),128)) tmp
call this%CheckOpen(.true.)
tmp = str
write(this%unit,form, advance='NO') tmp
end subroutine WriteLeftAligned
subroutine WriteInLineItems(this, S1, S2,S3,S4,S5,S6)
class(TTextFile) :: this
class(*), intent(in) :: S1
class(*), intent(in), optional :: S2, S3,S4,S5,S6
call this%WriteInLine(S1)
if (present(S2)) call this%WriteInLine(S2)
if (present(S3)) call this%WriteInLine(S3)
if (present(S4)) call this%WriteInLine(S4)
if (present(S5)) call this%WriteInLine(S5)
if (present(S6)) call this%WriteInLine(S6)
end subroutine WriteInLineItems
subroutine WriteItemsTxt(this, S1, S2,S3,S4,S5,S6)
class(TTextFile) :: this
class(*), intent(in) :: S1
class(*), intent(in), optional :: S2, S3,S4,S5,S6
call this%WriteInLineItems(S1, S2,S3,S4,S5,S6)
call this%NewLine()
end subroutine WriteItemsTxt
subroutine WriteInLineItem(this,str,form)
class(TTextFile) :: this
class(*), intent(in) :: str
character(LEN=*), intent(in), optional :: form
call this%WriteItemTxt(str,form,.false.)
end subroutine WriteInLineItem
subroutine WriteInLineArray(this,str,form,n)
class(TTextFile) :: this
class(*), intent(in) :: str(:)
character(LEN=*), intent(in), optional :: form
integer, intent(in), optional :: n
call this%WriteArrayTxt(str,form,.false.,number=n)
end subroutine WriteInLineArray
subroutine TTextFile_WriteItem(this,R)
class(TTextFile) :: this
class(*), intent(in) :: R
call this%WriteItemTxt(R,advance=.true.)
end subroutine TTextFile_WriteItem
subroutine TTextFile_WriteArray(this,R,n)
class(TTextFile) :: this
class(*), intent(in) :: R(:)
integer, intent(in), optional :: n
call this%WriteArrayTxt(R,number=n,advance=.true.)
end subroutine TTextFile_WriteArray
subroutine TTextFile_WriteOneAndArray(this, I, R)
class(TTextFile) :: this
class(*), intent(in) :: I
class(*), intent(in) :: R(:)
call this%WriteInlineItem(I)
call this%WriteArrayTxt(R)
end subroutine TTextFile_WriteOneAndArray
subroutine WriteItemTxt(this, str, form, advance)
class(TTextFile) :: this
class(*), intent(in) :: str
character(LEN=*), intent(in), optional :: form
logical, intent(in), optional :: advance
character(LEN=3) :: Ad
call this%CheckOpen(.true.)
Ad = this%DefaultAdvance(advance)
select type(str)
type is (character(LEN=*))
if (Ad=='YES') then
write(this%unit,PresentDefault('(a)',form)) trim(str)
else
write(this%unit,PresentDefault('(a)',form), advance=Ad) str
end if
type is (real)
write(this%unit,PresentDefault(this%RealFormat,form), advance=Ad) str
type is (double precision)
write(this%unit,PresentDefault(this%RealFormat,form), advance=Ad) str
type is (integer)
write(this%unit,PresentDefault(this%IntegerFormat,form), advance=Ad) str
type is (logical)
write(this%unit,PresentDefault('(L2)',form), advance=Ad) str
class default
call this%Error('unknown type to write')
end select
end subroutine WriteItemTxt
subroutine WriteArrayTxt(this, str, form, advance, number)
class(TTextFile) :: this
class(*), intent(in) :: str(:)
character(LEN=*), intent(in), optional :: form
logical, intent(in), optional :: advance
integer, intent(in), optional :: number
integer n
character(LEN=3) :: Ad