-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGConvert.f90
319 lines (211 loc) · 7.67 KB
/
GConvert.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
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
!find which lines are comment lines
!find embedded comment (need to rule out char-context)
!move embedded comments to next line
!move past-72 code to new line, if it isnt in a comment
!check for comment lines + change to multiform
!on remaining lines,check for past-72 characters and move them to new !-comment line
!on these lines, check col 6 for 0 and change to blank
!on remaining lines, check for continuation, change to &-mark
!
!
!embedded comments: check character context character by character and detect non-cc
! !-marks
Module texttools
Implicit None
Type sourcelines
Character (Len=132) :: line, linetype
! Logical :: charcont(132)
Integer :: plingcom
End Type
Type (sourcelines), Allocatable :: orig(:), edit(:)
Integer :: tot_lines, tot_edit_lines
Contains
Subroutine get_text(a, b) ! stores original source in an array
Type (sourcelines), Allocatable :: a(:), b(:)
Character (Len=80) :: filename
Integer :: i
! filename = './test_cases/con_lines.txt'
! print *,filename
! Open (Unit=9, File=filename)
tot_lines = 0
Do
Read (*, '(A)', End=100)
tot_lines = tot_lines + 1
End Do
100 Continue
Rewind 5
Allocate (a(tot_lines), b(2*tot_lines))
Do i = 1, tot_lines
Read (*, '(A)') a(i)%line
Print *, a(i)%line
End Do
End Subroutine
Subroutine find_com_lns(a)
Type (sourcelines) :: a(:)
Integer :: i, j
line: Do i = 1, tot_lines
If (a(i)%line(1:1)=='!' .Or. a(i)%line(1:1)=='*' .Or. &
a(i)%line(1:1)=='C' .Or. a(i)%line(1:1)=='c') Then
a(i)%line(1:1) = "!"
a(i)%linetype = 'cmtline'
Cycle line
Else If (a(i)%line==' ') Then
a(i)%linetype = 'cmtline'
Cycle line
Else
charac: Do j = 2, 132
If (a(i)%line(j:j)==' ') Then
Cycle charac
Else
If (a(i)%line(j:j)=='!') Then
If (j/=6) Then
a(i)%linetype = 'cmtline'
Cycle line
Else
a(i)%linetype = 'code'
Cycle line
End If
Else
a(i)%linetype = 'code'
Cycle line
End If
End If
End Do charac
End If
End Do line
End Subroutine
Subroutine find_emb_com(a)
Type (sourcelines) :: a(:)
Integer :: i, j
Logical :: charnow
Character :: quotecharac
charnow = .False. !begin not in character context
line: Do i = 1, tot_lines
a(i)%plingcom = -1
If (a(i)%linetype=='cmtline') Then !skip comment lines
Cycle line
End If
! a(i)%charcont = .false. !initialise all charcters to be not
!in character context
charac: Do j = 7, 72 ! look in code field
If (.Not. charnow) Then ! if not in char context
If (a(i)%line(j:j)=='''' .Or. a(i)%line(j:j)=='"') Then
quotecharac = a(i)%line(j:j)
charnow = .True.
Cycle charac
!quotes when not in char context puts us in char context
Else If (a(i)%line(j:j)=='!') Then
a(i)%plingcom = j
Cycle line
!!-mark when not in character context means an embedded comment, so we skip
!the rest of the line
Else
Cycle charac
End If
Else !if in char context
If (a(i)%line(j:j)==quotecharac) Then !closequote detected
charnow = .False.
Cycle charac
Else
charnow = .True.
Cycle charac
End If
End If
End Do charac
End Do line
! Do i = 1, tot_lines
! Print *, '-------------'
! Print *, a(i)%plingcom
! Do j = 7, 72
! Print *, a(i)%line(j:j)
! End Do
! End Do
End Subroutine
!go if in character context and find endquote, find how many, set = quoteno = temp
!
Subroutine move_lines(a, b)
Type (sourcelines) :: a(:), b(:)
Integer :: i, j
j = 1
Do i = 1, tot_lines
If ((a(i)%linetype=='cmtline')) Then
!if its a comment, its left alone
b(j) = a(i)
j = j + 1
!independantly advancing j by 1, because 1 line in b was produced
!for 1 line in a
Cycle
Else If (a(i)%plingcom/=-1) Then
!embedded comment, move to new line
b(j) = sourcelines(line=a(i)%line(1:a(i)%plingcom-1), &
linetype='code', plingcom=-1)
!jth line of b reads up to embedded comment. it is code and has no embedded comment
b(j+1) = sourcelines(line=' '//a(i)%line(a(i)%plingcom:132), &
linetype='cmtline', plingcom=-1)
j = j + 2
Cycle
Else If ((a(i)%plingcom==-1) .And. a(i)%linetype=='code') Then
If (a(i)%line(73:132)/=' ') Then
!text past col-72 is moved to a new line as a comment
b(j) = sourcelines(line=a(i)%line(1:72), linetype='code', &
plingcom=-1)
b(j+1) = sourcelines(line=' !'//a(i)%line(73:132), &
linetype='cmtline', plingcom=-1)
j = j + 2
Cycle
Else !code is copied over unedited
b(j) = a(i)
j = j + 1
Cycle
End If
End If
End Do
tot_edit_lines = j
! Print *, '***********'
! Do i = 1, j
! Print *, b(i)%line
! End Do
End Subroutine
! cases: comment line, embedded comment, code that goes past col 72
!is it a comment, is it an emb com, is it past 72
Subroutine edit_con_lns(b)
Type (sourcelines) :: b(:)
Integer :: i, j
lines: Do i = 1, tot_edit_lines
If (b(i)%linetype=='code') Then
If (b(i)%line(6:6)==' ') Then
Cycle !code that is not a continuation
Else If (b(i)%line(6:6)=='0') Then
b(i)%line(6:6) = ' '
Cycle !code that is not a continuation
Else !code thta is a continuation
b(i)%line(6:6) = '&' !set cont. character as an &-mark
Do j = i - 1, 1, -1 !search preceding lines for non-comment line
If (b(j)%linetype=='cmtline') Then
Cycle
Else
b(j)%line(73:73) = '&' !first non-comment line gets a trailing
!&-mark and exits the loop
Cycle lines
End If
End Do
End If
Else
Cycle ! if line is a comment, leave it
End If
End Do lines
End Subroutine
End Module
Program convert
Use texttools
Call get_text(orig, edit) !get text into an array
Call find_com_lns(orig) !find comment lines
Call find_emb_com(orig) !find embedded comments
Call move_lines(orig, edit) !move embedded comment and text past column 72
Call edit_con_lns(edit)
! call find_con_lines
Print *, '::::::::::::::::'
Do i = 1, tot_edit_lines
Print *, edit(i)%line
End Do
End Program