/
inifile.f90
480 lines (367 loc) · 12.5 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
!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.
!This version March 2005.
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.
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
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)
Type (TNameValueList) :: L
L%Count = 0
L%Capacity = 0
L%Delta = 128
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) :: L
character(LEN=*), intent(in) :: AName
CHARACTER(LEN=*) :: 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
subroutine TNameValueList_Add(L, AName, AValue)
Type (TNameValueList) :: L
character(LEN=*), intent(in) :: AName, AValue
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)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: AInLine
integer EqPos, slashpos, lastpos
character (LEN=len(AInLine)) :: AName, S, InLine
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)
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)
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
subroutine Ini_Open_File(Ini, filename, unit_id, error, slash_comments)
Type(TIniFile) :: Ini
character (LEN=*), intent(IN) :: filename
integer, intent(IN) :: unit_id
logical, intent(OUT) :: error
logical, optional, intent(IN) :: slash_comments
character (LEN=120) :: InLine
call TNameValueList_Init(Ini%L)
call TNameValueList_Init(Ini%ReadValues)
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 /= '') call Ini_NameValue_Add(Ini,InLine)
end do
400 close(unit_id)
error=.false.
return
500 error=.true.
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)
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_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 (Ini_fail_on_not_found) then
write(*,*) 'key not found : '//Key
stop
end if
if (present(NotFoundFail)) then
if (NotFoundFail) then
write(*,*) 'key not found : '//Key
stop
end if
end if
end function Ini_Read_String_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_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_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