//// Main. M // calculate the number of lines of code /// created by Apple on 13-8-12. // copyright (c) 2013 apple. all rights reserved. // # import <Foundation/Foundation. h> // use the nsfilemanger, nsstring, and nsarray classes // calculate the number of lines of code in a single file (the file may be a folder) // path: the full path returned value of the file: intint codelinecount (nsstring * path) {// nsfilemanger is a single object nsfilemanager * Mgr = [nsfilemanager defaultmanager]; // determines whether the path is a file or a folder bool dir = no; // mark as a folder // determine whether the path has B OOl exist = [Mgr fileexistsatpath: path isdirectory: & dir]; // if not, 0 if (! Exist) {nslog (@ "this path does not exist"); Return 0 ;}if (DIR) {nslog (@ "Yes folder"); nsarray * array = [Mgr contentsofdirectoryatpath: path error: Nil]; // nslog (@ "% @", array); int sum = 0; For (nsstring * filename in array) {// full path nsstring * fullname = [nsstring stringwithformat: @ "% @/% @", path, filename]; sum + = codelinecount (fullname );} return sum;} else {nslog (@ "yes file"); // determine the file extension name nsstring * Extentsion = [[path pat Hextension] lowercasestring]; nslog (@ "% @", Extentsion); If (! [Extentsion isequaltostring: @ "H"] &! [Extentsion isequaltostring: @ "M"] &! [Extentsion isequaltostring: @ "C"] &! [Extentsion isequaltostring: @ "docx"]) {return 0;} // read the file (Load file content) nsstring * Contents = [nsstring stringwithcontentsoffile: path encoding: nsutf8stringencoding error: nil]; // nslog (@ "% @", contents); // use \ n to split the file nsarray * array = [contents componentsseparatedbystring: @ "\ n"]; // For (id obj in array) // {// nslog (@ "% @", OBJ); //} nsange range = [path rangeofstring: @ "users/Apple/desktop/"]; nsstring * Str = [Path stringbyreplacingcharactersinrange: range withstring: @ ""]; nslog (@ "% @ % ZD", STR, array. count); // return (INT) contents. length; Return (INT) array. count;} // The number of file shards is the number of file rows} int main (INT argc, const char * argv []) {// nsstring * STR = @ "Jak \ njdfs"; // [STR writetofile: @ "/users/Apple/desktop/data.txt" atomically: Yes encoding: nsutf8stringencoding error: nil]; int COUNT = codelinecount (@ "/E:/. RTF "); Nslog (@" the number of rows is ~~ % D ", count); Return 0 ;}