0001 function make_contents(aString, flags, start_dir)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 if nargin < 1,
0040 aString =' ';
0041 end
0042 if nargin < 2
0043 flags = '';
0044 end
0045 if isempty(flags)
0046 flags = ' ';
0047 end
0048 if nargin < 3
0049 start_dir = '';
0050 end
0051 if isempty(start_dir)
0052 start_dir = pwd;
0053 end
0054
0055
0056 if any(flags == 'c')
0057 cont_file = 'contents.m';
0058 else
0059 cont_file = 'Contents.m';
0060 end
0061 if any(flags == 'p')
0062 cont_dir = pwd;
0063 else
0064 cont_dir = start_dir;
0065 end
0066 disp(['Creating "' cont_file '" in ' cont_dir])
0067 cont_path = fullfile(cont_dir, cont_file);
0068 if ~any(flags == 'd')
0069 if exist(cont_path, 'file')
0070 copyfile(cont_path, ...
0071 fullfile(cont_dir, [cont_file(1:end-1) 'old']));
0072 delete(cont_path)
0073 end
0074 end
0075
0076
0077 line1 = ['% ' aString];
0078 fcontents = fopen(cont_path,'wt');
0079 if fcontents == -1
0080 error(['Could not open file: ' cont_path]);
0081 end
0082 fprintf(fcontents,'%s\n',line1);
0083 if ~any(flags == 'n')
0084 line2 = ['% Path -> ' start_dir];
0085 fprintf(fcontents,'%s\n',line2);
0086 end
0087
0088
0089 flags = [flags '1'];
0090
0091
0092 do_list(start_dir, fcontents, flags);
0093 fclose(fcontents);
0094
0095
0096
0097 if isunix
0098 unix(['chmod go+r ' cont_path]);
0099 end
0100 return
0101
0102 function do_list(dirname, fcontents, flags)
0103 persistent START_DIR ST_D_LEN;
0104 if any(flags == '1')
0105 START_DIR = dirname;
0106 ST_D_LEN = length(dirname) + 2;
0107 end
0108
0109 if any(flags == 'r')
0110
0111 dirlist = dir(dirname);
0112 dirnames = {dirlist([dirlist.isdir]).name};
0113 dirnames = dirnames(~(strcmp('.', dirnames) | strcmp('..', dirnames)));
0114 else
0115 dirnames = {};
0116 end
0117
0118
0119 files = what(dirname);
0120
0121
0122 files = files(1);
0123
0124
0125 files.m = files.m(logical(~strcmpi(files.m,'contents.m')));
0126 if length(files.m)==0
0127 warning(['No m-files found in directory ' dirname])
0128 return
0129 end
0130 fprintf(fcontents,'%%\n');
0131
0132
0133 if ~any(flags == 'i')
0134 dirlab = dirname(ST_D_LEN:end);
0135 if ~any(flags == '1')
0136 dirlab = [dirlab filesep];
0137 end
0138 else
0139 dirlab = [dirname filesep];
0140 end
0141
0142 maxlen = size(char(files.m),2) + length(dirlab);
0143
0144
0145 for i = 1:length(files.m)
0146 fname = fullfile(files.path, files.m{i});
0147 fid=fopen(fname, 'rt');
0148 if fid == -1, error(['Error opening file: ' fname]); end
0149 aLine = '';
0150 while(isempty(aLine) | length(aLine) < 8)
0151 aLine = fgetl(fid);
0152 end
0153 if strcmp(aLine(1:8),'function'),
0154 count_percent = 0;
0155 while count_percent < 1 & feof(fid)==0;
0156 line = fgetl(fid);
0157 if length(line) > 0
0158 if ~isempty(findstr(line,'%'))
0159 count_percent = count_percent + 1;
0160 rr=line(2:length(line));
0161 if ~any(flags == 'f')
0162 [tt,rr]=strtok(line(2:length(line)));
0163 end
0164 rr = fliplr(deblank(fliplr(rr)));
0165 fn = [dirlab strtok(char(files.m(i)),'.')];
0166 n = maxlen - length(fn) - 1;
0167 line = ['% ' fn blanks(n) '- ' rr];
0168 fprintf(fcontents,'%s\n',line);
0169 end
0170 end
0171 if feof(fid)==1
0172 fn = [dirlab strtok(char(files.m(i)),'.')];
0173 n = maxlen - length(fn) - 1;
0174 line = ['% ' fn blanks(n) '- (No help available)'];
0175 fprintf(fcontents,'%s\n',line);
0176 end
0177 end
0178 end
0179 fclose(fid);
0180 end
0181
0182 flags = flags(flags ~= '1');
0183 for d = 1:length(dirnames)
0184 do_list(fullfile(dirname, dirnames{d}), fcontents, flags);
0185 end
0186 return