Skip to content

Commit

Permalink
Merge pull request #8 from bf4/handle_encoding
Browse files Browse the repository at this point in the history
[#6] [Fix] Read stats files without crashing on encoding
  • Loading branch information
bf4 committed Sep 26, 2013
2 parents 2bf0bc3 + 45f0f6d commit 501c14f
Showing 1 changed file with 4 additions and 1 deletion.
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

0 comments on commit 501c14f

Please sign in to comment.