Sha256: eb1d2a9faba96f42c92647643c18dd9411b2dfc870fc33626199665ed1a04e8a

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module RdbCSVReader
  class Reader
    def initialize(fp, db, delimiter, options)
      @fp = fp
      @db = db
      @delimiter = delimiter
      @escape = options[:escape] || "\\" # 1 char
      @linefeed = options[:linefeed] || "\n"
      @quote = options[:quote] || '"'
    end

    def each_line
      str = ''
      row = []
      quote_num = 0

      each_char do |char|
        if quote_num > 0
          if str[-1] == @quote && char == @quote
            quote_num -= 1
          elsif str[-1] != @quote && char == @quote
            quote_num -= 1
          elsif char == @quote
            quote_num += 1
          end
          str += char
        else
          if char == @delimiter
            row << str
            str = ''
          elsif char == @linefeed
            row << str
            yield RdbCSV::CSV::Row.new(row)
            row = []
            str = ''
          else
            quote_num += 1 if (str.size == 0 || str[0] == @quote) && char == @quote
            str += char
          end
        end
      end
    end

    private

    def each_char
      str = ''
      @fp.each_char do |char|
        if str[-1] == @escape
          str += char
          yield str
          str = ''
          next
        end

        str += char

        if str[@linefeed.size-1..-1] == @linefeed
          yield str
          str = ''
          next
        end

        if str[-1] != @escape
          yield str
          str = ''
        end
      end

      yield str unless str.empty?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rdb_csv-0.4.1 lib/rdb_csv/reader.rb
rdb_csv-0.4.0 lib/rdb_csv/reader.rb