Home > marsbar > release > make_contents.m

make_contents

PURPOSE ^

MAKECONTENTS makes Contents file, usually in current working directory.

SYNOPSIS ^

function make_contents(aString, flags, start_dir)

DESCRIPTION ^

 MAKECONTENTS makes Contents file, usually in current working directory.
   MAKECONTENTS(STRING [FLAGS [START_DIR]]) 
   creates a standard "Contents.m" file in the
   current directory by assembling the first comment (H1) line in
   each function found in the current working directory.  If a 
   "Contents.m" file exists, it is renamed to "Contents.old", before
   a new "Contents.m" file is created.  STRING is inserted as the 
   first line of the "Contents.m" file;  if omitted, a blank line 
   is inserted instead.  The function changes permission on the 
   resultant "Contents.m" file to rw-r--r-- on Unix systems.

   FLAGS can contain none or more of
      'n'    - suppress path name at top of Contents file
      'f'    - include first word of first line (excluded by default)
      'c'    - use filename 'contents.m' instead of 'Contents.m'
      'r'    - recursively list subdirectory contents also
      'i'    - include starting directory from file name path list
      'p'    - save contents file in current rather than listed directory 
      'd'    - don't make backup of old contents file  
 
   START_DIR can be omitted, giving a listing of current working
       directory, or it can specify the directory to list

 Updated 29 June 2000.
 Revised to recurse down directories, handle options by
 Matthew Brett; 28 June 2003

 See also CONTENTS.

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function make_contents(aString, flags, start_dir)
0002 % MAKECONTENTS makes Contents file, usually in current working directory.
0003 %   MAKECONTENTS(STRING [FLAGS [START_DIR]])
0004 %   creates a standard "Contents.m" file in the
0005 %   current directory by assembling the first comment (H1) line in
0006 %   each function found in the current working directory.  If a
0007 %   "Contents.m" file exists, it is renamed to "Contents.old", before
0008 %   a new "Contents.m" file is created.  STRING is inserted as the
0009 %   first line of the "Contents.m" file;  if omitted, a blank line
0010 %   is inserted instead.  The function changes permission on the
0011 %   resultant "Contents.m" file to rw-r--r-- on Unix systems.
0012 %
0013 %   FLAGS can contain none or more of
0014 %      'n'    - suppress path name at top of Contents file
0015 %      'f'    - include first word of first line (excluded by default)
0016 %      'c'    - use filename 'contents.m' instead of 'Contents.m'
0017 %      'r'    - recursively list subdirectory contents also
0018 %      'i'    - include starting directory from file name path list
0019 %      'p'    - save contents file in current rather than listed directory
0020 %      'd'    - don't make backup of old contents file
0021 %
0022 %   START_DIR can be omitted, giving a listing of current working
0023 %       directory, or it can specify the directory to list
0024 %
0025 % Updated 29 June 2000.
0026 % Revised to recurse down directories, handle options by
0027 % Matthew Brett; 28 June 2003
0028 %
0029 % See also CONTENTS.
0030 %
0031 % $Id$
0032 
0033 % Author(s): L. Bertuccioli
0034 %            A. Prasad
0035 
0036 % Based on mkcontents.m by Denis Gilbert
0037 
0038 % Default value of input string
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 % parse flags
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 % Header lines
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 % set first past flag
0089 flags = [flags '1'];
0090 
0091 % do write
0092 do_list(start_dir, fcontents, flags);
0093 fclose(fcontents);
0094 
0095 % Change permissions on Contents.m file
0096 % only valid for Unix systems, no effect in Win32 systems
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') % first pass through
0105   START_DIR = dirname;
0106   ST_D_LEN = length(dirname) + 2; 
0107 end
0108 
0109 if any(flags == 'r')
0110   % find directories
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 % find m files
0119 files = what(dirname);  
0120 
0121 % fix apparent bug in what function
0122 files = files(1);
0123 
0124 % exclude any contents files
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 % maybe exclude starting path from listing
0133 if ~any(flags == 'i')  
0134   dirlab = dirname(ST_D_LEN:end);
0135   if ~any(flags == '1') % not first pass
0136     dirlab = [dirlab filesep];
0137   end
0138 else % not excluding starting directory
0139   dirlab = [dirname filesep];
0140 end
0141     
0142 maxlen = size(char(files.m),2) + length(dirlab);
0143 
0144 % Write first lines to Contents.m if they exist
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') % remove first word
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 % if ~isempty
0170       end % if length
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 % if feof
0177     end % while
0178   end % if strcmp
0179   fclose(fid);
0180 end
0181 % recurse down directory tree
0182 flags = flags(flags ~= '1'); % reset first pass flag
0183 for d = 1:length(dirnames)
0184   do_list(fullfile(dirname, dirnames{d}), fcontents, flags);
0185 end
0186 return

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019