-
Notifications
You must be signed in to change notification settings - Fork 3
/
m_string.f90
294 lines (230 loc) · 7.4 KB
/
m_string.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
module m_string
use m_precision
implicit none
interface to_string
module procedure to_string_integer,to_string_real
end interface
contains
function logical_to_yn(input) result(str)
logical,intent(in)::input
character(1)::str
if (input) str = 'y'
if (.not.input) str = 'n'
end function
function is_numeric(string)
character(len=*), intent(in) :: string
logical :: is_numeric
real :: x
integer :: e
read(string,*,iostat=e) x
is_numeric = e == 0
end function is_numeric
function to_string_integer(i) result(s)
implicit none
character(:),allocatable :: s
integer :: i
character(32) :: ss
integer :: l
write(ss, *) i
ss = adjustl(ss)
l = len_trim(ss)
allocate(character(l)::s)
s = trim(ss)
end function
pure function to_string_real(input) result(to_string)
real(fp_kind),intent(in):: input
character(len=:),allocatable :: to_string
character*50::dum
if((abs(input).lt.1d3).and.(abs(input).gt.1d-3)) then
write(dum, '(f9.3)') input
elseif((abs(input).lt.1d-16)) then
write(dum, '(i1)') 0
else
write(dum, '(e10.3)') input
endif
allocate(character(len = len_trim(adjustl(dum))) :: to_string)
to_string = trim(adjustl(dum))
end function
elemental function str2int(str) result(int)
implicit none
! Arguments
character(len=*),intent(in) :: str
integer :: int
integer :: stat_
read(str,*,iostat=stat_) int
end function str2int
!Allows thickness to be read in via:
!Single value in the format: [z1]
!List of values in the format: [z1],[z2],[z3]
!A sequence of values in the format: [zstart]:[zstop]:[zstep]
subroutine read_sequence_string(string,lstring,nz,zarray,minstep)
character(len=lstring), intent(in) :: string
integer*4,intent(in)::lstring
integer*4,intent(inout)::nz
real(fp_kind),optional::zarray(nz),minstep
character(len=1)::sgn
integer*4::i,ii,j,nsteps,zi
real(fp_kind):: zseq(3),z
logical:: is_sequence,is_list
!See if ':' is string
is_sequence = (index(string,':').ne.0)
!See if ',' is string
is_list = (index(string,',').ne.0)
if (is_sequence.and.is_list) then
pause "Both of the characters ',', for a list of values, and ':', for an ordered sequence of values, were found in the input, please rectify and re-run program"
stop
endif
if(is_sequence) then
i=1
do ii=1,2
j = index(string(i:),':')+i-2
if (is_numeric(string(i:j))) then
read(string(i:j),*) zseq(ii)
else
write(*,*) "Expected numeric input of the kind [start]:[stop]:[step], got: "//string(i:j)
stop
endif
i=j+2
enddo
!Read final value into array
if (is_numeric(string(i:))) then
read(string(i:),*) zseq(ii)
if(present(minstep)) zseq(ii) = max(minstep,zseq(ii))
else
write(*,*) "Expected numeric input of the kind [start]:[stop]:[step], got: "//string(i:)
stop
endif
if(zseq(2)<zseq(1)*sign(1.0_fp_kind,zseq(3))) then
if(zseq(3)>0) then
sgn = ">"
else
sgn = "<"
endif
write(*,*) "Expected numeric input of the kind [start]:[stop]:[step], however [start] = "//to_string(zseq(1))//sgn//" [stop] = "//to_string(zseq(2))//" with [step] = "//to_string(zseq(3))
stop
endif
z=zseq(1)
nsteps = 0
do while(z<=zseq(2))
nsteps = nsteps+1
z = z+zseq(3)
enddo
!nsteps = nint((zseq(2)-zseq(1)/zseq(3)))+1
if (present(zarray)) then
zarray = (/(zi, zi=0,nsteps, 1)/)*zseq(3)+zseq(1)
else
nz = nsteps
endif
elseif(is_list) then
i=1
ii=0
do while (index(string(i:),',').ne.0)
!write(*,*) i,index(string(i:),',')
j = index(string(i:),',')+i-2
ii=ii+1
if (present(zarray)) then
if (.not.is_numeric(string(i:j))) then
write(*,*) "Expected numeric input, got: "//string(i:j)
stop
endif
read(string(i:j),*) zarray(ii)
endif
i=j+2
end do
nz = ii+1
!Read final value into array
if (.not.is_numeric(string(i:))) then
write(*,*) "Expected numeric input, got: "//string(i:)
stop
endif
if (present(zarray)) read(string(i:),*) zarray(ii+1)
else
if (present(zarray)) then
read(string,*) zarray(1)
else
nz = 1
endif
endif
end subroutine
function zero_padded_int(num, width)
implicit none
integer*4,intent(in) :: num, width
character(len=width) :: zero_padded_int
character(10) :: fmt
10 format('(i', i1, '.', i1, ')')
if (num.ge.0) then
write(fmt, 10) width, width
else
write(fmt, 10) width, width-1
endif
write(zero_padded_int, fmt) num
end function
function to_upper(strIn) result(strOut)
! Adapted from http://www.star.le.ac.uk/~cgp/fortran.html (25 May 2012)
! Original author: Clive Page
implicit none
character(len=*), intent(in) :: strIn
character(len=len(strIn)) :: strOut
integer :: i,j
do i = 1, len(strIn)
j = iachar(strIn(i:i))
if (j>= iachar("a") .and. j<=iachar("z") ) then
strOut(i:i) = achar(iachar(strIn(i:i))-32)
else
strOut(i:i) = strIn(i:i)
end if
end do
end function to_upper
function to_lower(strIn) result(strOut)
! Adapted from http://www.star.le.ac.uk/~cgp/fortran.html (25 May 2012)
! Original author: Clive Page
implicit none
character(len=*), intent(in) :: strIn
character(len=len(strIn)) :: strOut
integer :: i,j
do i = 1, len(strIn)
j = iachar(strIn(i:i))
if (j>= iachar("A") .and. j<=iachar("Z") ) then
strOut(i:i) = achar(iachar(strIn(i:i))+32)
else
strOut(i:i) = strIn(i:i)
end if
end do
end function to_lower
FUNCTION Reduce_Blanks(s) RESULT (outs)
CHARACTER(*) :: s
CHARACTER(LEN_TRIM(s)) :: outs
INTEGER :: i, k, n
n = 0 ; k = LEN_TRIM(s) ! k=index last non-blank (may be null)
if (k==0) then ! If s is all blanks return nothing
outs= ''
return
endif
DO i = 1,k-1 ! dont process last char yet
n = n+1 ; outs(n:n) = s(i:i)
IF (s(i:i+1) == ' ') n = n-1 ! backup/discard consecutive output blank
END DO
n = n+1 ; outs(n:n) = s(k:k) ! last non-blank char output (may be null)
IF (n < k) outs(n+1:) = ' ' ! pad trailing blanks
END FUNCTION Reduce_Blanks
! ------------------
FUNCTION Replace_Text (s,text,rep) RESULT(outs)
CHARACTER(*) :: s,text,rep
CHARACTER(LEN(s)+100) :: outs ! provide outs with extra 100 char len
INTEGER :: i, nt, nr
outs = s ; nt = LEN_TRIM(text) ; nr = LEN_TRIM(rep)
DO
i = INDEX(outs,text(:nt)) ; IF (i == 0) EXIT
outs = outs(:i-1) // rep(:nr) // outs(i+nt:)
END DO
END FUNCTION Replace_Text
subroutine command_line_title_box(title)
CHARACTER(*),intent(in):: title
integer*4::numdash,i
numdash = len(trim(adjustl(title)))+10
write(6,*)
write(6,*) '|',('-',i=1,numdash),'|'
write(6,*) '| ',trim(adjustl(title)),' |'
write(6,*) '|',('-',i=1,numdash),'|',char(10),char(10)
end subroutine
end module