Page 1 of 2

Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 7:42 pm
by Nituvious
How can I view how many lines of code my program has? For example if I am building a project that spans several include files?

Re: Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 8:56 pm
by ts-soft
Click on details when compiling!
But the project must be large to see the lines :wink:

Re: Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 9:29 pm
by Trond
If you put all your code in one folder (with subfolders) you can use this code:

Code: Select all

Procedure ExamineDirectoryRecursive(Path.s, Pattern.s, List _List.s())
  Protected Dir = ExamineDirectory(#PB_Any, Path, Pattern)
  If Dir
    While NextDirectoryEntry(Dir)
      AddElement(_List())
      _List() = Path + DirectoryEntryName(Dir)
    Wend
    FinishDirectory(Dir)
  EndIf
  Dir = ExamineDirectory(#PB_Any, Path, "")
  If Dir
    While NextDirectoryEntry(Dir)
      If DirectoryEntryType(Dir) = #PB_DirectoryEntry_Directory
        If DirectoryEntryName(Dir) <> "." And DirectoryEntryName(Dir) <> ".." And FindString(DirectoryEntryName(Dir), "Examples", 0) = 0
          ExamineDirectoryRecursive(Path + DirectoryEntryName(Dir) + "\", Pattern, _List())
        EndIf
      EndIf
    Wend
    FinishDirectory(Dir)
  EndIf
EndProcedure

Dir.s = "C:\project\" ; use final \
NewList _List.s()
ExamineDirectoryRecursive(Dir, "*.pp", _List())

I = 0
ForEach _List()
  ;Debug _List()
  ReadFile(0, _List())
  N = I
  While Not Eof(0)
    I + 1
    S.s = ReadString(0)
    If Trim(s) = ""
      Empty + 1
    EndIf
  Wend
  Debug LSet(Str(I-N), 5) + _list() ;ReplaceString(_List(), Dir, "")
Next
Debug "Total lines: " + Str(I)
Debug "Non-empty lines: " + Str(I - Empty)
Debug "Empty lines: " + Str(Empty)

Re: Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 10:38 pm
by Nituvious
:shock:

You mean to tell me there isn't a magic button I can press to get it? I guess writing a compiler tool for this would be an excellent learning experiment. :?

Re: Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 11:11 pm
by Fluid Byte
What's the point of counting the lines? Wanna' show off?

Re: Easy way to count total lines of code?

Posted: Wed Oct 06, 2010 11:22 pm
by Nituvious
Fluid Byte wrote:What's the point of counting the lines? Wanna' show off?
Curiosity mainly. And trimming redundant code. And it would just be nice to know.

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 7:52 am
by Rings
Nituvious wrote:
Fluid Byte wrote:What's the point of counting the lines? Wanna' show off?
Curiosity mainly. And trimming redundant code. And it would just be nice to know.
could it possible that you use the DEMO version
and wanna avoid the Restrictions ?

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 7:59 am
by blueznl
Would it matter? :-)

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 10:51 am
by Trond
Type "pbcompiler yourfile.pb" and it will tell you the number of lines processed. If you include some file multiple times it will get counted multiple times, though.

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 11:42 am
by Nituvious
Rings wrote:could it possible that you use the DEMO version
and wanna avoid the Restrictions ?
Yes I use the demo version but it's not because I want to avoid the restriction. I haven't ever gotten close to the restriction before. I am simply curious how many lines of code I put into a project, that's all.

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 3:59 pm
by Fluid Byte
:lol:

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 9:53 pm
by PB
There's nothing illegal about reducing the number of lines anyway.

Re: Easy way to count total lines of code?

Posted: Thu Oct 07, 2010 11:29 pm
by Nituvious
PB wrote:There's nothing illegal about reducing the number of lines anyway.
Thank you! :?

Re: Easy way to count total lines of code?

Posted: Fri Oct 08, 2010 1:18 pm
by Kwai chang caine
It's not for a project...but if that can help you ...
http://www.purebasic.fr/english/viewtop ... 60#p274560

Re: Easy way to count total lines of code?

Posted: Sat Oct 09, 2010 12:53 am
by Vera
Hi Trond,

thanks for your edifying snippet :)

here are the actual results of the pb\example\sources codes-lines from the Linux edition ;)
Total lines: 5644
Non-empty lines: 4266
Empty lines: 1378
cheers ~ Vera