1. #! /bin/sh
  2. # example: find ./ -name "*.[ch]pp" -exec cat {} \; | sh ./code-stat.sh
  3. set -e
  4. bc --version >/dev/null
  5. awk --version >/dev/null
  6. file=$(mktemp)
  7. cat > $file
  8. without_spaces() { grep -v "^[ ]*$"; }
  9. without_brackets() { grep -v "^ *[()] *$" | grep -v "^ *); *$" | grep -v "^ *[}{] *$" | grep -v "^ *}; *$"; }
  10. without_directives() { grep -v "^ *#"; }
  11. structures() { grep "struct \| class" | grep -v ";"; }
  12. all_size=$(cat $file | wc -l)
  13. without_spaces_size=$(cat $file | without_spaces | wc -l)
  14. without_brackets_size=$(cat $file | without_brackets | wc -l)
  15. without_directives_size=$(cat $file | without_directives | wc -l)
  16. structures_size=$(cat $file | structures | wc -l)
  17. pure_code_size=$(cat $file | without_spaces | without_brackets | without_directives | wc -l;)
  18. echo -n "Empty lines: "
  19. echo "($all_size - $without_spaces_size) / $all_size" | bc -l
  20. echo -n "Empty lines with brackets: "
  21. echo "($all_size - $without_brackets_size) / $all_size" | bc -l
  22. echo -n "Preprocessor directives: "
  23. echo "($all_size - $without_directives_size) / $all_size" | bc -l
  24. echo -n "Lines per classes: "
  25. echo "$pure_code_size / $structures_size" | bc -l
  26. pure_code_chars=$(cat $file | without_spaces | without_brackets | without_directives | wc -m)
  27. echo -n "Lines average length: "
  28. echo "$pure_code_chars / $pure_code_size" | bc -l
  29. rm $file