lib/rio/ext/csv.rb in rio-0.3.7 vs lib/rio/ext/csv.rb in rio-0.3.8

- old
+ new

@@ -1,8 +1,8 @@ #-- # =============================================================================== -# Copyright (c) 2005, Christopher Kleckner +# Copyright (c) 2005, 2006 Christopher Kleckner # All rights reserved # # This file is part of the Rio library for ruby. # # Rio is free software; you can redistribute it and/or modify @@ -20,11 +20,11 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # =============================================================================== #++ # # To create the documentation for Rio run the command -# rake rdoc +# ruby build_doc.rb # from the distribution directory. Then point your browser at the 'doc/rdoc' directory. # # Suggested Reading # * RIO::Doc::SYNOPSIS # * RIO::Doc::INTRO @@ -33,13 +33,32 @@ # # <b>Rio is pre-alpha software. # The documented interface and behavior is subject to change without notice.</b> +# begin +# require 'faster_csv' # first choice--for speed + +# # A CSV compatible interface for FasterCSV. +# module CSV # :nodoc: +# def self.parse_line( line, field_sep=nil, row_sep=nil ) +# FasterCSV.parse_line( line, :col_sep => field_sep || ",", +# :row_sep => row_sep || :auto ) +# end + +# def self.generate_line( array, field_sep=nil, row_sep=nil ) +# FasterCSV.generate_line( array, :col_sep => field_sep || ",", +# :row_sep => row_sep || "" ) +# end +# end +# rescue LoadError +# require 'csv' # second choice--slower but standard +# end + require 'csv' -require 'rio/record' +$EXTEND_CSV_RESULTS = false module RIO module Ext module CSV module Cx def csv(fs=',',rs=nil,&block) @@ -90,25 +109,46 @@ def to_a() @csv_s_to_rec.call(self) end end end + end +end +module RIO + module Ext module CSV module Input protected +# def ior() +# p cx['stream_itertype'] +# case cx['stream_itertype'] +# when 'lines',nil +# self.ioh.iostack[-2] +# else +# self.ioh +# end +# end +# def each_rec_(&block) +# self.ior.each { |line| +# yield line +# } +# self +# end def to_rec_(raw_rec) #_init_cols_from_line(raw_rec) if @recno == 0 #p "#{callstr('to_rec_',raw_rec,@recno)} ; itertype=#{cx['stream_itertype']}" case cx['stream_itertype'] when 'lines' - unless copying_from? - raw_rec.extend(RIO::Ext::CSV::Str) - raw_rec.csv_s_to_rec = _s_to_rec_proc(cx['csv_fs'],cx['csv_rs']) + if $EXTEND_CSV_RESULTS + unless copying_from? + raw_rec.extend(RIO::Ext::CSV::Str) + raw_rec.csv_s_to_rec = _s_to_rec_proc(cx['csv_fs'],cx['csv_rs']) + end end raw_rec when 'records' _l2record(raw_rec,cx['csv_fs'],cx['csv_rs']) when 'rows' @@ -152,18 +192,23 @@ cols.each do |i| tfields << fields[i] end tfields end - def _l2a(line,fs,rs) + def parse_line_(line,fs,rs) ::CSV.parse_line(line,fs,rs) end + def _l2a(line,fs,rs) + parse_line_(line,fs,rs) + end def _l2record(line,fs,rs) #p callstr('_l2record',line,fs,rs,cols) - fields = trim(::CSV.parse_line(line,fs,rs)) - unless copying_from? - fields.extend(RIO::Ext::CSV::Ary) - fields.csv_rec_to_s = _rec_to_s_proc(fs,rs) + fields = trim(parse_line_(line,fs,rs)) + if $EXTEND_CSV_RESULTS + unless copying_from? + fields.extend(RIO::Ext::CSV::Ary) + fields.csv_rec_to_s = _rec_to_s_proc(fs,rs) + end end fields end def cnames(num) @cnames ||= trim((0...num).map { |n| "Col#{n}" })