QB64 Phoenix Edition
Program line counter - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Program line counter (/showthread.php?tid=1500)



Program line counter - johannhowitzer - 02-25-2023

Very simply, this little program will count the lines in your code and output the results
to a file.  You specify the code file and output file, then it scans and counts.  It ignores
whitespace and comments, and gives a line count for each sub and function, as well as a total
for the whole program.

Not terribly useful, but gives more exact and detailed info than just copypasting your code
into something like pastebin.



Code: (Select All)
$noprefix

const true = -1
const false = 0

screen 12
cls , 15
color 0, 15

do
  input "Code file: ", f1$
loop until fileexists(f1$) = true
do
  input "Output file: ", f2$
loop until fileexists(f2$) = false

open f1$ for input as #1
line_count = 0
dim l$(line_count)
do until eof(1)
  line_count = line_count + 1
  redim preserve l$(line_count)
  line input #1, l$(line_count)
loop
close #1

total_lines = 0
sub_count = 0
for n = 1 to line_count
  if processed_left$(l$(n), 4) = "sub " or processed_left$(l$(n), 9) = "function " then sub_count = sub_count + 1
  if processed_left$(l$(n), 1) = "'" or trim$(l$(n)) = "" then continue
  total_lines = total_lines + 1
next n

dim sub_lines(sub_count)
dim sub_name$(sub_count)
sub_name$(0) = "[Main]"

current_sub = 0
for n = 1 to line_count
  if processed_left$(l$(n), 1) = "'" or trim$(l$(n)) = "" then continue
  if processed_left$(l$(n), 4) = "sub " or processed_left$(l$(n), 9) = "function " then
      current_sub = current_sub + 1
      sub_name$(current_sub) = before$(ltrim$(lcase$(l$(n))), "(")
  end if
  sub_lines(current_sub) = sub_lines(current_sub) + 1
next n

print
total_test = 0
open f2$ for output as #1
for n = 0 to sub_count
  double_print sub_name$(n) + ":" + str$(sub_lines(n))
  total_test = total_test + sub_lines(n)
next n
double_print ""
double_print "Total by count:" + str$(total_lines)
double_print "Total by sum:" + str$(total_test)
close #1



function processed_left$(t$, c)
processed_left$ = left$(ltrim$(lcase$(t$)), c)
end function


sub double_print(t$)
print t$
print #1, t$
end sub


function before$(t$, c$)
p = instr(t$, c$)
if p = false then p = len(t$) + 1
before$ = left$(t$, p - 1)
end function