class CoSVON

CoSVON: Comma Separated Value Object Notation (yokohamarb)

nabetani.sakura.ne.jp/yokohamarb/2014.01.cosvon/

Constants

HSALSKCAB
VERSION

VERSION string

Public Class Methods

csv(s,__opt=Hash.new) click to toggle source

parses csv string into 2D array. quoted commas/LFs and escaped quotations are supported.

# File lib/cosvon.rb, line 14
def self.csv(s,__opt=Hash.new)
        opt={:col_sep=>',',:quote_char=>'"'}.merge(__opt)
        csv=[]
        line=[]
        quoted=false
        quote=false
        backslash=0
        cur=''
        linebreak=nil
        s.each_char{|c|
                #if c=="\r" #ignore CR
                if c==opt[:quote_char]
                        if !quoted #start of quote
                                quoted=true
                        elsif !quote #end of quote? Let's determine using next char
                                quote=true
                                if backslash==1
                                        backslash=2
                                end
                        else #escape rather than end of quote
                                quote=false
                                cur<<opt[:quote_char]
                                if backslash==2
                                        backslash=0
                                end
                        end
                else
                        if quote
                                quote=false
                                if backslash==2
                                        cur<<opt[:quote_char]
                                else
                                        quoted=false
                                end
                        end
                        if (c=="\n"||c=="\r")&&!quoted
                                if !linebreak||linebreak==c
                                        line<<cur
                                        cur=''
                                        csv<<line
                                        line=[]
                                        linebreak=c
                                end
                        elsif c==opt[:col_sep]&&!quoted
                                line<<cur
                                cur=''
                        else
                                backslash=0
                                if HSALSKCAB.include?(c)
                                        backslash=1
                                end
                                quote=false
                                cur<<c
                        end
                end
        }
        line<<cur if !cur.empty?
        csv<<line if !line.empty?
        csv
end
generate(h) click to toggle source

generates CoSVON string from Hash.

# File lib/cosvon.rb, line 92
def self.generate(h)
        s="CoSVON:0.1\n"
        h.each{|k,v|
                s+=%Q("#{k.gsub('"','""')}","#{v.gsub('"','""')}"\n)
        }
        s
end
load(path,parser=self.method(:csv)) click to toggle source

parses CoSVON file into Hash.

# File lib/cosvon.rb, line 88
def self.load(path,parser=self.method(:csv))
        self.parse(File.read(path),parser)
end
parse(s,parser=self.method(:csv)) click to toggle source

parses CoSVON string into Hash. parser can be CoSVON.method(:csv), CSV.method(:parse), etc…

# File lib/cosvon.rb, line 77
def self.parse(s,parser=self.method(:csv))
        csv=parser[s]
        return nil if csv.empty?||csv[0].empty?||csv[0][0]!='CoSVON:0.1'
        csv.shift
        h={}
        csv.each{|e|
                h[e[0]]=e[1] if e.size>1&&e[0]&&!e[0].empty?&&e[1]&&!e[1].empty?
        }
        h
end
save(h,path) click to toggle source

generates CoSVON file from Hash.

# File lib/cosvon.rb, line 104
def self.save(h,path)
        File.write(path,generate(h))
end
stringify(h) click to toggle source

kind-of-alias of self.generate

# File lib/cosvon.rb, line 100
def self.stringify(h)
        self.generate(h)
end