packages=dir; %get directries to all packages

N_packages=length(packages);

info=cell(N_packages-2,2); %store package name and size

for i=3:N_packages %for each package
    if isfolder(packages(i).name)    %make sure the package is a folder
        cd(packages(i).name);
        sub_dir=dir;
        info{i-2,1}=sub_dir(3).name;
        cd(sub_dir(3).name)

        if isfolder('R') %make sure /R is a folder
            cd('R');
            info{i-2,2}=count_lines_r; %count lines of R script
            cd ..;
        end

        if isfolder('src') %make sure /src is a folder
            cd('src');
            info{i-2,2}=info{i-2,2}+count_lines_c; %count lines of C script
            cd ..;
        end

        cd ..;
        cd ..;
    end
end
disp('done!');


%definition of count_lines_r
function N_lines=count_lines_r()

    files=dir();
    N_files=length(files)-2;
    N_lines=1;

    for i=1:N_files % for each file
        if isfile(files(i+2).name)
            fid=fopen(files(i+2).name,'r');
            while ~feof(fid)
                inline=fgetl(fid);  %reads one line at time   
                if ~strcmp(inline,'') % not an empty line
                    if ~strcmp(inline(1),'#') % not a comment line
                        N_lines=N_lines+1;
                    end
                end
            end
            fclose(fid);
        end
    end
end

%definition of count_lines_c
function N_lines=count_lines_c()

    files=dir();
    N_files=length(files)-2;
    N_lines=1;

    for i=1:N_files % for each file
        if isfile(files(i+2).name)
            fid=fopen(files(i+2).name,'r');
            flag_comment=0;
            while ~feof(fid)
                inline=fgetl(fid);    %reads one line at time    
                if ~strcmp(inline,'') %not an empty line
                    if length(inline)>=2 %lines with just "{" or "}" are counted 
                        if strcmp(inline(1:2),'/*') %block comment
                            flag_comment=1;
                        elseif strcmp(inline(end-1:end),'*/') && flag_comment==1
                            flag_comment=0;
                        elseif flag_comment==0 && strcmp(inline(1:2),'//') %in-line comment
                            N_lines=N_lines+1;
                        end            
                    else
                        N_lines=N_lines+1; 
                    end
                end
            end
            fclose(fid);
        end
    end
end