您的位置:首页 > 移动开发 > 微信开发

分享一个用于统计MATLAB代码行数的小程序

2015-07-31 12:37 585 查看
声明:这个程序不是我写的,所有权归作者所有。其copyright如下:

Copyright (c) 2011, Zhiqiang Zhang
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met:

    * Redistributions of source code must retain the above copyright 
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in 
      the documentation and/or other materials provided with the distribution

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.


代码如下:

classdef MCount < handle
%MCount counts the number of M-codes you have written
%
% MCount.lines(path) count the lines you have written under
%   directory filepath.
%
% MCount.reallines(path) count the lines you have written under
%   directory filepath, but omits the emtpy lines and comment lines.
%
% MCount.size(path) adds up the size of your m-files under directory
%   filepath
%
% All of them support multi-paths (in cells), for example,
% MCount.lines({'patha', 'pathb/pathc', 'filed'});
%
% My(zhang@zhiqiang.org) result: in recent 1 year, I wrote 19524 lines of
% M-codes totally, 12311 lines except for empty lines and comments lines,
% 603K in file size.
%
% author: zhang@zhiqiang.org, 2011,
% url: http://zhiqiang.org/blog/it/how-many-codes-have-you-written.html % version: 2011-03-24 

    methods (Static = true)
        function l = lines(filepath)
            if nargin == 0
                filepath = pwd;
            end
            if iscell(filepath)
                l = 0;
                for i = 1:numel(filepath)
                    l = l + MCount.lines(filepath{i});
                end
                return
            end
            l = 0;
            if nargin == 0, filepath = cd; end

            if ~exist(filepath, 'file')
                error([filepath ' is not a file or directory']);
            end

            if exist(filepath, 'dir')
                files = dir(filepath);
                for i = 1:numel(files)
                    file = files(i);
                    if ~strcmp(file.name, '.') && ~strcmp(file.name, '..')
                        l = l + MCount.lines([filepath, '/', file.name]);
                    end
                end                                
            else
                if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
                    l = MCount.getfilelines(filepath);
                end
            end
        end

        function l = reallines(filepath)
            if nargin == 0
                filepath = pwd;
            end            
            if iscell(filepath)
                l = 0;
                for i = 1:numel(filepath)
                    l = l + MCount.reallines(filepath{i});
                end
                return
            end            
            l = 0;
            if nargin == 0, filepath = cd; end

            if ~exist(filepath, 'file')
                error([filepath ' is not a file or directory']);
            end

            if exist(filepath, 'dir')
                files = dir(filepath);
                for i = 1:numel(files)
                    file = files(i);
                    if ~strcmp(file.name, '.') && ~strcmp(file.name, '..')
                        l = l + MCount.reallines([filepath, '/', file.name]);
                    end
                end                                
            else
                if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
                    l = MCount.getfilereallines(filepath);
                end
            end
        end

        function l = size(filepath)
            if nargin == 0
                filepath = pwd;
            end         
            if iscell(filepath)
                l = 0;
                for i = 1:numel(filepath)
                    l = l + MCount.size(filepath{i});
                end
                return
            end            
            l = 0;
            if nargin == 0, filepath = cd; end

            if ~exist(filepath, 'file')
                error([filepath ' is not a file or directory']);
            end

            if exist(filepath, 'dir')
                files = dir(filepath);
                for i = 1:numel(files)
                    file = files(i);
                    if ~strcmp(file.name, '.') && ~strcmp(file.name, '..') && file.isdir == 1
                        l = l + MCount.size([filepath, '/', file.name]);
                    elseif length(file.name) > 2 && file.name(end) == 'm' && file.name(end-1) == '.'
                        l = l + file.bytes;
                    end
                end                                
            else
                if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
                    t = dir(filepath);
                    l = l + t(1).bytes;
                end
            end
        end        

        function l = getfilelines(filename)
            fid = fopen(filename, 'rt');
            if fid<0
                error(['Can''t open file for reading (' filename ')'])
            end

            l = 0;
            while(~feof(fid))
                fgetl(fid);
                l = l+1;
            end
            fclose(fid);
        end

        function l = getfilereallines(filename)
            fid = fopen(filename, 'r');
            if fid<0
                error(['Can''t open file for reading (' filename ')'])
            end

            l = 0;
            while ~feof(fid)
                t = fgetl(fid);        

                for i = 1:numel(t)
                    if t(i) ~= ' '
                        break; 
                    end
                end
                if numel(t) && i < numel(t) && t(i) ~= '%'
                    l = l + 1;
                end
            end
            fclose(fid);
        end        
    end
end


使用方法:

MCount.lines(directory_path) 统计目录下m文件总代码行数。

MCount.reallines(directory_path)统计目录下代码行数(不计空行和注释行)。

MCount.size(directory_path) 统计目录下代码总文件大小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: