go_rc.F90 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. !#################################################################
  2. !
  3. ! call ReadRc( rcfile, 'test.flag', l, status [,default=.false.] )
  4. !
  5. ! return status :
  6. ! <0 : key not found, value set to default
  7. ! 0 : key found and value read without errors
  8. ! >0 : some errors
  9. !
  10. ! Search for extended keys:
  11. !
  12. ! call ReadRc( rcfile, 'test', (/'* ','all','b '/), flag, status, default=.true. )
  13. !
  14. ! will search for (dots are inserted automatically):
  15. !
  16. ! test.* : F
  17. ! test.all : F
  18. ! test.b : T
  19. !
  20. ! The last found key overwrites all previous values.
  21. !
  22. !#################################################################
  23. !
  24. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  25. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  26. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  27. !
  28. !#################################################################
  29. module GO_Rc
  30. implicit none
  31. ! --- in/out ---------------------
  32. private
  33. public :: TrcFile, RcBuffer
  34. public :: Init, Done
  35. public :: ReadRc
  36. ! --- const ---------------------------------
  37. character(len=*), parameter :: mname = 'GO_Rc'
  38. ! maximum line length in rc file:
  39. integer, parameter :: buflen = 512
  40. ! --- types ---------------------------------
  41. type RcBuffer
  42. character(len=buflen) :: key
  43. character(len=buflen) :: value
  44. end type RcBuffer
  45. type TrcFile
  46. character(len=80) :: fname
  47. integer :: rcitems
  48. type(RcBuffer),dimension(:), pointer :: Rc_Table
  49. end type TrcFile
  50. ! --- interfaces -------------------------------------
  51. interface Init
  52. module procedure rcfile_Init
  53. end interface
  54. interface Done
  55. module procedure rcfile_Done
  56. end interface
  57. interface ReadRc
  58. module procedure ReadRc_i
  59. module procedure ReadRcs_i
  60. module procedure ReadRc_i1
  61. module procedure ReadRc_r
  62. module procedure ReadRcs_r
  63. module procedure ReadRc_r1
  64. module procedure ReadRc_l
  65. module procedure ReadRcs_l
  66. module procedure ReadRc_s
  67. module procedure ReadRcs_s
  68. end interface
  69. contains
  70. ! ================================================================
  71. ! ===
  72. ! === init, done
  73. ! ===
  74. ! ================================================================
  75. subroutine rcfile_Init( rcfile, fname, status )
  76. use GO_Print, only : gol, goErr
  77. ! --- in/out ---------------------------
  78. type(TrcFile), intent(out) :: rcfile
  79. character(len=*), intent(in) :: fname
  80. integer, intent(out) :: status
  81. ! --- const ---------------------------
  82. character(len=*), parameter :: rname = mname//'/rcfile_Init'
  83. ! --- local --------------------------
  84. logical :: exist=.False.
  85. ! --- begin ---------------------------
  86. ! file not present ?
  87. inquire( file=trim(fname), exist=exist )
  88. if ( .not. exist ) then
  89. write (gol,'("rcfile not found :")'); call goErr
  90. write (gol,'(" ",a)') trim(fname); call goErr
  91. TRACEBACK; status=1; return
  92. end if
  93. ! store file name:
  94. rcfile%fname = trim(fname)
  95. ! empty yet:
  96. rcfile%rcitems = 0
  97. nullify( rcfile%Rc_Table )
  98. ! parse rcfile: read and store keys and values in a table
  99. call Parse_Rcfile(rcfile,status)
  100. if (status/=0) then
  101. write (gol,'("rcfile seems empty")'); call goErr
  102. write (gol,'(" ",a)') trim(fname); call goErr
  103. TRACEBACK; status=1; return
  104. end if
  105. ! ok
  106. status = 0
  107. end subroutine rcfile_Init
  108. ! ***
  109. subroutine rcfile_Done( rcfile, status )
  110. ! --- in/out ---------------------------
  111. type(TrcFile), intent(inout) :: rcfile
  112. integer, intent(out) :: status
  113. ! --- const ---------------------------
  114. character(len=*), parameter :: rname = mname//'/rcfile_Done'
  115. ! --- begin ---------------------------
  116. if ( associated(rcfile%Rc_Table) ) deallocate(rcfile%Rc_Table)
  117. nullify( rcfile%Rc_Table )
  118. ! ok
  119. status = 0
  120. end subroutine rcfile_Done
  121. ! ================================================================
  122. ! ===
  123. ! === parse rcfile into memory
  124. ! ===
  125. ! ================================================================
  126. subroutine Parse_Rcfile( rcfile, status )
  127. use GO_Print , only : gol, goErr
  128. use GO_String, only : goSplitLine, goTab2Space
  129. use GO_File , only : TTextFile, Init, Done, ReadLine, RewindFile
  130. ! --- in/out ----------------------
  131. type(TrcFile), intent(inout) :: rcfile
  132. integer, intent(out) :: status
  133. ! --- const ---------------------------
  134. character(len=*), parameter :: rname = mname//'/ParseRcfile'
  135. ! --- local -----------------------
  136. type(TTextFile) :: file
  137. integer :: iostat
  138. Integer :: nfound
  139. character(len=512) :: s, skey, sdata
  140. integer :: l
  141. ! --- begin --------------------------
  142. ! open commented text file:
  143. call Init( file, rcfile%fname, status, status='old', comment='!' )
  144. IF_NOTOK_RETURN(status=1)
  145. ! no matching lines found yet ...
  146. nfound = 0
  147. ! count all lines
  148. do
  149. ! read next non empty, non comment line:
  150. call ReadLine( file, s, status )
  151. if (status<0) exit ! end of file
  152. nfound = nfound + 1
  153. IF_NOTOK_RETURN(status=1)
  154. enddo
  155. RcFile%rcitems = nfound
  156. if ( associated (rcfile%Rc_Table) ) deallocate(rcfile%Rc_Table)
  157. allocate(rcfile%Rc_Table(nfound), stat = status)
  158. IF_NOTOK_RETURN(status=1)
  159. call RewindFile(file, status)
  160. IF_NOTOK_RETURN(status=1)
  161. ! parse file in buffer:
  162. nfound = 0
  163. do
  164. ! read next non empty, non comment line:
  165. call ReadLine( file, s, status )
  166. if (status<0) exit ! end of file
  167. nfound = nfound + 1
  168. IF_NOTOK_RETURN(status=1)
  169. ! Andy Jacobson, 10 Apr 2006. Allows tabs in rc file.
  170. call goTab2Space( s )
  171. ! split at colon:
  172. call goSplitLine( s, skey, ':', sdata, status )
  173. IF_NOTOK_RETURN(status=1)
  174. rcfile%Rc_Table(nfound)%key = trim(skey)
  175. rcfile%Rc_Table(nfound)%value = trim(sdata)
  176. end do
  177. ! close:
  178. call Done( file, status )
  179. IF_NOTOK_RETURN(status=1)
  180. ! not found ? warning status
  181. if ( nfound == 0 ) then
  182. status=-1; return
  183. end if
  184. ! ok
  185. status = 0
  186. end subroutine Parse_Rcfile
  187. ! ================================================================
  188. ! ===
  189. ! === general read
  190. ! ===
  191. ! ================================================================
  192. ! Searches the file <filenameResource> for the string
  193. ! "<key> : "
  194. ! and save all characters behind the equal sign in <buffer>.
  195. ! The Resource file may contain comment lines starting with a "!"
  196. subroutine ReadRcItem( rcfile, key, buffer, status )
  197. use GO_Print , only : gol, goErr
  198. ! --- in/out ----------------------
  199. type(TrcFile), intent(in) :: rcfile
  200. character(len=*), intent(in) :: key
  201. character(len=*), intent(out) :: buffer
  202. integer, intent(out) :: status
  203. ! --- const ---------------------------
  204. character(len=*), parameter :: rname = mname//'/ReadRcItem'
  205. ! --- local -----------------------
  206. Integer :: nfound
  207. character(len=512) :: skey
  208. integer :: l, i
  209. ! --- begin --------------------------
  210. ! no matching lines found yet ...
  211. nfound = 0
  212. ! scan all lines
  213. do i=1,rcfile%rcitems
  214. ! starts with requested key, and no extra text between key and colon ? then found!
  215. skey = rcfile%Rc_Table(i)%key
  216. if ( (index(skey,key)==1) .and. (len_trim(key)==len_trim(skey))) then
  217. buffer = rcfile%Rc_Table(i)%value
  218. nfound = nfound + 1
  219. end if
  220. end do
  221. ! not found ? warning status
  222. if ( nfound == 0 ) then
  223. status=-1; return
  224. end if
  225. ! multiple matches ?
  226. if ( nfound > 1 ) then
  227. write (gol,'("found more than one matching keys in rcfile:")'); call goErr
  228. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  229. write (gol,'(" key : ",a)') trim(key); call goErr
  230. write (gol,'(" found : ",i4," times")') nfound
  231. TRACEBACK; status=1; return
  232. end if
  233. ! ok
  234. status = 0
  235. end subroutine ReadRcItem
  236. ! ================================================================
  237. ! ===
  238. ! === integer
  239. ! ===
  240. ! ================================================================
  241. subroutine ReadRc_i( rcfile, key, i, status, default )
  242. use GO_Print, only : gol, goErr
  243. ! --- in/out ----------------------------
  244. type(TrcFile), intent(in) :: rcfile
  245. character(len=*), intent(in) :: key
  246. integer, intent(out) :: i
  247. integer, intent(out) :: status
  248. integer, intent(in), optional :: default
  249. ! --- const ----------------------------
  250. character(len=*), parameter :: rname = mname//'/ReadRc_i'
  251. ! --- local -----------------------------
  252. character(len=buflen) :: buffer
  253. ! --- begin -----------------------------
  254. ! search key line in rcfile:
  255. call ReadRcItem( rcfile, key, buffer, status )
  256. if ( status < 0 ) then
  257. ! not found; set to default or leave with error:
  258. if ( present(default) ) then
  259. i = default
  260. status = -1 ; return
  261. else
  262. write (gol,'("key not found and no default specified ...")'); call goErr
  263. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  264. write (gol,'(" key : ",a)') trim(key); call goErr
  265. TRACEBACK; status=1; return
  266. end if
  267. else if ( status == 0 ) then
  268. ! key found; set value:
  269. read (buffer,*,iostat=status) i
  270. if ( status /= 0 ) then
  271. write (gol,'("while reading integer:")'); call goErr
  272. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  273. write (gol,'(" key : ",a)') trim(key); call goErr
  274. write (gol,'(" value : ",a)') trim(buffer); call goErr
  275. TRACEBACK; status=1; return
  276. end if
  277. else
  278. ! some error ...
  279. TRACEBACK; status=1; return
  280. end if
  281. ! ok
  282. status = 0
  283. end subroutine ReadRc_i
  284. ! ***
  285. subroutine ReadRcs_i( rcfile, key, keys, i, status, default )
  286. use GO_Print, only : gol, goErr
  287. ! --- in/out ----------------------------
  288. type(TrcFile), intent(in) :: rcfile
  289. character(len=*), intent(in) :: key
  290. character(len=*), intent(in) :: keys(:)
  291. integer, intent(out) :: i
  292. integer, intent(out) :: status
  293. integer, intent(in), optional :: default
  294. ! --- const ----------------------------
  295. character(len=*), parameter :: rname = mname//'/ReadRcs_i'
  296. ! --- local -----------------------------
  297. logical :: found
  298. integer :: ikey
  299. integer :: i_curr
  300. ! --- begin -----------------------------
  301. ! pessimistic assumption ...
  302. found = .false.
  303. ! loop over all key extensions:
  304. do ikey = 1, size(keys)
  305. ! try to read key;
  306. ! provide default to return without error if key is not found:
  307. call ReadRc( rcfile, trim(key)//'.'//trim(keys(ikey)), i_curr, status, default=0 )
  308. if ( status < 0 ) then
  309. ! not found; try next
  310. cycle
  311. else if ( status == 0 ) then
  312. ! found and value read:
  313. found = .true.
  314. i = i_curr
  315. else
  316. ! error ...
  317. TRACEBACK; status=1; return
  318. end if
  319. end do ! loop over keys
  320. ! not found ?
  321. if ( .not. found ) then
  322. ! default provided ?
  323. if ( present(default) ) then
  324. ! set to default:
  325. i = default
  326. else
  327. ! error ...
  328. write (gol,'("key(s) not found and no default specified ...")'); call goErr
  329. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  330. do ikey = 1, size(keys)
  331. write (gol,'(" key : ",a,".",a)') trim(key), trim(keys(ikey)); call goErr
  332. end do
  333. TRACEBACK; status=1; return
  334. end if
  335. end if
  336. ! ok
  337. status = 0
  338. end subroutine ReadRcs_i
  339. ! ***
  340. subroutine ReadRc_i1( rcfile, key, i, status, default )
  341. use GO_Print, only : gol, goErr
  342. ! --- in/out ----------------------------
  343. type(TrcFile), intent(in) :: rcfile
  344. character(len=*), intent(in) :: key
  345. integer, intent(out) :: i(:)
  346. integer, intent(out) :: status
  347. integer, intent(in), optional :: default
  348. ! --- const ----------------------------
  349. character(len=*), parameter :: rname = mname//'/ReadRc_i1'
  350. ! --- local -----------------------------
  351. character(len=buflen) :: buffer
  352. ! --- begin -----------------------------
  353. ! search key line in rcfile:
  354. call ReadRcItem( rcfile, key, buffer, status )
  355. if ( status < 0 ) then
  356. ! not found; set to default or leave with error:
  357. if ( present(default) ) then
  358. i = default
  359. status = -1 ; return
  360. else
  361. write (gol,'("key not found and no default specified ...")'); call goErr
  362. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  363. write (gol,'(" key : ",a)') trim(key); call goErr
  364. TRACEBACK; status=1; return
  365. end if
  366. else if ( status == 0 ) then
  367. ! key found; set value:
  368. read (buffer,*,iostat=status) i
  369. if ( status /= 0 ) then
  370. write (gol,'("while reading integer:")'); call goErr
  371. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  372. write (gol,'(" key : ",a)') trim(key); call goErr
  373. write (gol,'(" value : ",a)') trim(buffer); call goErr
  374. TRACEBACK; status=1; return
  375. end if
  376. else
  377. ! some error ...
  378. TRACEBACK; status=1; return
  379. end if
  380. ! ok
  381. status = 0
  382. end subroutine ReadRc_i1
  383. ! ================================================================
  384. ! ===
  385. ! === real
  386. ! ===
  387. ! ================================================================
  388. subroutine ReadRc_r( rcfile, key, r, status, default )
  389. use GO_Print, only : gol, goErr
  390. ! --- in/out ----------------------------
  391. type(TrcFile), intent(in) :: rcfile
  392. character(len=*), intent(in) :: key
  393. real, intent(out) :: r
  394. integer, intent(out) :: status
  395. real, intent(in), optional :: default
  396. ! --- const ----------------------------
  397. character(len=*), parameter :: rname = mname//'/ReadRc_r'
  398. ! --- local -----------------------------
  399. character(len=buflen) :: buffer
  400. ! --- begin -----------------------------
  401. ! search key line in rcfile:
  402. call ReadRcItem( rcfile, key, buffer, status )
  403. if ( status < 0 ) then
  404. ! not found; set to default or leave with error:
  405. if ( present(default) ) then
  406. r = default
  407. status = -1 ; return
  408. else
  409. write (gol,'("key not found and no default specified ...")'); call goErr
  410. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  411. write (gol,'(" key : ",a)') trim(key); call goErr
  412. TRACEBACK; status=1; return
  413. end if
  414. else if ( status == 0 ) then
  415. ! key found; set value:
  416. read (buffer,*,iostat=status) r
  417. if ( status /= 0 ) then
  418. write (gol,'("while reading real :")'); call goErr
  419. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  420. write (gol,'(" key : ",a)') trim(key); call goErr
  421. write (gol,'(" value : ",a)') trim(buffer); call goErr
  422. TRACEBACK; status=1; return
  423. end if
  424. else
  425. ! some error ...
  426. TRACEBACK; status=1; return
  427. end if
  428. ! ok
  429. status = 0
  430. end subroutine ReadRc_r
  431. ! ***
  432. subroutine ReadRcs_r( rcfile, key, keys, r, status, default )
  433. use GO_Print, only : gol, goErr
  434. ! --- in/out ----------------------------
  435. type(TrcFile), intent(in) :: rcfile
  436. character(len=*), intent(in) :: key
  437. character(len=*), intent(in) :: keys(:)
  438. real, intent(out) :: r
  439. integer, intent(out) :: status
  440. real, intent(in), optional :: default
  441. ! --- const ----------------------------
  442. character(len=*), parameter :: rname = mname//'/ReadRcs_r'
  443. ! --- local -----------------------------
  444. logical :: found
  445. integer :: ikey
  446. real :: r_curr
  447. ! --- begin -----------------------------
  448. ! pessimistic assumption ...
  449. found = .false.
  450. ! loop over all key extensions:
  451. do ikey = 1, size(keys)
  452. ! try to read key;
  453. ! provide default to return without error if key is not found:
  454. call ReadRc( rcfile, trim(key)//'.'//trim(keys(ikey)), r_curr, status, default=0.0 )
  455. if ( status < 0 ) then
  456. ! not found; try next
  457. cycle
  458. else if ( status == 0 ) then
  459. ! found and value read:
  460. found = .true.
  461. r = r_curr
  462. else
  463. ! error ...
  464. TRACEBACK; status=1; return
  465. end if
  466. end do ! loop over keys
  467. ! not found ?
  468. if ( .not. found ) then
  469. ! default provided ?
  470. if ( present(default) ) then
  471. ! set to default:
  472. r = default
  473. else
  474. ! error ...
  475. write (gol,'("key(s) not found and no default specified ...")'); call goErr
  476. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  477. do ikey = 1, size(keys)
  478. write (gol,'(" key : ",a,".",a)') trim(key), trim(keys(ikey)); call goErr
  479. end do
  480. TRACEBACK; status=1; return
  481. end if
  482. end if
  483. ! ok
  484. status = 0
  485. end subroutine ReadRcs_r
  486. ! ***
  487. subroutine ReadRc_r1( rcfile, key, r, status, default )
  488. use GO_Print, only : gol, goErr
  489. ! --- in/out ----------------------------
  490. type(TrcFile), intent(in) :: rcfile
  491. character(len=*), intent(in) :: key
  492. real, intent(out) :: r(:)
  493. integer, intent(out) :: status
  494. real, intent(in), optional :: default
  495. ! --- const ----------------------------
  496. character(len=*), parameter :: rname = mname//'/ReadRc_r1'
  497. ! --- local -----------------------------
  498. character(len=buflen) :: buffer
  499. integer :: k
  500. ! --- begin -----------------------------
  501. ! search key line in rcfile:
  502. call ReadRcItem( rcfile, key, buffer, status )
  503. if ( status < 0 ) then
  504. ! not found; set to default or leave with error:
  505. if ( present(default) ) then
  506. r = default
  507. status = -1 ; return
  508. else
  509. write (gol,'("key not found and no default specified ...")'); call goErr
  510. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  511. write (gol,'(" key : ",a)') trim(key); call goErr
  512. TRACEBACK; status=1; return
  513. end if
  514. else if ( status == 0 ) then
  515. ! key found; set value:
  516. read (buffer,*,iostat=status) r
  517. if ( status /= 0 ) then
  518. write (gol,'("while reading real :")'); call goErr
  519. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  520. write (gol,'(" key : ",a)') trim(key); call goErr
  521. write (gol,'(" value : ",a)') trim(buffer); call goErr
  522. TRACEBACK; status=1; return
  523. end if
  524. else
  525. ! some error ...
  526. TRACEBACK; status=1; return
  527. end if
  528. ! ok
  529. status = 0
  530. end subroutine ReadRc_r1
  531. ! ================================================================
  532. ! ===
  533. ! === logical
  534. ! ===
  535. ! ================================================================
  536. subroutine ReadRc_l( rcfile, key, l, status, default )
  537. use GO_Print, only : gol, goErr
  538. ! --- in/out ----------------------------
  539. type(TrcFile), intent(in) :: rcfile
  540. character(len=*), intent(in) :: key
  541. logical, intent(out) :: l
  542. integer, intent(out) :: status
  543. logical, intent(in), optional :: default
  544. ! --- const ----------------------------
  545. character(len=*), parameter :: rname = mname//'/ReadRc_l'
  546. ! --- local -----------------------------
  547. character(len=buflen) :: buffer
  548. ! --- begin -----------------------------
  549. ! search key line in rcfile:
  550. call ReadRcItem( rcfile, key, buffer, status )
  551. if ( status < 0 ) then
  552. ! not found; set to default or leave with warning:
  553. if ( present(default) ) then
  554. l = default
  555. status = -1 ; return
  556. else
  557. write (gol,'("key not found and no default specified ...")'); call goErr
  558. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  559. write (gol,'(" key : ",a)') trim(key); call goErr
  560. TRACEBACK; status=1; return
  561. end if
  562. else if ( status == 0 ) then
  563. ! key found; set value:
  564. read (buffer,*,iostat=status) l
  565. if ( status /= 0 ) then
  566. write (gol,'("while reading logical :")'); call goErr
  567. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  568. write (gol,'(" key : ",a)') trim(key); call goErr
  569. write (gol,'(" value : ",a)') trim(buffer); call goErr
  570. TRACEBACK; status=1; return
  571. end if
  572. else
  573. ! some error ...
  574. TRACEBACK; status=1; return
  575. end if
  576. ! ok
  577. status = 0
  578. end subroutine ReadRc_l
  579. ! ***
  580. subroutine ReadRcs_l( rcfile, key, keys, l, status, default )
  581. use GO_Print, only : gol, goErr
  582. ! --- in/out ----------------------------
  583. type(TrcFile), intent(in) :: rcfile
  584. character(len=*), intent(in) :: key
  585. character(len=*), intent(in) :: keys(:)
  586. logical, intent(out) :: l
  587. integer, intent(out) :: status
  588. logical, intent(in), optional :: default
  589. ! --- const ----------------------------
  590. character(len=*), parameter :: rname = mname//'/ReadRcs_l'
  591. ! --- local -----------------------------
  592. logical :: found
  593. integer :: ikey
  594. logical :: l_curr
  595. ! --- begin -----------------------------
  596. ! pessimistic assumption ...
  597. found = .false.
  598. ! loop over all key extensions:
  599. do ikey = 1, size(keys)
  600. ! try to read key;
  601. ! provide default to return without error if key is not found:
  602. call ReadRc( rcfile, trim(key)//'.'//trim(keys(ikey)), l_curr, status, default=.false. )
  603. if ( status < 0 ) then
  604. ! not found; try next
  605. cycle
  606. else if ( status == 0 ) then
  607. ! found and value read:
  608. found = .true.
  609. l = l_curr
  610. else
  611. ! error ...
  612. TRACEBACK; status=1; return
  613. end if
  614. end do ! loop over keys
  615. ! not found ?
  616. if ( .not. found ) then
  617. ! default provided ?
  618. if ( present(default) ) then
  619. ! set to default and leave with warning:
  620. l = default
  621. status = -1 ; return
  622. else
  623. ! error ...
  624. write (gol,'("key(s) not found and no default specified ...")'); call goErr
  625. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  626. do ikey = 1, size(keys)
  627. write (gol,'(" key : ",a,".",a)') trim(key), trim(keys(ikey)); call goErr
  628. end do
  629. TRACEBACK; status=1; return
  630. end if
  631. end if
  632. ! ok
  633. status = 0
  634. end subroutine ReadRcs_l
  635. ! ================================================================
  636. ! ===
  637. ! === character string
  638. ! ===
  639. ! ================================================================
  640. subroutine ReadRc_s( rcfile, key, s, status, default )
  641. use GO_Print, only : gol, goErr
  642. ! --- in/out ----------------------------
  643. type(TrcFile), intent(in) :: rcfile
  644. character(len=*), intent(in) :: key
  645. character(len=*), intent(out) :: s
  646. integer, intent(out) :: status
  647. character(len=*), intent(in), optional :: default
  648. ! --- const ----------------------------
  649. character(len=*), parameter :: rname = mname//'/ReadRc_s'
  650. ! --- local -----------------------------
  651. character(len=buflen) :: buffer
  652. ! --- begin -----------------------------
  653. ! search key line in rcfile:
  654. call ReadRcItem( rcfile, key, buffer, status )
  655. if ( status < 0 ) then
  656. ! not found; set to default or leave with error:
  657. if ( present(default) ) then
  658. s = trim(default)
  659. status = -1 ; return
  660. else
  661. write (gol,'("key not found and no default specified ...")'); call goErr
  662. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  663. write (gol,'(" key : ",a)') trim(key); call goErr
  664. TRACEBACK; status=1; return
  665. end if
  666. else if ( status == 0 ) then
  667. ! key found; set value:
  668. s = trim(buffer)
  669. else
  670. ! some error ...
  671. TRACEBACK; status=1; return
  672. end if
  673. ! ok
  674. status = 0
  675. end subroutine ReadRc_s
  676. ! ***
  677. subroutine ReadRcs_s( rcfile, key, keys, s, status, default )
  678. use GO_Print, only : gol, goErr
  679. ! --- in/out ----------------------------
  680. type(TrcFile), intent(in) :: rcfile
  681. character(len=*), intent(in) :: key
  682. character(len=*), intent(in) :: keys(:)
  683. character(len=*), intent(out) :: s
  684. integer, intent(out) :: status
  685. character(len=*), intent(in), optional :: default
  686. ! --- const ----------------------------
  687. character(len=*), parameter :: rname = mname//'/ReadRcs_l'
  688. ! --- local -----------------------------
  689. logical :: found
  690. integer :: ikey
  691. character(len=buflen) :: s_curr
  692. ! --- begin -----------------------------
  693. ! pessimistic assumption ...
  694. found = .false.
  695. ! loop over all key extensions:
  696. do ikey = 1, size(keys)
  697. ! try to read key;
  698. ! provide default to return without error if key is not found:
  699. call ReadRc( rcfile, trim(key)//'.'//trim(keys(ikey)), s_curr, status, default='-' )
  700. if ( status < 0 ) then
  701. ! not found; try next
  702. cycle
  703. else if ( status == 0 ) then
  704. ! found and value read:
  705. found = .true.
  706. s = trim(s_curr)
  707. else
  708. ! error ...
  709. TRACEBACK; status=1; return
  710. end if
  711. end do ! loop over keys
  712. ! not found ?
  713. if ( .not. found ) then
  714. ! default provided ?
  715. if ( present(default) ) then
  716. ! set to default:
  717. s = default
  718. ! warning status
  719. status=-1; return
  720. else
  721. ! error ...
  722. write (gol,'("key(s) not found and no default specified ...")'); call goErr
  723. write (gol,'(" rcfile : ",a)') trim(rcfile%fname); call goErr
  724. do ikey = 1, size(keys)
  725. write (gol,'(" key : ",a,".",a)') trim(key), trim(keys(ikey)); call goErr
  726. end do
  727. TRACEBACK; status=1; return
  728. end if
  729. end if
  730. ! ok
  731. status = 0
  732. end subroutine ReadRcs_s
  733. end module GO_Rc