-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncHeader
More file actions
executable file
·331 lines (276 loc) · 9.01 KB
/
syncHeader
File metadata and controls
executable file
·331 lines (276 loc) · 9.01 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use File::Copy;
# Get the name of the header file.
my $headerName = `grep -l 'common/static/' *.h`;
chomp $headerName;
print "Checking to see if the header file $headerName is consistent with " .
"set_labels.f\n\n";
# Extract the real and integer declaration lists as well as the static
# and dynamic common blocks from the header file.
my $file = "$headerName";
open my $headerFile, '<', $file or die "can't open $file: $!";
my ( $line, $listref, $nStaticHeader, $nDynamicHeader, $nHeader );
my @realList = ();
my @integerList = ();
my @headerVars = ();
my $readAnother = 1;
while ( ! eof($headerFile) ) {
if( $readAnother == 1 ) {
$line = readline($headerFile);
chomp $line;
} else {
$readAnother = 1;
}
if( $line =~ ' real' ) {
$line =~ s/real//;
($line, $listref) = readBlockFromHeader( $line, $headerFile, \@realList );
@realList = @$listref;
$readAnother = 0;
}
if( $line =~ ' integer' ) {
$line =~ s/integer//;
($line, $listref) = readBlockFromHeader( $line, $headerFile, \@integerList);
@integerList = @$listref;
$readAnother = 0;
}
if( $line =~ ' common/static/' ) {
$line =~ s/common\/static\///;
($line, $listref) = readBlockFromHeader( $line, $headerFile, \@headerVars );
@headerVars = @$listref;
$nStaticHeader = @headerVars;
$readAnother = 0;
}
if( $line =~ ' common/dynamic/' ) {
$line =~ s/common\/dynamic\///;
($line, $listref) = readBlockFromHeader( $line, $headerFile, \@headerVars );
@headerVars = @$listref;
$readAnother = 0;
}
}
close $headerFile or die "can't close $file: $!";
$nHeader = @headerVars;
$nDynamicHeader = $nHeader - $nStaticHeader;
# Extract a list of input variables from the set_labels.f file.
$file = 'set_labels.f';
open my $labelsFile, '<', $file or die "can't open $file: $!";
my ( $root, $num, $prev, $posRealDynamic, $posIntegerDynamic,
$posEnd, $saveVar, $addition, @labels, @decomp );
my $posRealStatic = 0;
my $posIntegerStatic = 0;
my $n = 0;
my $alreadyRead = 0;
$prev = 'blank';
while ( ! eof($labelsFile) ) {
if( $alreadyRead == 0 ) {
$line = readline($labelsFile);
chomp $line;
}
$alreadyRead = 0;
if( $line =~ 'lab\(i\)' ) {
@decomp = split(/'/, $line);
$saveVar = $decomp[1];
if( $decomp[1] =~ '[1-9][0-9]$' ) {
$prev = substr $decomp[1], 0, -2;
$readAnother = 1;
}
if( $decomp[1] =~ '[1-9]$' ) {
$prev = substr $decomp[1], 0, -1;
$readAnother = 1;
}
while( $readAnother != 0 ) {
$line = readline($labelsFile);
chomp $line;
if( $line =~ 'lab\(i\)' ) {
@decomp = split(/'/, $line);
if( $decomp[1] =~ '[1-9][0-9]$' ) {
$root = substr $decomp[1], 0, -2;
$num = substr $decomp[1], -2, 2;
}
elsif( $decomp[1] =~ '[1-9]$' ) {
$root = substr $decomp[1], 0, -1;
$num = substr $decomp[1], -1, 1;
} else {
$root = $decomp[1];
}
if( $root eq $prev ) {
$saveVar = $root . "($num)";
} else {
$readAnother = 0;
$alreadyRead = 1;
}
} else {
$readAnother = 0;
$alreadyRead = 1;
}
}
$n ++;
push(@labels, $saveVar);
}
if( $posIntegerStatic != 0 && $line =~ 'Integers below this line' ) {
$posIntegerDynamic = $n;
}
if( $posIntegerStatic == 0 && $line =~ 'Integers below this line' ) {
$posIntegerStatic = $n;
}
if( $line =~ 'Dynamic parameters' ) {
$posRealDynamic = $n;
}
}
$posEnd = $n;
my $nStaticReal = $posIntegerStatic - $posRealStatic;
my $nStaticInteger = $posRealDynamic - $posIntegerStatic;
my $nStaticLabels = $nStaticReal + $nStaticInteger;
my $nDynamicReal = $posIntegerDynamic - $posRealDynamic;
my $nDynamicInteger = $posEnd - $posIntegerDynamic;
my $nDynamicLabels = $nDynamicReal + $nDynamicInteger;
my $nLabels = $nStaticLabels + $nDynamicLabels;
close $labelsFile or die "can't close $file: $!";
# Check to see if any of the input variables need to be explicitly typed
# as either real or integer, then check whether or not these variables are
# contained in the respective declaration statements in the header file.
# If not, add them to the variable list and signal that the header file
# should be rebuilt.
my $rebuildHeader = 0;
for( my $i=$posRealStatic; $i<$posIntegerStatic; $i++ ) {
if( substr($labels[$i],0,1) =~ '[i-n,I-N]' ) {
$addition = $labels[$i];
$addition =~ s/\(\d*\)//;
unless( grep( /^$addition$/, @realList ) ) {
unshift( @realList, $addition );
$rebuildHeader = 1;
}
}
}
for( my $i=$posIntegerStatic; $i<$posRealDynamic; $i++ ) {
if( substr($labels[$i],0,1) =~ '[a-h,o-z,A-H,O-Z]' ) {
$addition = $labels[$i];
$addition =~ s/\(\d*\)//;
unless( grep( /^$addition$/, @integerList ) ) {
unshift( @integerList, $addition );
$rebuildHeader = 1;
}
}
}
for( my $i=$posRealDynamic; $i<$posIntegerDynamic; $i++ ) {
if( substr($labels[$i],0,1) =~ '[i-n,I-N]' ) {
$addition = $labels[$i];
$addition =~ s/\(\d*\)//;
unless( grep( /^$addition$/, @realList ) ) {
unshift( @realList, $addition );
$rebuildHeader = 1;
}
}
}
for( my $i=$posIntegerDynamic; $i<$posEnd; $i++ ) {
if( substr($labels[$i],0,1) =~ '[a-h,o-z,A-H,O-Z]' ) {
$addition = $labels[$i];
$addition =~ s/\(\d*\)//;
unless( grep( /^$addition$/, @integerList ) ) {
unshift( @integerList, $addition );
$rebuildHeader = 1;
}
}
}
# Check to see if the common blocks in the header file are consistent with the
# variable delcarations in set_labels.
if( $nLabels != $nHeader ) {
$rebuildHeader = 1;
} else {
for( my $i=0; $i<$nLabels; $i++ ) {
if( $headerVars[$i] ne $labels[$i] ) {$rebuildHeader=1}
}
}
# If the labels are out of sync with the common block we need to rebuild
# the header file
if( $rebuildHeader == 1 ) {
print "Inconsistiencies found; rebuilding $headerName\n\n";
move( "$headerName", "$headerName.bak" );
open my $headerOld, '<', "$headerName.bak" or die "can't open $headerName.bak: $!";
open my $headerNew, '>', "$headerName" or die "can't open $headerName: $!";
$alreadyRead = 0;
while ( ! eof($headerOld) ) {
if( $alreadyRead == 0 ) {
$line = readline($headerOld);
}
$alreadyRead = 0;
if( $line =~ ' real' ) {
writeNewBlock( ' real ', 0, scalar(@realList)-1,
' ', 0, $headerNew,
\@realList );
$line = skipOldBlock( $headerOld );
$alreadyRead = 1;
} elsif( $line =~ ' integer' ) {
writeNewBlock( ' integer', 0, scalar(@integerList)-1, ' ', 0, $headerNew,
\@integerList );
$line = skipOldBlock( $headerOld );
$alreadyRead = 1;
} elsif( $line =~ ' common/static/' ) {
writeNewBlock( ' common/static/', 0, $posIntegerStatic-1, ' ', 1,
$headerNew, \@labels );
writeNewBlock( '&', $posIntegerStatic, $posRealDynamic-1, ' ', 0,
$headerNew, \@labels );
$line = skipOldBlock( $headerOld );
$alreadyRead = 1;
} elsif( $line =~ ' common/dynamic/' ) {
writeNewBlock( ' common/dynamic/', $posRealDynamic, $posIntegerDynamic-1,
' ', 1, $headerNew, \@labels );
writeNewBlock( '&', $posIntegerDynamic, $posEnd-1, ' ', 0, $headerNew,
\@labels );
$line = skipOldBlock( $headerOld );
$alreadyRead = 1;
} else {
print $headerNew $line;
}
}
close $headerOld or die "can't close $headerName.bak: $!";
close $headerNew or die "can't close $headerName: $!";
} else {
print "No inconsistencies found; $headerName has not been changed\n\n";
}
exit;
sub readBlockFromHeader {
my ( $line, $fileHandle, $listref ) = @_;
my @list = @$listref;
my $readAnother = 1;
$line =~ s/\s+//g; # gets rid of all white space
push(@list, split(/,/, $line));
while ( ! eof($fileHandle) && $readAnother == 1 ) {
$line = readline($headerFile);
chomp $line;
if( $line =~ '&' ) {
$line =~ s/\&//;
$line =~ s/\s+//g; # gets rid of all white space
push(@list, split(/,/, $line));
} else {
$readAnother = 0;
}
}
return($line, \@list);
}
sub writeNewBlock {
my( $identifier, $posStart, $posStop, $pad, $commaAtEnd, $fileHandle,
$varsref ) = @_;
my @vars = @$varsref;
my $lineOut = ' '.$identifier;
for( $n=$posStart; $n<=$posStop; $n++ ) {
my $prev = $lineOut;
$lineOut = $lineOut . ' ' . $vars[$n];
if( length($lineOut) > 71 ) {
print $fileHandle "$prev\n";
$lineOut = ' &' . $pad . $vars[$n];
}
unless( $n == $posStop && $commaAtEnd == 0 ) { $lineOut = $lineOut . ',' }
}
print $fileHandle "$lineOut\n";
}
sub skipOldBlock {
my( $fileHandle ) = @_;
my $readAnother = 1;
while ( ! eof($fileHandle) && $readAnother == 1 ) {
$line = readline($fileHandle);
unless( $line =~ '&' ) { $readAnother = 0 }
}
return $line;
}