C++ Line Counting in Ruby
keywords = %w(case do else enum for if public private protected
class struct union switch while try catch)
punct = /(;|,)/
lines = 0
empty = 0
comments = 0
prepro = 0
kwd = 0
statements = 0
while line = gets()
lines += 1
if line =~ /^$/
empty += 1
next
end
if line =~ /^\s*\/\//
comments += 1
next
end
if line =~ /^#/
prepro += 1
next
end
line.scan(/\w+/) do |word|
kwd += 1 if keywords.include?(word)
end
line.scan(punct) do |p|
statements += 1
end
end
puts "Lines: #{lines}"
puts "Blank lines: #{empty}"
puts "Comments(C++ style): #{comments}"
puts "Preprocessor: #{prepro}"
puts "Keywords: #{kwd}"
puts "Statements: #{statements}"
sloc = statements + kwd + prepro puts "SLOC: #{sloc}"