menu

hjk41的日志

Avatar

three ways of saying "count lines of code of my project"

C++ code:


// C++  code, counts all *.java code in a directory
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <string>
using namespace std;

long long count_file(const string & path){
        string str;
        long long loc=0;
        ifstream in(path.c_str());
        while(in.good()){
                getline(in,str);
                loc++;
        }
        return loc;
}

long long count_dir(const string & dir){
        DIR *dp;
        struct dirent *ep;
        long long loc=0;

        dp = opendir (dir.c_str());
        if (dp != NULL) {
                while (ep = readdir (dp)){
                        string filename=ep->d_name;
                        // skip . and ..
                        if(filename=="." || filename==".." || ep->d_type==DT_LNK)
                                continue;

                        string path=dir+"/"+filename;
                        if(ep->d_type==DT_DIR){
                                cout<<"*********** entering dir: "<<path<<endl;
                                loc+=count_dir(path);
                        }
                        else{
                                cout<<"checksum file: "<<path<<endl;
                                loc+=count_file(path);
                        }
                }
                closedir (dp);
        }
        else{
                cout<<"Couldn't open the directory "<<dir<<endl;
        }
        return loc;
}

int main (int argc, char * argv[]){
        if(argc!=2){
                cout<<"usage: "<<argv[0]<<" dir"<<endl;
                return 1;
        }

        long long loc=count_dir(argv[1]);
        cout<<"================="<<endl;
        cout<<"total loc: "<<loc<<endl;

        return 0;
}

python code:


#!/usr/bin/python

import os
import sys

total_loc=0

def file_loc(file_path):
        global total_loc
        print("counting %s\n"%file_path)
        loc=0
        f=open(file_path,'r')
        for line in f:
                loc+=1
        total_loc+=loc

def dir_loc(exts, dir_name, files):
        loc=0
        for file in files:
                file_wanted=0
                for ext in exts:
                        if os.path.splitext(file)[1]==ext:
                                file_wanted=1
                                break
                if file_wanted:
                        file_loc(dir_name+"/"+file)

def main():
        global total_loc
        if(len(sys.argv)<3):
                print("usage: %s dir file_exts ...\n" % sys.argv[0])
                return
        dir=sys.argv[1]
        exts=[]
        for ext in sys.argv[2:]:
                exts.append("."+ext)
        total_loc=0
        os.path.walk(dir, dir_loc, exts)
        print("===================\ntotal loc: %d \n"%total_loc)

main()

shell code:


find . -name *.py | xargs wc -l

These code are written by myself. I am a newbie to python and shell programming. If you have better method for attacking this problem, or if you have other options, such as perl, Ruby, please let me know. :-)

各有所长