Dark Horse Programmer---tool class---OC custom Function---Calculates the total number of rows for all code files under the current path. Total number of C\.H\.M files

Source: Internet
Author: User

------iOS training, Java training, Android training, iOS learning technology blog, looking forward to communicating with you------

Calculates the total number of rows for all code files under the current path . total number of c\.h\.m files

/*

calculates the number of rows for all code files under the current path . Total number of c\.h\.m files

involving nsarray,nsstring,nsfilemanager

*/

calculates the number of rows for all files under the current full path (file \ folder)

receive parameter path: Full path to file ( file, folder )

Nsuinteger codelinescount (nsstring *path)

{

//1. get the File Manager Singleton mode: During the whole program, the object of theNsfilemanager class is the only one

nsfilemanager *fmg = [nsfilemanager defaultmanager];

//2. determine if path is a folder or a file path

BOOL isdir = NO; // mark is a folder

// path exists (valid)

BOOL isexist = [FMG fileexistsatpath:p ath isdirectory:&isdir];

//3. returns the 0 Exit Function directly if the path does not exist

if (!isexist)

{

NSLog(@ " file path is invalid ");

return 0;

}

// path exists

if (isdir)

{

// is a folder

//NSLog (@ " is a folder ");

// Returns a list of all (paths)--- files, folder paths ( arrays ) in the current path (the folder) directory

// array element not full path

nsarray *filepathslist = [FMG contentsofdirectoryatpath:p ath error:nil];

//NSLog (@ "%@", filepathslist);

Nsuinteger count = 0; // number of rows

// iterate through all paths in the array (file name \ folder name)

for (nsstring *filepath in filepathslist)

{

// Stitching full path of the current path folder (sub -Files \ Subfolders ) of the full path

nsstring *fullpath = [nsstring stringwithformat:@ "%@/%@", Path, FilePath];

//NSLog (@ "%@", FullPath);

/*

recursion here

the call itself returns the number of rows per final child file

Codelinescount (FullPath);

*/

// Add the total number of rows per sub-path to each final subpath file

Count + = codelinescount(FullPath); // essence

}

return count;

}

Else

{ // is a file

//NSLog (@ " is a file ");

/*

filtering out . c\.h\.m files

It 's a file. file extension

returns the extension name (ignoring case --- all lowercase first)

*/

nsstring *extension = [[Path pathextension] lowercasestring];

//NSLog (@ " Extended name %@", extension);

If (! ( [Extension isequaltostring:@ "C"]

|| [Extension isequaltostring:@ "H"]

|| [Extension isequaltostring:@ "M"] )

{

return 0;

}

//1. loads the contents of a file into a string object

nsstring *content = [nsstring stringwithcontentsoffile:p ath encoding: Nsutf8stringencoding error:nil];

//2. The contents of the file (string content) are sliced with ' \ n ' each line , and the contents of the Shard are put back into the array .

nsarray *array = [content componentsseparatedbystring:@ "\ n"];

// Each array element is a nsstring object, which is the contents of a row in the file

int i = 0;

For (NSString *str in array)

//        {

i++;

NSLog (@ "%d---%@", I, str);

//        }

//NSLog (@ " file name:%@--- lines:%ld", Path, array.count);

nsstring *shortpath = [path stringbyreplacingcharactersinrange: [Path rangeofstring:@ "/ users/mac/desktop/"] withstring:@" "];

NSLog(@ " file name:%@--- lines:%ld", ShortPath, array.count);

return array.     Count; // array element number is the number of rows

}

}

Dark Horse Programmer---tool class---OC custom Function---Calculates the total number of rows for all code files under the current path. Total number of C\.H\.M files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.