Mercurial > hg > ltpda
comparison m-toolbox/m/MakeContents.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 function MakeContents(toolbox_name, version_str) | |
2 % MAKECONTENTS makes Contents file in current working directory and subdirectories | |
3 % | |
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
5 % | |
6 % DESCRIPTION: MAKECONTENTS makes Contents file in current working directory and | |
7 % subdirectories. MakeContents creates a standard "Contents.m" file | |
8 % in the current directory by assembling the first comment (H1) | |
9 % line in each function found in the current working directory. The | |
10 % Contents file in the mean directory will also contain the H1 | |
11 % comments from the files in the subdirectories. | |
12 % MakeContents create also Contents files in the subdirectories with | |
13 % the H1 comments of the files in this subdirectories. | |
14 % | |
15 % CALL: MakeContents('toolbox_name', 'Version xxx dd-mmm-yyyy'); | |
16 % MakeContents('ltpda', '0.2 (R2007a) 21-May-2007'); | |
17 % | |
18 % H1 COMMENT Function name in upper case letter (optional) plus the description | |
19 % DEFINITION: e.g.: FUNCTION_NAME (optional) description | |
20 % | |
21 % VERSION: $Id: MakeContents.m,v 1.10 2008/07/31 18:41:53 ingo Exp $ | |
22 % | |
23 % HISTORY: 05-06-2007 Diepholz | |
24 % Creation | |
25 % | |
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
27 | |
28 | |
29 %% set default variables | |
30 if nargin < 1 | |
31 toolbox_name = 'no toolbox name'; | |
32 end | |
33 | |
34 if nargin < 2 | |
35 flags = ''; | |
36 version_str = 'no version number'; | |
37 end | |
38 | |
39 contents_file = 'Contents.m'; | |
40 | |
41 percent_line(1:80) = '%'; | |
42 header = ['% Toolbox ' toolbox_name]; | |
43 version = ['% Version ' version_str]; | |
44 contents_path = ['% Contents path ' pwd]; | |
45 | |
46 fcontents_main = fopen(contents_file,'w'); | |
47 | |
48 fprintf(fcontents_main, '%% %s Toolbox\n', toolbox_name); | |
49 fprintf(fcontents_main, '%% Version %s\n', version_str); | |
50 fprintf(fcontents_main, '%s\n%%\n', percent_line); | |
51 fprintf(fcontents_main, '%s\n%%\n', header); | |
52 fprintf(fcontents_main, '%s\n%%\n',version); | |
53 | |
54 fprintf(fcontents_main, '%s\n%%\n%%\n',contents_path); | |
55 | |
56 do_list('.', fcontents_main); | |
57 | |
58 fclose (fcontents_main); | |
59 | |
60 return | |
61 | |
62 %% Subfunction | |
63 | |
64 function do_list(path_in, fcontents_main) | |
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
66 % | |
67 % DESCRIPTION: DO_LIST go recursive through the directories and create the | |
68 % standart Contents "Contents.m" file. | |
69 % | |
70 % CALL: do_list('starting_directory', file_identifier); | |
71 % do_list('.', fcontents_main); | |
72 % | |
73 % H1 COMMENT Function name in upper case letter (optional) plus the description | |
74 % DEFINITION: FUNCTION_NAME (optional) description | |
75 % | |
76 % VERSION: $Id: MakeContents.m,v 1.10 2008/07/31 18:41:53 ingo Exp $ | |
77 % | |
78 % HISTORY: 05-06-2007 Diepholz | |
79 % Creation | |
80 % | |
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
82 | |
83 %% Define directory list and m-file list | |
84 dirlist = dir(path_in); | |
85 dir_i = [dirlist.isdir]; | |
86 dirnames = {dirlist(dir_i).name}; | |
87 | |
88 % remove . and .. from directory list | |
89 dirnames = dirnames(~(strcmp('.', dirnames) | strcmp('..', dirnames))); | |
90 | |
91 filenames = {dirlist(~dir_i).name}; | |
92 mfiles = {}; | |
93 % Find .m files, excluding Contents file | |
94 for f = 1:length(filenames) | |
95 fname = filenames{f}; | |
96 | |
97 [pathstr,name,ext] = fileparts(fname); | |
98 | |
99 if length(fname) > 2 && ... | |
100 strcmp(ext, '.m') && ... | |
101 ~strcmpi('contents.m', fname) | |
102 | |
103 mfiles = [mfiles {fname}]; | |
104 end | |
105 end | |
106 | |
107 %% Define the print directory | |
108 if strcmp(path_in, '.') | |
109 print_dir = ''; | |
110 else | |
111 % Remove the './' from the path_in | |
112 print_dir = [path_in(3:end) filesep]; | |
113 end | |
114 | |
115 %% Create Contents.m files in the current search path | |
116 fcontents = []; | |
117 contents_file = 'Contents.m'; | |
118 | |
119 if ~strcmp(path_in, '.') && ~isempty(mfiles) | |
120 fcontents = fopen([path_in filesep contents_file], 'w'); | |
121 end | |
122 | |
123 % Remove 'classes/@' from print_dir | |
124 % hint: Matlab have a problem to display a class path by using a relative path | |
125 if length (print_dir) > 8 && strcmp(print_dir(1:9), 'classes/@') | |
126 print_dir = print_dir(10:end); | |
127 header = ['%%%%%%%%%%%%%%%%%%%% class: ' ... | |
128 print_dir(1:end-1) ... | |
129 ' %%%%%%%%%%%%%%%%%%%%']; | |
130 else | |
131 header = ['%%%%%%%%%%%%%%%%%%%% path: ' ... | |
132 print_dir(1:end-1) ... | |
133 ' %%%%%%%%%%%%%%%%%%%%']; | |
134 end | |
135 | |
136 if ~isempty(mfiles) | |
137 fprintf (fcontents_main, '%s\n%%\n', header); | |
138 if ~isempty(fcontents) | |
139 fprintf (fcontents , '%s\n%%\n', header); | |
140 end | |
141 end | |
142 | |
143 maxlen = size(char(mfiles),2) + length(print_dir); | |
144 | |
145 %% Each m-file in the directory | |
146 for i = 1:length(mfiles) | |
147 | |
148 %% Open the mfile to get the H1 line | |
149 mfile = mfiles{i}; | |
150 fid=fopen(fullfile(path_in, mfile),'r'); | |
151 if fid == -1 | |
152 error(['Could not open file: ' fullfile(path_in, mfile)]); | |
153 end | |
154 | |
155 % line = ''; | |
156 % while(isempty(line)) | |
157 % line = fgetl(fid); | |
158 % end | |
159 % | |
160 % % Remove leading and trailing white spaces | |
161 % line = strtrim(line); | |
162 % | |
163 % if length(line) > 7 && strcmp(line(1:8),'function') == 1, | |
164 | |
165 found = 0; | |
166 while found < 1 && feof(fid)==0; | |
167 line = fgetl(fid); | |
168 | |
169 %% End of file is reached | |
170 if feof(fid)==1 | |
171 fn = [print_dir strtok(mfile,'.')]; | |
172 n = maxlen - length(fn) - 1; | |
173 line = ['% ' fn blanks(n) '- (No help available)']; | |
174 | |
175 fprintf(fcontents_main,'%s\n',line); | |
176 if ~isempty(fcontents) | |
177 fprintf(fcontents ,'%s\n',line); | |
178 end | |
179 | |
180 elseif ~isempty(line) | |
181 | |
182 if ~isempty(findstr(line,'%')) | |
183 found = found + 1; | |
184 rr=line(2:length(line)); | |
185 | |
186 % Remove leading and trailing white spaces | |
187 rr = strtrim(rr); | |
188 | |
189 % get first word | |
190 [tt,rr2]=strtok(line(2:length(line))); | |
191 | |
192 % Is first word equal to the m-file then remove it | |
193 if findstr (lower(mfile), lower(tt)) | |
194 rr = rr2; | |
195 end | |
196 | |
197 if isempty(rr) | |
198 rr = '(No help available)'; | |
199 end | |
200 | |
201 fn = [print_dir strtok(mfile,'.')]; | |
202 asd = ['<a href="matlab:help ' fn '">' fn '</a>']; | |
203 n = maxlen - length(fn) - 1; | |
204 line = ['% ' asd blanks(n) '- ' rr]; | |
205 | |
206 fprintf(fcontents_main,'%s\n',line); | |
207 if ~isempty(fcontents) | |
208 fprintf(fcontents ,'%s\n',line); | |
209 end | |
210 | |
211 end % if ~isempty | |
212 end % if length | |
213 | |
214 end % while | |
215 % end % if strcmp | |
216 fclose(fid); | |
217 end % for | |
218 | |
219 if ~isempty(mfiles) | |
220 fprintf(fcontents_main,'%%\n%%\n'); | |
221 end | |
222 | |
223 if ~isempty(fcontents) | |
224 fclose (fcontents); | |
225 end | |
226 | |
227 %% recurse down directory tree | |
228 for d = 1:length(dirnames) | |
229 do_list(fullfile(path_in, dirnames{d}), fcontents_main); | |
230 end | |
231 | |
232 return |