- //
- // NSString+Tools.m
- // FileLineNumbers
- //
- // Created by 疯兔 on 14/12/1.
- // Copyright (c) 2014年 Mr.z. All rights reserved.
- //
- //本代码用于快速统计代码行数
- #import "NSString+Tools.h"
- #define HFILE @"h"
- #define MFILE @"m"
- #define CFILE @"c"
- #define CODECOMMENTS @"//"
- #define CODECOMMENTSTYPEBEGIN @"/*"
- #define CODECOMMENTSTYPEEND @"*/"
- /*
- 本程序可统计.h/.m/.c文件类型
- 可识别文件中的注释行数,但未进行有效注释计算
- 可识别无效行数
- 返回值为有效代码行数,包含全部注释行数
- 注意:不接受URL路径
- */
- @implementation NSString (Tools)
- + (NSUInteger)stringFileLineNumbers:(NSString *)path
- {
- BOOL isDir = NO;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- BOOL isRightPath = [fileManager fileExistsAtPath:path isDirectory:&isDir];//返回路径是否存在BOOL,传入为是否文件夹BOOL
-
- if (!isRightPath) {
- NSLog(@"无法识别路径");
- return 0;
- }
-
- if (isDir) {
-
- __block int count = 0;
- NSArray *dirFiles = [fileManager contentsOfDirectoryAtPath:path error:Nil];
- [dirFiles enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- NSString *dirFilePath = [NSString stringWithFormat:@"%@/%@",path,obj];
- count += [NSString stringFileLineNumbers:dirFilePath];
- }];
-
- return count;
- }else{
-
- //取出扩展名,并进行小写转化
- NSString *pathEx = [[path pathExtension]lowercaseString];
- if (![pathEx isEqualToString:HFILE]
- &&
- ![pathEx isEqualToString:MFILE]
- &&
- ![pathEx isEqualToString:CFILE]) {
- return 0;
- }
-
-
- NSArray *getFileName = [path componentsSeparatedByString:@"/"];
- NSString *fileName = [getFileName lastObject];
-
- //1.加载文件内容
- // NSLog(@"开始加载:%@文件",fileName);
-
- NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:Nil];
- NSArray *contendArray = [content componentsSeparatedByString:@"\n"];//2.分割字符串
-
- __block int noCode = 0;//无效行数
- __block int comments = 0;//注释行数
- __block BOOL isComments = NO;
-
- [contendArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- NSString * stringObj = obj;
- if (stringObj.length < 1) {
- noCode++;
- }
-
- if ([stringObj rangeOfString:CODECOMMENTS].location != NSNotFound) {
- comments++;
- }
- if ([stringObj rangeOfString:CODECOMMENTSTYPEBEGIN].location != NSNotFound) {
- isComments = YES;
- }
- if ([stringObj rangeOfString:CODECOMMENTSTYPEEND].location != NSNotFound) {
- isComments = NO;
- }
- if (isComments) {
- comments++;
- }
-
-
- }];
- NSUInteger codeCount = contendArray.count - noCode;
- NSLog(@"文件名:%@中有效代码行数:%ld,无效代码行数:%d,注释行数:%d,总行数:%ld",fileName,codeCount,noCode,comments,contendArray.count);
- return contendArray.count - noCode;
- }
-
- }
- + (void)dateLog
- {
- NSDate *date = [NSDate date];
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- NSString *dateString = [formatter stringFromDate:date];
- NSLog(@"本次统计开始时间为 %@",dateString);
- }
- @end
复制代码
|
|