Skip to content

Commit

Permalink
Add C++ loc script (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamaguchi1024 authored Oct 28, 2024
1 parent 055cd02 commit c0fd826
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions analytics_tools/loc/c_loc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import os

def count_lines_in_file(filename):
with open(filename, 'r', encoding='utf-8', errors='ignore') as file:
in_multiline_comment = False
code_lines = 0

for line in file:
stripped_line = line.strip()

if in_multiline_comment:
if '*/' in stripped_line:
# End of multi-line comment
in_multiline_comment = False
stripped_line = stripped_line.split('*/', 1)[1]
else:
continue # Skip everything inside multi-line comments

# Remove single-line comments
if '//' in stripped_line:
stripped_line = stripped_line.split('//', 1)[0]

# Check for the start of a multi-line comment
if '/*' in stripped_line:
if '*/' in stripped_line:
# Multi-line comment starts and ends on the same line
before_comment = stripped_line.split('/*', 1)[0]
after_comment = stripped_line.split('*/', 1)[1]
stripped_line = before_comment + after_comment
else:
# Multi-line comment starts here and continues
stripped_line = stripped_line.split('/*', 1)[0]
in_multiline_comment = True

# Count the line if it's not empty after stripping comments and whitespace
if stripped_line.strip():
code_lines += 1

return code_lines

def main():
total_lines = 0

for root, _, files in os.walk('.'):
for filename in files:
if filename.endswith('.c'):
filepath = os.path.join(root, filename)
lines = count_lines_in_file(filepath)
print(f"{filepath}: {lines} lines")
total_lines += lines

print(f"Total lines of code: {total_lines}")

if __name__ == "__main__":
main()

0 comments on commit c0fd826

Please sign in to comment.