MacOS 开发 - 计算代码行数
2017-10-06 10:31
363 查看
一核心代码
1获取全路径下该文件的行数
2获取全路径下该文件夹下全部文件的行数总和
3获取inpath 路径下面所有合格hm的文件路径
4打开选择文件夹
二推荐 Demo - PPRows
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 上搜索,即可下载。可拖拽选择文件夹,界面效果如下:相关文章推荐
- ios开发计算代码行数
- 黑马程序员---Foundation常用类练习(计算文件代码行数)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- C#代码行数的计算工具(递归的实现)
- Foundation - 练习:计算代码行数
- Java计算代码行数
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载][续]
- JAVA作业—从命令行输入两个参数打开文件并计算代码行数 .
- PyQt5学习随笔01--计算一个目录里我们码的代码行数&&PyQt的多线程通信
- iOS开发_统计xcode代码行数
- iOS开发_统计xcode代码行数
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- 计算代码行数Demo源码
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- iOS——计算项目代码行数
- XCode 显示代码开发行数
- 《Objective-c》Foundation框架 -(练习:计算整个文件夹的代码行数)
- 黑马程序员--ios基础--oc-foundation--NSString,NSArray与计算代码行数的练习
- Visual Studio 2005插件开发(代码行数统计器)之二