Page 1 of 2
PBLC - Line Count
Posted: Sat Jan 17, 2009 8:38 pm
by flaith
Hi,
just because i needed to know the number of lines for each of my sources programs, i made this little code, i also added the number of procedures inside each program
The program counts only line of code and extracts empty and remarks' lines
Code: Select all
./pblc *.pb
or
./pblc adir_*.pb
or
./pblc adir_*.pb adir_*.pbi
or
./pblc adir_*.pb?
Code: Select all
; *****************************
; *** ***
; *** pblc - count line ***
; *** ***
; *****************************
;
; count effectives lines of PureBasic programs without remarks and empty lines
; and count number of procedures
Global argc.i, thefile.s, totline.i, totproc.i
Procedure.i Get_NBLINE(thefile.s)
Protected NewList line_prg.s(), tmp.s
Protected ligne.i = 0, proc.i = 0, i.i = 1
Protected rem.i = #False : car.i = #False
ClearList(Line_prg())
If ReadFile(0, thefile)
While Eof(0) = 0
tmp = ReadString(0)
AddElement(Line_prg())
line_prg() = tmp
Wend
CloseFile(0)
Else
PrintN("Unable to read " + thefile)
End
EndIf
ResetList(line_prg())
ForEach line_prg()
l = Len(line_prg())
rem = #False : car = #False : i = 1
While i <= l
a$ = Mid(line_prg(),i,1)
If a$ = Chr(32) : i + 1 : EndIf
If a$ = ";" And car = #False : i + 1 : rem = #True : EndIf
If a$ > Chr(32) And a$ < Chr(128) : car = #True : EndIf
i + 1
Wend
If car = #True And rem = #False : ligne + 1 : EndIf
If FindString(line_prg(),"EndProcedure",1) And rem = #False : proc + 1 : EndIf
Next
PrintN(RSet(Str(ligne),6," ") + " " + thefile + "("+Str(proc)+")")
totproc + proc
ProcedureReturn ligne
EndProcedure
OpenConsole()
argc = CountProgramParameters()
totproc = 0
If argc = 0
PrintN("Usage : pblc <filename>")
End
EndIf
If argc > 1 ; several files (because we're using joker/wildcard "*" or "?")
For i = 0 To argc - 1
thefile = ProgramParameter(i)
totline + Get_NBLINE(thefile)
Next i
PrintN("------------")
PrintN(RSet(Str(totline),6," ") + " total("+Str(totproc)+")")
Else
Get_NBLINE(ProgramParameter())
EndIf
CloseConsole()
Totally inspired by the "wc : word count" of Linux shell
Thanks to Captn. Jinguji & GBeebe for the ProgramParameter joker/wildcard.
not tested under win
Posted: Sun Jan 18, 2009 5:19 pm
by Kwai chang caine
Thanks for sharing your code
But like usually, only one in this forum, have not understand how can i do for use it
If you have a little bit of time for explain in just several line

Posted: Sun Jan 18, 2009 5:59 pm
by flaith
Hi KCC
first of all, open a console (cmd.exe under windows or Xterm under GNU/Linux or MacOSX?)
under the prompt
Windows :
C:\>pblc *.pb
Un*x
$./pblc *.pb
result => gives you the total number of lines except empty & remarks lines and the number of procedure for each files.
new version : check "IncludeFile" and automatically add the file included
Code: Select all
; *****************************
; *** ***
; *** pblc - count line ***
; *** ***
; *****************************
;
; count effectives lines of PureBasic programs without remarks and empty lines
; and count number of procedures
; added : get include files
Global argc.i, thefile.s, totline.i, totproc.i
Global NewList prg.s()
Procedure.i Get_NBLINE(thefile.s)
Protected NewList line_prg.s(), tmp.s
Protected ligne.i = 0, proc.i = 0, i.i = 1
Protected rem.i = #False : car.i = #False
ClearList(Line_prg())
If ReadFile(0, thefile)
While Eof(0) = 0
tmp = ReadString(0)
AddElement(Line_prg())
line_prg() = tmp
Wend
CloseFile(0)
Else
PrintN("Unable to read " + thefile)
End
EndIf
ResetList(line_prg())
ForEach line_prg()
l = Len(line_prg())
rem = #False : car = #False : i = 1
While i <= l
a$ = Mid(line_prg(),i,1)
If a$ = Chr(32) : i + 1 : EndIf
If a$ = ";" And car = #False : i + 1 : rem = #True : EndIf
If a$ > Chr(32) And a$ < Chr(128) : car = #True : EndIf
i + 1
Wend
If car = #True And rem = #False : ligne + 1 : EndIf
If FindString(line_prg(),"EndProcedure",1) And rem = #False : proc + 1 : EndIf
Next
PrintN(RSet(Str(ligne),6," ") + " " + thefile + "("+Str(proc)+")")
totproc + proc
ProcedureReturn ligne
EndProcedure
Procedure MakeUnique()
Protected Dim prg_tmp.s(1)
Protected i.i = 0, l.i = 0
Protected tmp.s
l = ListSize(prg())
ReDim prg_tmp(l)
SortList(prg(),#PB_Sort_Ascending)
ResetList(prg())
ForEach prg()
prg_tmp(i) = prg()
i+1
Next
ClearList(prg())
i = 0
tmp = prg_tmp(i)
While i < l
If prg_tmp(i+1) <> tmp
AddElement(prg())
prg() = tmp
EndIf
i+1
tmp = prg_tmp(i)
Wend
EndProcedure
Procedure CheckInclude()
Protected pos.i, pos2.i, pos3.i
Protected ltmp.s, ltmp2.s
ResetList(prg())
ForEach prg()
If ReadFile(0, prg())
While Eof(0) = 0
ltmp = ReadString(0)
pos = FindString(ltmp,"IncludeFile",1)
If pos
pos2 = FindString(ltmp,Chr(34),pos+1)
If pos2 > pos
pos3 = FindString(ltmp,Chr(34),pos2+1)
If pos3 > pos2
ltmp2 = Mid(ltmp,pos2+1,(pos3)-(pos2+1))
AddElement(prg())
prg() = ltmp2
EndIf
EndIf
EndIf
Wend
CloseFile(0)
Else
PrintN("Unable to read " + prg())
End
EndIf
Next
EndProcedure
OpenConsole()
argc = CountProgramParameters()
totproc = 0
If argc = 0
PrintN("Usage : pblc <filename>")
End
EndIf
For i = 0 To argc - 1
AddElement(prg())
prg() = ProgramParameter(i)
Next i
CheckInclude()
MakeUnique()
ResetList(prg())
If ListSize(prg()) > 1 ; several files (because we're using joker/wildcard "*" or "?")
ForEach prg()
totline + Get_NBLINE(prg())
Next
PrintN("------------")
PrintN(RSet(Str(totline),6," ") + " total("+Str(totproc)+")")
Else
FirstElement(prg())
Get_NBLINE(prg())
EndIf
CloseConsole()

Posted: Mon Jan 19, 2009 11:38 am
by Kwai chang caine
I don't understant why it's not works.
I have open the Console mode by "cmd"
I have compile your code in "PLBC.exe"
I have write "C:\PLBC *.pb" and even "c:\PLBC Essai.pb" but nothing better
Another black windows open one second aand disappears
Decidely 2009 it's not a good year for KCC

Posted: Mon Jan 19, 2009 1:38 pm
by flaith
Kwaï chang caïne wrote:I don't understant why it's not works.
I have open the Console mode by "cmd"
I have compile your code in "PLBC.exe"
I have write "C:\PLBC *.pb" and even "c:\PLBC Essai.pb" but nothing better
Another black windows open one second aand disappears
Decidely 2009 it's not a good year for KCC


you're right i forgot to tell, it's GNU/Linux only, here is a new version, for both Win & Linux
Code: Select all
; *****************************
; *** ***
; *** pblc - count line ***
; *** ***
; *****************************
;
; count effectives lines of PureBasic programs without remarks and empty lines
; and count number of procedures
; added : get include files
Global argc.i, thefile.s, totline.i, totproc.i
Global NewList prg.s()
Procedure.i Get_NBLINE(thefile.s)
Protected NewList line_prg.s(), tmp.s
Protected ligne.i = 0, proc.i = 0, i.i = 1
Protected rem.i = #False : car.i = #False
ClearList(Line_prg())
If ReadFile(0, thefile)
While Eof(0) = 0
tmp = ReadString(0)
AddElement(Line_prg())
line_prg() = tmp
Wend
CloseFile(0)
Else
PrintN("Unable to read " + thefile)
End
EndIf
ResetList(line_prg())
ForEach line_prg()
l = Len(line_prg())
rem = #False : car = #False : i = 1
While i <= l
a$ = Mid(line_prg(),i,1)
If a$ = Chr(32) : i + 1 : EndIf
If a$ = ";" And car = #False : i + 1 : rem = #True : EndIf
If a$ > Chr(32) And a$ < Chr(128) : car = #True : EndIf
i + 1
Wend
If car = #True And rem = #False : ligne + 1 : EndIf
If FindString(line_prg(),"EndProcedure",1) And rem = #False : proc + 1 : EndIf
Next
PrintN(RSet(Str(ligne),6," ") + " " + thefile + "("+Str(proc)+")")
totproc + proc
ProcedureReturn ligne
EndProcedure
Procedure MakeUnique()
Protected Dim prg_tmp.s(1)
Protected i.i = 0, l.i = 0
Protected tmp.s
l = ListSize(prg())
ReDim prg_tmp(l)
SortList(prg(),#PB_Sort_Ascending)
ResetList(prg())
ForEach prg()
prg_tmp(i) = prg()
i+1
Next
ClearList(prg())
i = 0
tmp = prg_tmp(i)
While i < l
If prg_tmp(i+1) <> tmp
AddElement(prg())
prg() = tmp
EndIf
i+1
tmp = prg_tmp(i)
Wend
EndProcedure
Procedure CheckInclude()
Protected pos.i, pos2.i, pos3.i
Protected ltmp.s, ltmp2.s
ResetList(prg())
ForEach prg()
If ReadFile(0, prg())
While Eof(0) = 0
ltmp = ReadString(0)
pos = FindString(ltmp,"IncludeFile",1)
If pos
pos2 = FindString(ltmp,Chr(34),pos+1)
If pos2 > pos
pos3 = FindString(ltmp,Chr(34),pos2+1)
If pos3 > pos2
ltmp2 = Mid(ltmp,pos2+1,(pos3)-(pos2+1))
AddElement(prg())
prg() = ltmp2
EndIf
EndIf
EndIf
Wend
CloseFile(0)
Else
PrintN("Unable to read " + prg())
End
EndIf
Next
EndProcedure
OpenConsole()
argc = CountProgramParameters()
totproc = 0
If argc = 0
PrintN("Usage : pblc <filename>")
End
EndIf
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
; from GBeebe
file.s = ProgramParameter()
dir = ExamineDirectory(#PB_Any, GetPathPart(file.s), GetFilePart(file.s))
While NextDirectoryEntry(dir)
AddElement(prg())
prg() = DirectoryEntryName(dir)
Wend
FinishDirectory(dir)
CompilerCase #PB_OS_Linux
For i = 0 To argc - 1
AddElement(prg())
prg() = ProgramParameter(i)
Next i
CompilerEndSelect
CheckInclude()
MakeUnique()
ResetList(prg())
If ListSize(prg()) > 1 ; several files (because we're using joker/wildcard "*" or "?")
ForEach prg()
totline + Get_NBLINE(prg())
Next
PrintN("------------")
PrintN(RSet(Str(totline),6," ") + " total("+Str(totproc)+")")
Else
FirstElement(prg())
Get_NBLINE(prg())
EndIf
CloseConsole()
Posted: Tue Jan 20, 2009 7:11 am
by kinglestat
Very nice

was in my todo list #1977893...er probably when I am 387 years old!
Posted: Tue Jan 20, 2009 7:16 am
by kinglestat
BUT
it does not count nested includefiles....
Posted: Tue Jan 20, 2009 10:05 am
by Kwai chang caine
you're right i forgot to tell, it's GNU/Linux only, here is a new version, for both Win & Linux
Thanks FLAITH
Aparently my PC is idle
He don't want count, i have the same result with your new code
Perhaps because i'm on W2000 :roll:
Or surely because, i'm a mule

Posted: Tue Jan 20, 2009 7:09 pm
by flaith
kinglestat wrote:BUT
it does not count nested includefiles....
Thanks, i'm adding it in my todos list
Kwaï chang caïne wrote:you're right i forgot to tell, it's GNU/Linux only, here is a new version, for both Win & Linux
Thanks FLAITH
Aparently my PC is idle
He don't want count, i have the same result with your new code
Perhaps because i'm on W2000 :roll:

sorry about that, could you tell me what you have done before running the code ? (did you opened a console via cmd.exe ? at the prompt, did you entered C:\>pblc *.pb ?)
Thanks for giving me those results
Kwaï chang caïne wrote:Or surely because, i'm a mule

of course you're not
Posted: Wed Jan 21, 2009 9:30 am
by Kwai chang caine
Thanks FLAITH, but sometimes, i have somes doubt
Everybody say to the creator of a code : "Yees great !!!! thanks for sharing ......." or "Great job XXXX"
And poor KCC is alone in the middle of his desktop with his "TV with keyboard and mouse" and he can't be happy because, when he clic.....then his "TV with keyboard and mouse" say "Biiiiinnnng !!!"
And his TV show a splendid board with a red cross
Well i have do all you have say to me.
1/ Copy your code
2/ Paste in IDE v4.30
3/ compile in "c:\pblc.exe"
4/ Open console of W2000 (Cmd)
5/ write c:\pblc *.pb and return
6/ It appear another windows console, and she close immediatly.
I can't have the time to read her
If i try another time, it's the same result , appear and close in one second.
Like if in PB there are not the loop of event :roll:
Excuse me to be a ball and chain
If you don't find how, it's not very important, because apparently just KCC have this problem.
KCC also, have want to see the result of your program
But don't loose your precious time for KCC

Posted: Wed Jan 21, 2009 1:50 pm
by gnozal
KCC : compile the source as CONSOLE executable (not as WINDOWS executable).
Posted: Wed Jan 21, 2009 2:35 pm
by flaith
gnozal wrote:KCC : compile the source as CONSOLE executable (not as WINDOWS executable).

......

....

..

Posted: Thu Jan 22, 2009 12:17 pm
by Kwai chang caine
gnozal wrote:KCC : compile the source as CONSOLE executable (not as WINDOWS executable).
Flaith have right........i'm not a mule

................i have a brain oyster
KCC is the future candidate for a brain transplant
Who know better KCC, that the big GNOZAL. :roll:
All is possible with KCC
I say to all, only KCC can find a bug where there not are
KCC is the terror of the hotline
KCC is the first who find a bug in a empty page of IDE
Is impossible to count, all the error that KCC can find, with a code who works fine
Thanks again GNOZAL you have win a new game with KCC
flaith wrote:gnozal wrote:KCC : compile the source as CONSOLE executable (not as WINDOWS executable).

......

....

..

Excuse me FLAITH, you must learn much years again in psychology for have a chance to save KCC
With your successive SMILEY "

......

....

..

", you make me laughting like a whale.
Now i'm the "MobyDick" of the forum
Thank you very much for your patience and your kindness.
And also thanks for your good job and for sharing
I wish you a very good day
Moby dick
Posted: Thu Jan 22, 2009 7:36 pm
by flaith

Thanks a lot KCC
Re: PBLC - Line Count
Posted: Mon May 06, 2013 4:22 pm
by MaxOhnesorg
Hello - I wanted to Check my project's code and so found this thread - But found the old code quite clumpsy.
New Features:
- Comments
- supports "includePath"- command
- Also "scan only file and its includes" - mode
- Finds ALL Folders & Subfolders
- Calcs Project COST.
Code: Select all
; *****************************
; *** ***
; *** pblc - count line ***
; *** ***
; *****************************
;
; count effectives lines of PureBasic programs without remarks and empty lines
; and count number of procedures
; added : get include files
; *****************************
; original from "flaith" - http://www.purebasic.fr/english/viewtopic.php?p=274560
; edited from Max Wolfgang Aigner - http://www.gemusoft.com
; DAte: 06.05.2013
; added - get all include files, find "includePath", use as openconsole()-software (windows)
; *****************************
; - Version 1.0.0
; *****************************
#Count_ProjectWise_InsteadOf_Folderwise = 0 ; change this to = 1 if you want to scan only the includes of a project and not the whole folder and subfolders.
Global argc.i, thefile.s
Global totline.i ; total amount of counting lines (only real code lines)
Global totproc.i
Global Zei_All_Lines_Count_File ; counts every single line -> to compare, how much lines you have wasted.
Global Zei_All_Lines_Count_ALL ; all lines alltogether
Global NewList prg.s()
Procedure.i Get_NBLINE(thefile.s) ; count all real-code-files in the prg.s() list
Protected NewList line_prg.s(), tmp.s
Protected ligne.i = 0, proc.i = 0, i.i = 1
Protected rem.i = #False : car.i = #False
Zei_All_Lines_Count_File =0
ClearList(line_prg())
; load the whole file into the list line_prg()
If ReadFile(0, thefile)
While Eof(0) = 0
tmp = ReadString(0)
AddElement(line_prg())
line_prg() = tmp
Wend
CloseFile(0)
Else
PrintN("Unable to read " + thefile)
Input()
End
EndIf
ResetList(line_prg())
ForEach line_prg()
l = Len(line_prg())
rem = #False : car = #False : i = 1
While i <= l ; parse every single character i to L
a$ = Mid(line_prg(),i,1)
If a$ = Chr(32) : i + 1 : EndIf ; char is empty -> spacebar-char. -> skip it
If a$ = ";" And car = #False : i + 1 : rem = #True : EndIf ; first char is a semicolon -> comment
If a$ > Chr(32) And a$ < Chr(128) : car = #True : EndIf ; first sign is a letter -> Command -> counts!
i + 1
Wend
If car = #True And rem = #False : ligne + 1 : EndIf
Zei_All_Lines_Count_File + 1 ; counts every single line -> no if needed.
If FindString(line_prg(),"EndProcedure",1) And rem = #False : proc + 1 : EndIf
Next
PrintN ( RSet(Str(ligne),6," ") + " " + thefile + "("+Str(proc)+")")
totproc + proc
Zei_All_Lines_Count_ALL + Zei_All_Lines_Count_File
ProcedureReturn ligne
EndProcedure
Procedure MakeUnique()
Protected Dim prg_tmp.s(1)
Protected i.i = 0, l.i = 0
Protected tmp.s
l = ListSize(prg())
ReDim prg_tmp(l)
SortList(prg(),#PB_Sort_Ascending)
ResetList(prg())
ForEach prg()
prg_tmp(i) = prg()
i+1
Next
ClearList(prg())
i = 0
tmp = prg_tmp(i)
While i < l
If prg_tmp(i+1) <> tmp
AddElement(prg())
prg() = tmp
EndIf
i+1
tmp = prg_tmp(i)
Wend
EndProcedure
Procedure CheckInclude() ; Searches through every file in prg() list -> extracts "include" and "includepath" -> adds files to prg() list.
Protected pos.i, pos2.i, pos3.i
Protected ltmp.s, ltmp2.s
Static pfad_IncludePath.s
ResetList(prg())
ForEach prg()
PrintN ( "process: " + prg())
If ReadFile(0, prg())
While Eof(0) = 0
ltmp = ReadString(0)
pos = FindString(ltmp,"IncludeFile",1)
If pos
pos2 = FindString(ltmp,Chr(34),pos+1)
If pos2 > pos
pos3 = FindString(ltmp,Chr(34),pos2+1)
If pos3 > pos2
ltmp2 = Mid(ltmp,pos2+1,(pos3)-(pos2+1))
AddElement(prg())
prg() = pfad_IncludePath + "\" + ltmp2
PrintN ( "found include: " + prg())
PreviousElement ( prg())
EndIf
EndIf
Else
;
pos_semikolon = FindString ( ltmp , ";" ,1)
pos = FindString(ltmp,"IncludePath",1)
If (pos < pos_semikolon And pos > 0) Or (pos_semikolon = 0 And pos > 0); wenns nicht auskommentiert wurde
pos2 = FindString(ltmp,Chr(34),pos+1)
If pos2 > pos
pos3 = FindString(ltmp,Chr(34),pos2+1)
If pos3 > pos2
pfad_IncludePath = Mid(ltmp,pos2+1,(pos3)-(pos2+1))
Debug "includepath found: " + pfad_IncludePath
EndIf
EndIf
EndIf
EndIf
Wend
CloseFile(0)
Else
PrintN("Unable to read " + prg())
Continue
EndIf
Next
EndProcedure
Procedure CheckFolder ( path.s) ; Checks every source-file in a folder and every subfolder
PrintN ( "CheckFolder: " + path)
dir = ExamineDirectory ( #PB_Any, path, "*.*") ; change pattern at line 151 -> where "if ext =".. stands. this has to be *.*, to find the folders
If IsDirectory ( dir ) > 0
While NextDirectoryEntry ( dir )
If DirectoryEntryType ( dir ) = #PB_DirectoryEntry_Directory
If Not ( DirectoryEntryName (dir) = "." Or DirectoryEntryName (dir) = ".." ) ; filter out the standard-directorys that we dont want to use..
checkfolder ( path + DirectoryEntryName (dir) + "\")
Else
Debug path + DirectoryEntryName (dir) + "\"
EndIf
ElseIf DirectoryEntryType ( dir ) = #PB_DirectoryEntry_File
ext.s = GetExtensionPart ( DirectoryEntryName ( dir ))
If ext = "pb" Or ext = "pbi"
AddElement ( prg())
prg ( ) = path +DirectoryEntryName(dir)
PrintN ( "File Found : " + DirectoryEntryName(dir))
EndIf
Else
EndIf
Wend
FinishDirectory ( dir )
Else
PrintN ( "ERROR - Can't initialize the directory. maybe you don't have enough rights")
EndIf
EndProcedure
Debug "hallo"
OpenConsole ()
CompilerIf #Count_ProjectWise_InsteadOf_Folderwise = 1 ; check only the Project-main-file by entering its name and its includes.
totproc = 0
; from GBeebe
file.s = Input()
SetCurrentDirectory ( GetPathPart ( file ))
SetClipboardText ( file )
dir = ExamineDirectory(#PB_Any, GetPathPart(file.s), GetFilePart(file.s))
While NextDirectoryEntry(dir)
AddElement(prg())
prg() = DirectoryEntryName(dir)
Debug "dir found: " + DirectoryEntryName(dir)
Wend
FinishDirectory(dir)
CheckInclude()
CompilerElse ; check the whole folder
PrintN ( "-----------------------------------------------------")
PrintN ( " Welcome in LoC-Counter-PB. Initiated by 'Flaith' ,")
PrintN ( " Edited And Enhanced by 'MaxOhnesorg - Max Wolfgang Aigner'.")
PrintN ( " Counts the Lines of Code (LoC) of your project. " + Chr(10) + "Copy this file to your Projectfolder and run it.")
PrintN ( "-----------------------------------------------------")
PrintN ( "")
CheckFolder ( GetPathPart (ProgramFilename ()))
CompilerEndIf
MakeUnique()
ResetList(prg())
If ListSize(prg()) > 1 ; several files (because we're using joker/wildcard "*" or "?")
ForEach prg()
totline + Get_NBLINE(prg())
Next
PrintN("------------")
PrintN(RSet(Str(totline),6," ") + " total("+Str(totproc)+")")
Else
If FirstElement(prg()) ; if there is any element.
lastcount = Get_NBLINE(prg())
EndIf
EndIf
PrintN(RSet(Str(Zei_All_Lines_Count_ALL),6) + " <- All Lines, even the blank ones in Code ")
If totline > 0
PrintN(RSet(Str(totline/Zei_All_Lines_Count_ALL * 100),6) + " % RealLines/AllLines Ratio")
PrintN(RSet(Str(totline * 5 ),6) + " EUR Production Cost (Standard Salery - 50 EUR per 10 LoC)") ; works better, the bigger your program is -> because of the planning and debugging - phasis, which a small program doesn't have that much.
Else
PrintN(RSet(Str(lastcount * 5 ),6) + " EUR Production Cost (Standard Salery - 50 EUR per 10 LoC)")
EndIf
PrintN ( Chr(10) + "Goodbye - Auf Wiedersehen" )
Input()
[Edit] Also supports Cost-Calculation now[/Edit]
-> Code can scan your whole Project Directory