-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetGlobals.m
69 lines (49 loc) · 1.43 KB
/
getGlobals.m
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
function [bounds, dim] = getGlobals(filename, funNum)
% GETGLOBALS parses 'fun.m' in order to make 'dim' and
% 'bounds' variables available as globals prior to the execution of the rest
% of the code
% 'filename' ('fun.m') is passed as a string
% [vector, int] <- (string, int)
fid = fopen(filename, 'r');
line = fgetl(fid);
caseToken = sprintf('case %d', funNum);
boundsTmp = [];
dimTmp = [];
while ischar(line)
match = strfind(line, caseToken);
if (length(match) == 1)
break
end
line = fgetl(fid);
end
line = fgetl(fid);
while ischar(line)
% if declaration of dim
if isempty(dimTmp)
[token, r] = regexp(line, '\<dim\s*=\s*(\d+)\s*;', 'once', 'tokens');
if r
value = token{:};
dimTmp = str2double(value);
end
end
% if declaration of bounds
if isempty(boundsTmp)
[token, r] = regexp(line,'\[([-]?[0-9]*\.?[0-9]+[,]?\s*[-]?[0-9]*\.?[0-9]+)\]', 'once', 'tokens');
if r
value = token{:};
boundsTmp = str2num(value);
end
end
if ~(isempty(dimTmp) || isempty(boundsTmp))
fclose(fid);
if nargout == 2
bounds = boundsTmp;
dim = dimTmp
else
bounds = boundsTmp;
end
return
end
line = fgetl(fid);
end
end