分享一个用于统计MATLAB代码行数的小程序
2015-07-31 12:37
585 查看
声明:这个程序不是我写的,所有权归作者所有。其copyright如下:
代码如下:
使用方法:
MCount.lines(directory_path) 统计目录下m文件总代码行数。
MCount.reallines(directory_path)统计目录下代码行数(不计空行和注释行)。
MCount.size(directory_path) 统计目录下代码总文件大小。
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) 统计目录下代码总文件大小。
相关文章推荐
- 微信支付开发--名词解释
- 微信-支付模式
- 报表与微信公众号(企业号)集成方案
- 报表与微信公众号(企业号)集成方案
- 微信公众平台完整开发教程
- 微信二维码1-服务号推广(将网页存储为图片存储到服务器)
- 获取微信openID 的步骤
- Android开发模仿微信发表朋友圈图片多选及图片浏览实现-Assur
- 微信红包随机生成算法(PHP版)
- 模仿仿微信发表朋友圈图片多选实现
- NodeJS微信公众平台开发
- NodeJS微信公众平台开发
- 【微信分享】王团结:如何用Hadoop/Spark构建七牛数据平台
- 微信服务号订阅号企业号区别
- 微信OAuth2网页授权获取用户信息
- 微信JS接口
- 微信与财付通接口签名调试总结
- [027] 微信公众帐号开发教程第3篇-开发模式启用及接口配置
- C# 写的一个生成随机汉语名字的小程序
- 微信开发 接口测试