Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slcountlines
Home > sltoolbox > fileio > slcountlines.m

slcountlines

PURPOSE ^

SLCOUNTLINES Count the lines of m-files in a folder and make a report

SYNOPSIS ^

function R = slcountlines(folderpath, fnreport)

DESCRIPTION ^

SLCOUNTLINES Count the lines of m-files in a folder and make a report

 $ Syntax $
   - R = slcountlines(folderpath)
   - slcountlines(folderpath, fnreport)
   - R = slcountlines(folderpath, fnreport)

 $ Arguments $
   - folderpath:       the path of the target folder 
   - fnreport:         the filename of the report
   - R:                the struct array storing the statistics     

 $ Description $
   - R = slcountlines(folderpath) makes statistics on the source codes
     in a matlab toolbox folder and returns the statistics by R.
     R is a struct with following fields:
       1. name:     the current level name of the folder
       2. info:     the struct array recording the statistics, the
                    statistics struct has following fields:
                    a. filename
                    b. length (number of bytes of the whole file)
                    c. totallines (total number of source code lines)
                    d. codelines
                    e. commentlines
                    f. emptylines
       3. subdir:   the struct array of sub-folders
       4. stat:     a struct representing the summarized statistics:
                    a. numfiles
                    b. totallines
                    c. codelines
                    d. commentlines
                    e. emptylines
   - slcountlines(folderpath, fnreport) makes statistics on the source
     codes in a matlab toolbox folder and generates a report file.

   - R = slcountlines(folderpath, fnreport) makes statistics on the 
     source codes in a matlab toolbox folder, generates a report file,
     and returns the struct.

 $ History $
   - Created by Dahua Lin on May 2nd, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slreadtext SLREADTEXT Reads in a text file into a cell array
  • slignorevars SLIGNOREVARS Ignores the input variables
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function R = slcountlines(folderpath, fnreport)
0002 %SLCOUNTLINES Count the lines of m-files in a folder and make a report
0003 %
0004 % $ Syntax $
0005 %   - R = slcountlines(folderpath)
0006 %   - slcountlines(folderpath, fnreport)
0007 %   - R = slcountlines(folderpath, fnreport)
0008 %
0009 % $ Arguments $
0010 %   - folderpath:       the path of the target folder
0011 %   - fnreport:         the filename of the report
0012 %   - R:                the struct array storing the statistics
0013 %
0014 % $ Description $
0015 %   - R = slcountlines(folderpath) makes statistics on the source codes
0016 %     in a matlab toolbox folder and returns the statistics by R.
0017 %     R is a struct with following fields:
0018 %       1. name:     the current level name of the folder
0019 %       2. info:     the struct array recording the statistics, the
0020 %                    statistics struct has following fields:
0021 %                    a. filename
0022 %                    b. length (number of bytes of the whole file)
0023 %                    c. totallines (total number of source code lines)
0024 %                    d. codelines
0025 %                    e. commentlines
0026 %                    f. emptylines
0027 %       3. subdir:   the struct array of sub-folders
0028 %       4. stat:     a struct representing the summarized statistics:
0029 %                    a. numfiles
0030 %                    b. totallines
0031 %                    c. codelines
0032 %                    d. commentlines
0033 %                    e. emptylines
0034 %   - slcountlines(folderpath, fnreport) makes statistics on the source
0035 %     codes in a matlab toolbox folder and generates a report file.
0036 %
0037 %   - R = slcountlines(folderpath, fnreport) makes statistics on the
0038 %     source codes in a matlab toolbox folder, generates a report file,
0039 %     and returns the struct.
0040 %
0041 % $ History $
0042 %   - Created by Dahua Lin on May 2nd, 2006
0043 %
0044 
0045 %% parse and verify input arguments
0046 
0047 if nargin >= 2
0048     report_required = true;
0049 else
0050     report_required = false;
0051 end
0052 
0053 
0054 %% Carry out tasks
0055 
0056 % make statistics
0057 
0058 R = make_folder_stat(folderpath);
0059 
0060 % make report
0061 
0062 if report_required
0063     fh = fopen(fnreport, 'wt');    
0064     if fh > 0
0065         make_folder_report(fh, [], R);
0066         fclose(fh);
0067     else
0068         error('sltoolbox:ioerror', ...
0069             'Fail to create file %s for reporting', fnreport);
0070     end            
0071 end
0072 
0073 
0074 
0075 %% The recursive function for making folder-level statistics
0076 function R = make_folder_stat(folderpath)
0077 
0078 
0079 % initialize the struct
0080 [pstr, foldername] = fileparts(folderpath);
0081 slignorevars(pstr);
0082 clear pstr;
0083 
0084 R.name = foldername;
0085 R.info = struct( ...
0086     'filename', {}, ...
0087     'length', {}, ...
0088     'totallines', {}, ...
0089     'codelines', {}, ...
0090     'commentlines', {}, ...
0091     'emptylines', {});
0092 
0093 R.subdir = struct( ...
0094     'name', {}, ...
0095     'info', {}, ...
0096     'subdir', {}, ...
0097     'stat', {});
0098 
0099 R.stat = struct( ...
0100     'numfiles', 0, ...
0101     'totallines', 0, ...
0102     'codelines', 0, ...
0103     'commentlines', 0, ...
0104     'emptylines', 0);
0105 
0106 % list candidates
0107 L = dir(folderpath);
0108 nitems = length(L);
0109 countdir = 0;
0110 countfile = 0;
0111 
0112 % process items
0113 for i = 1 : nitems
0114     
0115     curitem = L(i);
0116     
0117     if curitem.isdir    % for a sub folder
0118         if ~strcmp(curitem.name, '.') && ~strcmp(curitem.name, '..') % non-trivial folder
0119             cursub = make_folder_stat(fullfile(folderpath, curitem.name));                        
0120             if ~isempty(cursub.info) || ~isempty(cursub.subdir)     % non-empty folder
0121                 countdir = countdir + 1;
0122                 R.subdir(countdir) = cursub;
0123                 
0124                 R.stat.numfiles = R.stat.numfiles + cursub.stat.numfiles;
0125                 R.stat.totallines = R.stat.totallines + cursub.stat.totallines;
0126                 R.stat.codelines = R.stat.codelines + cursub.stat.codelines;
0127                 R.stat.commentlines = R.stat.commentlines + cursub.stat.commentlines;
0128                 R.stat.emptylines = R.stat.emptylines + cursub.stat.emptylines;
0129                 
0130             end
0131         end                
0132     else                % for a file
0133         if length(curitem.name) > 2 && strcmpi(curitem.name(end-1:end), '.m') % an m-file
0134             curinfo.filename = curitem.name;
0135             curinfo.length = curitem.bytes;
0136             curinfo = make_source_stat(fullfile(folderpath, curitem.name), curinfo);
0137             
0138             countfile = countfile + 1;                                   
0139             R.info(countfile) = curinfo;
0140             
0141             R.stat.numfiles = R.stat.numfiles + 1;
0142             R.stat.totallines = R.stat.totallines + curinfo.totallines;
0143             R.stat.codelines = R.stat.codelines + curinfo.codelines;
0144             R.stat.commentlines = R.stat.commentlines + curinfo.commentlines;
0145             R.stat.emptylines = R.stat.emptylines + curinfo.emptylines;
0146             
0147         end            
0148     end
0149     
0150 end
0151 
0152 
0153 %% The function for making source file-level statistics
0154 function info = make_source_stat(filepath, info)
0155 
0156 txt = slreadtext(filepath);
0157 n = length(txt);
0158 
0159 labels = zeros(n, 1);
0160 % label meanings:
0161 %   0 - empty line
0162 %   1 - comment line
0163 %   2 - code line
0164 
0165 for i = 1 : n    
0166     curline = strtrim(txt{i});
0167     if ~isempty(curline)
0168         if curline(1) == '%'
0169             labels(i) = 1;
0170         else
0171             labels(i) = 2;
0172         end
0173     end    
0174 end
0175 
0176 info.totallines = n;
0177 info.codelines = sum(labels == 2);
0178 info.commentlines = sum(labels == 1);
0179 info.emptylines = sum(labels == 0);
0180 
0181 
0182 %% The function for making folder-level reports
0183 function make_folder_report(fh, parentpath, R)
0184 
0185 curpath = fullfile(parentpath, R.name);
0186 
0187 % print header
0188 fprintf(fh, 'Folder: %s \r\n', curpath);
0189 fprintf(fh, '------------------------------------------------------------\r\n');
0190 fprintf(fh, '\n');
0191 
0192 % print files
0193 nfiles = length(R.info);
0194 if nfiles > 0
0195     fprintf(fh, '%-25s \t %15s \t %15s \t %15s \t %15s \r\n', ...
0196         'filename', 'totallines', 'code', 'comment', 'empty');
0197     for i = 1 : nfiles
0198         make_file_reportline(fh, R.info(i)); 
0199     end
0200 end
0201 fprintf(fh, '\r\n');
0202 
0203 % print sub-folders
0204 nsubdirs = length(R.subdir);
0205 for i = 1 : nsubdirs
0206     make_folder_report(fh, curpath, R.subdir(i));
0207 end
0208 
0209 % print summarized statistics
0210 fprintf(fh, 'Summary: \n');
0211 vinfo.filename = sprintf('total files %d', R.stat.numfiles);
0212 vinfo.totallines = R.stat.totallines;
0213 vinfo.codelines = R.stat.codelines;
0214 vinfo.commentlines = R.stat.commentlines;
0215 vinfo.emptylines = R.stat.emptylines;
0216 make_file_reportline(fh, vinfo);
0217 
0218 % print margin blank
0219 fprintf(fh, '\r\n \r\n');
0220 
0221 
0222 %% The function for making file-level report
0223 function make_file_reportline(fh, info)
0224 
0225 fprintf(fh, '%-25s \t %15d \t %15d \t %15d \t %15d \r\n', ...
0226     info.filename, ...
0227     info.totallines, ...
0228     info.codelines, ...
0229     info.commentlines, ...
0230     info.emptylines);
0231 
0232 
0233 
0234 
0235 
0236 
0237 
0238 
0239 
0240 
0241 
0242 
0243 
0244 
0245 
0246

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com