Codebase list ohcount / run/8fccc97f-1091-4606-8a80-1b8fb386766a/main ruby / gestalt / rules / c_header_rule.rb
run/8fccc97f-1091-4606-8a80-1b8fb386766a/main

Tree @run/8fccc97f-1091-4606-8a80-1b8fb386766a/main (Download .tar.gz)

c_header_rule.rb @run/8fccc97f-1091-4606-8a80-1b8fb386766a/mainraw · history · blame

module Ohcount
	module Gestalt
		# Triggers if a c or cpp header is present
		class CHeaderRule < FileRule
			attr_reader :headers

			def initialize(*args)
				options = args.pop if args.last.is_a?(Hash)
				@headers = args
				super(options)
			end

      def process_source_file(source_file)
				return unless ['c','cpp'].include?(source_file.polyglot)

        ['c','cpp'].each do |lang|
          next unless source_file.language_breakdown(lang)
          md = regexp.match(source_file.language_breakdown(lang).code)
          @count += (md && md.size).to_i
        end
      end

			def regexp
				@regexp ||= begin
					header_term = "(" + headers.join("|") + ")"
					Regexp.new("include\s+['<\"]#{ header_term }[\">']", Regexp::IGNORECASE)
				end
			end
		end
	end
end