您的位置:首页 > 编程语言

MacOS 开发 - 计算代码行数

2017-10-06 10:31 363 查看
一核心代码
1获取全路径下该文件的行数

2获取全路径下该文件夹下全部文件的行数总和

3获取inpath 路径下面所有合格hm的文件路径

4打开选择文件夹

二推荐 Demo - PPRows

一、核心代码

1、获取全路径下该文件的行数

#pragma mark - 获取全路径下该文件的行数
- (int)rows:(NSString *)filepath{

char c;
int  h = 0;
FILE *fp;

fp = fopen([filepath UTF8String], "r");

if (fp == NULL){

return -1;

}

while ((c = fgetc(fp)) != EOF) {

if (c == '\n')

h++;

else {

c = fgetc(fp);//这里处理最后一行可能没有换行标记

if (c == EOF){

h++;

break;

} else if (c == '\n')

h++;
}
}

return h;
}


2、获取全路径下该文件夹下全部文件的行数总和

#pragma mark - 获取全路径下该文件夹下全部文件的行数总和
- (void)getNumsOfFilePath:(NSString *)filePath{

NSLog(@"文件路径:%@", filePath);

self.pathLabel.stringValue = filePath;

//检查文件是否存在

NSFileManager *filemanager = [NSFileManager defaultManager];

if ([filemanager fileExistsAtPath:filePath]) {

int num = 0;

NSArray *arr = [self GetAllDirInPath:filePath];

for (NSString *inpath in arr) {
num += [self rows:inpath];
}

self.rowsLabel.stringValue = [NSString stringWithFormat:@"总行数: %d", num];

} else {

self.rowsLabel.stringValue = @"请选择正确的路径";

}
}


3、获取inpath 路径下面所有合格(.h/.m)的文件路径

#pragma mark - 获取inpath 路径下面所有合格(.h/.m)的文件路径
- (NSArray *)GetAllDirInPath:(NSString *)inpath{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableArray *dirArray = [[NSMutableArray alloc] init];

BOOL isDir = NO;

[fileManager fileExistsAtPath:inpath isDirectory:(&isDir)];

if (isDir == NO){

if ([self checkClassFile:inpath]){

[dirArray addObject:inpath];
}
}

isDir = NO;

NSError *error = nil;

NSArray *fileList = nil;

//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组

fileList = [fileManager contentsOfDirectoryAtPath:inpath error:&error];

//在上面那段程序中获得的fileList中列出文件夹名

for (NSString *file in fileList) {

NSString *path = [inpath stringByAppendingPathComponent:file];

[fileManager fileExistsAtPath:path isDirectory:(&isDir)];

if (!isDir) {

if ([self checkClassFile:path]){

[dirArray addObject:path];
}

} else {

[dirArray addObjectsFromArray:[self GetAllDirInPath:path]];
}

isDir = NO;

}

//    NSLog(@"dirArray : %@",dirArray);

return dirArray;
}

#pragma mark - 检查文件后缀名
- (BOOL)checkClassFile:(NSString *)filePath {

NSString *extensionName = [filePath pathExtension];

//    NSLog(@"后缀名:%@", extensionName);

if ([extensionName isEqualToString:@"h"] ||

[extensionName isEqualToString:@"m"]){

return YES;
}

return NO;
}


4、打开选择文件夹

这里只选择了一个文件夹,你也可以改为多选。

- (void)pickFile{

NSOpenPanel* panel = [NSOpenPanel openPanel];

[panel setAllowsMultipleSelection:YES];  //是否允许多选file
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:YES];

[panel beginWithCompletionHandler:^(NSInteger result) {
if (result == NSOKButton) {
NSMutableArray* filePaths = [[NSMutableArray alloc] init];
for (NSURL* elemnet in [panel URLs]) {
[filePaths addObject:[elemnet path]];
}

NSLog(@"filePaths : %@",filePaths);

if (filePaths.count > 0) {

[self getNumsOfFilePath:filePaths.firstObject];
}
}

}];
}


二、推荐 Demo - PPRows

GitHub 上搜索,即可下载。可拖拽选择文件夹,界面效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: