Easy way to count total lines of code?

Working on new editor enhancements?
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Easy way to count total lines of code?

Post by WilliamL »

Simple and cross-platform...

Code: Select all

; count all lines in several files
EnableExplicit
#gadget1=0

Structure prgdata
    filename.s
    totallines.l
    blanklines.l
    remlines.l
EndStructure
Global Dim prg.prgdata(30) ; totals in prg(0)\
Global totalprgs

Define w,h,cnt,filename$

Procedure ReadaFile(filecnt)
    Define lne.s
    
    If ReadFile(0,prg(filecnt)\filename)
        While Not Eof(0)
            lne=ReadString(0)
            prg(filecnt)\totallines+1
            If Len(Trim(lne))=0 : prg(filecnt)\blanklines+1 : EndIf
            If Left(Trim(lne),1)=";" : prg(filecnt)\remlines+1 : EndIf
        Wend
    Else
        MessageRequester("Reading file...notfound",prg(filecnt)\filename+Str(filecnt))
    EndIf
    prg(filecnt)\totallines-4 ; take out pb inserted lines
    prg(filecnt)\remlines-4   ; take out pb inserted lines
    AddGadgetItem(#gadget1,-1,Str(filecnt)+" File name: "+GetFilePart(prg(filecnt)\filename))
    AddGadgetItem(#gadget1,-1,"    Total number of lines in this file: "+Str(prg(filecnt)\totallines))
    AddGadgetItem(#gadget1,-1,"    Total number of blanklines in this file: "+Str(prg(filecnt)\blanklines))
    AddGadgetItem(#gadget1,-1,"    Total number of comment lines in this file: "+Str(prg(filecnt)\remlines))
EndProcedure

w=640
h=400
OpenWindow(1,0,0,w,h,"Lines in program(s)",#PB_Window_SystemMenu | #PB_Window_WindowCentered) 
    filename$=OpenFileRequester("File(s) to count...","MyFile.pb","",0,#PB_Requester_MultiSelection)
    If filename$
        ListViewGadget(#gadget1,0,0,w,h) ; for showing data
        
        totalprgs=1
        prg(totalprgs)\filename=filename$ ;: Debug prg()
        While filename$
            totalprgs+1
            filename$ = NextSelectedFileName()
            prg(totalprgs)\filename=filename$ ;: Debug prg()
        Wend
        totalprgs-1
        
        ;read lines in each file
        For cnt=1 To totalprgs
            ReadaFile(cnt)
            prg(0)\totallines+prg(cnt)\totallines
            prg(0)\blanklines+prg(cnt)\blanklines
            prg(0)\remlines+prg(cnt)\remlines
            AddGadgetItem(#gadget1,-1,"")
        Next
        
        AddGadgetItem(#gadget1,-1,"Total lines in all files: "+Str(prg(0)\totallines))
        AddGadgetItem(#gadget1,-1,"Total blank lines in all files: "+Str(prg(0)\blanklines))
        AddGadgetItem(#gadget1,-1,"Total rem lines in all files: "+Str(prg(0)\remlines))
    EndIf
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
End
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Easy way to count total lines of code?

Post by Vera »

Hi WilliamL,

confirmed for Linux and Win - thanks for sharing :)

btw: of course I straight trapped into an IMA wanting to evaluate all 82 example files at once not realizing that there's a limitation - but I worked around it :lol: and here they are -

- the actual results of the pb\example\sources codes-lines from the WIN edition
Total lines in all files: 5667
Total blank lines in all files: 1475
Total rem lines in all files: 783
greetings ~ Vera
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Easy way to count total lines of code?

Post by WilliamL »

Hi Vera!

Oh, I couldn't imagine having more that 30 include files! :) (you had to redimension prg.prgdata(30)) Well, include files is what I had in mind but, I guess, it would work for any bunch of files.

Seem like this thread should be in another forum (Coding Questions ?).
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply