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
|
!--------------------------------------------------------------------------------------------------!
! DFTB+: general package for performing fast atomistic simulations !
! Copyright (C) 2006 - 2023 DFTB+ developers group !
! !
! See the LICENSE file for terms of usage and distribution. !
!--------------------------------------------------------------------------------------------------!
#:include 'common.fypp'
!> Contains the low level utilities for parsing xml-data into intrinsic Fortran types.
!>
!> This module contains the utilities which can parse a strings into Fortran intrinsic types. Tokens
!> are assumed to be separated by white space, therefore strings with spaces inside can not
!> currently be handled.
module dftbp_io_tokenreader
use dftbp_common_accuracy, only : dp
use dftbp_extlibs_xmlf90, only : string, assignment(=)
use dftbp_io_charmanip, only : whiteSpaces, tolower, complementaryScan, unquotedScan
use dftbp_io_message, only : error
implicit none
private
public :: getNextToken, TOKEN_OK, TOKEN_EOS, TOKEN_ERROR
public :: LOGICAL_TRUE, LOGICAL_FALSE, LOGICAL_TRUE_LO, LOGICAL_FALSE_LO
!> Flag for signals successful token reading
integer, parameter :: TOKEN_OK = 0
!> Flag for signals end of string
integer, parameter :: TOKEN_EOS = -1
!> Flag for signals reading error
integer, parameter :: TOKEN_ERROR = -2
!> Contains procedures which read the next token from a string
interface getNextToken
module procedure getNextToken_string
module procedure getNextToken_integer
module procedure getNextToken_integerR1
module procedure getNextToken_real
module procedure getNextToken_realR1
module procedure getNextToken_complex
module procedure getNextToken_complexR1
module procedure getNextToken_logical
module procedure getNextToken_logicalR1
end interface getNextToken
!> Character representation of the logical true value
character(len=*), parameter :: LOGICAL_TRUE = "Yes"
!> Lower cased character representation of the logical true value
character(len=*), parameter :: LOGICAL_TRUE_LO = "yes"
!> Character representation of the logical false value
character(len=*), parameter :: LOGICAL_FALSE = "No"
!> Lower cased character representation of the logical false value
character(len=*), parameter :: LOGICAL_FALSE_LO = "no"
contains
!> Returns the next token from the provided string as integer
subroutine getNextToken_integer(str, tokenValue, start, iostat)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
integer, intent(out) :: tokenValue
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
integer :: iStart, iError, tokStart, tokLen, tokEnd
tokenValue = 0
iStart = start
call getNextToken_local(str, tokStart, tokEnd, tokLen, iStart)
if (tokLen == 0) then
iError = TOKEN_EOS
else if (.not. validIntegerStart(str(tokStart:tokStart))) then
! Workaround:PGI 17.10 -> strings starting with certain letters are interpreted as integers
! therefore we check explicitly
iError = TOKEN_ERROR
else
read (str(tokStart:tokEnd), *, iostat=iError) tokenValue
if (iError /= 0) then
iError = TOKEN_ERROR
else
iError = TOKEN_OK
start = iStart
end if
end if
if (present(iostat)) then
iostat = iError
elseif (iError /= TOKEN_OK) then
call error("Integer reading error")
end if
end subroutine getNextToken_integer
!> Returns the next token from the provided string as rank one integer array
subroutine getNextToken_integerR1(str, tokenValue, start, iostat, nItem)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
integer, intent(out) :: tokenValue(:)
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
!> Nr. of read items
integer, intent(out), optional :: nItem
integer :: iStart, iError, nReadItem
integer :: ii
integer :: tmp
tokenValue(:) = 0
iError = TOKEN_OK
iStart = start
do ii = 1, size(tokenValue)
call getNextToken(str, tmp, iStart, iError)
if (iError /= TOKEN_OK) then
exit
end if
tokenValue(ii) = tmp
end do
if (iError == TOKEN_OK) then
start = iStart
nReadItem = size(tokenValue)
else
nReadItem = ii - 1
end if
if (present(nItem)) then
nItem = nReadItem
end if
if (present(iostat)) then
iostat = iError
elseif (iError /= TOKEN_OK) then
call error("Integer reading error")
end if
end subroutine getNextToken_integerR1
!> Returns the next token from the provided string as string
subroutine getNextToken_string(str, tokenValue, start, iostat)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
type(string), intent(inout) :: tokenValue
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
integer :: iError, tokStart, tokEnd, tokLen
call getNextToken_local(str, tokStart, tokEnd, tokLen, start, &
&ignoreQuotation=.false.)
if (tokLen == 0) then
iError = TOKEN_EOS
else
iError = TOKEN_OK
tokenValue = str(tokStart:tokEnd)
end if
if (present(iostat)) then
iostat = iError
end if
end subroutine getNextToken_string
!> Returns the next token from the provided string as a real value.
subroutine getNextToken_real(str, tokenValue, start, iostat)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
real(dp), intent(out) :: tokenValue
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
integer :: iStart, iError, tokStart, tokEnd, tokLen
tokenValue = 0.0_dp
iStart = start
call getNextToken_local(str, tokStart, tokEnd, tokLen, iStart)
if (tokLen == 0) then
iError = TOKEN_EOS
else
read (str(tokStart:tokEnd), *, iostat=iError) tokenValue
if (iError /= 0) then
iError = TOKEN_ERROR
else
iError = TOKEN_OK
start = iStart
end if
end if
if (present(iostat)) then
iostat = iError
else
if (iError == TOKEN_ERROR) then
call error("Real reading error")
end if
end if
end subroutine getNextToken_real
!> Returns the next token from the provided string as rank one real array
subroutine getNextToken_realR1(str, tokenValue, start, iostat, nItem)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
real(dp), intent(out) :: tokenValue(:)
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
!> Nr. of read items
integer, intent(out), optional :: nItem
integer :: iStart, iError, nReadItem
integer :: ii
real(dp) :: tmp
tokenValue(:) = 0.0_dp
iError = TOKEN_OK
iStart = start
do ii = 1, size(tokenValue)
call getNextToken(str, tmp, iStart, iError)
if (iError /= TOKEN_OK) then
exit
end if
tokenValue(ii) = tmp
end do
if (iError == TOKEN_OK) then
start = iStart
nReadItem = size(tokenValue)
else
nReadItem = ii - 1
end if
if (present(nItem)) then
nItem = nReadItem
end if
if (present(iostat)) then
iostat = iError
elseif (iError /= TOKEN_OK) then
call error("Real reading error")
end if
end subroutine getNextToken_realR1
!> Returns the next token from the provided string as a real value.
subroutine getNextToken_complex(str, tokenValue, start, iostat)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
complex(dp), intent(out) :: tokenValue
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
integer :: iStart, iError, tokStart, tokEnd, tokLen, sepPos
real(dp) :: tmp_re, tmp_im
tokenValue = (0.0_dp, 0.0_dp)
iStart = start
processComplexToken: block
call getNextToken_local(str, tokStart, tokEnd, tokLen, iStart)
iError = TOKEN_EOS
if (tokLen == 0) exit processComplexToken
iError = TOKEN_ERROR
sepPos = findComplexSeparator_(str(tokStart:tokEnd)) + tokStart - 1
if (sepPos < tokStart) then
! Token contains only one number, either the imaginary or the real part
if (str(tokEnd:tokEnd) == "i") then
read(str(tokStart : tokEnd - 1), *, iostat=iError) tmp_im
tokenValue = cmplx(tokenValue%re, tmp_im, kind=dp)
else
read(str(tokStart : tokEnd), *, iostat=iError) tmp_re
tokenValue = cmplx(tmp_re, tokenValue%im, kind=dp)
end if
if (iError /= 0) exit processComplexToken
else
! Token contains two numbers, the real part and the imaginary part
if (str(tokEnd:tokEnd) /= "i") exit processComplexToken
if (findComplexSeparator_(str(sepPos + 1 : tokEnd)) /= 0) exit processComplexToken
read(str(tokStart : sepPos - 1), *, iostat=iError) tmp_re
tokenValue = cmplx(tmp_re, tokenValue%im, kind=dp)
if (iError /= 0) exit processComplexToken
read(str(sepPos : tokEnd - 1), *, iostat=iError) tmp_im
tokenValue = cmplx(tokenValue%re, tmp_im, kind=dp)
if (iError /= 0) exit processComplexToken
end if
iError = TOKEN_OK
start = iStart
end block processComplexToken
if (present(iostat)) then
iostat = iError
else if (iError == TOKEN_ERROR) then
call error("Complex reading error")
end if
contains
! Returns separator position between real and imaginary parts, or 0 if not found.
pure function findComplexSeparator_(str) result(pos)
character(*), intent(in) :: str
integer :: pos
character :: cur, prev
integer :: ii
pos = 0
if (len(str) < 2) return
! Search for +/- which is not the first character and is not part of an exponential notation
do ii = 2, len(str)
prev = str(ii - 1 : ii - 1)
cur = str(ii : ii)
if ((cur == "+" .or. cur == "-") .and. .not. (prev == "e" .or. prev == "E")) then
pos = ii
return
end if
end do
end function findComplexSeparator_
end subroutine getNextToken_complex
!> Returns the next token from the provided string as rank one complex array
subroutine getNextToken_complexR1(str, tokenValue, start, iostat, nItem)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
complex(dp), intent(out) :: tokenValue(:)
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
!> Nr. of read items
integer, intent(out), optional :: nItem
integer :: iStart, iError, nReadItem
integer :: ii
complex(dp) :: tmp
tokenValue(:) = 0.0_dp
iError = TOKEN_OK
iStart = start
do ii = 1, size(tokenValue)
call getNextToken(str, tmp, iStart, iError)
if (iError /= TOKEN_OK) then
exit
end if
tokenValue(ii) = tmp
end do
if (iError == TOKEN_OK) then
start = iStart
nReadItem = size(tokenValue)
else
nReadItem = ii - 1
end if
if (present(nItem)) then
nItem = nReadItem
end if
if (present(iostat)) then
iostat = iError
elseif (iError /= TOKEN_OK) then
call error("Complex reading error")
end if
end subroutine getNextToken_complexR1
!> Returns the next token from the provided string as logical
subroutine getNextToken_logical(str, tokenValue, start, iostat)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
logical, intent(out) :: tokenValue
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
integer :: iStart, iError, tokStart, tokEnd, tokLen
character(len=len(str)) :: buffer
tokenValue = .false.
iStart = start
iError = TOKEN_OK
call getNextToken_local(str, tokStart, tokEnd, tokLen, iStart)
if (tokLen == 0) then
iError = TOKEN_EOS
else
buffer = tolower(str(tokStart:tokEnd))
if (trim(buffer) == LOGICAL_TRUE_LO) then
tokenValue = .true.
elseif (trim(buffer) == LOGICAL_FALSE_LO) then
tokenValue = .false.
else
iError = TOKEN_ERROR
end if
end if
start = iStart
if (present(iostat)) then
iostat = iError
else
if (iError == TOKEN_ERROR) then
call error("Token reading error")
end if
end if
end subroutine getNextToken_logical
!> Returns the next token from the provided string as logical
subroutine getNextToken_logicalR1(str, tokenValue, start, iostat, nItem)
!> String to parse
character(len=*), intent(in) :: str
!> Contains the value of the token on return
logical, intent(out) :: tokenValue(:)
!> Starting position for the parsing on call, first position after the end of the token on
!> return.
integer, intent(inout) :: start
!> Token reader i/o status flag on return
integer, intent(out), optional :: iostat
!> Nr. of read items
integer, intent(out), optional :: nItem
integer :: iStart, iError, nReadItem
integer :: ii
logical :: tmp
tokenValue = .false.
iStart = start
iError = TOKEN_OK
do ii = 1, size(tokenValue)
call getNextToken(str, tmp, iStart, iError)
if (iError /= TOKEN_OK) then
exit
end if
tokenValue(ii) = tmp
end do
if (iError == TOKEN_OK) then
start = iStart
nReadItem = size(tokenValue)
else
nReadItem = ii - 1
end if
if (present(nItem)) then
nItem = nReadItem
end if
if (present(iostat)) then
iostat = iError
elseif (iError /= TOKEN_OK) then
call error("Logical reading error")
end if
end subroutine getNextToken_logicalR1
!> Returns the next token from the provided string
!>
!> If the string does not contain any tokens, the empty string is returned.
subroutine getNextToken_local(str, tokStart, tokEnd, tokLen, start, ignoreQuotation)
!> String to parse
character(len=*), intent(in) :: str
!> stating location of token in string
integer, intent(out) :: tokStart
!> end position of token in string
integer, intent(out) :: tokEnd
!> length of the token
integer, intent(out) :: tokLen
!> Starting position for the parsing on call, first position after the end of the token on
!> return
integer, intent(inout) :: start
!> ignore quotation marks (Default: yes)
logical, intent(in), optional :: ignoreQuotation
integer :: lenStr
logical :: tIgnoreQuotation
@:ASSERT(start > 0)
if (present(ignoreQuotation)) then
tIgnoreQuotation = ignoreQuotation
else
tIgnoreQuotation = .true.
end if
lenStr = len(str)
tokStart = complementaryScan(str(start:), whiteSpaces)
if (tokStart == 0) then
tokLen = 0
tokEnd = 0
return
else
tokStart = tokStart + start - 1
end if
if (tIgnoreQuotation) then
tokEnd = scan(str(tokStart:), whiteSpaces)
else
tokEnd = unquotedScan(str(tokStart:), whiteSpaces)
end if
if (tokEnd == 0) then
tokEnd = lenStr
else
tokEnd = tokEnd + tokStart - 2
end if
tokLen = tokEnd - tokStart + 1
start = tokEnd + 2
end subroutine getNextToken_local
!> Checks whether a given character represents a valid staring character for an integer.
pure function validIntegerStart(char) result(tValid)
!> Character to check
character, intent(in) :: char
!> Whether it can be a starting character of a string representing an integer.
logical :: tValid
integer :: ind
ind = iachar(char)
! Either a digit 0-9 or - or +
tValid = ((ind >= 48 .and. ind <= 57) .or. ind == 45 .or. ind == 43)
end function validIntegerStart
end module dftbp_io_tokenreader
|