Skip to content
Browse files

Merge branch 'alexis'

  • Loading branch information...
2 parents e7c7324 + 8bd559f commit 0182b3f32d9911d7a9d41acfd22223958f473840 @JEG2 committed Jun 2, 2011
Showing with 30 additions and 38 deletions.
  1. +30 −38 lib/faster_csv.rb
View
68 lib/faster_csv.rb
@@ -1318,9 +1318,7 @@ def self.table(path, options = Hash.new)
# even if it occurs in a quoted field,
# assuming that you would have the same
# line endings there. If none of those
- # sequences is found, +data+ is
- # <tt>ARGF</tt>, <tt>STDIN</tt>,
- # <tt>STDOUT</tt>, or <tt>STDERR</tt>,
+ # sequences is found,
# or the stream is only available for
# output, the default
# <tt>$INPUT_RECORD_SEPARATOR</tt>
@@ -1745,43 +1743,37 @@ def init_separators(options)
# automatically discover row separator when requested
if @row_sep == :auto
- if [ARGF, STDIN, STDOUT, STDERR].include?(@io) or
- (defined?(Zlib) and @io.class == Zlib::GzipWriter)
- @row_sep = $INPUT_RECORD_SEPARATOR
- else
- begin
- saved_pos = @io.pos # remember where we were
- while @row_sep == :auto
- #
- # if we run out of data, it's probably a single line
- # (use a sensible default)
- #
- if @io.eof?
- @row_sep = $INPUT_RECORD_SEPARATOR
- break
- end
-
- # read ahead a bit
- sample = @io.read(1024)
- sample += @io.read(1) if sample[-1..-1] == "\r" and not @io.eof?
-
- # try to find a standard separator
- if sample =~ /\r\n?|\n/
- @row_sep = $&
- break
- end
- end
- # tricky seek() clone to work around GzipReader's lack of seek()
- @io.rewind
- # reset back to the remembered position
- while saved_pos > 1024 # avoid loading a lot of data into memory
- @io.read(1024)
- saved_pos -= 1024
+ begin
+ saved_pos = @io.pos # remember where we were (@io.pos will raise an axception if @io is pipe or not opened for reading)
+ while @row_sep == :auto
+ break if @io.eof? # if we run out of data, it's probably a single line - ensure block will set default value
+
+ # read ahead a bit
+ sample = @io.read(1024)
+ sample += @io.read(1) if sample[-1..-1] == "\r" and not @io.eof?
+
+ # try to find a standard separator
+ if sample =~ /\r\n?|\n/
+ @row_sep = $&
+ break
end
- @io.read(saved_pos) if saved_pos.nonzero?
- rescue IOError # stream not opened for reading
- @row_sep = $INPUT_RECORD_SEPARATOR
end
+
+ # tricky seek() clone to work around GzipReader's lack of seek()
+ @io.rewind
+ # reset back to the remembered position
+ while saved_pos > 1024 # avoid loading a lot of data into memory
+ @io.read(1024)
+ saved_pos -= 1024
+ end
+ @io.read(saved_pos) if saved_pos.nonzero?
+
+ rescue IOError # not opened for reading
+ rescue NoMethodError # Zlib::GzipWriter streem doesn't have eof? method
+ rescue SystemCallError # pipe
+
+ ensure # stream not opened for reading, or pipe, or single-line data
+ @row_sep = $INPUT_RECORD_SEPARATOR if @row_sep == :auto
end
end

0 comments on commit 0182b3f

Please sign in to comment.
Something went wrong with that request. Please try again.