-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyvalue.F
More file actions
1545 lines (1277 loc) · 42.1 KB
/
keyvalue.F
File metadata and controls
1545 lines (1277 loc) · 42.1 KB
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
C--------------------------------------------------------
C input routines
C author: H. Kino
C date: Nov. 25, 2002
C last modify: Oct. 6, 2003
C Copyright, all rights reserved.
C-------------------------------------------------------
c This routine is a flexible general readin routine applicable to read
c real(8),integer(4), logical, arrays (real or integer), char.
c Also it can return <foo> </foo> block in the input file as the file handle.
c
c These usage examples below corresponds to an input test file "IN" as
c {keyword} {data arry} format as
c ----IN file start here (remove "c " at head of each line) -----------
c int_1 325
c delta 2.35
c L_test .true.
c n1n2n3 34 57 58 68 98
c vv 3.2 4.6 2.5
c <blockname>
c abc dgf
c 32 1.4 4.5 9.0 k1 k2
c 18 3.4 4.5 kkk v1
c </blockname>
c --- to here -------------------------------------------------------------
c The data in the <blockname> is a bit special because getkeyvalue just returns
c file hundle (and line numbers.) to read the contents. See example 5 below.
c In order to use getkeyvalue, you first have to declear "use keyvalue".
c See main/kino_input_test.f with "IN" file to see how it works.
c
c 1. call getkeyvalue("IN","int_1",ikey,default=999,status=ret )
c 2. call getkeyvalue("IN","delta",rkey,default=1430d0,status=ret)
c
c 3. logical:: lkey
c call getkeyvalue("IN","L_test",lkey,status=ret )
c
c 4. integer(4):: ivkey(5),nsize=5
c call getkeyvalue("IN","n1n2n3",ivkey,nsize,status=ret )
c
c 5. real(8):: rvkey(3)
c integer(4)::nsize=3
c call getkeyvalue("IN","vv", rvkey,nsize,status=ret,
c & default=(/10.0d0,10.0d0,10.0d0/))
c : Note nsize is required. If default is not assigned, this cause stop
c within getkeyvalue.
c
c 6. call getkeyvalue("IN","char", ckey, status=ret)
c 7. call getkeyvalue("IN","<blockname>",unit=file,status=ret,errstop='on')
c : unit is the file handle to read contants sandwitched by
c <blockname> and </blockname>. ret= number of the lines.
C--------------------------------------------------------------------------
module keyvalue
implicit none
integer,private :: nf_input=0,output=6,default_nf_input=50
character(120),private::
& filename0="adfadjpfijwpojobuwhfqeijqope"
integer,private,parameter:: input_err_success=0
integer,private,parameter:: input_err_conv=-1
integer,private,parameter:: input_err_lessvaluenum=-2
integer,private,parameter:: input_err_nokey=-3
integer,private,parameter:: input_err_nofile=-4
integer,private,parameter:: nlogical=14
character(7),private:: logicalstr(nlogical)=
i (/'true ', 'ok ', '.true. ', 'yes ','on ',
i '1 ', 'T ',
i 'false ', 'ng ', '.false.', 'no ','off ',
i '0 ', 'F '/)
integer,private :: logicalint(nlogical) =
i (/1,1,1,1,1,1,1,0,0,0,0,0,0,0/)
c debug
integer,private:: printlevel=0
c error report
integer,private,parameter:: max_error_count=5
integer,private :: error_count=0
character(120),private:: error_message(max_error_count)
interface getkeyvalue
module procedure getkeyvalue_i, getkeyvalue_r8,
i getkeyvalue_iv, getkeyvalue_rv, getkeyvalue_l ,
i getkeyvalue_c
end interface
c ----------------------------------------------------------
contains
c----------------------------------------------------------
c used only for internal use
c
integer function unusedfid()
implicit none
integer:: i
logical ::L
c check the last file is still open or not?
c nf_input==0 means the first time, no check is necessary.
c if (nf_input.ne.0) then
c inquire(nf_input,opened=L)
! takao aug2005. This is because nf_input might be closed and opened in other part---
inquire(opened=L,file=filename0)
if (L) then
call getkeyvalue_err_exit(
. 'unusedfid(getkeyvalue module)',' The last file is still opened. Close it first.')
endif
c endif
c find new unused file id
do i=10,999
inquire(i,opened=L)
if (.not.L) then
unusedfid=i
return
endif
enddo
c error
call getkeyvalue_err_exit(
. 'unusedfid(getkeyvalue module)' ,' no fid left')
end function unusedfid
c----------------------------------------------------------
subroutine input_delfirstspc(buf)
implicit none
character(*):: buf
integer:: i
buf=adjustl(buf)
c cut comment
if (buf(1:1).eq.'#' .or. buf(1:1).eq.'!') then
buf=''
return
endif
c do i=1,len_trim(buf)
c if (buf(i:i).eq.'#' .or. buf(i:i).eq.'!') buf(i:)=''
c enddo
end subroutine input_delfirstspc
c-----------------------------------------------------------
subroutine input_toupper(buf)
implicit none
character(*) :: buf
integer:: i,n
n=len_trim(buf)
do i=1,n
if ( ichar(buf(i:i)).ge.ichar('a') .and.
i ichar(buf(i:i)).le.ichar('z')) then
buf(i:i) = char(ichar(buf(i:i))+ichar('Z')-ichar('z'))
endif
enddo
end subroutine input_toupper
c-----------------------------------------------------------
subroutine input_tolower(buf)
implicit none
character(*) :: buf
integer:: i,n
n=len_trim(buf)
do i=1,n
if ( ichar(buf(i:i)).ge.ichar('A') .and.
i ichar(buf(i:i)).le.ichar('Z')) then
buf(i:i) = char(ichar(buf(i:i))+ichar('z')-ichar('Z'))
endif
enddo
end subroutine input_tolower
c-----------------------------------------------------------
subroutine input_debugstart(id)
integer,intent(in),optional:: id
if (present(id) ) then
printlevel=id
else
printlevel=1
endif
end subroutine input_debugstart
subroutine input_debugend()
printlevel=0
end subroutine input_debugend
c----------------------------------------------------------
subroutine input_registerError(msg)
implicit none
character(*):: msg
error_count=error_count+1
if (error_count<=max_error_count) then
error_message(error_count)=msg
endif
end subroutine input_registerError
c----------------------------------------------------------
integer function input_printError(file)
implicit none
integer,optional,intent(in):: file
integer:: i,n,nf
character(120) :: buf
n=error_count
if (error_count>max_error_count) n=max_error_count
nf=6
if (present(file)) nf=file
do i=1,n
buf = error_message(i)
write(nf,'(a)') buf(:len_trim(buf))
enddo
input_printError=error_count
end function input_printError
c----------------------------------------------------------
subroutine input_open(name,nf,success,outputfile)
implicit none
character(*),intent(in)::name
integer,intent(in):: nf
logical,optional,intent(out)::success
integer,optional,intent(in):: outputfile
logical:: flag
nf_input = nf
if (printlevel>0) then
write(output,*) 'filename=<',name,'>'
endif
open(unit=nf_input,file=name,status='old',err=1000)
c open(unit=nf_input,file=name,err=1000)
filename0=name
if (printlevel>0) then
write(output,*) 'succeed to open a file ',name
endif
flag=.true.
error_count=0 ! reset error
goto 2000
1000 flag=.false.
write(output,*) 'failed to open a file ',name
2000 if (present(success)) success=flag
output=6
if (printlevel>0) write(6,*) 'input/output=',nf_input,output
if (present(outputfile)) output=outputfile
end subroutine input_open
c----------------------------------------------------------
subroutine input_redirect(nfinp,name,nf,success,outputfile)
implicit none
character(*),intent(in)::name
integer,intent(in):: nf,nfinp
logical,optional,intent(out)::success
integer,optional,intent(in):: outputfile
logical:: flag
character(200):: buf
nf_input = nf
write(output,*) 'filename=<',name,'>'
open(unit=nf_input,file=name,status='unknown',err=1000)
write(output,*) 'succeed to open a file ',name
do
read(nfinp,'(a200)',end=1500) buf
write(nf_input,'(a)') buf(:len_trim(buf))
enddo
1500 continue
filename0=name
close(nf_input)
open(unit=nf_input,file=name,status='old',err=2000)
flag=.true.
9000 continue
error_count=0 ! reset error
if (present(success)) success=flag
output=6
if (present(outputfile)) output=outputfile
return
!--------------------------
1000 flag=.false.
write(output,*) 'failed to open a file ',name
error_count=0 ! reset error
if (present(success)) success=flag
output=6
if (present(outputfile)) output=outputfile
return
!---------------------------
2000 flag=.false.
write(output,*) 'failed to open a file ',name
error_count=0 ! reset error
if (present(success)) success=flag
output=6
if (present(outputfile)) output=outputfile
return
!--------------------------
end subroutine input_redirect
c----------------------------------------------------------
subroutine input_reopen(nf)
integer,intent(out),optional:: nf
if (present(nf)) then
nf =nf_input
endif
open(unit=nf_input,file=filename0(:len_trim(filename0)),status='old',err=1000)
return
1000 continue
write(output,*) 'failed to open a file ',filename0(:len_trim(filename0))
end subroutine input_reopen
c----------------------------------------------------------
subroutine input_close
implicit none
close(nf_input)
nf_input=0
end subroutine input_close
c----------------------------------------------------------------
subroutine input_characterv(key0,n,i,defaultvalue,success,nout)
implicit none
character(*),intent(in)::key0
integer,intent(in):: n
character(*),intent(out):: i(n)
character(*), intent(in):: defaultvalue(n)
integer,intent(out),optional:: success
character(120):: buf,keybuf,key
integer:: nkey,nkeybuf,j
integer :: flag,ix,noutx
!
integer,optional :: nout !sep2006 takao
i=defaultvalue
key=key0
call input_toupper(key)
c write(output,*) 'key=(',key,')'
nkey=len_trim(key)
rewind(nf_input)
flag=input_err_nokey
buf=' '
cccccccccccccccccc
ix=0
ccccccccccccccccc
do
c
c read and convert them to uppercase string
c
cccccccccccccccccccccccccccccccccccc
c ix=ix+1
c write(6,"(i4,a120)")ix, buf
cccccccccccccccccccccccccccccccccc
read(nf_input,'(a120)',end=1000) buf
if (printlevel>10) then
write(output,*) '(',buf(:len_trim(buf)),')'
endif
call input_delfirstspc(buf)
c
if (len_trim(buf).eq.0) cycle
c
c read the part corresponding to key, -> uppercase
c
read(buf,*) keybuf
call input_toupper(keybuf)
nkeybuf=len_trim(keybuf)
if (printlevel>10) then
write(output,*) key,keybuf
endif
c
c compare length and strings
c
if ( keybuf(1:nkeybuf).eq.key(1:nkey) ) then
flag=input_err_lessvaluenum
if (printlevel.gt.10) then
write(output,*) 'input_characterv:found<',buf(nkey+1:),
. '>'
endif
c read(buf(nkey+1:),*,end=2000,err=2000) i(1:n)
buf=buf(nkey+1:)
buf= adjustl(buf)
c
call input_septoken(buf,n,i,noutx) !takao sep2006
write(6,*) 'readin nout=',noutx
if(present(nout)) nout=noutx
if (printlevel.ge.1) then
write(output,*)'characterv:', key(:len_trim(key)), (i(j),j=1,noutx)
endif
flag= input_err_success
goto 1000
endif
enddo
1000 continue
! write(output,*) '1000 end'
if (present(success)) success =flag
return
c
c unexpected intrrupt of read
c
2000 continue !takao now no jump to 2000. So dummy below...----------
if (printlevel.ge.10) then
write(output,*)'parameter error, key=',key(:nkey),
i ' must has ',n,' value(s)'
endif
write(buf,*)'parameter error, key=',key(:nkey),
i ' must has ',n,' value(s)'
call input_registerError(buf)
flag=input_err_lessvaluenum
if (present(success)) success =flag
end subroutine input_characterv
c----------------------------------------------------------------
subroutine input_logicalv(key,n,i,default,success)
implicit none
character(*),intent(in):: key
integer,intent(in):: n
logical,intent(out):: i(n)
logical,intent(in):: default(n)
integer,intent(out),optional:: success
integer :: flag
integer ::j,strlen,logicallen,istr,ilog
character(10):: str(n),defstr(n),str1,str2
character(80):: buf
c set defaults
do j=1,n
i(j) = default(j)
defstr(j)=' '
str(j)=' '
enddo
c read as strings
call input_characterv(key,n,str,defstr,flag)
if (printlevel>10) then
write(output,*) 'after input_characterv: ',flag,str
endif
if (flag.eq.input_err_success) then
c successfully read, then convert
c read(..., end) is done in input_characterv()
do istr=1,n
str1=str(istr)
call input_tolower(str1)
strlen=len_trim(str1)
do ilog=1,nlogical
str2=logicalstr(ilog)
call input_tolower(str2)
logicallen=len_trim(str2)
if (printlevel>10) then
write(output,*)str1(:len_trim(str1)),str2(:len_trim(str2)),
i str1(:strlen).eq.str2(:logicallen)
endif
if (
i str1(:strlen).eq.str2(:logicallen) ) then
if (logicalint(ilog).eq.1) then
i(istr) = .true.
else
i(istr) = .false.
endif
goto 1000
endif
enddo
goto 2000
1000 continue
enddo
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=',(i(j),j=1,n)
endif
return
endif
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=',(i(j),j=1,n)
endif
return
2000 continue ! error
if (present(success)) success=input_err_conv
write(buf,*) 'conversion error, key=', key(:len_trim(key)),
i ' ', n,' values=logical(s)'
call input_registerError(buf)
end subroutine input_logicalv
c----------------------------------------------------------------
subroutine input_integerv(key,n,i,defaultvalue,success)
implicit none
character(*),intent(in)::key
integer,intent(in):: n
integer,intent(out):: i(n)
integer,intent(in):: defaultvalue(n)
c integer,intent(out),optional:: success
integer,optional:: success
character(40):: str(n),defstr(n)
character(120) :: buf
integer :: j
integer :: flag,nout
c set defaults
do j=1,n
i(j) = defaultvalue(j)
defstr(j)=''
enddo
c read as strings
call input_characterv(key,n,str,defstr,flag,nout)
if (printlevel>10) then
write(output,*) 'after input_characterv: ',flag,str
endif
c ---
if(flag.eq.input_err_success) then
c successfully read, then convert
c read(..., end) is done in input_characterv()
read(str,*,err=2000,end=3000) (i(j),j=1,nout) !takao sep2006
write(6,*)" aaaaaaaaaaa success",nout,success
if (present(success)) then
if(success/=-9999) then
if(n/=nout) goto 3000
endif
success = nout
endif
else
if (present(success)) success = flag
endif
if( printlevel.ge.1 ) then
write(output,*) key(:len_trim(key)), (i(j),j=1,nout)
endif
return
c
c conversion error of read()
c
2000 flag=input_err_conv
do j=nout+1,n
i(j) = defaultvalue(j)
enddo
if (present(success)) success = -nout + 1000*flag
write(buf,*) 'conversion error, key=', key(:len_trim(key)),
i ' ', n,' values=integer'
call input_registerError(buf)
return
3000 success=input_err_lessvaluenum
return
end subroutine input_integerv
c---------------------------------------------------------------
subroutine input_real8v(key,n,i,defaultvalue,success)
implicit none
character(*),intent(in)::key
integer,intent(in):: n
real*8,intent(out):: i(n)
real*8, intent(in):: defaultvalue(n)
integer,intent(out),optional:: success
character(40):: str(n),defstr(n)
character(120) :: buf
integer :: j
integer :: flag
c set defaults
do j=1,n
i(j) = defaultvalue(j)
defstr(j)=''
enddo
c read as strings
call input_characterv(key,n,str,defstr,flag)
if (printlevel>10) then
write(output,*) 'after input_characterv: ',flag,str
endif
if (flag.eq.input_err_success) then
c successfully read, then convert
c read(..., end) is done in input_characterv()
read(str,*,err=2000,end=3000) (i(j),j=1,n)
if (present(success)) success = n
else
if (present(success)) success = flag
endif
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)), (i(j),j=1,n)
endif
return
c
c conversion error of read()
c
2000 flag=input_err_conv
do j=1,n
i(j) = defaultvalue(j)
enddo
if (present(success)) success = flag
write(buf,*) 'conversion error, key=', key(:len_trim(key)),
i ' ',n,' values=real*8'
call input_registerError(buf)
return
3000 success= input_err_lessvaluenum
return
end subroutine input_real8v
c----------------------------------------------------------------
function input_character(
i key,defaultvalue,success)
implicit none
character(*),intent(in)::key
character(*),intent(in):: defaultvalue
integer,intent(out),optional:: success
character(120) input_character
character(120):: i(1),defaultvalue0(1)
integer:: flag
input_character= defaultvalue
defaultvalue0(1)=defaultvalue
c use input_characterv
call input_characterv(key,1,i,defaultvalue0,flag)
if (flag.eq.input_err_success) then
input_character=i(1)
endif
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)), '=',
i input_character(:len_trim(input_character))
endif
if (present(success)) success=flag
end function input_character
c---------------------------------------------------------------
integer function input_integer(key,defaultvalue,success)
implicit none
character(*),intent(in)::key
integer,intent(in):: defaultvalue
integer,intent(out),optional:: success
character(120):: i(1),defaultvalue0(1),buf
integer:: flag
input_integer=defaultvalue
defaultvalue0(1)=''
call input_characterv(key,1,i,defaultvalue0,flag)
if (flag.eq.input_err_success) then
read(i(1),*,err=2000) input_integer
else
input_integer = defaultvalue
endif
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=', input_integer
endif
return
c
c conversion error of read()
c
2000 flag= input_err_conv
input_integer=defaultvalue
if (present(success)) success=flag
write(buf,*) 'conversion error, key=',key(:len_trim(key)),
i ' value=integer'
call input_registerError(buf)
end function input_integer
c---------------------------------------------------------------
real*8 function input_real8(key,defaultvalue,success)
implicit none
character(*),intent(in)::key
real*8,intent(in):: defaultvalue
integer,intent(out),optional:: success
character(120):: i(1),defaultvalue0(1),buf
integer:: flag
input_real8=defaultvalue
defaultvalue0(1)=''
call input_characterv(key,1,i,defaultvalue0,flag)
if (flag.eq.input_err_success) then
c write(output,*) i(1)
read(i(1),*,err=2000) input_real8
endif
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=', input_real8
endif
return
c
c conversion error of read()
c
2000 flag=input_err_conv
input_real8=defaultvalue
if (present(success)) success=flag
write(buf,*) 'conversion error, key=',key(:len_trim(key)),
i ' value=real*8'
call input_registerError(buf)
end function input_real8
c----------------------------------------------------------------
integer function input_character2integer(
i key,n,ans,vans,success)
implicit none
integer,intent(in):: n,vans(n)
character(*),intent(in):: key,ans(n)
integer,intent(out),optional::success
character(120):: buf,compstr
integer:: flag
integer :: nbuf,i
c character(120) input_character
c
c read a key and its value
c
buf= input_character(key,' ',flag)
if (printlevel>10) then
write(output,*)'key=',key, 'value=',buf,'flag=',flag
endif
input_character2integer=vans(1) !default value
if (flag.eq.input_err_success) then
flag=input_err_conv
call input_toupper(buf)
nbuf=len_trim(buf)
do i=1,n
compstr=ans(i)
call input_toupper(compstr)
if (printlevel>10) then
write(output,*) i,buf(:nbuf),compstr(:nbuf)
endif
c
c compare them in the upper case
c
if (
i buf(:nbuf).eq. compstr(:len_trim(compstr)) ) then
input_character2integer=vans(i)
flag=input_err_success
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=',
i input_character2integer
endif
return
endif
enddo
! --- no matching
write(buf,*)'conversion error: key=',key(:len_trim(key))
call input_registerError(buf)
endif
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=',input_character2integer,
i ' (default value)'
endif
if (present(success)) success=flag
end function input_character2integer
c----------------------------------------------------------------
logical function input_logical(key,default,success)
implicit none
character(*),intent(in):: key
logical,intent(in):: default
integer,intent(out),optional:: success
integer :: flag
integer ::ret
c integer input_character2integer
ret=input_character2integer(key,nlogical
i , logicalstr, logicalint, flag)
if (flag.eq.input_err_success) then
if (ret==1) then
input_logical=.true.
else
input_logical=.false.
endif
else
input_logical=default
endif
if (present(success)) success=flag
if (printlevel.ge.1) then
write(output,*) key(:len_trim(key)),'=', input_logical
endif
end function input_logical
c----------------------------------------------------------------
logical function input_find(key0,nf,cont)
implicit none
character(*),intent(in)::key0
integer,intent(out):: nf ! maybe out
logical,intent(in),optional:: cont
character(120):: buf,key
integer:: nkey,nbuf
logical:: flag
nf=0
key=key0
call input_toupper(key)
nkey=len_trim(key)
flag=.true.
if (present(cont)) then
if (cont) then
flag=.false.
endif
endif
if (flag) rewind(nf_input)
do
read(nf_input,*,end=1000) buf
if (len_trim(buf).eq.0) cycle
call input_delfirstspc(buf)
nbuf=len_trim(buf)
call input_toupper(buf)
if (printlevel>10) then
write(output,*) '<',buf(:nkey),'|',key(:nkey),'>',
. buf(:nbuf).eq.key(:nkey)
endif
if ( buf(:nbuf).eq.key(:nkey)) then
if (printlevel>0) then
write(output,*) key(:len_trim(key)),' found'
endif
input_find=.true.
nf=nf_input
return
endif
enddo
1000 input_find=.false.
end function input_find
c-------------------------------------------------------------
logical function input_last(key0)
implicit none
character(*),intent(in)::key0
character(120):: buf,key
integer:: nkey
key=key0
call input_toupper(key)
nkey=len_trim(key)
read(nf_input,'(a120)',end=1000) buf
call input_delfirstspc(buf)
call input_toupper(buf)
c write(output,*) buf(:nkey),key(:nkey),
c . buf(1:nkey).eq.key(:nkey)
if (
. buf(1:len_trim(buf)).eq.key(:nkey)) then
if (printlevel>0) then
write(output,*) key(:len_trim(key)),' found'
endif
input_last=.true.
return
endif
1000 continue
input_last=.false.
end function input_last
c-------------------------------------------------------------
integer function input_existfiles(n,filenames,ret)
implicit none
integer,intent(in):: n
character(*),intent(in) :: filenames(n)
integer,intent(in):: ret(n+1)
integer:: i
character(80):: buf
logical :: fexist
do i=1,n
buf=filenames(i)
if (printlevel>0)then
write(6,*) 'open ',buf(:len_trim(buf))
endif
c open(fileid,file=buf(:len_trim(buf)),status='old',err=1000)
INQUIRE(file=buf(:len_trim(buf)),EXIST=FEXIST)
if (fexist) then
if (printlevel>0) then
write(6,*) 'exist'
endif
input_existfiles=ret(i)
c close(fileid)
return
else
if (printlevel>0) then
write(6,*) 'not exist'
endif
endif
enddo
input_existfiles=ret(n+1)
end function input_existfiles
c-------------------------------------------------------------
subroutine input_septoken(line,n,bufs,nout)
implicit none
integer,intent(in):: n
character(*),intent(in):: line
character(*),intent(out):: bufs(n)
integer::i,j,max,ibuf
integer:: nout
do i=1,n
bufs(i)=' '
enddo
max=len_trim(line)
if (printlevel>0)then
write(*,*) max,line(:max)
endif
ibuf=0
i=1
do
do while (line(i:i).eq.' ')
i=i+1
if (i>max) goto 1000
enddo
j=i+1
do while (line(j:j).ne.' ')
j=j+1
if (j>max) exit
enddo
j=j-1
if (printlevel>0) then
write(*,*) i,j,line(i:j)
endif
ibuf=ibuf+1
bufs(ibuf)=line(i:j)
if (ibuf>=n) goto 1000
i=j+1
if (i>max) goto 1000
enddo
1000 continue
nout=ibuf !takao Sep2006
end subroutine input_septoken
c-------------------------------------------------------------
subroutine input_replacekeys(str,nkey,key,val,ret,ierror)
implicit none
integer,intent(in) :: nkey
character(*),intent(in) :: str,val(nkey),key(nkey)
character(*),intent(out) :: ret
integer,intent(out),optional:: ierror
character(200):: buf,buf2,buf3,buf4
integer :: i,lenstr,lenkey,ikey,ipos(nkey)
logical :: flag
lenstr=len_trim(str)
ipos(:)=0