黑馬程式員---工具類---OC自訂函數---計算當前路徑下所有代碼檔案的總行數 .c\.h\.m檔案的總行數

來源:互聯網
上載者:User

標籤:

------iOS培訓、Java培訓、Android培訓, iOS學習型技術部落格,期待與您交流------

計算當前路徑下所有代碼檔案的總行數  .c\.h\.m檔案的總行數

/*

 計算當前路徑下所有代碼檔案的行數  .c\.h\.m檔案的總行數

 涉及到NSArray、NSString、NSFileManager

 */

 

// 計算當前全路徑(檔案\檔案夾)下所有檔案的行數

// 接收參數path:檔案的全路徑(檔案、檔案夾)

NSUInteger codeLinesCount(NSString *path)

{

    // 1.獲得檔案管理者    單例模式:整個程式的運行過程中,NSFileManager類的對象只有這一個

    NSFileManager *fmg = [NSFileManager defaultManager];

    

    // 2.判斷path是檔案夾還是檔案路徑

    BOOL isDir = NO;  // 標記是否為檔案夾

    // 路徑是否存在(有效)

    BOOL isExist = [fmg fileExistsAtPath:path isDirectory:&isDir];

    

    // 3.如果路徑不存在,直接返回0退出函數

    if (!isExist)

    {

        NSLog(@"檔案路徑無效");

        return 0;

    }

    

    // 路徑存在

    if (isDir)

    {

        // 是檔案夾

        // NSLog(@"是個檔案夾");

        // 返回  當前路徑(該檔案夾)目錄下所有(路徑)---檔案、檔案夾路徑 列表(數組)

        // 數組元素  並不是全路徑

        NSArray *filePathsList = [fmg contentsOfDirectoryAtPath:path error:nil];

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

        

        NSUInteger count = 0;   // 行數

        

        // 遍曆數組中的所有路徑(檔案名稱\檔案夾名)

        for (NSString *filePath in filePathsList)

        {

            // 拼接全路徑  當前路徑檔案夾(子檔案\子檔案夾)的全路徑

            NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, filePath];

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

            

            /*

             此處 遞迴

             調用本身  返回  每個最終子檔案的行數

             codeLinesCount(fullPath);

             */

            // 把每個最終子路徑檔案的行數  累加每個子路徑的總行數

            count += codeLinesCount(fullPath);    // 精髓

            

        }

        return count;

    }

    else

    {   // 是個檔案

        // NSLog(@"是個檔案");

        

        /*

          過濾出.c\.h\.m檔案

          是個檔案  判斷檔案拓展名

          返回拓展名(忽略大小寫---先全部轉為小寫)

         */

        NSString *extension = [[path pathExtension] lowercaseString];

        // NSLog(@"拓展名為 %@", extension);

        if ( !([extension isEqualToString:@"c"]

              ||[extension isEqualToString:@"h"]

              ||[extension isEqualToString:@"m"]) )

        {

            return 0;

        }

        

        // 1.負載檔案內容,到一個字串對象

        NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

        

        // 2.將檔案內容(字串內容)以‘\n‘每一行進行 切分, 切分好後的內容放入數組 返回

        NSArray *array = [content componentsSeparatedByString:@"\n"];

        

        // 每一個數組元素都是一個NSString對象,就是檔案中一行的內容

//        int i = 0;

//        for (NSString *str in array)

//        {

//            i++;

//            NSLog(@"%d---%@",i, str);

//        }

        

        // NSLog(@"檔案名稱:%@ --- 行數:%ld", path, array.count);

        NSString *shortPath = [path stringByReplacingCharactersInRange:[path rangeOfString:@"/Users/Mac/Desktop/"] withString:@" "];

        NSLog(@"檔案名稱:%@ --- 行數:%ld", shortPath, array.count);

        return array.count;     // 數組元素個數就是行數

    }

    

}

黑馬程式員---工具類---OC自訂函數---計算當前路徑下所有代碼檔案的總行數 .c\.h\.m檔案的總行數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.