Handy line counter

User projects written in or related to FreeBASIC.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Handy line counter

Postby pestery » May 01, 2011 9:52

Hi all. May I present a handy line counter for freebasic files. It will count the number of lines in any .bas and .bi file it finds in the current directory. It also recursively enter sub folders and checks them too.

Code: Select all

#Include "dir.bi"

Dim Shared As UInteger total_code = 0, total_comments = 0, total_blank = 0, total_total = 0

Declare Sub count_lines(ByRef filename As String)
Declare Sub find_files(ByVal folder_level As Integer = 0)

Print
find_files()
Color 7
Print
Print "Total lines of code:     " & total_code
Print "Total lines of comments: " & total_comments
Print "Total blank lines:       " & total_blank
Print "Total lines:             " & total_total
Sleep
End

Sub count_lines(ByRef filename As String)
   Dim As UInteger code = 0, comments = 0, blank = 0, total = 0
   Dim As Integer fn = FreeFile
   If Open(filename For Input As #fn) = 0 Then
      Dim As String text
      Dim As Integer in_text
      Dim As Integer in_comment
      Dim As Integer code_line
      Dim As Integer i, imax
      While Not Eof(fn)
         Line Input #fn, text
         text = Trim(text, Any Chr(9, 32))
         If text = "" Then ' Blank line
            blank += 1
            total_blank += 1
            total += 1
            total_total += 1
         Else
            imax = Len(text) - 1
            If in_comment Then code_line = 0 Else code_line = 1
            For i = 0 To imax
               If text[i] = 34 Then ' Quote
                  If in_comment = 0 Then in_text Xor= 1
               ElseIf text[i] = Asc("/") Then ' Open comment
                  If i < imax Then
                     If text[i + 1] = Asc("'") Then
                        If in_text = 0 Then
                           If i = 0 Then code_line = 0
                           in_comment += 1
                           i += 1
                           Continue For
                        EndIf
                     EndIf
                  EndIf
               ElseIf text[i] = Asc("'") Then ' Close comment
                  If i < imax Then
                     If text[i + 1] = Asc("/") Then
                        If in_text = 0 Then
                           If in_comment > 0 Then
                              in_comment -= 1
                              i += 1
                              If i < imax Then code_line = 1
                              Continue For
                           Else ' End-of-line comment
                              Exit For
                           EndIf
                        EndIf
                     ElseIf i = 0 Then ' End-of-line comment
                        code_line = 0
                     EndIf
                  ElseIf i = 0 Then ' End-of-line comment
                     code_line = 0
                  EndIf
               EndIf
            Next
            If code_line Then
               code += 1
               total_code += 1
            Else
               comments += 1
               total_comments += 1
            EndIf
            total += 1
            total_total += 1
         EndIf
      Wend
   EndIf
   Close #fn
   Print Space(42 - Pos) & " ... " & code & " code, " & comments & " comments, " & total & " total"
End Sub

Sub find_files(ByVal folder_level As Integer = 0)
   Dim As String filename, name_backup
   
   ' Error check
   If folder_level > 100 Then
      Print
      Print "An error may have occured. Now ending"
      Sleep
      End
   EndIf
   
   ' Find .bi files
   Color 2
   filename = Dir("*.bi")
   While filename <> ""
      Print Space(folder_level * 2) & "Reading file " & filename;
      count_lines(filename)
      filename = Dir
   Wend
   
   ' Find .bas files
   Color 2
   filename = Dir("*.bas")
   While filename <> ""
      Print Space(folder_level * 2) & "Reading file " & filename;
      count_lines(filename)
      filename = Dir
   Wend
   
   ' Find folders
   Color 5
   filename = Dir("*", fbDirectory)
   While filename <> ""
      If filename = "." Then
      ElseIf filename = ".." Then
      Else
         Print Space(folder_level * 2) & "Checking folder " & filename
         name_backup = filename
         If ChDir(filename) = 0 Then
            find_files(folder_level + 1)
            ChDir ".."
            filename = Dir("*", fbDirectory)
            While (filename <> "") And (filename <> name_backup)
               filename = Dir
            Wend
         Else
            Print "ERROR: Could not enter folder " & filename
         EndIf
      EndIf
      filename = Dir
   Wend
   
End Sub

After writing this I noticed there are a couple of other line counters from 2005 and 2006, but I like my one better anyway :P
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » May 01, 2011 18:13

I will use your post to shamelessly bump my own counter! :)

http://www.freebasic.net/forum/viewtopi ... lines+code

-Vince
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Postby Gonzo » May 01, 2011 19:41

Total lines of code: 15841
seems to work :)
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Postby pestery » May 02, 2011 2:15

No worries Vince, glad to help ;)

For my current project, which is mostly just a gui and the moment, Total lines of code: 10901
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » May 02, 2011 2:35

Code: Select all

Total lines of code:     349639
Total lines of comments: 42221
Total blank lines:       59997
Total lines:             451857


Do I win? :P
(I think that's all my code, except maybe chisock and multiput / rotozoom are in there, but it will count some projects multiple times because I have code backups.)
Mihail_B
Posts: 271
Joined: Jan 29, 2008 11:20
Location: Romania
Contact:

What about mine ? :)

Postby Mihail_B » May 02, 2011 13:38

Total lines of code: 133633
Total lines of comments: 12695
Total blank lines: 7263
Total lines: 153591

Ahh .... but just wait and see :)

Return to “Projects”

Who is online

Users browsing this forum: Google [Bot] and 11 guests