#!/usr/bin/env ruby # ############################################################# # # author: mathias gumz # date : Die 12 Aug 2003 14:17:46 CEST # file : slk2csv.rb # about : # ############################################################# # # Copyright (c) 2003 Mathias Gumz # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # require 'getoptlong' HELP = < ${i%.*}.csv" slk2csv.rb $i > ${i%.*}.csv done or make an alias (bash too): alias allslk2csv='for i in *.slk; do echo "$i => ${i%.*}.csv";slk2csv.rb $i > ${i%.*}.csv; done' AUTHOR mathias gumz END begin s= "," parser = GetoptLong.new parser.set_options( [ "--seperator", "-s", GetoptLong::REQUIRED_ARGUMENT ], [ "--help", "-h", GetoptLong::NO_ARGUMENT ] ) ignore= "" parser.each do |opt, arg| case opt when "--help" puts HELP exit 0 when "--seperator" s= "," end end if ARGV.length < 1 puts "no inputfile given" exit 0 end text = IO.readlines(ARGV.shift) table= Hash.new() x= 0 y= 0 w= 0 h= 0 for line in 0..text.length-1 case text[line] when /^F;/ chunks= text[line].split(/;/) l= chunks.length if chunks[l-2] =~ /^X(\d*)/ x= $1.to_i if chunks[l-1] =~ /^Y(\d*)/ y= $1.to_i end elsif chunks[l-2] =~ /^Y(\d*)/ y= $1.to_i if chunks[l-1] =~ /^X(\d*)/ x= $1.to_i end elsif chunks[l-1] =~ /^X(\d*)/ x= $1.to_i elsif chunks[l-1] =~ /^Y(\d*)/ y= $1.to_i end #puts "#{x}|#{y} #{text[line]}" when /^C;K(.*)/ val= $1.sub(/^\"/,'').sub(/\".*$/,'').strip #puts "#{x}|#{y} : #{val.sub(/^\"/,'').sub(/\".*$/,'')}" if !table[y] table[y]= Hash.new end table[y][x]= val w= x > w ? x : w h= y > h ? y : h end end #final output for i in 0..h if !table[i] 0.step(w, 1) { |d| print "#{s}" } print ("\n") else for j in 0..w print "#{table[i][j]}" if j != w print "#{s}" else print "\n" end end end end end # # # ############################################################# # # #############################################################