This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inifile.f90
828 lines (642 loc) · 23.9 KB
/
inifile.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
!Module to read in name/value pairs from a file, with each line of the form line 'name = value'
!Should correctly interpret FITS headers
!Antony Lewis (http://cosmologist.info/). Released to the public domain.
!Apr 11, added support for INCLUDE(file); check for duplicate keys
!Jun 12, added support for DEFAULT(file), provides default values for keys not defined in the read file
module IniFile
implicit none
public
integer, parameter :: Ini_max_name_len = 128
integer, parameter :: Ini_max_string_len = 1024
logical :: Ini_fail_on_not_found = .false.
logical :: Ini_Echo_Read = .false.
logical :: Ini_AllowDuplicateKeys = .false.
type TNameValue
!no known way to make character string pointers..
character(Ini_max_name_len) :: Name
character(Ini_max_string_len):: Value
end type TNameValue
type TNameValue_pointer
Type(TNameValue), pointer :: P
end type TNameValue_pointer
Type TNameValueList
integer Count
integer Delta
integer Capacity
logical ignoreDuplicates
type(TNameValue_pointer), dimension(:), pointer :: Items
end Type TNameValueList
Type TIniFile
logical SlashComments
Type (TNameValueList) :: L, ReadValues
end Type TIniFile
Type(TIniFile) :: DefIni
contains
subroutine TNameValueList_Init(L, ignoreDuplicates)
Type (TNameValueList) :: L
logical, intent(in), optional :: ignoreDuplicates
L%Count = 0
L%Capacity = 0
L%Delta = 128
L%ignoreDuplicates = .false.
if (present(ignoreDuplicates)) L%ignoreDuplicates=ignoreDuplicates
nullify(L%Items)
end subroutine TNameValueList_Init
subroutine TNameValueList_Clear(L)
Type (TNameValueList) :: L
integer i, status
do i=L%count,1,-1
deallocate (L%Items(i)%P, stat = status)
end do
deallocate (L%Items, stat = status)
call TNameValueList_Init(L)
end subroutine TNameValueList_Clear
subroutine TNameValueList_ValueOf(L, AName, AValue)
Type (TNameValueList), intent(in) :: L
character(LEN=*), intent(in) :: AName
CHARACTER(LEN=*), intent(out) :: AValue
integer i
do i=1, L%Count
if (L%Items(i)%P%Name == AName) then
AValue = L%Items(i)%P%Value
return
end if
end do
AValue = ''
end subroutine TNameValueList_ValueOf
function TNameValueList_IndexOf(L, AName) result (AValue)
Type (TNameValueList), intent(in) :: L
character(LEN=*), intent(in) :: AName
integer :: AValue
integer i
do i=1, L%Count
if (L%Items(i)%P%Name == AName) then
AValue = i
return
end if
end do
AValue = -1
end function TNameValueList_IndexOf
function TNameValueList_HasKey(L, AName) result (AValue)
Type (TNameValueList), intent(in) :: L
character(LEN=*), intent(in) :: AName
logical :: AValue
AValue = TNameValueList_IndexOf(L,AName) /= -1
end function TNameValueList_HasKey
subroutine TNameValueList_Add(L, AName, AValue, only_if_undefined)
Type (TNameValueList) :: L
character(LEN=*), intent(in) :: AName, AValue
logical, optional, intent(in) :: only_if_undefined
logical isDefault
if (present(only_if_undefined)) then
isDefault = only_if_undefined
else
isDefault = .true.
end if
if ((.not. Ini_AllowDuplicateKeys .or. isDefault) .and. TNameValueList_HasKey(L,AName)) then
if (L%ignoreDuplicates .or. isDefault) return
write (*,*) 'IniFile,TNameValueList_Add: duplicate key name in file: '//trim(AName)
stop
end if
if (L%Count == L%Capacity) call TNameValueList_SetCapacity(L, L%Capacity + L%Delta)
L%Count = L%Count + 1
allocate(L%Items(L%Count)%P)
L%Items(L%Count)%P%Name = AName
L%Items(L%Count)%P%Value = AValue
end subroutine TNameValueList_Add
subroutine TNameValueList_SetCapacity(L, C)
Type (TNameValueList) :: L
integer C
type(TNameValue_pointer), dimension(:), pointer :: TmpItems
if (L%Count > 0) then
if (C < L%Count) stop 'TNameValueList_SetCapacity: smaller than Count'
allocate(TmpItems(L%Count))
TmpItems = L%Items(1:L%Count)
deallocate(L%Items)
allocate(L%Items(C))
L%Items(1:L%Count) = TmpItems
deallocate(TmpItems)
else
allocate(L%Items(C))
end if
L%Capacity = C
end subroutine TNameValueList_SetCapacity
subroutine TNameValueList_Delete(L, i)
Type (TNameValueList) :: L
integer, intent(in) :: i
deallocate(L%Items(i)%P)
if (L%Count > 1) L%Items(i:L%Count-1) = L%Items(i+1:L%Count)
L%Count = L%Count -1
end subroutine TNameValueList_Delete
subroutine Ini_NameValue_Add(Ini,AInLine,only_if_undefined)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: AInLine
integer EqPos, slashpos, lastpos
logical, optional, intent(in) :: only_if_undefined
logical isDefault
character (LEN=len(AInLine)) :: AName, S, InLine
if (present(only_if_undefined)) then
isDefault = only_if_undefined
else
isDefault = .false.
end if
InLine=trim(adjustl(AInLine))
EqPos = scan(InLine,'=')
if (EqPos/=0 .and. InLine(1:1)/='#' .and. InLine(1:7) /= 'COMMENT' ) then
AName = trim(InLine(1:EqPos-1))
S = adjustl(InLine(EqPos+1:))
if (Ini%SlashComments) then
slashpos=scan(S,'/')
if (slashpos /= 0) then
S = S(1:slashpos-1)
end if
end if
lastpos=len_trim(S)
if (lastpos>1) then
if (S(1:1)=='''' .and. S(lastpos:lastpos)=='''') then
S = S(2:lastpos-1)
end if
end if
call TNameValueList_Add(Ini%L, AName, S,only_if_undefined = isDefault )
end if
end subroutine Ini_NameValue_Add
subroutine Ini_Open(filename, unit_id, error, slash_comments)
character (LEN=*), intent(IN) :: filename
integer, intent(IN) :: unit_id
logical, optional, intent(OUT) :: error
logical, optional, intent(IN) :: slash_comments
logical aerror
call TNameValueList_Init(DefIni%L)
call TNameValueList_Init(DefIni%ReadValues, .true.)
if (present(slash_comments)) then
call Ini_Open_File(DefIni,filename,unit_id,aerror,slash_comments)
else
call Ini_Open_File(DefIni,filename,unit_id,aerror)
end if
if (present(error)) then
error = aerror
else
if (aerror) then
write (*,*) 'Ini_Open: Error opening file ' // trim(filename)
stop
end if
end if
end subroutine Ini_Open
function Ini_ExtractFilePath(aname)
character(LEN=*), intent(IN) :: aname
character(LEN=Ini_max_string_len) Ini_ExtractFilePath
integer len, i
len = len_trim(aname)
do i = len, 1, -1
if (aname(i:i)=='/') then
Ini_ExtractFilePath = aname(1:i)
return
end if
end do
Ini_ExtractFilePath = ''
end function Ini_ExtractFilePath
recursive subroutine Ini_Open_File(Ini, filename, unit_id, &
error, slash_comments, append,only_if_undefined)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: filename
integer, intent(IN) :: unit_id
logical, intent(OUT) :: error
logical, optional, intent(IN) :: slash_comments
logical, optional, intent(in) :: append, only_if_undefined
character (LEN=Ini_max_string_len) :: InLine, IncludeFile
integer lastpos, i
Type (TNameValueList) IncudeFiles, DefaultValueFiles
logical doappend, FileExists, isDefault
if (present(append)) then
doappend=append
else
doappend=.false.
end if
if (present(only_if_undefined)) then
isDefault = only_if_undefined
else
isDefault = .false.
end if
if (.not. doappend) then
call TNameValueList_Init(Ini%L)
call TNameValueList_Init(Ini%ReadValues, .true.)
end if
call TNameValueList_Init(IncudeFiles)
call TNameValueList_Init(DefaultValueFiles)
if (present(slash_comments)) then
Ini%SlashComments = slash_comments
else
Ini%SlashComments = .false.
end if
open(unit=unit_id,file=filename,form='formatted',status='old', err=500)
do
read (unit_id,'(a)',end=400) InLine
if (InLine == 'END') exit;
if (InLine(1:8) == 'INCLUDE(') then
lastpos = scan(InLine,')')
if (lastpos/=0) then
call TNameValueList_Add(IncudeFiles, trim(adjustl(InLine(9:lastpos-1))),'')
else
stop 'Ini_Open_File: error in INCLUDE line'
end if
elseif (InLine(1:8) == 'DEFAULT(') then
!Settings to read in as defaults, overridden by subsequent re-definitions
lastpos = scan(InLine,')')
if (lastpos/=0) then
call TNameValueList_Add(DefaultValueFiles, trim(adjustl(InLine(9:lastpos-1))),'')
else
stop 'Ini_Open_File: error in DEFAULT line'
end if
elseif (InLine /= '') then
call Ini_NameValue_Add(Ini,InLine, only_if_undefined=isDefault)
end if
end do
400 close(unit_id)
error=.false.
do i=1, IncudeFiles%Count
if (error) exit
IncludeFile=IncudeFiles%Items(i)%P%Name
inquire(file=IncludeFile, exist = FileExists)
if (.not. FileExists) then
IncludeFile=trim(Ini_ExtractFilePath(filename))//trim(IncludeFile)
inquire(file=IncludeFile, exist = FileExists)
if (.not. FileExists) then
write(*,*) 'Ini_Open_File: INCLUDE file not found: '//trim(IncudeFiles%Items(i)%P%Name)
stop
end if
end if
call Ini_Open_File(Ini, IncludeFile, unit_id, &
error, slash_comments, append=.true.,only_if_undefined=isDefault)
end do
do i=1, DefaultValueFiles%Count
if (error) exit
IncludeFile=DefaultValueFiles%Items(i)%P%Name
inquire(file=IncludeFile, exist = FileExists)
if (.not. FileExists) then
IncludeFile=trim(Ini_ExtractFilePath(filename))//trim(IncludeFile)
inquire(file=IncludeFile, exist = FileExists)
if (.not. FileExists) then
write(*,*) 'Ini_Open_File: DEFAULT file not found:' //trim(DefaultValueFiles%Items(i)%P%Name)
stop
end if
end if
call Ini_Open_File(Ini, IncludeFile, unit_id, &
error, slash_comments, append=.true., only_if_undefined=.true.)
end do
call TNameValueList_Clear(IncudeFiles)
call TNameValueList_Clear(DefaultValueFiles)
return
500 error=.true.
call TNameValueList_Clear(IncudeFiles)
call TNameValueList_Clear(DefaultValueFiles)
end subroutine Ini_Open_File
subroutine Ini_Open_Fromlines(Ini, Lines, NumLines, slash_comments)
Type(TIniFile) :: Ini
integer, intent(IN) :: NumLines
character (LEN=*), dimension(NumLines), intent(IN) :: Lines
logical, intent(IN) :: slash_comments
integer i
call TNameValueList_Init(Ini%L)
call TNameValueList_Init(Ini%ReadValues, .true.)
Ini%SlashComments = slash_comments
do i=1,NumLines
call Ini_NameValue_Add(Ini,Lines(i))
end do
end subroutine Ini_Open_Fromlines
subroutine Ini_Close
call Ini_close_File(DefIni)
end subroutine Ini_Close
subroutine Ini_Close_File(Ini)
Type(TIniFile) :: Ini
call TNameValueList_Clear(Ini%L)
call TNameValueList_Clear(Ini%ReadValues)
end subroutine Ini_Close_File
function Ini_Read_String(Key, NotFoundFail) result(AValue)
character (LEN=*), intent(IN) :: Key
logical, optional, intent(IN) :: NotFoundFail
character(LEN=Ini_max_string_len) :: AValue
if (present(NotFoundFail)) then
AValue = Ini_Read_String_File(DefIni, Key, NotFoundFail)
else
AValue = Ini_Read_String_File(DefIni, Key)
end if
end function Ini_Read_String
function Ini_Read_String_Default(Key, Default,AllowBlank) result(AValue)
character (LEN=*), intent(IN) :: Key, Default
character(LEN=Ini_max_string_len) :: AValue
logical, intent(in), optional :: AllowBlank
if (present(AllowBlank)) then
AValue = Ini_Read_String_Default_File(DefIni, Key, Default, AllowBlank)
else
AValue = Ini_Read_String_Default_File(DefIni, Key, Default)
end if
end function Ini_Read_String_Default
function Ini_Read_String_File(Ini, Key, NotFoundFail) result(AValue)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: Key
logical, optional, intent(IN) :: NotFoundFail
character(LEN=Ini_max_string_len) :: AValue
call TNameValueList_ValueOf(Ini%L, Key, AValue)
if (AValue/='') then
call TNameValueList_Add(Ini%ReadValues, Key, AValue)
if (Ini_Echo_Read) write (*,*) trim(Key)//' = ',trim(AValue)
return
end if
if (present(NotFoundFail)) then
if (NotFoundFail) then
write(*,*) 'key not found : '//trim(Key)
stop
end if
else if (Ini_fail_on_not_found) then
write(*,*) 'key not found : '//trim(Key)
stop
end if
end function Ini_Read_String_File
function Ini_Read_String_Default_File(Ini, Key, Default, AllowBlank) result(AValue)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: Key, Default
character(LEN=Ini_max_string_len) :: AValue
logical, intent(in), optional :: AllowBlank
if (Ini_HasKey_File(Ini,Key)) then
AValue = Ini_Read_String_file(Ini, Key, .false.)
if (present(AllowBlank)) then
if (AllowBlank) return
end if
if (AValue=='') AValue = Default
else
AValue = Default
end if
end function Ini_Read_String_Default_File
function Ini_HasKey(Key) result(AValue)
character (LEN=*), intent(IN) :: Key
logical AValue
AValue = Ini_HasKey_File(DefIni, Key)
end function Ini_HasKey
function Ini_HasKey_File(Ini, Key) result(AValue)
type(TIniFile), intent(in) :: Ini
character (LEN=*), intent(IN) :: Key
logical AValue
Avalue = TNameValueList_HasKey(Ini%L, Key)
end function Ini_HasKey_File
function Ini_Key_To_Arraykey(Key, index) result(AValue)
character (LEN=*), intent(IN) :: Key
integer, intent(in) :: index
character(LEN=Ini_max_string_len) :: AValue
character(LEN=32) :: numstr
write (numstr,*) index
numstr=adjustl(numstr)
AValue = trim(Key) // '(' // trim(numStr) // ')'
end function Ini_Key_To_Arraykey
function Ini_Read_String_Array(Key, index, NotFoundFail) result(AValue)
character (LEN=*), intent(IN) :: Key
integer, intent(in) :: index
logical, optional, intent(IN) :: NotFoundFail
character(LEN=Ini_max_string_len) :: AValue
if (present(NotFoundFail)) then
AValue = Ini_Read_String_Array_File(DefIni, Key, index, NotFoundFail)
else
AValue = Ini_Read_String_Array_File(DefIni, Key, index)
end if
end function Ini_Read_String_Array
function Ini_Read_String_Array_File(Ini, Key, index, NotFoundFail) result(AValue)
Type(TIniFile) :: Ini
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
logical, optional, intent(IN) :: NotFoundFail
character(LEN=Ini_max_string_len) :: AValue
character(LEN=Ini_max_string_len) :: ArrayKey
ArrayKey = Ini_Key_To_Arraykey(Key,index)
if (present(NotFoundFail)) then
AValue = Ini_Read_String_File(Ini, ArrayKey, NotFoundFail)
else
AValue = Ini_Read_String_File(Ini, ArrayKey)
end if
end function Ini_Read_String_Array_File
function Ini_Read_Int_Array(Key, index, Default)
integer, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
integer Ini_Read_Int_Array
if (present(Default)) then
Ini_Read_Int_Array = Ini_Read_Int_Array_File(DefIni, Key, index, Default)
else
Ini_Read_Int_Array = Ini_Read_Int_Array_File(DefIni, Key, index)
end if
end function Ini_Read_Int_Array
function Ini_Read_Int_Array_File(Ini,Key, index, Default)
!Reads Key(1), Key(2), etc.
Type(TIniFile) :: Ini
integer Ini_Read_Int_Array_File
integer, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: ArrrayKey
ArrrayKey = Ini_Key_To_Arraykey(Key,index)
if (present(Default)) then
Ini_Read_Int_Array_File = Ini_Read_Int_File(Ini, ArrrayKey, Default)
else
Ini_Read_Int_Array_File = Ini_Read_Int_File(Ini, ArrrayKey)
end if
end function Ini_Read_Int_Array_File
function Ini_Read_Int(Key, Default)
integer, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
integer Ini_Read_Int
if (present(Default)) then
Ini_Read_Int = Ini_Read_Int_File(DefIni, Key, Default)
else
Ini_Read_Int = Ini_Read_Int_File(DefIni, Key)
end if
end function Ini_Read_Int
function Ini_Read_Int_File(Ini, Key, Default)
Type(TIniFile) :: Ini
integer Ini_Read_Int_File
integer, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: S
S = Ini_Read_String_File(Ini, Key,.not. present(Default))
if (S == '') then
if (.not. present(Default)) then
write(*,*) 'no value for key: '//Key
stop
end if
Ini_Read_Int_File = Default
write (S,*) Default
call TNameValueList_Add(Ini%ReadValues, Key, S)
else
if (verify(trim(S),'-+0123456789') /= 0) goto 10
read (S,*, err = 10) Ini_Read_Int_File
end if
return
10 write (*,*) 'error reading integer for key: '//Key
stop
end function Ini_Read_Int_File
function Ini_Read_Double(Key, Default)
double precision, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
double precision Ini_Read_Double
if (present(Default)) then
Ini_Read_Double = Ini_Read_Double_File(DefIni, Key, Default)
else
Ini_Read_Double = Ini_Read_Double_File(DefIni, Key)
end if
end function Ini_Read_Double
function Ini_Read_Double_File(Ini,Key, Default)
Type(TIniFile) :: Ini
double precision Ini_Read_Double_File
double precision, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: S
S = Ini_Read_String_File(Ini,Key,.not. present(Default))
if (S == '') then
if (.not. present(Default)) then
write(*,*) 'no value for key: '//Key
stop
end if
Ini_Read_Double_File = Default
write (S,*) Default
call TNameValueList_Add(Ini%ReadValues, Key, S)
else
read (S,*, err=10) Ini_Read_Double_File
end if
return
10 write (*,*) 'error reading double for key: '//Key
stop
end function Ini_Read_Double_File
function Ini_Read_Double_Array(Key, index, Default)
double precision, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
double precision Ini_Read_Double_Array
if (present(Default)) then
Ini_Read_Double_Array = Ini_Read_Double_Array_File(DefIni, Key, index, Default)
else
Ini_Read_Double_Array = Ini_Read_Double_Array_File(DefIni, Key, index)
end if
end function Ini_Read_Double_Array
function Ini_Read_Double_Array_File(Ini,Key, index, Default)
!Reads Key(1), Key(2), etc.
Type(TIniFile) :: Ini
double precision Ini_Read_Double_Array_File
double precision, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: ArrrayKey
ArrrayKey = Ini_Key_To_Arraykey(Key,index)
if (present(Default)) then
Ini_Read_Double_Array_File = Ini_Read_Double_File(Ini, ArrrayKey, Default)
else
Ini_Read_Double_Array_File = Ini_Read_Double_File(Ini, ArrrayKey)
end if
end function Ini_Read_Double_Array_File
function Ini_Read_Real(Key, Default)
real, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
real Ini_Read_Real
if (present(Default)) then
Ini_Read_Real = Ini_Read_Real_File(DefIni, Key, Default)
else
Ini_Read_Real = Ini_Read_Real_File(DefIni, Key)
end if
end function Ini_Read_Real
function Ini_Read_Real_File(Ini,Key, Default)
Type(TIniFile) :: Ini
real Ini_Read_Real_File
real, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: S
S = Ini_Read_String_File(Ini,Key,.not. present(Default))
if (S == '') then
if (.not. present(Default)) then
write(*,*) 'no value for key: '//Key
stop
end if
Ini_Read_Real_File = Default
write (S,*) Default
call TNameValueList_Add(Ini%ReadValues, Key, S)
else
read (S,*, err=10) Ini_Read_Real_File
end if
return
10 write (*,*) 'error reading double for key: '//Key
stop
end function Ini_Read_Real_File
function Ini_Read_Real_Array(Key, index, Default)
real, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
real Ini_Read_Real_Array
if (present(Default)) then
Ini_Read_Real_Array = Ini_Read_Real_Array_File(DefIni, Key, index, Default)
else
Ini_Read_Real_Array = Ini_Read_Real_Array_File(DefIni, Key, index)
end if
end function Ini_Read_Real_Array
function Ini_Read_Real_Array_File(Ini,Key, index, Default)
!Reads Key(1), Key(2), etc.
Type(TIniFile) :: Ini
real Ini_Read_Real_Array_File
real, optional, intent(IN) :: Default
integer, intent(in) :: index
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: ArrrayKey
ArrrayKey = Ini_Key_To_Arraykey(Key,index)
if (present(Default)) then
Ini_Read_Real_Array_File = Ini_Read_Real_File(Ini, ArrrayKey, Default)
else
Ini_Read_Real_Array_File = Ini_Read_Real_File(Ini, ArrrayKey)
end if
end function Ini_Read_Real_Array_File
function Ini_Read_Logical(Key, Default)
Logical, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
logical Ini_Read_Logical
if (present(Default)) then
Ini_Read_Logical = Ini_Read_Logical_File(DefIni, Key, Default)
else
Ini_Read_Logical = Ini_Read_Logical_File(DefIni, Key)
end if
end function Ini_Read_Logical
function Ini_Read_Logical_File(Ini, Key, Default)
Type(TIniFile) :: Ini
logical Ini_Read_Logical_File
logical, optional, intent(IN) :: Default
character (LEN=*), intent(IN) :: Key
character(LEN=Ini_max_string_len) :: S
S = Ini_Read_String_File(Ini,Key,.not. present(Default))
if (S == '') then
if (.not. present(Default)) then
write(*,*) 'no value for key: '//Key
stop
end if
Ini_Read_Logical_File = Default
write (S,*) Default
call TNameValueList_Add(Ini%ReadValues, Key, S)
else
if (verify(trim(S),'10TF') /= 0) goto 10
read (S,*, err = 10) Ini_Read_Logical_File
end if
return
10 write (*,*) 'error reading logical for key: '//Key
stop
end function Ini_Read_Logical_File
subroutine Ini_SaveReadValues(afile,unit_id)
character(LEN=*) :: afile
integer, intent(in) :: unit_id
call Ini_SaveReadValues_File(DefIni, afile, unit_id)
end subroutine Ini_SaveReadValues
subroutine Ini_SaveReadValues_File(Ini, afile, unit_id)
Type(TIniFile) :: Ini
character(LEN=*), intent(in) :: afile
integer, intent(in) :: unit_id
integer i
open(unit=unit_id,file=afile,form='formatted',status='replace', err=500)
do i=1, Ini%ReadValues%Count
write (unit_id,'(a)') trim(Ini%ReadValues%Items(i)%P%Name) // ' = ' &
//trim(Ini%ReadValues%Items(i)%P%Value)
end do
close(unit_id)
return
500 write(*,*) 'Ini_SaveReadValues_File: Error creating '//trim(afile)
end subroutine Ini_SaveReadValues_File
end module IniFile