-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractFunctions.m
184 lines (141 loc) · 4.62 KB
/
extractFunctions.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
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
function [ numFunc, funcs ] = extractFunctions( filename, varargin )
%EXTRACTFUNCTIONS Extract all functions that are within a specified file
%
% This function will extract all the functions present in the file and save
% them inside the approrpiately named file inside the current directory.
%
% The file will first be copied to a new file named 'testFile.m' before
% being extracted. Ensure there are no functions named 'testFile' to
% prevent this file from possibly being overwritten and corrupted during
% the extraction process.
%
% This will only extract functions at the first indentation level (no
% whitespace in front of the function declaration, so it leaves nested
% functions intact.
%
% This function is able to extract the breakpoint data from the file, and
% then transfer the breakpoints to the newly extracted functions. If
% brekpoint extraction is not selected, then the file is created with no
% breakpoints enabled.
%
%
% Usage:
% [ numFunc, funcs ] = extractFunctions( filename );
% [ numFunc, funcs ] = extractFunctions( filename, ExtractBreakpoints );
%
% Inputs:
% filename - The name of the file to parse
% ExtractBreakpoints - Flag to extract breakpoints (0=don't extrac,
% 1=extract). Defaults to not extracting breakpoints.
%
% Outputs:
% numFunc - The number of functions extracted from the file
% funcs - The names of the functions extracted from the file (in a cell
% array)
%
%
% Created by: Ian McInerney
% Created on: February 20, 2018
% Version: 1.2
% Last Modified: February 17, 2019
%
% Revision History
% 1.0 - Initial release
% 1.1 - Added breakpoint extraction
% 1.2 - Made read happen from a temp file to prevent file overwriting
%% Parse the optional input
p = inputParser;
addOptional(p, 'ExtractBreakpoints', 0);
parse(p, varargin{:});
eb = p.Results.ExtractBreakpoints;
%% Copy the file
copyfile(filename, 'tempFile.m', 'f');
%% Open the file
fr = fopen('tempFile.m');
if (fr == -1)
error('Unable to open the file.');
end
%% Initialize some variables for the loop
fw = -1;
numFunc = 0;
funcs = {};
files = {};
lnum = 1; % The line number
fLine = [1, 1];
%% Loop while not end of file
while ( ~feof(fr) )
% Read the line and see if it contains a function declaration
l = fgets(fr);
isFunc = regexpi(l, '^[^%\s]*function[\s\S]*\([\s\S]*\)');
% The line defines a function
if (~isempty( isFunc ) )
% Close the current function file
if (fw ~= -1)
fLine(numFunc, 2) = lnum-1;
fclose(fw);
end
% Increment the counter
numFunc = numFunc + 1;
% Save the starting line
fLine(numFunc, 1) = lnum;
% Extract the function name
fname = regexpi(l, '[\S]*\(', 'match');
fname = fname{:};
fname = fname(1:end-1);
% Save the function name for return
funcs{numFunc} = fname;
% Make the file name
fname = [fname, '.m'];
files{numFunc} = fname;
% Open the file
fw = fopen(fname, 'w');
% If the file was unable to be opened for some reason, throw an
% error and close the reading file
if (fw == -1)
fclose(fr);
error(['Unable to open file ', fname]);
end
end
lnum = lnum + 1;
% Write to the function file if it is open
if (fw ~= -1)
fprintf(fw, '%s', l);
end
end
% Add the last line
fLine(numFunc, 2) = lnum;
%% Close the files that are open
fclose(fr);
if (fw ~= -1)
fclose(fw);
end
%% Delete the temp file
delete('tempFile.m');
%% Force MATLAB to find the new functions
rehash
%% Clear the debug information for the files that have been extracted
for ( i=1:1:numFunc )
eval(['dbclear ', files{i}]);
end
%% Extract debug information if requested
if (eb)
% Get the breakpoint information for the original file
bp = dbstatus(filename);
numBP = length(bp);
% Iterate through each breakpoint
for ( i=1:1:numBP )
% Figure out which function it goes with
condMet = (bp(i).line >= fLine(:,1)) + (bp(i).line <= fLine(:,2));
fInd = find( condMet == 2 );
lNum = bp(i).line - fLine(fInd,1) + 1;
% Extract the expression if it is there
expre = bp(i).expression{:};
% Set the new breakpoint
if ( isempty(expre) )
eval(['dbstop in ', files{fInd}, ' at ', num2str(lNum), ';']);
else
eval(['dbstop in ', files{fInd}, ' at ', num2str(lNum), ' if ''', expre, ''';']);
end
end
end
end