summaryrefslogtreecommitdiff
path: root/lib/jt9_input_validation.f90
blob: bb59bd6fbbc7c3de1c19758e40769e2b2404bc2d (plain)
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
module jt9_input_validation
  use, intrinsic :: ieee_arithmetic, only: ieee_is_finite
  implicit none

  private
  public :: parse_integer, parse_real, parse_wav_filename_nutc

contains

  subroutine parse_integer(text, value, ok)
    character(len=*), intent(in) :: text
    integer, intent(out) :: value
    logical, intent(out) :: ok

    integer :: ios

    value = 0
    if (.not. is_integer_token(text)) then
      ok = .false.
      return
    end if
    read(text, *, iostat=ios) value
    ok = ios == 0
  end subroutine parse_integer

  subroutine parse_real(text, value, ok)
    character(len=*), intent(in) :: text
    real(kind=8), intent(out) :: value
    logical, intent(out) :: ok

    integer :: ios

    value = 0.d0
    if (.not. is_real_token(text)) then
      ok = .false.
      return
    end if
    read(text, *, iostat=ios) value
    ok = ios == 0 .and. ieee_is_finite(value)
  end subroutine parse_real

  pure logical function is_integer_token(text)
    character(len=*), intent(in) :: text

    integer :: first

    first = 1
    if (len(text) > 0) then
      if (text(1:1) == '+' .or. text(1:1) == '-') first = 2
    end if
    is_integer_token = first <= len(text) .and. &
         verify(text(first:), '0123456789') == 0
  end function is_integer_token

  pure logical function is_real_token(text)
    character(len=*), intent(in) :: text

    integer :: digits, i, length

    is_real_token = .false.
    length = len(text)
    if (length == 0) return

    i = 1
    if (text(i:i) == '+' .or. text(i:i) == '-') i = i + 1
    if (i > length) return

    digits = 0
    do while (i <= length)
      if (text(i:i) < '0' .or. text(i:i) > '9') exit
      digits = digits + 1
      i = i + 1
    end do
    if (i <= length) then
      if (text(i:i) == '.') then
        i = i + 1
        do while (i <= length)
          if (text(i:i) < '0' .or. text(i:i) > '9') exit
          digits = digits + 1
          i = i + 1
        end do
      end if
    end if
    if (digits == 0) return

    if (i <= length) then
      if (text(i:i) /= 'e' .and. text(i:i) /= 'E') return
      i = i + 1
      if (i <= length) then
        if (text(i:i) == '+' .or. text(i:i) == '-') i = i + 1
      end if
      if (i > length) return
      digits = 0
      do while (i <= length)
        if (text(i:i) < '0' .or. text(i:i) > '9') return
        digits = digits + 1
        i = i + 1
      end do
      if (digits == 0) return
    end if

    is_real_token = i > length
  end function is_real_token

  subroutine parse_wav_filename_nutc(filename, nutc, includes_seconds)
    character(len=*), intent(in) :: filename
    integer, intent(out) :: nutc
    logical, optional, intent(out) :: includes_seconds

    integer :: dot, ios, length
    character(len=:), allocatable :: stem

    nutc = 0
    if (present(includes_seconds)) includes_seconds = .false.
    length = len_trim(filename)
    if (length == 0) return

    dot = index(filename(:length), '.', back=.true.)
    if (dot == 0) return
    if (.not. is_wav_extension(filename(dot:length))) return

    stem = filename(:dot - 1)
    if (len(stem) >= 6) then
      if (all_digits(stem(len(stem) - 5:))) then
        read(stem(len(stem) - 5:), *, iostat=ios) nutc
        if (ios == 0) then
          if (present(includes_seconds)) includes_seconds = .true.
          return
        end if
      end if
    end if
    if (len(stem) >= 5) then
      if (stem(len(stem) - 4:len(stem) - 4) == '_' .and. &
           all_digits(stem(len(stem) - 3:))) then
        read(stem(len(stem) - 3:), *, iostat=ios) nutc
        if (ios == 0) return
      end if
    end if
    nutc = 0
  end subroutine parse_wav_filename_nutc

  pure logical function is_wav_extension(extension)
    character(len=*), intent(in) :: extension

    is_wav_extension = len(extension) == 4 .and. &
         (extension == '.wav' .or. extension == '.WAV')
  end function is_wav_extension

  pure logical function all_digits(text)
    character(len=*), intent(in) :: text

    integer :: i

    all_digits = len(text) > 0
    do i = 1, len(text)
      if (text(i:i) < '0' .or. text(i:i) > '9') then
        all_digits = .false.
        return
      end if
    end do
  end function all_digits

end module jt9_input_validation