Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6] [Fix] Read stats files without crashing on encoding #8

Merged
merged 1 commit into from
Sep 26, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/code_metrics/statistics_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def add(code_metrics_calculator)
end

def add_by_file_path(file_path)
File.open(file_path) do |f|
file_flags = 'r:UTF-8'
file_flags << ':UTF-8' unless defined?(Encoding) && Encoding.default_internal == Encoding::UTF_8
File.open(file_path, file_flags) do |f|
self.add_by_io(f, file_type(file_path))
end
end
Expand All @@ -51,6 +53,7 @@ def add_by_io(io, file_type)
comment_started = false

while line = io.gets
line.encode!('UTF-8', 'UTF-8', :invalid => :replace) if line.respond_to?(:encode!)
@lines += 1

if comment_started
Expand Down