计算代码行数Demo源码
源码下载:
04-计算代码行数.zip
24.1 KB
//
// main.m
// 计算代码行数
//
// Created by apple on 13-8-12.
//技术博客http://www.cnblogs.com/ChenYilong/新浪微博http://weibo.com/luohanchenyilong
/*
* 考察NSString、NSArray的使用
* NSFileManager
*/
#import <Foundation/Foundation.h>
// 计算文件的代码行数
/*
path : 文件的全路径(可能是文件夹、也可能是文件)
返回值 int :代码行数
*/
NSString *oldPath;
NSUInteger codeLineCount(NSString *path)
{
if (!oldPath) oldPath = path;
// 1.获得文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 2.标记是否为文件夹
BOOL dir = NO; // 标记是否为文件夹
// 标记这个路径是否存在
BOOL exist = [mgr fileExistsAtPath:path isDirectory:&dir];
// 3.如果不存在,直接返回0
if(!exist)
{
NSLog(@"文件路径不存在!!!!!!");
return 0;
}
// 代码能来到着,说明路径存在
if (dir)
{ // 文件夹
// 获得当前文件夹path下面的所有内容(文件夹、文件)
NSArray *array = [mgr contentsOfDirectoryAtPath:path error:nil];
// 定义一个变量保存path中所有文件的总行数
int count = 0;
// 遍历数组中的所有子文件(夹)名
for (NSString *filename in array)
{
// 获得子文件(夹)的全路径
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filename];
// 累加每个子路径的总行数
count += codeLineCount(fullPath);
}
return count;
}
else
{ // 文件
// 判断文件的拓展名(忽略大小写)
NSString *extension = [[path pathExtension] lowercaseString];
if (![extension isEqualToString:@"h"]
&& ![extension isEqualToString:@"m"]
&& ![extension isEqualToString:@"c"])
{
// 文件拓展名不是h,而且也不是m,而且也不是c
return 0;
}
// 加载文件内容
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// 将文件内容切割为每一行
NSArray *array = [content componentsSeparatedByString:@"\n"];
// 删掉文件路径前面的/Users/apple/Desktop/iOS课堂共享/0722课堂共享/
NSRange range = [path rangeOfString:oldPath];
NSString *str = [path stringByReplacingCharactersInRange:range withString:@""];
// 打印文件路径和行数
NSLog(@"%@ - %ld", str, array.count);
return array.count;
}
}
int main()
{
NSUInteger count = codeLineCount(@"/Users/apple/Desktop/hhbb");
NSLog(@"%ld", count);
return 0;
}
void test()
{
NSString *str = @"jack\nrose\njim\njake";
[str writeToFile:@"/Users/apple/Desktop/abc.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];
for (NSString *line in array)
{
NSLog(@"%@", line);
}
////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490566.html
//int count = codeLineCount(@"/Users/apple/Desktop/iOS课堂共享/0722课堂共享/0811/代码/04-block/04-block/main.m");
//NSLog(@"count=%d", count);
计算代码行数Demo源码的更多相关文章
- php 计算代码行数
<?php header("Content-type:text/html;charset=utf-8"); // php 递归计算文件夹代码行数 function codeL ...
- find命令计算代码行数
[anonymous@localhost ~/lvs/ipvsadm- -regex '.*Makefile.*' -o -regex '.*\.[ch]' -exec cat {} \; | wc ...
- C#计算代码行数
class Program { static void Main(string[] args) { int totalLineCount = 0; string directory; if(args. ...
- vs计算代码行数
1.用vs打开程序 2.编辑——查找——在文件中查找 3.查找内容^b*[^:b#/]+.*$ 应用正则表达式,在整个解决方案中,文件类型空 4.查找全部,仔细盯着右下角数字,查找完毕后会自动消失 ...
- 一个简单的代码计算行数demo编写
最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录 ...
- PyQt5学习随笔01--计算一个目录里我们码的代码行数&&PyQt的多线程通信
今天突然想知道自学习Python以来我一共码了多少行代码了,于是写了一个简单的程序: __author__ = 'jiangzhiheng' # coding=utf-8 from PyQt5.QtC ...
- Python计算一个项目中含有的代码行数
最近想要知道以前做过的project有多少行代码,因为文件太多,直接手工数效率太低,于是编写一个python程序用来计算一个project有多少代码行. 首先,在一个项目中,有很多子文件夹,子文件夹中 ...
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...
- 【Python基础】计算项目代码行数
统计代码行数 # coding: utf-8 import os import sys import time def get_line_count(file_path): ""& ...
随机推荐
- 判断电脑CPU硬件支不支持64位
你可以在注册表中查看: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSO ...
- **leetcode笔记--4 Sum of Two Integers
question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + a ...
- 安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口
解决方法:找到 C:\Program Files (x86)\Microsoft Office\root\Office16\STARTUP目录下的MathType Commands 6 For Wor ...
- C#下16进制和BCD码转换代码
private static Byte[] ConvertFrom(string strTemp) { try { if (Convert.ToBoolean(strTemp.Length & ...
- [模板]BZOJ4756线段树合并
题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...
- swarm 服务器安装
apt install docker.io-----------------------配置加速器.私有仓库地址---------------------mkdir -p /etc/dockertee ...
- ajax中用jsonp接收json数据
最近在做查快递网页时遇到一个问题,调用的快递100的api,但是快递100api不允许跨域请求,就是用127.0.0.1发的请求会直接被拦截. 只是个简单的网页,不想自己做服务器转发,最后找到了一个y ...
- 福大软工1816:Alpha(2/10)
Alpha 冲刺 (2/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.学习qqbot库: 2.实 ...
- 剑指offer:跳台阶
目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). ...
- MySql动态生成SQL并执行
场景:由于一些表中设计了一些冗余字段,因此在主表修改了该冗余字段的值得时候,需要动态更新在其他表中冗余字段的值 BEGIN #Routine body goes here... /*SQL语句变量*/ ...